/*-------------------------------------------------------------------*/ DLL_EXPORT int hthread_obtain_lock( LOCK* plk, const char* location ) { int rc; U64 waitdur; ILOCK* ilk; TIMEVAL tv; ilk = (ILOCK*) plk->ilk; PTTRACE( "lock before", plk, NULL, location, PTT_MAGIC ); rc = hthread_mutex_trylock( &ilk->lock ); if (EBUSY == rc) { waitdur = host_tod(); rc = hthread_mutex_lock( &ilk->lock ); gettimeofday( &tv, NULL ); waitdur = host_tod() - waitdur; } else { gettimeofday( &tv, NULL ); waitdur = 0; } PTTRACE2( "lock after", plk, (void*) waitdur, location, rc, &tv ); if (rc) loglock( ilk, rc, "obtain_lock", location ); if (!rc || EOWNERDEAD == rc) { hthread_mutex_lock( &ilk->locklock ); ilk->location = location; ilk->tid = hthread_self(); memcpy( &ilk->time, &tv, sizeof( TIMEVAL )); hthread_mutex_unlock( &ilk->locklock ); } return rc; }
// Non-blocking busy-wait for a mutex. Unlike the usual mutex_lock, this // doesn't force a scheduling decision, and thus won't lead to a context // switch. void wait_on_lock(hthread_mutex_t * lock) { while(hthread_mutex_trylock(lock) != SUCCESS) { wait(1); } }
int main(int argc, char *argv[]) { hthread_mutexattr_t mutexAttr; hthread_mutex_t mutex; hthread_attr_t threadAttr; hthread_t thread; Huint arg; log_t log; //Initialize Log log_create( &log, 1024 ); //Mutex operations printf( "Starting mutex operations\n" ); hthread_mutexattr_init( &mutexAttr ); hthread_mutexattr_setnum( &mutexAttr, 0 ); hthread_mutexattr_getnum( &mutexAttr, &arg ); hthread_mutexattr_destroy( &mutexAttr ); hthread_mutex_init(&mutex, NULL); hthread_mutex_lock( &mutex ); hthread_mutex_unlock( &mutex ); hthread_mutex_trylock( &mutex ); hthread_mutex_unlock( &mutex ); hthread_mutex_destroy( &mutex ); //Condition Variable operations /* printf( "Starting condition variable operations\n" ); hthread_condattr_init( &condvarAttr ); hthread_condattr_setnum( &condvarAttr, 0 ); hthread_condattr_getnum( &condvarAttr, &arg ); hthread_condattr_destroy( &condvarAttr ); hthread_cond_init( &condvar, NULL ); hthread_mutex_lock( &mutex ); hthread_cond_wait( &condvar, &mutex ); hthread_mutex_unlock( &mutex ); hthread_cond_signal( &condvar ); hthread_cond_broadcast( &condvar ); hthread_cond_destroy( &condvar ); */ //Thread operations printf( "Starting thread operations\n" ); hthread_attr_init( &threadAttr ); hthread_create( &thread, &threadAttr, foo, &log ); hthread_attr_destroy( &threadAttr ); hthread_join( thread, NULL ); printf( " -- End of Program --\n" ); log_close_ascii( &log ); return 0; }
/*-------------------------------------------------------------------*/ DLL_EXPORT int hthread_test_lock( LOCK* plk, const char* location ) { int rc; ILOCK* ilk; UNREFERENCED( location ); ilk = (ILOCK*) plk->ilk; rc = hthread_mutex_trylock( &ilk->lock ); if (rc) return rc; hthread_mutex_unlock( &ilk->lock ); return 0; }
void * testThread ( void * arg ) { int retVal; hthread_mutex_t * mutex = (hthread_mutex_t *) arg; //Try to lock a locked mutex hthread_mutex_trylock( mutex ); //Test that another thread owns the mutex if ( _mutex_owner( mutex->num ) != hthread_self() ) retVal = SUCCESS; else retVal = FAILURE; hthread_exit( (void *) retVal ); return NULL; }
void * testThread ( void * arg ) { int retVal; hthread_mutex_t * mutex = (hthread_mutex_t *) arg; //Test that after trylock returns, on an unlocked mutex, the calling thread owns the mutex hthread_mutex_trylock( mutex ); if ( _mutex_owner( mutex->num ) == hthread_self() ) retVal = SUCCESS; else retVal = FAILURE; hthread_mutex_unlock( mutex ); hthread_exit( (void *) retVal ); return NULL; }
/*-------------------------------------------------------------------*/ DLL_EXPORT int hthread_try_obtain_lock( LOCK* plk, const char* location ) { int rc; ILOCK* ilk; TIMEVAL tv; ilk = (ILOCK*) plk->ilk; PTTRACE( "try before", plk, NULL, location, PTT_MAGIC ); rc = hthread_mutex_trylock( &ilk->lock ); gettimeofday( &tv, NULL ); PTTRACE2( "try after", plk, NULL, location, rc, &tv ); if (rc && EBUSY != rc) loglock( ilk, rc, "try_obtain_lock", location ); if (!rc || EOWNERDEAD == rc) { hthread_mutex_lock( &ilk->locklock ); ilk->location = location; ilk->tid = hthread_self(); memcpy( &ilk->time, &tv, sizeof( TIMEVAL )); hthread_mutex_unlock( &ilk->locklock ); } return rc; }