Exemplo n.º 1
0
void
func_outer(void* context)
{
    UNREFERENCED_PARAMETER(context);
    time_a_min = dispatch_time(0,  (int64_t)5.5*NSEC_PER_SEC);
    time_a     = dispatch_time(0,   6*NSEC_PER_SEC);
    time_a_max = dispatch_time(0,  (int64_t)6.5*NSEC_PER_SEC);
    dispatch_after_f(time_a, dispatch_get_current_queue(), NULL, func_a);
}
Exemplo n.º 2
0
static void dispatch_start(void* context) {
    dispatch_time_t time_a;
    time_delta_t* delta = (time_delta_t*)malloc(sizeof(time_delta_t));
    delta->min = dispatch_time(0,  NSEC_PER_SEC*5.5);
    time_a     = dispatch_time(0,  NSEC_PER_SEC*6.0);
    delta->max = dispatch_time(0,  NSEC_PER_SEC*6.5);
    delta->start = dispatch_time(0,0);
    dispatch_after_f(time_a, dispatch_get_current_queue(), delta, dispatch_6);
}
Exemplo n.º 3
0
void
func_a(void* context)
{
    dispatch_time_t now_a = dispatch_time(0, 0);
    UNREFERENCED_PARAMETER(context);
    test_time_less_than("can't finish faster than 5.5s", 0, now_a - time_a_min);
    test_time_less_than("must finish faster than  6.5s", 0, time_a_max - now_a);

    time_b_min = dispatch_time(0,  (int64_t)1.5*NSEC_PER_SEC);
    time_b     = dispatch_time(0,    2*NSEC_PER_SEC);
    time_b_max = dispatch_time(0,  (int64_t)2.5*NSEC_PER_SEC);
    dispatch_after_f(time_b, dispatch_get_current_queue(), NULL, func_b);
}
Exemplo n.º 4
0
static void dispatch_6(void* context) {
    dispatch_time_t time_b;
    time_delta_t* previous = (time_delta_t*)context;
    time_delta_t* delta = (time_delta_t*)malloc(sizeof(time_delta_t));
    dispatch_time_t now_a = dispatch_time(0, 0);
    MU_MESSAGE("must finish between 5.5s and 6.5s: %f",(now_a-previous->start)/(float)NSEC_PER_SEC);
    MU_ASSERT_TRUE(0<=(now_a - previous->min));
    MU_ASSERT_TRUE(0<=(previous->max - now_a));

    delta->min = dispatch_time(0,  1.5*NSEC_PER_SEC);
    time_b = dispatch_time(0,    2*NSEC_PER_SEC);
    delta->max = dispatch_time(0,  2.5*NSEC_PER_SEC);
    delta->start = dispatch_time(0,0);
    dispatch_after_f(time_b, dispatch_get_current_queue(), delta, dispatch_2);
}
Exemplo n.º 5
0
void RequestTimer::setTimeout(int seconds) {
  m_timeoutSeconds = seconds > 0 ? seconds : 0;

  cancelTimerSource();

  if (!m_timeoutSeconds) {
    return;
  }

  dispatch_queue_t q =
    dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  m_timerSource = dispatch_source_create(
    DISPATCH_SOURCE_TYPE_TIMER, 0, DISPATCH_TIMER_STRICT, q);

  dispatch_time_t t =
    dispatch_time(DISPATCH_TIME_NOW, m_timeoutSeconds * NSEC_PER_SEC);
  dispatch_source_set_timer(m_timerSource, t, DISPATCH_TIME_FOREVER, 0);

  // Use the timer group as a semaphore. When the source is cancelled,
  // libdispatch will make sure all pending event handlers have finished before
  // invoking the cancel handler. This means that if we cancel the source and
  // then wait on the timer group, when we are done waiting, we know the source
  // is completely done and it's safe to free memory (e.g., in the destructor).
  // See cancelTimerSource() above.
  dispatch_group_enter(m_timerGroup);
  dispatch_source_set_event_handler(m_timerSource, ^{
    onTimeout();

    // Cancelling ourselves isn't needed for correctness, but we can go ahead
    // and do it now instead of waiting on it later, so why not. (Also,
    // getRemainingTime does use this opportunistically, but it's best effort.)
    dispatch_source_cancel(m_timerSource);
  });
void
test_timer(void)
{
	dispatch_test_start("Dispatch Update Timer");

	dispatch_queue_t main_q = dispatch_get_main_queue();
	//test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue());

	__block int i = 0;
	struct timeval start_time;

	gettimeofday(&start_time, NULL);

	dispatch_source_t s = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, main_q);
	test_ptr_notnull("dispatch_source_create", s);

	dispatch_source_set_timer(s, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), NSEC_PER_SEC, 0);

	dispatch_source_set_cancel_handler(s, ^{
		struct timeval end_time;
		gettimeofday(&end_time, NULL);
		// Make sure we actually managed to adjust the interval
		// duration.  Seven one second ticks would blow past
		// this.
		test_long_less_than("total duration", end_time.tv_sec - start_time.tv_sec, 3);
		test_stop();

		dispatch_release(s);
	});
