DECLARE_TEST(mutex, sync) { mutex_t* mutex; thread_t thread[32]; size_t ith; mutex = mutex_allocate(STRING_CONST("test")); mutex_lock(mutex); for (ith = 0; ith < 32; ++ith) thread_initialize(&thread[ith], mutex_thread, mutex, STRING_CONST("mutex_thread"), THREAD_PRIORITY_NORMAL, 0); for (ith = 0; ith < 32; ++ith) thread_start(&thread[ith]); test_wait_for_threads_startup(thread, 32); mutex_unlock(mutex); test_wait_for_threads_finish(thread, 32); for (ith = 0; ith < 32; ++ith) thread_finalize(&thread[ith]); mutex_deallocate(mutex); EXPECT_EQ(thread_counter, 32 * 128); return 0; }
DECLARE_TEST( mutex, sync ) { mutex_t* mutex; object_t thread[32]; int ith; mutex = mutex_allocate( "test" ); mutex_lock( mutex ); for( ith = 0; ith < 32; ++ith ) { thread[ith] = thread_create( mutex_thread, "mutex_thread", THREAD_PRIORITY_NORMAL, 0 ); thread_start( thread[ith], mutex ); } test_wait_for_threads_startup( thread, 32 ); for( ith = 0; ith < 32; ++ith ) { thread_terminate( thread[ith] ); thread_destroy( thread[ith] ); } mutex_unlock( mutex ); test_wait_for_threads_exit( thread, 32 ); mutex_deallocate( mutex ); EXPECT_EQ( thread_counter, 32 * 128 ); return 0; }
void lua_modulemap_finalize(void) { for (size_t ibucket = 0; ibucket < _lua_modulemap->num_buckets; ++ibucket) { hashmap_node_t* bucket = _lua_modulemap->bucket[ibucket]; for (size_t inode = 0, nsize = array_size(bucket); inode < nsize; ++inode) memory_deallocate(bucket[inode].value); } hashmap_deallocate(_lua_modulemap); mutex_deallocate(_lua_modulemap_lock); }
DECLARE_TEST(mutex, basic) { mutex_t* mutex; mutex = mutex_allocate(STRING_CONST("test")); EXPECT_CONSTSTRINGEQ(mutex_name(mutex), string_const(STRING_CONST("test"))); EXPECT_TRUE(mutex_try_lock(mutex)); EXPECT_TRUE(mutex_lock(mutex)); EXPECT_TRUE(mutex_try_lock(mutex)); EXPECT_TRUE(mutex_lock(mutex)); EXPECT_TRUE(mutex_unlock(mutex)); EXPECT_TRUE(mutex_unlock(mutex)); EXPECT_TRUE(mutex_unlock(mutex)); EXPECT_TRUE(mutex_unlock(mutex)); log_set_suppress(0, ERRORLEVEL_WARNING); EXPECT_FALSE(mutex_unlock(mutex)); log_set_suppress(0, ERRORLEVEL_INFO); mutex_signal(mutex); thread_yield(); EXPECT_TRUE(mutex_try_wait(mutex, 1)); EXPECT_TRUE(mutex_unlock(mutex)); mutex_signal(mutex); thread_yield(); EXPECT_TRUE(mutex_wait(mutex)); EXPECT_TRUE(mutex_unlock(mutex)); log_set_suppress(0, ERRORLEVEL_WARNING); EXPECT_FALSE(mutex_try_wait(mutex, 100)); EXPECT_FALSE(mutex_unlock(mutex)); log_set_suppress(0, ERRORLEVEL_INFO); mutex_signal(mutex); thread_yield(); EXPECT_TRUE(mutex_try_wait(mutex, 1)); log_set_suppress(0, ERRORLEVEL_WARNING); EXPECT_FALSE(mutex_try_wait(mutex, 100)); EXPECT_TRUE(mutex_unlock(mutex)); EXPECT_FALSE(mutex_unlock(mutex)); log_set_suppress(0, ERRORLEVEL_INFO); mutex_deallocate(mutex); return 0; }
void _random_shutdown( void ) { int i, size; if( _random_mutex ) mutex_lock( _random_mutex ); for( i = 0, size = array_size( _random_state ); i < size; ++i ) memory_deallocate( _random_state[i] ); array_deallocate( _random_available_state ); array_deallocate( _random_state ); set_thread_state( 0 ); if( _random_mutex ) { mutex_unlock( _random_mutex ); mutex_deallocate( _random_mutex ); } }
DECLARE_TEST( mutex, signal ) { mutex_t* mutex; object_t thread[32]; int ith; mutex = mutex_allocate( "test" ); mutex_lock( mutex ); for( ith = 0; ith < 32; ++ith ) { thread[ith] = thread_create( thread_wait, "thread_wait", THREAD_PRIORITY_NORMAL, 0 ); thread_start( thread[ith], mutex ); } mutex_unlock( mutex ); test_wait_for_threads_startup( thread, 32 ); while( atomic_load32( &thread_waiting ) < 32 ) thread_yield(); thread_sleep( 1000 ); //Hack wait to give threads time to progress from atomic_incr to mutex_wait mutex_signal( mutex ); for( ith = 0; ith < 32; ++ith ) { thread_terminate( thread[ith] ); thread_destroy( thread[ith] ); } test_wait_for_threads_exit( thread, 32 ); EXPECT_EQ( atomic_load32( &thread_waited ), 32 ); EXPECT_FALSE( mutex_wait( mutex, 500 ) ); mutex_deallocate( mutex ); return 0; }
DECLARE_TEST(mutex, signal) { mutex_t* mutex; thread_t thread[32]; size_t ith; mutex = mutex_allocate(STRING_CONST("test")); mutex_lock(mutex); for (ith = 0; ith < 32; ++ith) thread_initialize(&thread[ith], thread_waiter, mutex, STRING_CONST("thread_wait"), THREAD_PRIORITY_NORMAL, 0); for (ith = 0; ith < 32; ++ith) thread_start(&thread[ith]); mutex_unlock(mutex); test_wait_for_threads_startup(thread, 32); while (atomic_load32(&thread_waiting) < 32) thread_yield(); thread_sleep(1000); //Hack wait to give threads time to progress from atomic_incr to mutex_wait mutex_signal(mutex); test_wait_for_threads_finish(thread, 32); for (ith = 0; ith < 32; ++ith) thread_finalize(&thread[ith]); EXPECT_EQ(atomic_load32(&thread_waited), 32); EXPECT_FALSE(mutex_try_wait(mutex, 500)); mutex_deallocate(mutex); return 0; }