Changeset bcd74f3 for libcfa/src/bits/locks.hfa
- Timestamp:
- May 8, 2020, 4:44:53 PM (4 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 5c9b20c
- Parents:
- e3bc51c (diff), 0e7e3c17 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/bits/locks.hfa
re3bc51c rbcd74f3 112 112 #endif 113 113 114 extern "C" { 115 char * strerror(int); 116 } 117 #define CHECKED(x) { int err = x; if( err != 0 ) abort("KERNEL ERROR: Operation \"" #x "\" return error %d - %s\n", err, strerror(err)); } 118 114 119 struct __bin_sem_t { 115 bool signaled;116 120 pthread_mutex_t lock; 117 121 pthread_cond_t cond; 122 int val; 118 123 }; 119 124 120 125 static inline void ?{}(__bin_sem_t & this) with( this ) { 121 signaled = false; 122 pthread_mutex_init(&lock, NULL); 123 pthread_cond_init (&cond, NULL); 126 // Create the mutex with error checking 127 pthread_mutexattr_t mattr; 128 pthread_mutexattr_init( &mattr ); 129 pthread_mutexattr_settype( &mattr, PTHREAD_MUTEX_ERRORCHECK_NP); 130 pthread_mutex_init(&lock, &mattr); 131 132 pthread_cond_init (&cond, 0p); 133 val = 0; 124 134 } 125 135 126 136 static inline void ^?{}(__bin_sem_t & this) with( this ) { 127 pthread_mutex_destroy(&lock);128 pthread_cond_destroy (&cond);137 CHECKED( pthread_mutex_destroy(&lock) ); 138 CHECKED( pthread_cond_destroy (&cond) ); 129 139 } 130 140 131 141 static inline void wait(__bin_sem_t & this) with( this ) { 132 142 verify(__cfaabi_dbg_in_kernel()); 133 pthread_mutex_lock(&lock);134 if(!signaled) { // this must be a loop, not if!143 CHECKED( pthread_mutex_lock(&lock) ); 144 while(val < 1) { 135 145 pthread_cond_wait(&cond, &lock); 136 146 } 137 signaled = false;138 pthread_mutex_unlock(&lock);147 val -= 1; 148 CHECKED( pthread_mutex_unlock(&lock) ); 139 149 } 140 150 141 151 static inline bool post(__bin_sem_t & this) with( this ) { 142 pthread_mutex_lock(&lock); 143 bool needs_signal = !signaled; 144 signaled = true; 145 pthread_mutex_unlock(&lock); 152 bool needs_signal = false; 146 153 147 if (needs_signal) pthread_cond_signal(&cond); 154 CHECKED( pthread_mutex_lock(&lock) ); 155 if(val < 1) { 156 val += 1; 157 pthread_cond_signal(&cond); 158 needs_signal = true; 159 } 160 CHECKED( pthread_mutex_unlock(&lock) ); 148 161 149 162 return needs_signal; 150 163 } 164 165 #undef CHECKED 151 166 #endif
Note: See TracChangeset
for help on using the changeset viewer.