static void* _profile_stream_thread(void* arg) { FOUNDATION_UNUSED(arg); thread_yield(); while (!thread_try_wait(4)) { profile_log(STRING_CONST("Thread message")); profile_begin_block(STRING_CONST("Thread block")); { profile_update_block(); profile_begin_block(STRING_CONST("Thread subblock")); { profile_log(STRING_CONST("Sub message")); profile_trylock(STRING_CONST("Trylock")); profile_lock(STRING_CONST("Trylock")); profile_wait(STRING_CONST("Wait")); profile_signal(STRING_CONST("Signal")); thread_sleep(2); profile_unlock(STRING_CONST("Trylock")); profile_log(STRING_CONST("End sub")); } profile_end_block(); profile_begin_block(STRING_CONST("Thread second subblock")); { profile_update_block(); profile_begin_block(STRING_CONST("Thread subblock")); { } profile_end_block(); } profile_end_block(); profile_trylock(STRING_CONST("Trylock")); thread_sleep(1); profile_lock(STRING_CONST("Trylock")); thread_sleep(4); profile_unlock(STRING_CONST("Trylock")); } profile_end_block(); atomic_add64(&_profile_generated_blocks, 14); } return 0; }
static void* _profile_stream_thread( object_t thread, void* arg ) { FOUNDATION_UNUSED( arg ); thread_yield(); while( !thread_should_terminate( thread ) ) { profile_log( "Thread message" ); profile_begin_block( "Thread block" ); { profile_update_block(); profile_begin_block( "Thread subblock" ); { profile_log( "Sub message" ); profile_trylock( "Trylock" ); profile_lock( "Trylock" ); profile_wait( "Wait" ); profile_signal( "Signal" ); thread_sleep( 2 ); profile_unlock( "Trylock" ); profile_log( "End sub" ); } profile_end_block(); profile_trylock( "Trylock" ); thread_sleep( 1 ); profile_lock( "Trylock" ); thread_sleep( 4 ); profile_unlock( "Trylock" ); } profile_end_block(); thread_sleep( 4 ); atomic_add64( &_profile_generated_blocks, 12 ); } return 0; }
bool mutex_lock(mutex_t* mutex) { #if !BUILD_DEPLOY profile_trylock(mutex->name.str, mutex->name.length); #endif #if FOUNDATION_PLATFORM_WINDOWS EnterCriticalSection((CRITICAL_SECTION*)mutex->csection); #elif FOUNDATION_PLATFORM_POSIX || FOUNDATION_PLATFORM_PNACL if (pthread_mutex_lock(&mutex->mutex) != 0) { FOUNDATION_ASSERT_FAILFORMAT("unable to lock mutex %s", mutex->name.str); return false; } #else # error mutex_lock not implemented #endif #if !BUILD_DEPLOY profile_lock(mutex->name.str, mutex->name.length); #endif FOUNDATION_ASSERT_MSGFORMAT(!mutex->lockcount || (thread_id() == mutex->lockedthread), "Mutex lock acquired with lockcount > 0 (%d) and locked thread not self (%" PRIx64 " != %" PRIx64 ")", mutex->lockcount, mutex->lockedthread, thread_id()); if (!mutex->lockcount) mutex->lockedthread = thread_id(); ++mutex->lockcount; return true; }
bool mutex_try_lock(mutex_t* mutex) { bool was_locked; #if !BUILD_DEPLOY profile_trylock(mutex->name.str, mutex->name.length); #endif #if FOUNDATION_PLATFORM_WINDOWS was_locked = TryEnterCriticalSection((CRITICAL_SECTION*)mutex->csection); #elif FOUNDATION_PLATFORM_POSIX || FOUNDATION_PLATFORM_PNACL was_locked = (pthread_mutex_trylock(&mutex->mutex) == 0); #else # error mutex_try_lock not implemented was_locked = false; #endif #if !BUILD_DEPLOY if (was_locked) profile_lock(mutex->name.str, mutex->name.length); #endif if (was_locked) { FOUNDATION_ASSERT(!mutex->lockcount || (thread_id() == mutex->lockedthread)); if (!mutex->lockcount) mutex->lockedthread = thread_id(); ++mutex->lockcount; } return was_locked; }
static void* _profile_fail_thread(void* arg) { FOUNDATION_UNUSED(arg); thread_sleep(10); while (!thread_try_wait(1)) { profile_log(STRING_CONST("Thread message")); profile_begin_block(STRING_CONST("Thread block")); { profile_update_block(); profile_begin_block(STRING_CONST("Thread subblock")); { profile_log(STRING_CONST("Sub message")); profile_trylock(STRING_CONST("Trylock")); profile_lock(STRING_CONST("Trylock")); profile_wait(STRING_CONST("Wait")); profile_signal(STRING_CONST("Signal")); profile_unlock(STRING_CONST("Trylock")); profile_log(STRING_CONST("End sub")); thread_yield(); } profile_end_block(); } profile_end_block(); } return 0; }