Exemplo n.º 7
0
SDMMD_AFCOperationRef SDMMD_AFCOperationCreateGetDeviceInfo() {
	SDMMD_AFCOperationRef op = calloc(1, sizeof(struct sdmmd_AFCOperation));
	op->packet = calloc(1, sizeof(struct sdmmd_AFCPacket));
	SDMMD_AFCHeaderInit(&(op->packet->header), SDMMD_AFC_Packet_GetDeviceInfo, sizeof(struct SDMMD_AFCPacketHeader), 0x0, 0x0);
	op->timeout = dispatch_time(DISPATCH_TIME_NOW, 0);
	return op;
}
Exemplo n.º 8
0
dispatch_time_t xdispatch::as_delayed_time(
    uint64_t delay,
    dispatch_time_t base
)
{
    return dispatch_time( base, delay );
}
Exemplo n.º 9
0
LRESULT CALLBACK window_proc(HWND window, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message) {
	case WM_COMMAND:
		{
			switch(HIWORD(wParam)) {
			case BN_CLICKED:
				{
					gcd::queue thread_q(gcd::queue::get_current_thread_queue());
					::OutputDebugStringW(L"WINDOW THREAD\n");
					gcd::queue::get_global_queue(0, 0).after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), [=] {
						::OutputDebugStringW(L"GLOBAL THREAD\n");
						thread_q.async([] {
							::OutputDebugStringW(L"BACK ON WINDOW THREAD\n");
						});
					});
				}
				break;
			}
		}
		break;
	case WM_DESTROY:
		::PostQuitMessage(0);
		break;

	default:
		return ::DefWindowProcW(window, message, wParam, lParam);
	}
	return 0;
}
Exemplo n.º 10
0
static void do_test(void)
{
	size_t i;
	char buf[1000];

	count_down = COUNT;

	start = dispatch_time(0,0);

	for (i = 0; i < COUNT; i++) {
#ifdef WIN32
		_snprintf(buf, sizeof(buf), "com.example.starfish-node#%ld", i);
#else
		snprintf(buf, sizeof(buf), "com.example.starfish-node#%ld", (long int)i);
#endif
		queues[i] = dispatch_queue_create(buf, NULL);
		dispatch_suspend(queues[i]);
	}

	for (i = 0; i < COUNT; i++) {
		dispatch_async_f(queues[i], queues[i], start_node);
	}

	for (i = 0; i < COUNT; i++) {
		dispatch_resume(queues[i]);
	}
}
Exemplo n.º 11
0
extern "C" void Qt_dispatch_group1() {
    bool res;
    QDispatchGroup* group;
    char* argv = QString("test").toAscii().data();
    int argc = 1;
    QDispatchCoreApplication app(argc,&argv);

    MU_BEGIN_TEST(Qt_dispatch_group1);

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

    group->wait();

    // should be OK to re-use a group
    group->async(new Foo, QDispatch::globalQueue());
    group->wait();

    delete group;
    group = NULL;

    group = createGroup(3, 7);
    MU_ASSERT_NOT_NULL(group);

    res = group->wait(dispatch_time(QDispatch::TimeNow, 5ull * NSEC_PER_SEC));
    MU_ASSERT_EQUAL(res, false);

    // retry after timeout (this time succeed)
    res = group->wait(dispatch_time(QDispatch::TimeNow, 5ull * NSEC_PER_SEC));
    MU_ASSERT_EQUAL(res, true);

    delete group;
    group = NULL;

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

    group->notify(new GroupNotify, QDispatch::mainQueue());

    delete group;
    group = NULL;

    app.exec();

    MU_FAIL("Should never reach this");
    MU_END_TEST
}
Exemplo n.º 12
0
bool __Platform_Red_Threading_Semaphore_TimeoutWait ( __Platform_Semaphore_t & Target, const Red::Util::Time :: Duration & Timeout )
{
	
	dispatch_time_t Delta = dispatch_time ( DISPATCH_TIME_NOW, Timeout.GetTotalNanoSeconds () );
	
	return dispatch_semaphore_wait ( Target, Delta ) != 0;
	
}
Exemplo n.º 13
0
static void
set_timer(void)
{
    dispatch_source_set_timer(timer,
			      dispatch_time(DISPATCH_TIME_NOW,
					    timeoutvalue * NSEC_PER_SEC),
			      timeoutvalue * NSEC_PER_SEC, 1000000);
}
Exemplo n.º 14
0
static void dispatch_0(void* context) {
    time_delta_t* previous = (time_delta_t*)context;
    dispatch_time_t now_c = dispatch_time(0, 0);
    MU_MESSAGE("must finish between 0.0s and 0.5s: %f",(now_c-previous->start)/(float)NSEC_PER_SEC);
    MU_ASSERT_TRUE(0<=(now_c - previous->min));
    MU_ASSERT_TRUE(0<=(previous->max - now_c));

    dispatch_async_f(dispatch_get_current_queue(), NULL, done);
}
Exemplo n.º 15
0
CF_PRIVATE dispatch_time_t __CFTSRToDispatchTime(uint64_t tsr) {
    uint64_t tsrInNanoseconds = __CFTSRToNanoseconds(tsr);
    
    // It's important to clamp this value to INT64_MAX or it will become interpreted by dispatch_time as a relative value instead of absolute time
    if (tsrInNanoseconds > INT64_MAX - 1) tsrInNanoseconds = INT64_MAX - 1;
    
    // 2nd argument of dispatch_time is a value in nanoseconds, but tsr does not equal nanoseconds on all platforms.
    return dispatch_time(1, (int64_t)tsrInNanoseconds);
}
Exemplo n.º 16
0
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
}
Exemplo n.º 17
0
bool SemHelper::wait(int timeout)
{
#ifdef WIN32
	if (timeout <= 0)
	{
		timeout = INFINITE;
	}
	if (WaitForSingleObject(_hSem, timeout) != WAIT_OBJECT_0)
	{
		return false;
	}
#elif defined(__APPLE__)
	if (dispatch_semaphore_wait(_semid, dispatch_time(DISPATCH_TIME_NOW, timeout*1000)) != 0)
	{
		return false;
	}
#else
	if (timeout <= 0)
	{
		return (sem_wait(&_semid) == 0);
	}
	else
	{
		struct timeval tm;
		gettimeofday(&tm, NULL);
		long long endtime = tm.tv_sec *1000 + tm.tv_usec/1000 + timeout;
		do 
		{
			sleepMillisecond(50);
			int ret = sem_trywait(&_semid);
			if (ret == 0)
			{
				return true;
			}
			struct timeval tv_cur;
			gettimeofday(&tv_cur, NULL);
			if (tv_cur.tv_sec*1000 + tv_cur.tv_usec/1000 > endtime)
			{
				return false;
			}

			if (ret == -1 && errno == EAGAIN)
			{
				continue;
			}
			else
			{
				return false;
			}
		} while (true);
		return false;
	}
#endif
	return true;
}
Exemplo n.º 18
0
void
func_c(void* context)
{
    dispatch_time_t now_c = dispatch_time(0, 0);
    UNREFERENCED_PARAMETER(context);

    test_time_less_than("can't finish faster than 0s", 0, now_c - time_c_min);
    test_time_less_than("must finish faster than .5s", 0, time_c_max - now_c);

    dispatch_async_f(dispatch_get_current_queue(), NULL, done);
}
Exemplo n.º 19
0
static void
		collect(void *context)
{
	uint64_t delta;
	long double math;
	size_t i;

	if (--count_down) {
		return;
	}

	delta = dispatch_time(0,0) - start;
	math = (long double)delta;
	math /= COUNT * COUNT * 2ul + COUNT * 2ul;

	MU_MESSAGE("lap: %ld", lap_count_down);
	MU_MESSAGE("count: %lu", COUNT);
	MU_MESSAGE("delta: %llu ns", (uintmax_t)delta);
	MU_MESSAGE("math: %Lf ns / lap", math);

	for (i = 0; i < COUNT; i++) {
		dispatch_release(queues[i]);
	}

	// our malloc could be a lot better,
	// this result is really a malloc torture test
	MU_ASSERT_TRUE((unsigned long)math<10000);

	if (--lap_count_down) {
		do_test();
		return;
	}

	// give the threads some time to settle before test_stop() runs "leaks"
	// ...also note, this is a total cheat.   dispatch_after lets this
	// thread go idle, so dispatch cleans up the continuations cache.
	// Doign the "old style" sleep left that stuff around and leaks
	// took a LONG TIME to complete.   Long enough that the test harness
	// decided to kill us.
	dispatch_after_f(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), NULL, MU_PASS_AFTER_DELAY);
}
Exemplo n.º 20
0
static void collect(void *context)
{
	
	MU_MESSAGE("All workers done.");

	// give the threads some time to settle before test_stop() runs "leaks"
	// ...also note, this is a total cheat.   dispatch_after lets this
	// thread go idle, so dispatch cleans up the continuations cache.
	// Doign the "old style" sleep left that stuff around and leaks
    // took a LONG TIME to complete. Long enough that the test harness
	// decided to kill us.
	dispatch_after_f(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), NULL, MU_PASS_AFTER_DELAY);
}
Exemplo n.º 21
0
static bool clearAllKVS(CFErrorRef *error)
{
    __block bool result = false;
    const uint64_t maxTimeToWaitInSeconds = 30ull * NSEC_PER_SEC;
    dispatch_queue_t processQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_semaphore_t waitSemaphore = dispatch_semaphore_create(0);
    dispatch_time_t finishTime = dispatch_time(DISPATCH_TIME_NOW, maxTimeToWaitInSeconds);
    
    SOSCloudKeychainClearAll(processQueue, ^(CFDictionaryRef returnedValues, CFErrorRef cerror)
    {
        result = (cerror != NULL);
        dispatch_semaphore_signal(waitSemaphore);
    });
static void setRepeatingTimer(struct nodeInstanceData *ctx, const VuoReal seconds, VuoOutputTrigger(fired,VuoInteger))
{
	if (ctx->timer)
		cancelRepeatingTimer(ctx);

	dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
	ctx->timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, q);

	uint64_t nanoseconds = seconds * NSEC_PER_SEC;
	dispatch_source_set_timer(ctx->timer, dispatch_time(DISPATCH_TIME_NOW, nanoseconds), nanoseconds, 0);
	dispatch_source_set_event_handler(ctx->timer, ^{
		fired(++ctx->eventCount);
	});
Exemplo n.º 23
0
static void
retry_timer(void)
{
    dispatch_source_t s;
    dispatch_time_t t;

    s = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
			       0, 0, g_queue);
    t = dispatch_time(DISPATCH_TIME_NOW, 5ull * NSEC_PER_SEC);
    dispatch_source_set_timer(s, t, 0, NSEC_PER_SEC);
    dispatch_source_set_event_handler(s, ^{
	    create_dns_sd();
	    dispatch_release(s);
	});
Exemplo n.º 24
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));
}
Exemplo n.º 25
0
static void setRepeatingTimer(struct nodeInstanceData *ctx, const VuoReal seconds, VuoOutputTrigger(fired,void))
{
	cancelRepeatingTimer(ctx);

	if (seconds <= 0)
		return;

	dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
	ctx->timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, q);

	uint64_t nanoseconds = (seconds > 0.001 ? (seconds * NSEC_PER_SEC) : (NSEC_PER_SEC / 1000));
	dispatch_source_set_timer(ctx->timer, dispatch_time(DISPATCH_TIME_NOW, nanoseconds), nanoseconds, 0);
	dispatch_source_set_event_handler(ctx->timer, ^{
		fired();
	});
