Changes in libcfa/src/bits/locks.hfa [6ec07e5:92e7631]
- File:
-
- 1 edited
-
libcfa/src/bits/locks.hfa (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/bits/locks.hfa
r6ec07e5 r92e7631 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 119 114 struct __bin_sem_t { 115 bool signaled; 120 116 pthread_mutex_t lock; 121 117 pthread_cond_t cond; 122 int val;123 118 }; 124 119 125 120 static inline void ?{}(__bin_sem_t & this) with( this ) { 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; 121 signaled = false; 122 pthread_mutex_init(&lock, NULL); 123 pthread_cond_init (&cond, NULL); 134 124 } 135 125 136 126 static inline void ^?{}(__bin_sem_t & this) with( this ) { 137 CHECKED( pthread_mutex_destroy(&lock));138 CHECKED( pthread_cond_destroy (&cond));127 pthread_mutex_destroy(&lock); 128 pthread_cond_destroy (&cond); 139 129 } 140 130 141 131 static inline void wait(__bin_sem_t & this) with( this ) { 142 132 verify(__cfaabi_dbg_in_kernel()); 143 CHECKED( pthread_mutex_lock(&lock));144 while(val < 1) {133 pthread_mutex_lock(&lock); 134 if(!signaled) { // this must be a loop, not if! 145 135 pthread_cond_wait(&cond, &lock); 146 136 } 147 val -= 1;148 CHECKED( pthread_mutex_unlock(&lock));137 signaled = false; 138 pthread_mutex_unlock(&lock); 149 139 } 150 140 151 141 static inline bool post(__bin_sem_t & this) with( this ) { 152 bool needs_signal = false; 142 pthread_mutex_lock(&lock); 143 bool needs_signal = !signaled; 144 signaled = true; 145 pthread_mutex_unlock(&lock); 153 146 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) ); 147 if (needs_signal) pthread_cond_signal(&cond); 161 148 162 149 return needs_signal; 163 150 } 164 165 #undef CHECKED166 151 #endif
Note:
See TracChangeset
for help on using the changeset viewer.