Exemplo n.º 1
0
/* Called when a new client connects */
BOOL mf_peer_init(freerdp_peer* client)
{
	client->ContextSize = sizeof(mfPeerContext);
	client->ContextNew = (psPeerContextNew) mf_peer_context_new;
	client->ContextFree = (psPeerContextFree) mf_peer_context_free;

	if (!freerdp_peer_context_new(client))
		return FALSE;
	
	info_event_queue = mf_event_queue_new();
	
	info_queue = dispatch_queue_create("FreeRDP.update.timer", DISPATCH_QUEUE_SERIAL);
	info_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, info_queue);
	
	if(info_timer)
	{
		//DEBUG_WARN( "created timer\n");
		dispatch_source_set_timer(info_timer, DISPATCH_TIME_NOW, 42ull * NSEC_PER_MSEC, 100ull * NSEC_PER_MSEC);
		dispatch_source_set_event_handler(info_timer, ^{
			//DEBUG_WARN( "dispatch\n");
			mfEvent* event = mf_event_new(MF_EVENT_TYPE_FRAME_TICK);
			mf_event_push(info_event_queue, (mfEvent*) event);}
						  );
		dispatch_resume(info_timer);
	}
Exemplo n.º 2
0
static VALUE
rb_source_timer(VALUE klass, VALUE sel, VALUE delay, VALUE interval, VALUE leeway, VALUE queue)
{
    Check_Queue(queue);
    dispatch_time_t start_time;
    VALUE argv[4] = {INT2FIX(SOURCE_TYPE_TIMER),
        INT2FIX(0), INT2FIX(0), queue};
    
    VALUE self = rb_class_new_instance(4, argv, cSource);
    rb_source_t *src = RSource(self);

    if (NIL_P(leeway)) {
        leeway = INT2FIX(0);
    }
    if (NIL_P(delay)) {
        start_time = DISPATCH_TIME_NOW;
    }
    else {
        start_time = rb_num2timeout(delay);
    }

    rb_dispatch_suspend(self, 0);
    dispatch_source_set_timer(src->source, start_time,
	    rb_num2nsec(interval), rb_num2nsec(leeway));
    rb_dispatch_resume(self, 0);
    return self;
}
Exemplo n.º 3
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.º 5
0
int
main(int argc, char* argv[])
{
	// interval is 1/10th of a second
	uint64_t interval = NSEC_PER_SEC / 10;
	// for 25 seconds
	struct timeval now_tv;
	struct timespec now_ts;

	interval_d = (double)interval / (double)NSEC_PER_SEC;
	target = (unsigned int)(25 / interval_d);

	test_start("Timer drift test");

	timeBeginPeriod(1);

	timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
	test_ptr_notnull("DISPATCH_SOURCE_TYPE_TIMER", timer);
	
	dispatch_source_set_event_handler_f(timer, timer_handler);
	
	gettimeofday(&now_tv, NULL);
	now_ts.tv_sec = now_tv.tv_sec;
	now_ts.tv_nsec = now_tv.tv_usec * NSEC_PER_USEC;

	dispatch_source_set_timer(timer, dispatch_walltime(&now_ts, interval), interval, 0);

	dispatch_resume(as_do(timer));

	dispatch_main();
	return 0;
}
Exemplo n.º 6
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.º 7
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.º 8
0
static void
event_handler(void* context)
{
	UNREFERENCED_PARAMETER(context);
	++i;
	fprintf(stderr, "%d\n", i);
	if (i >= 7) {
		dispatch_source_cancel(timer);
	} else if (i == 1) {
		dispatch_source_set_timer(timer, 0, 100, 0);
	}
}
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.º 10
0
Arquivo: timer.c Projeto: aosm/Heimdal
static void
reschedule_timer(void)
{
    const struct heim_event_data *e = heap_head(timer_heap);

    if (e == NULL) {
	/*
	 * if there are no more events, cancel timer by setting timer
	 * to forever, later calls will pull it down to !forever when
	 * needed again
	 */
	dispatch_source_set_timer(timer_source,
				  DISPATCH_TIME_FOREVER, 0, 10ull * NSEC_PER_SEC);
    } else {
	struct timespec ts;
	ts.tv_sec = e->t;
	ts.tv_nsec = 0;
	dispatch_source_set_timer(timer_source,
				  dispatch_walltime(&ts, 0),
				  0, 10ull * NSEC_PER_SEC);
    }
}
Exemplo n.º 11
0
static void resumeAudio(struct nodeInstanceData *context, VuoOutputTrigger(decodedAudio, VuoList_VuoAudioSamples))
{
	/* audio queue */
	if( VuoMovie_containsAudio(context->movie) && context->playbackRate == 1.)
	{
		dispatch_queue_t a_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
		context->audio_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, a_queue);

		// audio (plays at a steady rate)
		dispatch_source_set_timer(context->audio_timer, DISPATCH_TIME_NOW, DISPATCH_TIME_FOREVER, 0);
		dispatch_source_set_event_handler(context->audio_timer, ^{
											  playNextAudioFrame(context, decodedAudio);
										  });
Exemplo n.º 12
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.º 13
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.º 14
0
void
test_short_timer(void)
{
	// Add a large number of timers with suspended target queue in front of
	// the timer being measured <rdar://problem/7401353>
	g = dispatch_group_create();
	q = dispatch_queue_create("q", NULL);
	int i;
	for (i = 0; i < N; i++) {
		t[i] = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, q);
		dispatch_source_set_timer(t[i], DISPATCH_TIME_NOW, interval, 0);
		dispatch_group_enter(g);
		dispatch_source_set_registration_handler(t[i], ^{
			dispatch_suspend(t[i]);
			dispatch_group_leave(g);
		});
		dispatch_resume(t[i]);
	}
Exemplo n.º 15
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;
	});