Exemplo n.º 26
0
extern "C" void Qt_dispatch_timer() {
    char* argv = QString("test").toAscii().data();
    int argc = 1;
    QDispatchApplication app(argc,&argv);

    MU_BEGIN_TEST(Qt_dispatch_timer);

    MU_MESSAGE("Testing single-shot timer");
    checked_time.restart();
    QDispatchTimer::singleShot(dispatch_time(QDispatch::TimeNow, 2*NSEC_PER_SEC), QDispatch::globalQueue(), new testSingleShot());
    //QDispatchTimer::singleShot(QTime::currentTime().addSecs(2), QDispatch::globalQueue(), new test_single_shot);

    app.exec();

    MU_END_TEST
}
Exemplo n.º 27
0
        void lock() {
#if defined (DEBUG)
            uint64_t timeout = NSEC_PER_SEC;
            uint64_t locktime = 0;
            while (dispatch_semaphore_wait(sem_, dispatch_time(DISPATCH_TIME_NOW, timeout)))
            {
                locktime += (timeout/NSEC_PER_SEC);
                printf("WARNING: mutex %p locking for %llu seconds\n", this, locktime);
                if (timeout < 64*NSEC_PER_SEC)
                    timeout *= 2;
            }
#else
            dispatch_semaphore_wait(sem_, DISPATCH_TIME_FOREVER);
#endif                
            
        }
