void * consumer( void * nitems_arg) { int i; long nitems; prod_data_t * data; long thread_id; nitems = (long) nitems_arg; thread_id_assign(); thread_id = thread_id_get(); wait_for_all(); for (i = 0; i < nitems ; i++) { globus_mutex_lock(&queue_mutex); { while(globus_fifo_empty(&queue)) { # if (DEBUG_LEVEL > 1) { globus_stdio_lock(); { printf("%04ld: consumer() - waiting for data item %d\n", thread_id, i); } globus_stdio_unlock(); } # endif globus_cond_wait(&queue_cond, &queue_mutex); } data = globus_fifo_dequeue(&queue); } globus_mutex_unlock(&queue_mutex); globus_mutex_lock(&data->mutex); { data->done = GLOBUS_TRUE; globus_cond_signal(&data->cond); } globus_mutex_unlock(&data->mutex); } wait_for_all(); return NULL; }
virtual task *execute() { ContextPtr context = new Context( *m_context ); context->set( ScenePlug::scenePathContextName, m_scenePath ); Context::Scope scopedContext( context ); m_scenePlug->transformPlug()->getValue(); m_scenePlug->boundPlug()->getValue(); m_scenePlug->attributesPlug()->getValue(); m_scenePlug->objectPlug()->getValue(); ConstInternedStringVectorDataPtr childNamesData = m_scenePlug->childNamesPlug()->getValue(); const vector<InternedString> &childNames = childNamesData->readable(); set_ref_count( 1 + childNames.size() ); ScenePlug::ScenePath childPath = m_scenePath; childPath.push_back( InternedString() ); // space for the child name for( vector<InternedString>::const_iterator it = childNames.begin(), eIt = childNames.end(); it != eIt; it++ ) { childPath[m_scenePath.size()] = *it; SceneTraversalTask *t = new( allocate_child() ) SceneTraversalTask( m_scenePlug, m_context, childPath ); spawn( *t ); } wait_for_all(); return 0; }
task* execute () { count_t base = my_groupId * NumLeafTasks; set_ref_count(NumLeafTasks + 1); for ( count_t i = 0; i < NumLeafTasks; ++i ) spawn( *new(allocate_child()) LeafTask(base + i) ); wait_for_all(); return NULL; }
/*override*/ tbb::task* execute() { ASSERT( !(~theLocalState->m_flags & m_flag), NULL ); if( N < 2 ) return NULL; bool globalBarrierActive = false; if ( theLocalState->m_isMaster ) { if ( theGlobalBarrierActive ) { // This is the root task. Its N is equal to the number of threads. // Spawn a task for each worker. set_ref_count(N); for ( int i = 1; i < N; ++i ) spawn( *new( allocate_child() ) FibTask(20, m_flag, m_observer) ); if ( theTestMode & tmSynchronized ) { theGlobalBarrier.wait(); ASSERT( m_observer.m_entries >= N, "Wrong number of on_entry calls after the first barrier" ); // All the spawned tasks have been stolen by workers. // Now wait for workers to spawn some more tasks for this thread to steal back. theGlobalBarrier.wait(); ASSERT( !theGlobalBarrierActive, "Workers are expected to have reset this flag" ); } else theGlobalBarrierActive = false; wait_for_all(); return NULL; } } else { if ( theGlobalBarrierActive ) { if ( theTestMode & tmSynchronized ) { theGlobalBarrier.wait(); globalBarrierActive = true; } theGlobalBarrierActive = false; } } set_ref_count(3); spawn( *new( allocate_child() ) FibTask(N-1, m_flag, m_observer) ); spawn( *new( allocate_child() ) FibTask(N-2, m_flag, m_observer) ); if ( globalBarrierActive ) { // It's the first task executed by a worker. Release the master thread. theGlobalBarrier.wait(); } wait_for_all(); return NULL; }
void *student_tests(int arg1, void *arg2) { kmutex_init(&mutex); kmutex_init(&reader_mutex); kmutex_init(&writer_mutex); /* Student Test 1 */ int rv = 0; int i = 0; dbg_print("broadcast order test"); race2 = 0; for (i = 0; i < 10; i++ ) start_proc("broadcast order test", broadcast_order, 0); stop_until_queued(10, &wake_me_len2); race2 = 0; sched_broadcast_on(&wake_me_q); wait_for_all(); KASSERT(wake_me_len2 == 0 && "Error on wakeme bookkeeping"); /* Student Test 2 */ int checkVal = 0; dbg_print("reader writer test"); start_proc("reader writer test", reader, 0); start_proc("reader writer test", reader, 0); start_proc("reader writer test", writer, 0); checkVal++; start_proc("reader writer test", reader, 1); start_proc("reader writer test", reader, 1); start_proc("reader writer test", reader, 1); start_proc("reader writer test", writer, 0); checkVal++; start_proc("reader writer test", writer, 0); checkVal++; start_proc("reader writer test", reader, 3); start_proc("reader writer test", reader, 3); wait_for_all(); KASSERT(checkVal == rwval && "Error in Reader Writer!"); return NULL; }
int Get_MultiTask::svc () { ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) multitask started\n"))); ACE::HTTP::URL http_url; const char* url = get_url (); ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) parsing URL %C\n"), url)); if (http_url.parse (url)) { ACE::HTTP::ClientRequestHandler rh; http_url.open (rh); if (rh.response_stream ()) ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) opened URL %C\n"), http_url.to_string ().c_str ())); else ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t) failed to open URL %C\n"), http_url.to_string ().c_str ())); wait_for_all (); if (rh.response_stream ()) { ACE::IOS::CString_OStream sos; sos << rh.response_stream ().rdbuf (); ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) result downloaded\n"))); if (!sos.bad ()) { ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard_, lock_, 0); ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) marking result\n"))); --n_fails_; } } shutdown (); } ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%P|%t) task finished\n"))); return 0; }
/*override*/ task* execute() { if( !g_sandwich && n<2 ) { result = n; } else { int x,y; tbb::task_scheduler_init init(P_nested); task* self0 = &task::self(); set_ref_count( 3 ); if ( g_sandwich ) { spawn (*new( allocate_child() ) FibCilkSubtask(x,n-1)); spawn (*new( allocate_child() ) FibCilkSubtask(y,n-2)); } else { spawn (*new( allocate_child() ) FibTask(x,n-1)); spawn (*new( allocate_child() ) FibTask(y,n-2)); } wait_for_all(); task* self1 = &task::self(); ASSERT( self0 == self1, "failed to preserve TBB TLS" ); result = x+y; } return NULL; }
void mir_buffer_stream_set_scale_sync(MirBufferStream* opaque_stream, float scale) { auto wh = mir_buffer_stream_set_scale(opaque_stream, scale); if (wh) wh->wait_for_all(); }
tbb::task* execute () { set_ref_count(2); spawn ( *new(allocate_child()) tbb::empty_task ); wait_for_all(); return NULL; }
task* execute () { SpawnChildren(this); wait_for_all(); return NULL; }
int main( int argc, char * argv[]) { int i; int nitems; long thread_id; globus_thread_t thread; globus_thread_set_model("pthread"); globus_module_activate(GLOBUS_COMMON_MODULE); if (argc != 4) { globus_stdio_lock(); { printf("\nusage: globus_thread_test " "nproducers nconsumers nitems\n\n"); } globus_stdio_unlock(); exit(1); } nproducers = atoi(argv[1]); nconsumers = atoi(argv[2]); nitems = atoi(argv[3]); /* * Initialize queue and queue concurrency control structures */ globus_fifo_init(&queue); globus_mutex_init(&queue_mutex, (globus_mutexattr_t *) GLOBUS_NULL); globus_cond_init(&queue_cond, (globus_condattr_t *) GLOBUS_NULL); /* * Initialize shared (common) concurrency control structures */ globus_mutex_init(&common_mutex, (globus_mutexattr_t *) GLOBUS_NULL); globus_cond_init(&common_cond, (globus_condattr_t *) GLOBUS_NULL); /* * Assign a thread id to the main thread so that it's output is uniquely * tagged. Note: we do not use the return value of globus_thread_self() * since it could be a pointer or a structure, the latter which is * extremely hard to print without knowing the implementation details. */ thread_id_assign(); thread_id = thread_id_get(); /* * Start producer and consumer threads */ globus_stdio_lock(); { printf("%04ld: main() - starting %d producer and %d consumer threads\n", thread_id, nproducers, nconsumers); } globus_stdio_unlock(); for (i = 0 ; i < nproducers ; i ++) { int rc; int nitems_per_thread; nitems_per_thread = nitems / nproducers + ((i < nitems % nproducers) ? 1 : 0); rc = globus_thread_create( &thread, NULL, producer, (void *) nitems_per_thread); if (rc != 0) { globus_stdio_lock(); { printf("%04ld: main() - ERROR: " "unable to create producer thread %d\n", thread_id, i); exit(1); } globus_stdio_unlock(); } } for (i = 0 ; i < nconsumers ; i ++) { int rc; int nitems_per_thread; nitems_per_thread = nitems / nconsumers + ((i < nitems % nconsumers) ? 1 : 0); rc = globus_thread_create( &thread, NULL, consumer, (void *) nitems_per_thread); if (rc != 0) { globus_stdio_lock(); { printf("%04ld: main() - ERROR: " "unable to create consumer thread %d\n", thread_id, i); exit(1); } globus_stdio_unlock(); } } /* * Wait for all threads to be started */ wait_for_all(); globus_stdio_lock(); { printf("%04ld: main() - all threads started\n", thread_id); } globus_stdio_unlock(); /* * Wait for all threads to complete their work */ wait_for_all(); globus_stdio_lock(); { printf("%04ld: main() - all threads have completed their work\n", thread_id); } globus_stdio_unlock(); /* * Wait for all thread id data to be destroyed */ while (thread_ids_destruct_cnt < nproducers + nconsumers) { globus_thread_yield(); } globus_stdio_lock(); { printf("%04ld: main() - all threads terminated\n", thread_id); } globus_stdio_unlock(); globus_cond_destroy(&common_cond); globus_mutex_destroy(&common_mutex); globus_cond_destroy(&queue_cond); globus_mutex_destroy(&queue_mutex); globus_fifo_destroy(&queue); globus_module_deactivate(GLOBUS_COMMON_MODULE); exit(0); }
void * producer( void * nitems_arg) { long i; long nitems; prod_data_t data; long thread_id; nitems = (long) nitems_arg; globus_mutex_init(&data.mutex, (globus_mutexattr_t *) GLOBUS_NULL); globus_cond_init(&data.cond, (globus_condattr_t *) GLOBUS_NULL); thread_id_assign(); thread_id = thread_id_get(); wait_for_all(); for (i = 0; i < nitems ; i++) { data.done = GLOBUS_FALSE; globus_mutex_lock(&queue_mutex); { globus_fifo_enqueue(&queue, &data); globus_cond_signal(&queue_cond); } globus_mutex_unlock(&queue_mutex); globus_mutex_lock(&data.mutex); { while(data.done == GLOBUS_FALSE) { # if (DEBUG_LEVEL > 1) { globus_stdio_lock(); { printf("%04ld: producer() - " "waiting for confirmation %d\n", thread_id, i); } globus_stdio_unlock(); } # endif globus_cond_wait(&data.cond, &data.mutex); } } globus_mutex_unlock(&data.mutex); } globus_cond_destroy(&data.cond); globus_mutex_destroy(&data.mutex); wait_for_all(); return NULL; }
/* The core testproc code */ void *testproc(int arg1, void *arg2) { proc_thread_t pt; pid_t pid = -1; int rv = 0; int i = 0; #if CS402TESTS > 0 dbg_print("waitpid any test"); start_proc(&pt, "waitpid any test", waitpid_test, 23); wait_for_any(); dbg_print("waitpid test"); start_proc(&pt, "waitpid test", waitpid_test, 32); pid = do_waitpid(2323, 0, &rv); if ( pid != -ECHILD ) dbg_print("Allowed wait on non-existent pid\n"); wait_for_proc(pt.p); /* dbg_print("kthread exit test"); start_proc(&pt, "kthread exit test", kthread_exit_test, 0); wait_for_proc(pt.p); */ /* dbg_print("many test"); for (i = 0; i < 10; i++) start_proc(NULL, "many test", waitpid_test, i); wait_for_all();a */ #endif #if CS402TESTS > 1 dbg_print("Context switch test"); start_proc(&pt, "Context switch", racer_test, 0); wait_for_proc(pt.p); #endif #if CS402TESTS > 2 sched_queue_init(&wake_me_q); dbg_print("wake me test"); wake_me_len = 0; start_proc(&pt, "wake me test", wakeme_test, 0); /* Make sure p has blocked */ stop_until_queued(1, &wake_me_len); sched_wakeup_on(&wake_me_q); wait_for_proc(pt.p); KASSERT(wake_me_len == 0 && "Error on wakeme bookkeeping"); dbg_print("broadcast me test"); for (i = 0; i < 10; i++ ) start_proc(NULL, "broadcast me test", wakeme_test, 0); stop_until_queued(10, &wake_me_len); /* Make sure the processes have blocked */ sched_broadcast_on(&wake_me_q); wait_for_all(); KASSERT(wake_me_len == 0 && "Error on wakeme bookkeeping"); #endif #if CS402TESTS > 3 dbg_print("wake me uncancellable test"); start_proc(&pt, "wake me uncancellable test", wakeme_uncancellable_test, 0); /* Make sure p has blocked */ stop_until_queued(1, &wake_me_len); sched_wakeup_on(&wake_me_q); wait_for_proc(pt.p); KASSERT(wake_me_len == 0 && "Error on wakeme bookkeeping"); dbg_print("broadcast me uncancellable test"); for (i = 0; i < 10; i++ ) start_proc(NULL, "broadcast me uncancellable test", wakeme_uncancellable_test, 0); /* Make sure the processes have blocked */ stop_until_queued(10, &wake_me_len); sched_broadcast_on(&wake_me_q); wait_for_all(); KASSERT(wake_me_len == 0 && "Error on wakeme bookkeeping"); #endif #if CS402TESTS > 4 dbg_print("cancel me test"); start_proc(&pt, "cancel me test", cancelme_test, 0); /* Make sure p has blocked */ stop_until_queued(1, &wake_me_len); sched_cancel(pt.t); wait_for_proc(pt.p); KASSERT(wake_me_len == 0 && "Error on wakeme bookkeeping"); dbg_print("prior cancel me test"); start_proc(&pt, "prior cancel me test", cancelme_test, 0); /* Cancel before sleep */ sched_cancel(pt.t); wait_for_proc(pt.p); KASSERT(wake_me_len == 0 && "Error on wakeme bookkeeping"); dbg_print("cancel me head test"); start_proc(&pt, "cancel me head test", cancelme_test, 0); start_proc(NULL, "cancel me head test", wakeme_test, 0); stop_until_queued(2, &wake_me_len); sched_cancel(pt.t); sched_wakeup_on(&wake_me_q); wait_for_all(); KASSERT(wake_me_len == 0 && "Error on wakeme bookkeeping"); dbg_print("cancel me tail test"); start_proc(NULL, "cancel me tail test", wakeme_test, 0); start_proc(&pt, "cancel me tail test", cancelme_test, 0); stop_until_queued(2, &wake_me_len); sched_cancel(pt.t); sched_wakeup_on(&wake_me_q); wait_for_all(); KASSERT(wake_me_len == 0 && "Error on wakeme bookkeeping"); #endif #if CS402TESTS > 5 dbg_print("Reparenting test"); start_proc(NULL, "Reparenting test", reparent_test, 1); stop_until_queued(1, &wake_me_len); sched_wakeup_on(&wake_me_q); wait_for_all(); stop_until_zero(&wake_me_len); dbg_print("Reparenting stress test"); start_proc(NULL, "Reparenting stress test", reparent_test, 10); stop_until_queued(10, &wake_me_len); sched_broadcast_on(&wake_me_q); wait_for_all(); stop_until_zero(&wake_me_len); KASSERT(wake_me_len == 0 && "Error on wakeme bookkeeping"); #endif #if CS402TESTS > 6 kmutex_init(&mutex); dbg_print("show race test"); race = 0; for (i = 0; i < 10; i++ ) start_proc(NULL, "show race test", racer_test, 0); wait_for_all(); dbg_print("fix race test"); race = 0; for (i = 0; i < 10; i++ ) start_proc(NULL, "fix race test", mutex_uncancellable_test, 0); wait_for_all(); dbg_print("fix race test w/cancel"); race = 0; for (i = 0; i < 10; i++ ) { if ( i % 2 == 0) { start_proc(NULL, "fix race test w/cancel", mutex_test, 0); } else { start_proc(&pt, "fix race test w/cancel", mutex_test_cancelme, 0); sched_cancel(pt.t); } } wait_for_all(); #endif #if CS402TESTS > 7 dbg_print("kill all test"); for ( i=0 ; i < 10; i++ ) start_proc(NULL, "kill all test", cancelme_test, 0); stop_until_queued(10, &wake_me_len); proc_kill_all(); wait_for_all(); KASSERT(wake_me_len == 0 && "Error on wakeme bookkeeping"); #endif #if CS402TESTS > 8 student_tests(arg1, arg2); #endif dbg_print("All tests completed\n\n"); return NULL; }