static int _thread_rwlock_lock(rwlock_t *rwlock, int op, int tryonly) { /* consistency checks */ if (rwlock == NULL) return_errno(FALSE, EINVAL); if (!(rwlock->rw_state & THREAD_RWLOCK_INITIALIZED)) return_errno(FALSE, EDEADLK); /* lock lock */ if (op == RWLOCK_RW) { /* read-write lock is simple */ if (!_thread_mutex_lock(&(rwlock->rw_mutex_rw), tryonly)) return FALSE; rwlock->rw_mode = RWLOCK_RW; } else { /* read-only lock is more complicated to get right */ if (!_thread_mutex_lock(&(rwlock->rw_mutex_rd), tryonly)) return FALSE; rwlock->rw_readers++; if (rwlock->rw_readers == 1) { if (!_thread_mutex_lock(&(rwlock->rw_mutex_rw), tryonly)) { rwlock->rw_readers--; thread_mutex_unlock(&(rwlock->rw_mutex_rd)); return FALSE; } } rwlock->rw_mode = RWLOCK_RD; thread_mutex_unlock(&(rwlock->rw_mutex_rd)); } return TRUE; }
void task_destroy() { _task* self = (_task*)_thread_getspecific(); _thread_mutex_lock(&self -> down_mutex); self -> down_count++; uint32_t temp = ((self -> up_count) - (self -> down_count)); if(self -> up_count - self -> down_count) _thread_cond_wait(&self->down_cv, &self->down_mutex); _thread_mutex_unlock(&self -> down_mutex); // print_memory(self); _thread_setspecific((void*)self -> prev); //may not be needed //GC_FREE(self); free(self); self = NULL; }
inline int thread_mutex_lock(mutex_t *m) { return _thread_mutex_lock(m, FALSE); }
inline int thread_mutex_trylock(mutex_t *m) { return _thread_mutex_lock(m, TRUE); }
void task_deregister(_task* parent) { _thread_mutex_lock(&parent -> down_mutex); parent -> down_count++; if(!(parent -> up_count - parent -> down_count)) _thread_cond_signal(&parent->down_cv); _thread_mutex_unlock(&parent -> down_mutex); }