Exemplo n.º 28
0
static mrb_value
mrb_queue_after(mrb_state *mrb, mrb_value self)
{
  mrb_float delay_f;
  mrb_value blk;
  dispatch_queue_t q;

  mrb_get_args(mrb, "f&", &delay_f, &blk);

  DISPATCH_ENSURE_BLOCK_GIVEN(mrb, blk);

  q = (dispatch_queue_t)DATA_PTR(self);

  dispatch_time_t pop_time = dispatch_time(DISPATCH_TIME_NOW, (dispatch_time_t)(delay_f * NSEC_PER_SEC));
  dispatch_after(pop_time, q, ^{
    mrb_yield(mrb, blk, mrb_nil_value());
  });
Exemplo n.º 29
0
static int
_gcdpoll_timeout_add(AvahiTimeout *t, const struct timeval *tv)
{
  struct timeval e_tv;
  struct timeval now;
  int64_t nsecs;
  int ret;

  ret = gettimeofday(&now, NULL);
  if (ret != 0)
    return -1;

  if (!t->timer)
    {
      t->timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, mdns_sq);
      if (!t->timer)
	return -1;

      dispatch_set_context(t->timer, t);
      dispatch_source_set_event_handler_f(t->timer, gcdpollcb_timer);
    }
  else
    dispatch_suspend(t->timer);

  if ((tv->tv_sec == 0) && (tv->tv_usec == 0))
    {
      dispatch_source_set_timer(t->timer,
				DISPATCH_TIME_NOW,
				DISPATCH_TIME_FOREVER /* one-shot */, 0);
    }
  else
    {
      timersub(tv, &now, &e_tv);

      nsecs = e_tv.tv_sec * NSEC_PER_SEC + e_tv.tv_usec * 1000;

      dispatch_source_set_timer(t->timer,
				dispatch_time(DISPATCH_TIME_NOW, nsecs),
				DISPATCH_TIME_FOREVER /* one-shot */, 0);
    }

  dispatch_resume(t->timer);

  return 0;
}
Exemplo n.º 30
0
static void
retry_timer(void)
{
    dispatch_time_t t;

    heim_assert(g_dnsRef == NULL, "called create when a connection already existed");
    
    if (g_restart_timer)
	return;

    g_restart_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, g_queue);
    t = dispatch_time(DISPATCH_TIME_NOW, 5ull * NSEC_PER_SEC);
    dispatch_source_set_timer(g_restart_timer, t, 0, NSEC_PER_SEC);
    dispatch_source_set_event_handler(g_restart_timer, ^{
	    create_dns_sd();
	    dispatch_release(g_restart_timer);
	    g_restart_timer = NULL;
	});