static void main_context1 (void) { IrisScheduler *scheduler; IrisReceiver *receiver; IrisPort *port; IrisMessage *message; gint counter = 0; gint i; // Use basic scheduler scheduler = iris_scheduler_new (); g_assert (scheduler != NULL); port = iris_port_new (); g_assert (port != NULL); receiver = iris_arbiter_receive (scheduler, port, main_context1_cb, &counter, NULL); g_assert (receiver != NULL); for (i = 0; i < 100; i++) { message = iris_message_new (1); iris_port_post (port, message); } // THIS IS A RACE CONDITION. But seems to be long enough for my // testing so far. Of course, its the entire reason we will be // making IrisTask soon. g_usleep (G_USEC_PER_SEC / 4); g_assert (counter == 100); }
/** * iris_thread_shutdown: * @thread: An #IrisThread * * Sends a message to the thread asking it to shutdown. */ void iris_thread_shutdown (IrisThread *thread) { IrisMessage *message; iris_debug (IRIS_DEBUG_THREAD); g_return_if_fail (thread != NULL); message = iris_message_new (MSG_SHUTDOWN); g_async_queue_push (thread->queue, message); }
/* load-spread: make sure all available threads get used! */ static void test_load_spread () { IrisScheduler *work_scheduler; IrisProcess *process[4]; gint i, wait_state[4]; gboolean reached_state; work_scheduler = iris_scheduler_new_full (4, 4); iris_set_default_work_scheduler (work_scheduler); memset (wait_state, 0, 4 * sizeof(int)); for (i=0; i<4; i++) { process[i] = iris_process_new (wait_func, &wait_state[i], NULL); iris_process_enqueue (process[i], iris_message_new (0)); iris_process_close (process[i]); iris_process_run (process[i]); } /* This will hang if the scheduler does not queue the work properly, it * will only proceed if each work item was put in a separate thread. */ do { reached_state = TRUE; for (i=0; i<4; i++) if (!g_atomic_int_get (&wait_state[i])) reached_state = FALSE; g_thread_yield (); } while (!reached_state); for (i=0; i<4; i++) g_atomic_int_set (&wait_state[i], 2); g_object_unref (work_scheduler); }