void
test_timer(void)
{
    dispatch_test_start("Dispatch Suspend 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, i_prime = 0;
    __block int j = 0;

    tweedledee = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, main_q);
    test_ptr_notnull("dispatch_source_timer_create", tweedledee);

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

    dispatch_source_set_cancel_handler(tweedledee, ^ {
        dispatch_release(tweedledee);
    });
Exemplo n.º 17
0
void
test_timer(void)
{
	dispatch_test_start("Dispatch Source Timer, bit 31");

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

	struct timeval start_time;

	static dispatch_source_t s;
	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, 0x80000000ull), 0x80000000ull, 0);
	gettimeofday(&start_time, NULL);

	dispatch_source_set_event_handler(s, ^{
		dispatch_source_cancel(s);
	});
Exemplo n.º 18
0
static void playNextAudioFrame(struct nodeInstanceData *context, VuoOutputTrigger(decodedAudio, VuoList_VuoAudioSamples))
{
	if(context->lastAudioSamples)
	{
		// Send Audio
		if(VuoListGetCount_VuoAudioSamples(context->lastAudioSamples) > 0)
		{
			VuoAudioSamples as = VuoListGetValueAtIndex_VuoAudioSamples(context->lastAudioSamples, 1);
			decodedAudio(context->lastAudioSamples);
		}

		VuoRelease(context->lastAudioSamples);
		context->lastAudioSamples = NULL;
	}

	uint64_t cur_time = dispatch_time(DISPATCH_TIME_NOW, 0);

	if(!context->movie) return;

	context->lastAudioSamples = VuoListCreate_VuoAudioSamples();

	double frameTimestampInSecs = 0;

	bool gotFrame = VuoMovie_getNextAudioSample(context->movie, context->lastAudioSamples, &frameTimestampInSecs);
	if(gotFrame)
	{
		VuoRetain(context->lastAudioSamples);
		context->lastAudioTimestamp = frameTimestampInSecs;
	}
	else
	{
		VLog("bad");

		if(context->lastAudioSamples)
			VuoRelease(context->lastAudioSamples);

		context->lastAudioSamples = NULL;
	}

	uint64_t presentationTime = (cur_time + NSEC_PER_SEC * AUDIO_SEC_PER_SAMPLE - 100000);
	dispatch_source_set_timer(context->audio_timer, presentationTime, DISPATCH_TIME_FOREVER, NSEC_PER_SEC / 100000 );
}
Exemplo n.º 19
0
int
main(void)
{
	dispatch_queue_t main_q;

	test_start("Dispatch Update Timer");

	main_q = dispatch_get_main_queue();
	test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue());
	
	timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, main_q);
	dispatch_source_set_timer(timer, 1000000000ull, 0, 0);
	dispatch_source_set_cancel_handler_f(timer, cancel_handler);
	dispatch_source_set_event_handler_f(timer, event_handler);
	test_ptr_notnull("dispatch_source_timer_create", timer);

	gettimeofday(&start_time, NULL);
	dispatch_resume(as_do(timer));

	dispatch_main();
}
static void initCyclerTests(dispatch_group_t dgroup)
{
    /*
        We set up two timer sources:
        - getRequestsource waits 5 seconds, then gets every 5 seconds
        - exitTimerSource fires once after 300 seconds (5 minutes)
        All are created suspended, so they don't start yet
    */
    
    uint64_t delay = exitDelay;
    dispatch_time_t exitFireTime = dispatch_time(DISPATCH_TIME_NOW, delay);
    exitTimerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, (uintptr_t)NULL, 0, requestqueue);
    dispatch_source_set_timer(exitTimerSource, exitFireTime, 0ull, leeway);
        
    dispatch_source_set_event_handler(exitTimerSource,
    ^{
        xsecdebug("Test Exit: %lld", failCounter);
        printf("Test Exit: fail: %lld, total: %lld, changed: %lld\n", failCounter, putAttemptCounter, itemsChangedCount);
        tearDown();
        exit(0);
    });
