Beispiel #1
0
void test_dispatch_latency(unsigned samples, unsigned sleeptime)
{
    unsigned i;

    uint64_t *lat = (uint64_t *)malloc(sizeof(uint64_t) * 3);
    uint64_t *history = (uint64_t *)malloc(sizeof(uint64_t) * samples);

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();

    for (i = 0; i < samples; i++) {
        usleep(sleeptime);

        /*
         lat[0] to lat[1] is latency to block execution
         lat[1] to lat[2] is latency from block execution to block synchronization
         */
        lat[0] = libtime_cpu();
        dispatch_group_async(group, queue, ^{ lat[1] = libtime_cpu(); });
        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
        lat[2] = libtime_cpu();

        /*
        printf("%04u lat[0] = %" PRIu64 "\n", i, lat[0]);
        printf("%04u lat[1] = %" PRIu64 ", %" PRIu64 "\n", i, lat[1], lat[1] - lat[0]);
        printf("%04u lat[2] = %" PRIu64 ", %" PRIu64 "\n", i, lat[2], lat[2] - lat[1]);
        */

        history[i] = lat[1] - lat[0];
        //history[i] = lat[2] - lat[1];
    }
Beispiel #2
0
static void
render(unsigned char *img, int comps, int w, int h, int nsubsamples, int tilew, int tileh)
{

    float *fimg = (float *)malloc(sizeof(float) * w * h * 3);
    memset((void *)fimg, 0, sizeof(float) * w * h * 3);
    
    dispatch_queue_t dque = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //dispatch_queue_t dque = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
    dispatch_group_t dgroup = dispatch_group_create();
    
    int i, j;
    for (j = 0; j < h; j += tileh) {
        for (i = 0; i < w; i += tilew) {

            void (^renderblock)(void) = ^{
                render_tile(img, comps, fimg, w, h, nsubsamples, i, j, tilew, tileh);
            };
            //renderblock();
            dispatch_group_async(dgroup, dque, renderblock);
        }
    }
    
    dispatch_group_wait(dgroup, DISPATCH_TIME_FOREVER);
    //dispatch_release(dgroup);
}
Beispiel #3
0
static VALUE
rb_group_wait(VALUE self, SEL sel, int argc, VALUE *argv)
{
    VALUE num;
    rb_scan_args(argc, argv, "01", &num);
    return dispatch_group_wait(RGroup(self)->group, rb_num2timeout(num))
        == 0 ? Qtrue : Qfalse;
}
Beispiel #4
0
void ISPCSync() {
    if (!initialized) {
        fprintf(stderr, "You must call TasksInit() before launching tasks.\n");
        exit(1);
    }

    // Wait for all of the tasks in the group to complete before returning
    dispatch_group_wait(gcdGroup, DISPATCH_TIME_FOREVER);
}
void dispatch_group_function() {
	long res;
	dispatch_group_t group;

	MU_BEGIN_TEST(dispatch_group_function);

	group = create_group(100, 0);
	MU_ASSERT_NOT_NULL(group);

	dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
	
	// should be OK to re-use a group
	dispatch_group_async_f(group, dispatch_get_global_queue(0, 0), 0, foo);
	dispatch_group_wait(group, DISPATCH_TIME_FOREVER);

	dispatch_release(group);
	group = NULL;
	
	group = create_group(3, 7);
	MU_ASSERT_NOT_NULL(group);

	res = dispatch_group_wait(group, dispatch_time(DISPATCH_TIME_NOW, 5ull * NSEC_PER_SEC));
    MU_ASSERT_EQUAL(!res, 0);

	// retry after timeout (this time succeed)
	res = dispatch_group_wait(group, dispatch_time(DISPATCH_TIME_NOW, 5ull * NSEC_PER_SEC));
    MU_ASSERT_EQUAL(res, 0);

	dispatch_release(group);
	group = NULL;

	group = create_group(100, 0);
	MU_ASSERT_NOT_NULL(group);

	dispatch_group_notify_f(group, dispatch_get_main_queue(), 0, group_notify);
	
	dispatch_release(group);
	group = NULL;

	dispatch_main();

	MU_FAIL("Should never reach this");
	MU_END_TEST
}
Beispiel #6
0
void RequestTimer::cancelTimerSource() {
  if (m_timerSource) {
    dispatch_source_cancel(m_timerSource);
    dispatch_group_wait(m_timerGroup, DISPATCH_TIME_FOREVER);

    // At this point it is safe to free memory, the source or even ourselves (if
    // this is part of the destructor). See the way we set up the timer group
    // and cancellation handler in setTimeout() below.

    dispatch_release(m_timerSource);
    m_timerSource = nullptr;
  }
}
Beispiel #7
0
static void redirect_atexit(void) {
    int i;

    /* stdout is linebuffered, so flush the buffer */
    if (redirect_descriptors[STDOUT_FILENO].buf)
        fflush(stdout);

    /* Cancel all of our dispatch sources, so they flush to ASL */
    for (i=0; i < n_redirect_descriptors; i++)
        if (redirect_descriptors[i].read_source)
            dispatch_source_cancel(redirect_descriptors[i].read_source);

    /* Wait at least three seconds for our sources to flush to ASL */
    dispatch_group_wait(read_source_group, dispatch_time(DISPATCH_TIME_NOW, 3LL * NSEC_PER_SEC));
}
Beispiel #8
0
static DWORD WINAPI taskEntry(LPVOID arg) {
#else
static void *taskEntry(void *arg) {
#endif
    while (true) {
        workerSemaphore.Wait();
        // Try to get task from task queue
        Task *myTask = NULL;
        {   MutexLock lock(*taskQueueMutex);
            if (taskQueue.size() == 0)
                break;
            myTask = taskQueue.back();
            taskQueue.pop_back();
        }

        // Do work for _myTask_
        PBRT_STARTED_TASK(myTask);
        myTask->Run();
        PBRT_FINISHED_TASK(myTask);
        tasksRunningCondition.Lock();
        int unfinished = --numUnfinishedTasks;
        if (unfinished == 0)
            tasksRunningCondition.Signal();
        tasksRunningCondition.Unlock();
    }
    // Cleanup from task thread and exit
#ifdef PBRT_HAS_PTHREADS
    pthread_exit(NULL);
#endif // PBRT_HAS_PTHREADS
    return 0;
}


#endif // !PBRT_USE_GRAND_CENTRAL_DISPATCH
void WaitForAllTasks() {
#ifdef PBRT_USE_GRAND_CENTRAL_DISPATCH
    dispatch_group_wait(gcdGroup, DISPATCH_TIME_FOREVER);
#else
    tasksRunningCondition.Lock();
    while (numUnfinishedTasks > 0)
        tasksRunningCondition.Wait();
    tasksRunningCondition.Unlock();
#endif
}
Beispiel #9
0
long wait(dispatch_group_t g, long time) {
    dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, time);
    return dispatch_group_wait(g, timeout);
}
Beispiel #10
0
bool group::wait(
    dispatch_time_t time
)
{
    return dispatch_group_wait( m_native, time ) == 0;
}