static void * rdfunc(void * arg) { int ba = -1; struct timespec abstime = { 0, 0 }; PTW32_STRUCT_TIMEB currSysTime; const DWORD NANOSEC_PER_MILLISEC = 1000000; PTW32_FTIME(&currSysTime); abstime.tv_sec = (long)currSysTime.time; abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm; if ((int) (size_t)arg == 1) { abstime.tv_sec += 1; assert(pthread_rwlock_timedrdlock(&rwlock1, &abstime) == ETIMEDOUT); ba = 0; } else if ((int) (size_t)arg == 2) { abstime.tv_sec += 3; assert(pthread_rwlock_timedrdlock(&rwlock1, &abstime) == 0); ba = bankAccount; assert(pthread_rwlock_unlock(&rwlock1) == 0); } return ((void *)(size_t)ba); }
static void *th_fn(void *arg) { struct sigaction act; struct timespec abs_timeout; int rc = 0; handler_called = 0; /* Set up signal handler for SIGUSR1 */ act.sa_flags = 0; act.sa_handler = sig_handler; /* block all the signal when hanlding SIGUSR1 */ sigfillset(&act.sa_mask); sigaction(SIGUSR1, &act, 0); gettimeofday(&before_wait, NULL); abs_timeout.tv_sec = before_wait.tv_sec; abs_timeout.tv_nsec = before_wait.tv_usec * 1000; abs_timeout.tv_sec += TIMEOUT; printf("thread: attempt timed read lock, %d seconds\n", TIMEOUT); thread_state = ENTERED_THREAD; rc = pthread_rwlock_timedrdlock(&rwlock, &abs_timeout); if (rc != ETIMEDOUT) { printf("sig_thread: pthread_rwlock_timedlock returns %d\n", rc); exit(PTS_FAIL); } printf("thread: timer correctly expired\n"); gettimeofday(&after_wait, NULL); thread_state = EXITING_THREAD; pthread_exit(0); return NULL; }
static TACommandVerdict pthread_rwlock_timedrdlock_bad_cmd(TAThread thread, TAInputStream stream) { pthread_rwlock_t* rwlock; int nsec; struct timespec timeout; int res; // Prepare rwlock = readPointer(&stream); nsec = readInt(&stream); timeout = thread->start_time; timeout.tv_sec += 2; timeout.tv_nsec = nsec; START_TARGET_OPERATION(thread); // Execute res = pthread_rwlock_timedrdlock(rwlock, &timeout); END_TARGET_OPERATION(thread); // Response writeInt(thread, res); sendResponse(thread); return taDefaultVerdict; }
static TACommandVerdict pthread_rwlock_timedrdlock_cmd(TAThread thread, TAInputStream stream) { pthread_rwlock_t* rwlock; TimeUnit delta; struct timespec timeout; int res; // Prepare rwlock = readPointer(&stream); delta = readTimeUnit(&stream); // Reading the relative time BEFORE_BLOCKED_TARGET_OPERATION(thread); timeout = addTimeUnit(thread->start_time, delta); // Get the absolute timeout value writeString(thread, "Ok"); sendResponse(thread); START_TARGET_OPERATION(thread); // Execute res = pthread_rwlock_timedrdlock(rwlock, &timeout); END_TARGET_OPERATION(thread); // Response writeDeferredReaction(thread, "pthread_rwlock_timedrdlock_return"); writeInt(thread, res); sendResponse(thread); return taDefaultVerdict; }
static void * th (void *arg) { long int res = 0; int r; struct timespec t = { -2, 0 }; r = pthread_mutex_timedlock (&m1, &t); if (r != ETIMEDOUT) { puts ("pthread_mutex_timedlock did not return ETIMEDOUT"); res = 1; } r = pthread_rwlock_timedrdlock (&rw1, &t); if (r != ETIMEDOUT) { puts ("pthread_rwlock_timedrdlock did not return ETIMEDOUT"); res = 1; } r = pthread_rwlock_timedwrlock (&rw2, &t); if (r != ETIMEDOUT) { puts ("pthread_rwlock_timedwrlock did not return ETIMEDOUT"); res = 1; } return (void *) res; }
int main() { struct timespec abstime = { 0, 0 }; struct _timeb currSysTime; const DWORD NANOSEC_PER_MILLISEC = 1000000; _ftime(&currSysTime); abstime.tv_sec = currSysTime.time; abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm; abstime.tv_sec += 1; assert(rwlock == PTHREAD_RWLOCK_INITIALIZER); assert(pthread_rwlock_timedrdlock(&rwlock, &abstime) == 0); assert(rwlock != PTHREAD_RWLOCK_INITIALIZER); assert(rwlock != NULL); assert(pthread_rwlock_unlock(&rwlock) == 0); assert(pthread_rwlock_destroy(&rwlock) == 0); assert(rwlock == NULL); return 0; }
void test_multiple_readers(void) { fprintf(stderr, "test_multiple_readers\n"); tell_thread_to_acquire_lock(READ_LOCK); /* * Now attempt to acquire the lock on the main thread. * Since they are both readers this should succeed. * Try with tryrdlock, rdlock and timedrdlock. */ int rc = pthread_rwlock_tryrdlock(&g_rwlock); ASSERT_EQ(rc, 0); rc = pthread_rwlock_unlock(&g_rwlock); ASSERT_EQ(rc, 0); struct timespec t = { 0, 0 }; rc = pthread_rwlock_timedrdlock(&g_rwlock, &t); ASSERT_EQ(rc, 0); rc = pthread_rwlock_unlock(&g_rwlock); ASSERT_EQ(rc, 0); rc = pthread_rwlock_rdlock(&g_rwlock); ASSERT_EQ(rc, 0); rc = pthread_rwlock_unlock(&g_rwlock); ASSERT_EQ(rc, 0); tell_thread_to_release_lock(); }
int main() { pthread_t t; struct timespec abstime = { 0, 0 }; PTW32_STRUCT_TIMEB currSysTime; const DWORD NANOSEC_PER_MILLISEC = 1000000; PTW32_FTIME(&currSysTime); abstime.tv_sec = (long)currSysTime.time; abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm; abstime.tv_sec += 1; assert(pthread_rwlock_timedrdlock(&rwlock1, &abstime) == 0); assert(pthread_create(&t, NULL, func, NULL) == 0); Sleep(2000); assert(pthread_rwlock_unlock(&rwlock1) == 0); assert(washere == 1); return 0; }
bool rw_mutex::impl::read_lock(double sec) { #ifdef __linux__ if (!valid) return false; #ifdef JUBATUS_UTIL_CONCURRENT_RWMUTEX_ERRORCHECK assert(holders_lk.lock()==true); assert(holders.count(thread::id())==0); #endif bool result; if (sec<1e-9) { result = pthread_rwlock_tryrdlock(&lk); } else { timespec end=to_timespec(get_clock_time()+sec); result = pthread_rwlock_timedrdlock(&lk, &end)==0; } #ifdef JUBATUS_UTIL_CONCURRENT_RWMUTEX_ERRORCHECK if (result) holders.insert(thread::id()); assert(holders_lk.unlock()==true); #endif return result; #else return false; #endif }
int main(int argc, char** argv) { int r; pthread_mutex_t mutex; pthread_rwlock_t rwlock; struct timespec abs_timeout; time(&abs_timeout.tv_sec); abs_timeout.tv_nsec = 0; abs_timeout.tv_sec += 10; r = pthread_rwlock_init(&rwlock, NULL); assert(r == 0); fprintf(stderr, "Locking rwlock via pthread_rwlock_wrlock().\n"); r = pthread_rwlock_wrlock(&rwlock); assert(r == 0); r = pthread_rwlock_unlock(&rwlock); assert(r == 0); fprintf(stderr, "Locking rwlock via pthread_rwlock_trywrlock().\n"); r = pthread_rwlock_trywrlock(&rwlock); assert(r == 0); r = pthread_rwlock_unlock(&rwlock); assert(r == 0); fprintf(stderr, "Locking rwlock via pthread_rwlock_timedwrlock().\n"); #ifdef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK r = pthread_rwlock_timedwrlock(&rwlock, &abs_timeout); assert(r == 0); r = pthread_rwlock_unlock(&rwlock); assert(r == 0); #endif fprintf(stderr, "Locking rwlock via pthread_rwlock_rdlock().\n"); r = pthread_rwlock_rdlock(&rwlock); assert(r == 0); r = pthread_rwlock_rdlock(&rwlock); assert(r == 0); r = pthread_rwlock_rdlock(&rwlock); assert(r == 0); r = pthread_rwlock_unlock(&rwlock); assert(r == 0); r = pthread_rwlock_unlock(&rwlock); assert(r == 0); r = pthread_rwlock_unlock(&rwlock); assert(r == 0); fprintf(stderr, "Locking rwlock via pthread_rwlock_tryrdlock().\n"); r = pthread_rwlock_tryrdlock(&rwlock); assert(r == 0); r = pthread_rwlock_unlock(&rwlock); assert(r == 0); fprintf(stderr, "Locking rwlock via pthread_rwlock_timedrdlock().\n"); #ifdef HAVE_PTHREAD_RWLOCK_TIMEDRDLOCK r = pthread_rwlock_timedrdlock(&rwlock, &abs_timeout); assert(r == 0); r = pthread_rwlock_unlock(&rwlock); assert(r == 0); #endif fprintf(stderr, "Attempt to lock for writing recursively (not allowed).\n"); r = pthread_rwlock_wrlock(&rwlock); assert(r == 0); r = pthread_rwlock_wrlock(&rwlock); assert(r != 0); r = pthread_rwlock_unlock(&rwlock); assert(r == 0); r = pthread_rwlock_destroy(&rwlock); assert(r == 0); r = pthread_mutex_init(&mutex, NULL); assert(r == 0); fprintf(stderr, "Locking mutex via pthread_mutex_trylock().\n"); r = pthread_mutex_trylock(&mutex); assert(r == 0); r = pthread_mutex_unlock(&mutex); assert(r == 0); fprintf(stderr, "Locking mutex via pthread_mutex_lock().\n"); r = pthread_mutex_lock(&mutex); assert(r == 0); r = pthread_mutex_unlock(&mutex); assert(r == 0); fprintf(stderr, "Locking mutex via pthread_mutex_timedlock().\n"); #ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK r = pthread_mutex_timedlock(&mutex, &abs_timeout); assert(r == 0); r = pthread_mutex_unlock(&mutex); assert(r == 0); #endif r = pthread_mutex_destroy(&mutex); assert(r == 0); return 0; }
wod_ret_t wod_rwlock_rlock_timeout(wod_rwlock_t *rwlock,long long timeout) { struct timespec tm; tm.tv_sec = timeout/1000000; tm.tv_nsec = timeout-tm.tv_sec; return pthread_rwlock_timedrdlock(&rwlock->rw,&tm); }
void * rdfunc(void * arg) { int ba = 0; assert(pthread_rwlock_timedrdlock(&rwlock1, &abstime) == ETIMEDOUT); return ((void *)(size_t)ba); }
void test_reader_timedwait(void) { fprintf(stderr, "test_reader_timedwait\n"); tell_thread_to_acquire_lock(WRITE_LOCK); struct timespec t = { 0, 0 }; int rc = pthread_rwlock_timedrdlock(&g_rwlock, &t); ASSERT_EQ(rc, ETIMEDOUT); tell_thread_to_release_lock(); }
static void* fn_rd_1(void *arg) { thread_state = ENTERED_THREAD; struct timespec abs_timeout; int rc; pthread_rwlock_t rwlock; if(pthread_rwlock_init(&rwlock, NULL) != 0) { printf("thread1: Error at pthread_rwlock_init\n"); exit(PTS_UNRESOLVED); } currsec1 = time(NULL); /* Absolute time, not relative. */ abs_timeout.tv_sec = currsec1 + TIMEOUT; abs_timeout.tv_nsec = 0; printf("thread1: attempt timed read-lock\n"); rc = pthread_rwlock_timedrdlock(&rwlock, &abs_timeout); if(rc == ETIMEDOUT) { printf("thread1: timed read-lock expired\n"); expired = 1; } else if(rc == 0) { printf("thread1: acquired read lock\n"); expired = 0; printf("thread1: unlock read lock\n"); if(pthread_rwlock_unlock(&rwlock) != 0) { printf("thread1: failed to release read lock\n"); exit(PTS_UNRESOLVED); } } else { printf("thread1: Error in pthread_rwlock_timedrdlock().\n"); exit(PTS_UNRESOLVED); } if(pthread_rwlock_destroy(&rwlock) != 0) { printf("thread1: Error at pthread_rwlockattr_destroy()"); exit(PTS_UNRESOLVED); } thread_state = EXITING_THREAD; pthread_exit(0); return NULL; }
static void* fn_rd_2(void *arg) { thread_state = ENTERED_THREAD; struct timespec abs_timeout; int rc; pthread_rwlock_t rwlock; if(pthread_rwlock_init(&rwlock, NULL) != 0) { printf("thread2: Error at pthread_rwlock_init\n"); exit(PTS_UNRESOLVED); } currsec1 = time(NULL); /* Ensure that the abs_timeout has passed by _subtracting_ the timeout value of 1 * from the current time. */ abs_timeout.tv_sec = currsec1 - TIMEOUT; abs_timeout.tv_nsec = 0; printf("thread2: attempt timed read-lock\n"); rc = pthread_rwlock_timedrdlock(&rwlock, &abs_timeout); if(rc == ETIMEDOUT) { printf("thread2: timed read-lock expired\n"); expired = 1; } else if(rc == 0) { printf("thread2: acquired read lock\n"); expired = 0; printf("thread2: unlock read lock\n"); if(pthread_rwlock_unlock(&rwlock) != 0) { printf("thread2: failed to release read lock\n"); exit(PTS_UNRESOLVED); } } else { printf("thread2: Error in pthread_rwlock_timedrdlock().\n"); exit(PTS_UNRESOLVED); } if(pthread_rwlock_destroy(&rwlock) != 0) { printf("thread2: Error at pthread_rwlockattr_destroy()\n"); exit(PTS_UNRESOLVED); } thread_state = EXITING_THREAD; pthread_exit(0); return NULL; }
static void * reader_thread (void *nr) { struct timespec ts; struct timespec delay; int n; delay.tv_sec = 0; delay.tv_nsec = DELAY; for (n = 0; n < READTRIES; ++n) { int e; do { struct timeval tv; (void) gettimeofday (&tv, NULL); TIMEVAL_TO_TIMESPEC (&tv, &ts); ts.tv_nsec += TIMEOUT; if (ts.tv_nsec >= 1000000000) { ts.tv_nsec -= 1000000000; ++ts.tv_sec; } printf ("reader thread %ld tries again\n", (long int) nr); e = pthread_rwlock_timedrdlock (&lock, &ts); if (e != 0 && e != ETIMEDOUT) { puts ("timedrdlock failed"); exit (1); } } while (e == ETIMEDOUT); printf ("reader thread %ld succeeded\n", (long int) nr); nanosleep (&delay, NULL); if (pthread_rwlock_unlock (&lock) != 0) { puts ("unlock for reader failed"); exit (1); } printf ("reader thread %ld released\n", (long int) nr); } return NULL; }
void test_unlocked_with_zero_timestamp(void) { fprintf(stderr, "test_unlocked_with_zero_timestamp\n"); int rc; struct timespec abstime = { 0, 0 }; ASSERT_EQ(g_thread_has_lock, 0); fprintf(stderr, "Trying to lock the unlocked rwlock with a valid " "zero absolute timestamp. " "Expected to succeed instantly since the lock is free.\n"); rc = pthread_rwlock_timedrdlock(&g_rwlock, &abstime); ASSERT_EQ(rc, 0); rc = pthread_rwlock_unlock(&g_rwlock); ASSERT_EQ(rc, 0); }
bool cRwLock::Lock(bool Write, int TimeoutMs) { int Result = 0; struct timespec abstime; if (TimeoutMs) { if (!GetAbsTime(&abstime, TimeoutMs)) TimeoutMs = 0; } if (Write) Result = TimeoutMs ? pthread_rwlock_timedwrlock(&rwlock, &abstime) : pthread_rwlock_wrlock(&rwlock); else Result = TimeoutMs ? pthread_rwlock_timedrdlock(&rwlock, &abstime) : pthread_rwlock_rdlock(&rwlock); return Result == 0; }
static int rd_timedlock(pthread_rwlock_t *lock, int timeout) { tv_t now; ts_t abs; int ret; tv_time(&now); tv_to_ts(&abs, &now); abs.tv_sec += timeout; ret = pthread_rwlock_timedrdlock(lock, &abs); return ret; }
static void* fn_rd(void *arg) { thread_state = ENTERED_THREAD; struct timespec timeout, ts; int rc; #ifdef CLOCK_REALTIME printf("Test CLOCK_REALTIME\n"); clock_gettime(CLOCK_REALTIME, &ts); currsec1.tv_sec = ts.tv_sec; currsec1.tv_usec = ts.tv_nsec / 1000; #else gettimeofday(&currsec1, NULL); #endif /* Absolute time, not relative. */ timeout.tv_sec = currsec1.tv_sec + TIMEOUT; timeout.tv_nsec = currsec1.tv_usec * 1000; printf("thread: attempt timed read lock, %d secs\n", TIMEOUT); rc = pthread_rwlock_timedrdlock(&rwlock, &timeout); if (rc == ETIMEDOUT) printf("thread: timer expired\n"); else if (rc == 0) { printf("thread: acquired read lock\n"); printf("thread: unlock read lock\n"); if (pthread_rwlock_unlock(&rwlock) != 0) { exit(PTS_UNRESOLVED); } } else { printf("Error: thread: in pthread_rwlock_timedrdlock(), return code:%d\n", rc); exit(PTS_UNRESOLVED); } /* Get time after the pthread_rwlock_timedrdlock() call. */ #ifdef CLOCK_REALTIME clock_gettime(CLOCK_REALTIME, &ts); currsec2.tv_sec = ts.tv_sec; currsec2.tv_usec = ts.tv_nsec / 1000; #else gettimeofday(&currsec2, NULL); #endif thread_state = EXITING_THREAD; pthread_exit(0); return NULL; }
/** * TimeReadLock * @params[in] timeout * * This function will attempt to aquire a read lock. It will attempt to try for the timeout specific. * If it does aquire the lock true will be return. If the lock is not aquired false will be returned. * */ bool RWLock::TimeReadLock(const struct timespec *timeout) { int ret = pthread_rwlock_timedrdlock(&m_lock, timeout); if (ret == 0) return true; switch(ret) { case ETIMEDOUT: return false; default: abort(); } return false; }
static int do_test (void) { size_t cnt; for (cnt = 0; cnt < sizeof (kind) / sizeof (kind[0]); ++cnt) { pthread_rwlock_t r; pthread_rwlockattr_t a; if (pthread_rwlockattr_init (&a) != 0) FAIL_EXIT1 ("round %Zu: rwlockattr_t failed\n", cnt); if (pthread_rwlockattr_setkind_np (&a, kind[cnt]) != 0) FAIL_EXIT1 ("round %Zu: rwlockattr_setkind failed\n", cnt); if (pthread_rwlock_init (&r, &a) != 0) FAIL_EXIT1 ("round %Zu: rwlock_init failed\n", cnt); if (pthread_rwlockattr_destroy (&a) != 0) FAIL_EXIT1 ("round %Zu: rwlockattr_destroy failed\n", cnt); struct timespec ts; xclock_gettime (CLOCK_REALTIME, &ts); ++ts.tv_sec; /* Get a read lock. */ if (pthread_rwlock_timedrdlock (&r, &ts) != 0) FAIL_EXIT1 ("round %Zu: rwlock_timedrdlock failed\n", cnt); printf ("%zu: got timedrdlock\n", cnt); pthread_t th; if (pthread_create (&th, NULL, tf, &r) != 0) FAIL_EXIT1 ("round %Zu: create failed\n", cnt); void *status; if (pthread_join (th, &status) != 0) FAIL_EXIT1 ("round %Zu: join failed\n", cnt); if (status != NULL) FAIL_EXIT1 ("failure in round %Zu\n", cnt); if (pthread_rwlock_destroy (&r) != 0) FAIL_EXIT1 ("round %Zu: rwlock_destroy failed\n", cnt); } return 0; }
bool cThreadLockRw::TimedReadLock( unsigned int timeout ) { struct timespec t; GetTimeout( t, timeout ); int rv = pthread_rwlock_timedrdlock( &m_rwlock, &t ); if ( rv ) { assert( rv == ETIMEDOUT ); return false; } return true; }
LIB_EXPORT rc_t CC KRWLockTimedAcquireShared ( KRWLock *self, timeout_t *tm ) { int status; if ( self == NULL ) return RC ( rcPS, rcRWLock, rcLocking, rcSelf, rcNull ); status = pthread_rwlock_tryrdlock ( & self -> lock ); switch ( status ) { case 0: return 0; case EBUSY: if ( tm != NULL ) break; return RC ( rcPS, rcLock, rcLocking, rcRWLock, rcBusy ); case EAGAIN: return RC ( rcPS, rcRWLock, rcLocking, rcRWLock, rcExhausted ); case EINVAL: return RC ( rcPS, rcRWLock, rcLocking, rcRWLock, rcInvalid ); default: return RC ( rcPS, rcRWLock, rcLocking, rcNoObj, rcUnknown ); } if ( ! tm -> prepared ) TimeoutPrepare ( tm ); status = pthread_rwlock_timedrdlock ( & self -> lock, & tm -> ts ); switch ( status ) { case 0: break; case ETIMEDOUT: return RC ( rcPS, rcRWLock, rcLocking, rcTimeout, rcExhausted ); case EAGAIN: return RC ( rcPS, rcRWLock, rcLocking, rcRWLock, rcExhausted ); case EDEADLK: return RC ( rcPS, rcRWLock, rcLocking, rcThread, rcDeadlock ); case EINVAL: return RC ( rcPS, rcRWLock, rcLocking, rcTimeout, rcInvalid ); default: return RC ( rcPS, rcRWLock, rcLocking, rcNoObj, rcUnknown ); } return 0; }
// ReadLock with absolute timeout bool RWLockImplPosix::readLock(const ::icl_core::TimeStamp& timeout) { #ifdef _SYSTEM_DARWIN_ int ret = pthread_rwlock_tryrdlock(m_rwlock); while ((ret != 0) && ((timeout > icl_core::TimeStamp::now()))) { // one microsecond icl_core::os::usleep(1); ret = pthread_rwlock_tryrdlock(m_rwlock); } return (ret == 0); #else struct timespec timeout_timespec = timeout.timespec(); int ret = pthread_rwlock_timedrdlock(m_rwlock, &timeout_timespec); return (ret == 0); #endif }
lagopus_result_t lagopus_rwlock_reader_timedlock(lagopus_rwlock_t *rwlptr, lagopus_chrono_t nsec) { lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; if (rwlptr != NULL && *rwlptr != NULL) { int st; if (nsec < 0) { if ((st = pthread_rwlock_rdlock(&((*rwlptr)->m_rwl))) == 0) { (*rwlptr)->m_prev_cancel_state = -INT_MAX; ret = LAGOPUS_RESULT_OK; } else { errno = st; ret = LAGOPUS_RESULT_POSIX_API_ERROR; } } else { struct timespec ts; lagopus_chrono_t now; WHAT_TIME_IS_IT_NOW_IN_NSEC(now); now += nsec; NSEC_TO_TS(now, ts); if ((st = pthread_rwlock_timedrdlock(&((*rwlptr)->m_rwl), &ts)) == 0) { (*rwlptr)->m_prev_cancel_state = -INT_MAX; ret = LAGOPUS_RESULT_OK; } else { errno = st; if (st == ETIMEDOUT) { ret = LAGOPUS_RESULT_TIMEDOUT; } else { ret = LAGOPUS_RESULT_POSIX_API_ERROR; } } } } else { ret = LAGOPUS_RESULT_INVALID_ARGS; } return ret; }
bool ReadWriteLock::BeginReadTimeout(long milliseconds) { #if defined(MEDUSA_ANDROID)||defined(MEDUSA_IOS) const int sleepMillis = 5; StopWatch watch; watch.Start(); do { RETURN_TRUE_IF_TRUE(TryBeginRead()); Thread::Sleep(sleepMillis); watch.Shot(); } while (watch.ElapsedMilliseconds() < milliseconds); return false; #else struct timespec abstime = PerformanceCounter::ToAbsoluteTime(milliseconds); int result = pthread_rwlock_timedrdlock(&mLock, &abstime); return result == 0; #endif }
static void *th_fn(void *arg) { struct sigaction act; struct timespec abs_timeout; int rc; handler_state = 2; expired = 0; /* Set up handler for SIGUSR1 */ act.sa_flags = 0; act.sa_handler = sig_handler; /* block all the signal when hanlding SIGUSR1 */ sigfillset(&act.sa_mask); sigaction(SIGUSR1, &act, 0); gettimeofday(&before_wait, NULL); abs_timeout.tv_sec = before_wait.tv_sec + TIMEOUT; abs_timeout.tv_nsec = before_wait.tv_usec * 1000; thread_state = ENTERED_THREAD; printf("thread: attempt timed read lock, %d seconds\n", TIMEOUT); rc = pthread_rwlock_timedrdlock(&rwlock, &abs_timeout); if (rc == 0) { printf("thread: correctly acquired read lock\n"); expired = 0; } else if (rc == ETIMEDOUT) { printf("thread: timer expired, did not acquire read lock"); expired = 1; } else { printf("Error at pthread_rwlock_timedrdlock()"); exit(PTS_UNRESOLVED); } gettimeofday(&after_wait, NULL); thread_state = EXITING_THREAD; pthread_exit(0); return NULL; }
DWORD VmDirRWLockReadLock( PVMDIR_RWLOCK pLock, DWORD dwMilliSec ) { #ifdef __APPLE__ return VMDIR_ERROR_OPERATION_NOT_PERMITTED; #else DWORD dwError = 0; if (!pLock || !pLock->bInitialized) { dwError = VMDIR_ERROR_INVALID_PARAMETER; BAIL_ON_VMDIR_ERROR(dwError); } if (dwMilliSec) { struct timespec ts = {0}; uint64_t iTimeInMSec = 0; iTimeInMSec = dwMilliSec + VmDirGetTimeInMilliSec(); ts.tv_sec = iTimeInMSec / MSECS_PER_SEC; ts.tv_nsec = (iTimeInMSec % MSECS_PER_SEC) * NSECS_PER_MSEC; dwError = pthread_rwlock_timedrdlock(&pLock->lock, &ts); BAIL_ON_VMDIR_ERROR(dwError); } else { dwError = pthread_rwlock_rdlock(&pLock->lock); BAIL_ON_VMDIR_ERROR(dwError); } error: return dwError; #endif }
int main() { struct timespec abstime, reltime = { 1, 0 }; (void) pthread_win32_getabstime_np(&abstime, &reltime); assert(rwlock == PTHREAD_RWLOCK_INITIALIZER); assert(pthread_rwlock_timedrdlock(&rwlock, &abstime) == 0); assert(rwlock != PTHREAD_RWLOCK_INITIALIZER); assert(rwlock != NULL); assert(pthread_rwlock_unlock(&rwlock) == 0); assert(pthread_rwlock_destroy(&rwlock) == 0); assert(rwlock == NULL); return 0; }