Exemplo n.º 21
0
/* Called when a new client connects */
void mf_peer_init(freerdp_peer* client)
{
	client->context_size = sizeof(mfPeerContext);
	client->ContextNew = (psPeerContextNew) mf_peer_context_new;
	client->ContextFree = (psPeerContextFree) mf_peer_context_free;
	freerdp_peer_context_new(client);
    
    info_event_queue = mf_event_queue_new();
    
    info_queue = dispatch_queue_create("testing.101", DISPATCH_QUEUE_SERIAL);
    info_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, info_queue);
    
    if(info_timer)
    {
        //printf("created timer\n");
        dispatch_source_set_timer(info_timer, DISPATCH_TIME_NOW, 42ull * NSEC_PER_MSEC, 100ull * NSEC_PER_MSEC);
        dispatch_source_set_event_handler(info_timer, ^{
            //printf("dispatch\n");
            mfEvent* event = mf_event_new(MF_EVENT_TYPE_FRAME_TICK);
            mf_event_push(info_event_queue, (mfEvent*) event);}
                                          );
        dispatch_resume(info_timer);
    }
Exemplo n.º 22
0
int
main(int argc, char *argv[])
{
    dispatch_queue_t main_q;
    int i, n;
    int defaults[] = { 1, 10, 2, 7, 5, 3 };
    int *args;

    if (argc == 1)
    {
        // 1 10 2 7 5 5
        args = defaults;
        n = sizeof(defaults) / sizeof(int);
    }
    else
    {
        args = (int *)malloc(argc * sizeof(int));
        for (i = 0; i < argc - 1; i++)
            args[i] = atoi(argv[i+1]);
        n = argc - 1;
    }

    /*
     * Create our serial queues and get the main queue.
     */
    print_serial = dispatch_queue_create("Print Queue", NULL);
    ntimer_serial = dispatch_queue_create("Timers Queue", NULL);

    main_q = dispatch_get_main_queue();

    /*
     * Pick up arguments two at a time.
     */
    for (i = 0; i < n; i += 2)
    {
        uint64_t duration;
        dispatch_source_t src;

        /*
         * The count variable is declared with __block to make it read/write.
         */
        __block uint32_t count;

        /*
         * Timers are in nanoseconds.  NSEC_PER_SEC is defined by libdispatch.
         */
        duration = args[i] * NSEC_PER_SEC;
        count = args[i+1];

        /*
         * Create a dispatch source for a timer, and pair it with the main queue.
         */
        src = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, main_q);

        /*
         * Set the timer's duration (in nanoseconds).
         */
        dispatch_source_set_timer(src, 0, duration, 0);

        /*
         * Set an event handler block for the timer source.
         * This is the block of code that will be executed when the timer fires.
         */
        dispatch_source_set_event_handler(src, ^{
            /* Count down to zero */
            count--;
            dispatch_async(print_serial, ^{ printf("%d second timer   count %u\n", args[i], count); });

            if (count == 0) 
            {
                /*
                 * When the counter hits zero, we cancel and release the
                 * timer source, and decrement the count of active timers.
                 */
                dispatch_source_cancel(src);
                dispatch_release(src);
                dispatch_sync(ntimer_serial, ^{ ntimers--; });
            }
Exemplo n.º 23
0
static void mode_changed()
{
    switch (mmkd_mode) {
        /*
         * merge mode enabled: create event tap, cancel idle timer (if any)
         */
        case 1: {
            if (idle_timer) {
                dispatch_source_cancel(idle_timer);
                dispatch_release(idle_timer);
                idle_timer = NULL;
            }
            if (!event_tap) {

                /*
                 * reset ktab
                 */
                struct key_kind *i;
                for (i=ktab; i->kk_name; i++)
                    i->kk_num_pressed = 0;

                /*
                 * become root  (this is neccessary in order to create event tap)
                 */
                seteuid(0);

                /*
                 * based on alterkeys.c http://osxbook.com and http://dotdotcomorg.net
                 */
                CGEventMask        event_mask;
                CFRunLoopSourceRef run_loop_source;

                // Create an event tap. We are interested in key presses.
                event_mask = ((1 << kCGEventKeyDown) | (1 << kCGEventKeyUp) | (1<<kCGEventFlagsChanged));
                event_tap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0,
                                             event_mask, my_cg_event_callback, NULL);

                /*
                 * revert to self
                 */
                seteuid(getuid());

                if (!event_tap) {
                    fprintf(stderr, "failed to create event tap\n");
                    return;
                }

                // Create a run loop source.
                run_loop_source = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, event_tap, 0);

                // Add to the current run loop.
                CFRunLoopAddSource(CFRunLoopGetMain(), run_loop_source,
                                   kCFRunLoopCommonModes);

                // Enable the event tap.
                CGEventTapEnable(event_tap, true);
            }
            return;
        }
        /*
         * merge mode disabled: remove event tap (if any), setup idle timer
         */
        case 0: {
            if (event_tap) {
                CFMachPortInvalidate(event_tap);
                CFRelease(event_tap);
                event_tap = NULL;
            }
            if (!idle_timer) {
                idle_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
                                                    0, 0, dispatch_get_main_queue());
                if (!idle_timer) {
                    fprintf(stderr, "out of memory\n");
                    return;
                }
                dispatch_source_set_event_handler(idle_timer, ^{
                    CFRunLoopStop(CFRunLoopGetMain());
                });
                dispatch_source_set_timer(idle_timer,
                                          dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC*5LL),
                                          -1, NSEC_PER_SEC*1LL);
                dispatch_resume(idle_timer);
            }
            return;
        }
Exemplo n.º 24
0
	void scheduleSingleshot(DispatchSource & timerSource, int64_t delayInNS)
	{
		dispatch_time_t start = dispatch_time(0, delayInNS);
		// Schedule a single shot timer with a tolerance of 256ms
		dispatch_source_set_timer(timerSource.get(), start, DISPATCH_TIME_FOREVER, 256000000);
	}