source: src/libcfa/bits/locks.h@ a37133c

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr no_list persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since a37133c was 1997b4e, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

tentative fix of the linking ordering problem

  • Property mode set to 100644
File size: 4.0 KB
RevLine 
[ea7d2b0]1//
2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// bits/locks.h -- Fast internal locks.
8//
9// Author : Thierry Delisle
10// Created On : Tue Oct 31 15:14:38 2017
[b158d8f]11// Last Modified By : Peter A. Buhr
[b10affd]12// Last Modified On : Fri Mar 30 18:18:13 2018
13// Update Count : 9
[ea7d2b0]14//
15
16#pragma once
17
[875a72f]18#include "bits/debug.h"
[ea7d2b0]19#include "bits/defs.h"
[ea8b2f7]20#include <assert.h>
21
22#ifdef __cforall
23 extern "C" {
24 #include <pthread.h>
25 }
26#endif
[ea7d2b0]27
28// pause to prevent excess processor bus usage
29#if defined( __sparc )
30 #define Pause() __asm__ __volatile__ ( "rd %ccr,%g0" )
31#elif defined( __i386 ) || defined( __x86_64 )
32 #define Pause() __asm__ __volatile__ ( "pause" : : : )
[b158d8f]33#elif defined( __ARM_ARCH )
34 #define Pause() __asm__ __volatile__ ( "nop" : : : )
[ea7d2b0]35#else
36 #error unsupported architecture
37#endif
38
[b158d8f]39#if defined( __i386 ) || defined( __x86_64 ) || defined( __ARM_ARCH )
[ea7d2b0]40 // Intel recommendation
41 #define __ALIGN__ __attribute__(( aligned (128) ))
42#elif defined( __sparc )
43 #define __ALIGN__ CALIGN
44#else
45 #error unsupported architecture
46#endif
47
48struct __spinlock_t {
[13073be]49 // Wrap in struct to prevent false sharing with debug info
50 struct {
51 // Align lock on 128-bit boundary
52 __ALIGN__ volatile _Bool lock;
53 };
[ea7d2b0]54 #ifdef __CFA_DEBUG__
[13073be]55 // previous function to acquire the lock
[ea7d2b0]56 const char * prev_name;
[13073be]57 // previous thread to acquire the lock
[ea7d2b0]58 void* prev_thrd;
59 #endif
60} __ALIGN__;
61
[0cf5b79]62#ifdef __cforall
[dbe9b08]63 extern "C" {
64 extern void disable_interrupts();
65 extern void enable_interrupts_noPoll();
[1997b4e]66
67 #ifdef __CFA_DEBUG__
68 void __cfaabi_dbg_record(__spinlock_t & this, const char * prev_name);
69 #else
70 #define __cfaabi_dbg_record(x, y)
71 #endif
[dbe9b08]72 }
73
[ea7d2b0]74 extern void yield( unsigned int );
75
76 static inline void ?{}( __spinlock_t & this ) {
77 this.lock = 0;
78 }
79
80 // Lock the spinlock, return false if already acquired
[36982fc]81 static inline _Bool try_lock ( __spinlock_t & this __cfaabi_dbg_ctx_param2 ) {
[13073be]82 _Bool result = (this.lock == 0) && (__atomic_test_and_set( &this.lock, __ATOMIC_ACQUIRE ) == 0);
[dbe9b08]83 if( result ) {
84 disable_interrupts();
[9181f1d]85 __cfaabi_dbg_record( this, caller );
[dbe9b08]86 }
[ea7d2b0]87 return result;
88 }
89
90 // Lock the spinlock, spin if already acquired
[36982fc]91 static inline void lock( __spinlock_t & this __cfaabi_dbg_ctx_param2 ) {
[ea7d2b0]92 #ifndef NOEXPBACK
93 enum { SPIN_START = 4, SPIN_END = 64 * 1024, };
94 unsigned int spin = SPIN_START;
95 #endif
96
97 for ( unsigned int i = 1;; i += 1 ) {
[13073be]98 if ( (this.lock == 0) && (__atomic_test_and_set( &this.lock, __ATOMIC_ACQUIRE ) == 0) ) break;
[ea7d2b0]99 #ifndef NOEXPBACK
100 // exponential spin
101 for ( volatile unsigned int s = 0; s < spin; s += 1 ) Pause();
102
103 // slowly increase by powers of 2
104 if ( i % 64 == 0 ) spin += spin;
105
106 // prevent overflow
107 if ( spin > SPIN_END ) spin = SPIN_START;
108 #else
109 Pause();
110 #endif
111 }
[dbe9b08]112 disable_interrupts();
[9181f1d]113 __cfaabi_dbg_record( this, caller );
[ea7d2b0]114 }
115
116 static inline void unlock( __spinlock_t & this ) {
[dbe9b08]117 enable_interrupts_noPoll();
[13073be]118 __atomic_clear( &this.lock, __ATOMIC_RELEASE );
[ea7d2b0]119 }
[ea8b2f7]120
121
122 #ifdef __CFA_WITH_VERIFY__
123 extern bool __cfaabi_dbg_in_kernel();
124 #endif
125
126 struct __bin_sem_t {
[85b1deb]127 bool signaled;
128 pthread_mutex_t lock;
129 pthread_cond_t cond;
[ea8b2f7]130 };
131
132 static inline void ?{}(__bin_sem_t & this) with( this ) {
[85b1deb]133 signaled = false;
[ea8b2f7]134 pthread_mutex_init(&lock, NULL);
135 pthread_cond_init (&cond, NULL);
136 }
137
138 static inline void ^?{}(__bin_sem_t & this) with( this ) {
139 pthread_mutex_destroy(&lock);
140 pthread_cond_destroy (&cond);
141 }
142
143 static inline void wait(__bin_sem_t & this) with( this ) {
144 verify(__cfaabi_dbg_in_kernel());
145 pthread_mutex_lock(&lock);
[85b1deb]146 if(!signaled) { // this must be a loop, not if!
147 pthread_cond_wait(&cond, &lock);
148 }
149 signaled = false;
[ea8b2f7]150 pthread_mutex_unlock(&lock);
151 }
152
153 static inline void post(__bin_sem_t & this) with( this ) {
154 verify(__cfaabi_dbg_in_kernel());
[85b1deb]155
[ea8b2f7]156 pthread_mutex_lock(&lock);
[85b1deb]157 bool needs_signal = !signaled;
158 signaled = true;
[ea8b2f7]159 pthread_mutex_unlock(&lock);
[85b1deb]160
161 if (needs_signal)
[ea8b2f7]162 pthread_cond_signal(&cond);
[85b1deb]163 }
[b158d8f]164#endif
Note: See TracBrowser for help on using the repository browser.