int pthread_mutex_trylock(pthread_mutex_t *m) { int r = mutex_ref(m); if(r) return r; return mutex_unref(m,_mutex_trylock(m)); }
int pthread_mutex_timedlock(pthread_mutex_t *m, const struct timespec *ts) { unsigned long long t, ct; int r; mutex_t *_m; if (!ts) return pthread_mutex_lock(m); r = mutex_ref(m); if (r) return r; /* Try to lock it without waiting */ r = _mutex_trylock (m); if (r != EBUSY) return mutex_unref(m,r); _m = (mutex_t *)*m; if (_m->type != PTHREAD_MUTEX_NORMAL && COND_LOCKED(_m) && COND_OWNER(_m)) return mutex_unref(m,EDEADLK); ct = _pthread_time_in_ms(); t = _pthread_time_in_ms_from_timespec(ts); mutex_unref(m,r); r = pthread_mutex_lock_intern(m, (ct > t ? 0 : (t - ct))); return r; }
static int pthread_mutex_lock_intern (pthread_mutex_t *m, DWORD timeout) { mutex_t *_m; int r; HANDLE h; r = mutex_ref (m); if (r) return r; _m = (mutex_t *) *m; if (_m->type != PTHREAD_MUTEX_NORMAL) { if (COND_LOCKED(_m)) { if (COND_OWNER(_m)) { if (_m->type == PTHREAD_MUTEX_RECURSIVE) { InterlockedIncrement(&_m->count); return mutex_unref(m,0); } return mutex_unref(m, EDEADLK); } } } h = _m->h; mutex_unref (m, 0); r = do_sema_b_wait_intern (h, 1, timeout); if (r != 0) return r; r = mutex_ref (m); if (r) return r; _m->count = 1; SET_OWNER(_m); return mutex_unref (m, r); }
static int pthread_mutex_lock_intern(pthread_mutex_t *m, DWORD timeout) { mutex_t *_m; int r; r = mutex_ref(m); if(r) return r; _m = (mutex_t *)*m; if (_m->type != PTHREAD_MUTEX_NORMAL) { if (COND_LOCKED(_m)) { if (COND_OWNER(_m)) { if (_m->type == PTHREAD_MUTEX_RECURSIVE) { InterlockedIncrement(&_m->count); return mutex_unref(m,0); } return mutex_unref(m, EDEADLK); } } } else { #if !defined USE_MUTEX_Mutex if (COND_OWNER(_m)) { do { Sleep(0); /* waiter that owner gets released. */ } while (COND_OWNER(_m)); } #endif } #if defined USE_MUTEX_Mutex r = do_sema_b_wait_intern (_m->h, 1, timeout); #else /* USE_MUTEX_CriticalSection */ EnterCriticalSection(&_m->cs.cs); r = 0; #endif if (r == 0) { _m->count = 1; SET_OWNER(_m); } return mutex_unref(m,r); }
int pthread_mutex_timedlock(pthread_mutex_t *m, const struct timespec *ts) { unsigned long long t, ct; #ifndef USE_MUTEX_Mutex int i = 0; #endif int r; if (!ts) return pthread_mutex_lock(m); r = mutex_ref(m); if (r) return r; /* Try to lock it without waiting */ r=_mutex_trylock(m); if (r != EBUSY) return mutex_unref(m,r); mutex_t *_m = (mutex_t *)*m; if (_m->type != PTHREAD_MUTEX_NORMAL && COND_LOCKED(_m) && COND_OWNER(_m)) return mutex_unref(m,EDEADLK); ct = _pthread_time_in_ms(); t = _pthread_time_in_ms_from_timespec(ts); #ifdef USE_MUTEX_Mutex mutex_unref(m,r); r = pthread_mutex_lock_intern(m, (ct > t ? 0 : (t - ct))); #else mutex_unref(m,EDEADLK); while (1) { /* Have we waited long enough? A high count means we busy-waited probably.*/ if (ct >= t) { printf("%d: Timeout after %d times\n",(int)GetCurrentThreadId(), i); return ETIMEDOUT; } if ((i & 1) == 0) Sleep(0); /* waiter that owner gets released. */ /* Try to grab lock */ r = pthread_mutex_trylock(m); if (r != EBUSY) break; /* Get current time */ ct = _pthread_time_in_ms(); i ++; } #endif return r; }
static int pthread_mutex_lock_intern (pthread_mutex_t *m, DWORD timeout) { mutex_t *_m; int r; HANDLE h; #if 0 int waited = 0; #endif r = mutex_ref (m); if (r) return r; _m = (mutex_t *) *m; if (_m->type != PTHREAD_MUTEX_NORMAL) { if (COND_LOCKED(_m)) { if (COND_OWNER(_m)) { if (_m->type == PTHREAD_MUTEX_RECURSIVE) { #if 0 printf("thread %d, recursive increment %p\n", GetCurrentThreadId(), m); #endif InterlockedIncrement(&_m->count); return mutex_unref(m,0); } #if 0 printf("thread %d, non recursive increment?!? %p\n", GetCurrentThreadId(), m); #endif return mutex_unref(m, EDEADLK); } } } h = _m->h; mutex_unref (m, 0); if(_m->owner) { #if 0 waited = 1; printf("thread %d, waiting for thread: %d on mutex %p for %d time\n", GetCurrentThreadId(), _m->owner, m, timeout); #endif } r = do_sema_b_wait_intern (h, 1, timeout); #if 0 if(waited) { printf("thread %d, resumed\n", GetCurrentThreadId()); } #endif if (r != 0) return r; r = mutex_ref (m); if (r) return r; _m->count = 1; SET_OWNER(_m); #if 0 printf("thread %d, setting owner of mutex %p\n", GetCurrentThreadId(), m); #endif return mutex_unref (m, r); }