Пример #1
0
void unpause_idle_timer(void)
{
    debug("entry");
    assert(gRunLoop);
    assert(gTimer);
    CFRunLoopAddTimer(gRunLoop, gTimer, kCFRunLoopDefaultMode);
}
Пример #2
0
void
rwsched_cfrunloop_relocate_timers_to_main_mode(rwsched_tasklet_ptr_t sched_tasklet)
{
  //unsigned int i, j;
  unsigned int i;
  rwsched_CFRunLoopTimerRef rwsched_object;
  rwsched_instance_ptr_t instance;

  // Validate input parameters
  RW_CF_TYPE_VALIDATE(sched_tasklet, rwsched_tasklet_ptr_t);

  for (i = 1 ; i < sched_tasklet->cftimer_array->len ; i++) {
    if ((rwsched_object = g_array_index(sched_tasklet->cftimer_array, rwsched_CFRunLoopTimerRef, i)) != NULL) {
      RW_CF_TYPE_VALIDATE(rwsched_object, rwsched_CFRunLoopTimerRef);
      instance = rwsched_object->callback_context.instance;
      RW_CF_TYPE_VALIDATE(instance, rwsched_instance_ptr_t);
      //CFRunLoopRemoveTimer(rwsched_tasklet_CFRunLoopGetCurrent(sched_tasklet), rwsched_object->cf_object, instance->deferred_cfrunloop_mode);
      if (rwsched_object->onetime_timer_fired_while_blocked) {
        //(rwsched_object->cf_callout)(rwsched_object, rwsched_object->cf_context.info);
        RW_ASSERT(rwsched_object->onetime_timer);
        rwsched_object->cf_object = CFRunLoopTimerCreate(rwsched_object->ott.allocator,
                                                         CFAbsoluteTimeGetCurrent()+0.000001,
                                                         0,
                                                         rwsched_object->ott.flags,
                                                         rwsched_object->ott.order,
                                                         rwsched_cftimer_callout_intercept,
                                                         &rwsched_object->tmp_context);
        rwsched_object->onetime_timer_fired_while_blocked = 0;
      }
      CFRunLoopAddTimer(rwsched_tasklet_CFRunLoopGetCurrent(sched_tasklet), rwsched_object->cf_object, instance->main_cfrunloop_mode);
    }
  }
}
Пример #3
0
int
timer_callout_set(timer_callout_t * callout, 
		  CFAbsoluteTime relative_time, timer_func_t * func, 
		  void * arg1, void * arg2, void * arg3)
{
    CFRunLoopTimerContext 	context =  { 0, NULL, NULL, NULL, NULL };
    CFAbsoluteTime 		wakeup_time;

    if (callout == NULL) {
	return (0);
    }
    timer_cancel(callout);
    if (func == NULL) {
	return (0);
    }
    callout->func = func;
    callout->arg1 = arg1;
    callout->arg2 = arg2;
    callout->arg3 = arg3;
    callout->enabled = TRUE;
    callout->time_generation = S_time_generation;
    context.info = callout;
    wakeup_time = CFAbsoluteTimeGetCurrent() + relative_time;
    callout->timer_source 
	= CFRunLoopTimerCreate(NULL, wakeup_time,
			       0.0, 0, 0,
			       timer_callout_process,
			       &context);
    my_log(LOG_DEBUG, "timer: wakeup time is (%0.09g) %0.09g", 
	   relative_time, wakeup_time);
    my_log(LOG_DEBUG, "timer: adding timer source");
    CFRunLoopAddTimer(CFRunLoopGetCurrent(), callout->timer_source,
		      kCFRunLoopDefaultMode);
    return (1);
}
Пример #4
0
int main (int argc, char * const argv[]) 
{	
	g_MasterLoop = CFRunLoopGetCurrent();
		
	signal (SIGTERM, CancelExec);
	signal (SIGHUP, ForceStartup);
	signal (SIGALRM, ForceInventory);
	
	// a bit of runway for network services
	sleep(30);
	
	CheckRun(true); // initial check

	CFAuto<CFRunLoopTimerRef> SCATimer;
	
	SCATimer.Attach(
		CFRunLoopTimerCreate(
			kCFAllocatorDefault,
			CFAbsoluteTimeGetCurrent() + SurveyTime,
			SurveyTime, 
			0, 0, 
			InventoryCheckTimerFunc,
			NULL));
	
	CFRunLoopAddTimer(g_MasterLoop, SCATimer, kCFRunLoopDefaultMode);
	
	CFRunLoopRun();

    CFRunLoopTimerInvalidate( SCATimer );
}
Пример #5
0
void fx_setTimeout(txMachine* the)
{
	txTimeoutData* data;
#if mxMacOSX
	CFRunLoopTimerContext context;
#endif
	txNumber duration = fxToNumber(the, mxArgv(1)) / 1000;
	
	data = c_malloc(sizeof(txTimeoutData));
	mxElseError(data);
	c_memset(data, 0, sizeof(txTimeoutData));
	data->the = the;
	data->function.kind = mxArgv(0)->kind;
	data->function.value = mxArgv(0)->value;
	fxRemember(the, &(data->function));
	if (mxArgc > 2) {
		data->argument.kind = mxArgv(2)->kind;
		data->argument.value = mxArgv(2)->value;
	}
	fxRemember(the, &(data->argument));
#if mxMacOSX
	memset(&context, 0, sizeof(context));
	context.info = data;
	context.release = fx_setTimeoutRelease;
	data->timer = CFRunLoopTimerCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + duration, 0, 0, 0, fx_setTimeoutCallback, &context);
	CFRunLoopAddTimer(CFRunLoopGetCurrent(), data->timer, gxRunLoopMode);
#endif
}
Пример #6
0
static void
configuration_changed(SCDynamicStoreRef store, CFArrayRef changedKeys, void *info)
{
	CFRunLoopTimerContext	context	= { 0, (void *)store, CFRetain, CFRelease, NULL };
	CFAbsoluteTime		time_boot;
	CFAbsoluteTime		time_now ;

	// if active, cancel any in-progress attempt to resolve the primary IP address

	if (ptrTarget != NULL) {
		ptr_query_stop();
	}

	// if active, cancel any queued configuration change
	if (timer != NULL) {
		CFRunLoopTimerInvalidate(timer);
		CFRelease(timer);
		timer = NULL;
	}

	// queue configuration change
	time_boot = boottime() + SMB_STARTUP_DELAY;
	time_now  = CFAbsoluteTimeGetCurrent() + SMB_DEBOUNCE_DELAY;

	timer = CFRunLoopTimerCreate(NULL,
				     time_now > time_boot ? time_now : time_boot,
				     0,
				     0,
				     0,
				     smb_update_configuration,
				     &context);
	CFRunLoopAddTimer(rl, timer, kCFRunLoopDefaultMode);

	return;
}
Пример #7
0
void CFRunLoopAddMIDIRunLoopSource( CFRunLoopRef rl, struct CFMIDIRunLoopSource * source, CFStringRef mode ) {
  int i;
  if( source->cfrlt != NULL ) CFRunLoopAddTimer( rl, source->cfrlt, mode );
  for( i=0; i<source->length; i++ ) {
    if( source->cfrls[i] != NULL ) CFRunLoopAddSource( rl, source->cfrls[i], mode );
  }
}
Пример #8
0
void runLoop(TimerCallbackParameters* timerCallbackParameters, float frequency)
{
    CFRunLoopObserverContext observerContext;
    observerContext.version = 0;
    observerContext.info = 0;
    observerContext.retain = 0;
    observerContext.release = 0;
    observerContext.copyDescription = 0;

    auto observer = CFRunLoopObserverCreate(kCFAllocatorDefault, kCFRunLoopBeforeTimers, true, 0, observerCallback, &observerContext);
    CFRunLoopAddObserver(CFRunLoopGetCurrent(), observer, kCFRunLoopCommonModes);

    CFRunLoopTimerContext timerContext;
    timerContext.version = 0;
    timerContext.info = timerCallbackParameters;
    timerContext.retain = 0;
    timerContext.release = 0;
    timerContext.copyDescription = 0;

    auto timer = CFRunLoopTimerCreate(kCFAllocatorDefault,
                                      CFAbsoluteTimeGetCurrent(),
                                      1.0 / frequency,
                                      0, 0,
                                      timerCallBack,
                                      &timerContext);

    CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes);

    CFRunLoopRun();
}
Пример #9
0
void* RunLoopThread(void* args)
{
    unsigned i;

    InitUSB();

    currentRunLoop = CFRunLoopGetCurrent();

    CFRunLoopTimerContext context = {0, NULL, NULL, NULL, NULL};
    CFRunLoopTimerRef timer = CFRunLoopTimerCreate(kCFAllocatorDefault, 0.1, 0.1, 0, 0, &onTimerFired, &context);
    CFRunLoopAddTimer(currentRunLoop, timer, kCFRunLoopCommonModes);

    // Signal the parent that we are running
    adb_mutex_lock(&start_lock);
    adb_cond_signal(&start_cond);
    adb_mutex_unlock(&start_lock);

    CFRunLoopRun();
    currentRunLoop = 0;

    for (i = 0; i < vendorIdCount; i++) {
        IOObjectRelease(notificationIterators[i]);
    }
    IONotificationPortDestroy(notificationPort);

    usb_cleanup();

    DBG("RunLoopThread done\n");
    return NULL;    
}
Пример #10
0
void HeapTimer::synchronize()
{
    if (CFRunLoopGetCurrent() == m_runLoop.get())
        return;
    CFRunLoopRemoveTimer(m_runLoop.get(), m_timer.get(), kCFRunLoopCommonModes);
    m_runLoop = CFRunLoopGetCurrent();
    CFRunLoopAddTimer(m_runLoop.get(), m_timer.get(), kCFRunLoopCommonModes);
}
Пример #11
0
void DefaultGCActivityCallback::synchronize()
{
    if (CFRunLoopGetCurrent() == d->runLoop.get())
        return;
    CFRunLoopRemoveTimer(d->runLoop.get(), d->timer.get(), kCFRunLoopCommonModes);
    d->runLoop = CFRunLoopGetCurrent();
    CFRunLoopAddTimer(d->runLoop.get(), d->timer.get(), kCFRunLoopCommonModes);
}
static void
schedulePowerEvent(PowerEventBehavior *behave)
{
    static CFRunLoopTimerContext    tmr_context = {0,0,0,0,0};
    CFAbsoluteTime                  fire_time = 0.0;
    CFDictionaryRef                 upcoming = NULL;
    CFDateRef                       temp_date = NULL;

    if(behave->timer) 
    {
       CFRunLoopTimerInvalidate(behave->timer);
       CFRelease(behave->timer);
       behave->timer = 0;
    }
    
    // find upcoming time
    upcoming = copyEarliestUpcoming(behave);
    if(!upcoming)
    {
        // No scheduled events
        if (behave->noScheduledEventCallout) {
            (*behave->noScheduledEventCallout)(NULL);
        }
        return;
    }
        
    /* 
     * Perform any necessary actions at schedulePowerEvent time 
     */
    if ( behave->scheduleNextCallout ) {
        (*behave->scheduleNextCallout)(upcoming);    
    }    

    if (behave->currentEvent) {
        CFRelease(behave->currentEvent);
    }

    behave->currentEvent = (CFDictionaryRef)upcoming;
    tmr_context.info = (void *)behave;    
    
    temp_date = _getScheduledEventDate(upcoming);
    if(!temp_date) goto exit;

    fire_time = CFDateGetAbsoluteTime(temp_date);

    behave->timer = CFRunLoopTimerCreate(0, fire_time, 0.0, 0, 
                    0, handleTimerExpiration, &tmr_context);

    if(behave->timer)
    {
        CFRunLoopAddTimer( CFRunLoopGetCurrent(), 
                            behave->timer, 
                            kCFRunLoopDefaultMode);
    }

exit:
    return;
}
Пример #13
0
void cfRunLoopRun(ExternalTimerCallback callback, void* context) {
	externalCallback = callback;
	externalContext = context;

	timerRef = CFRunLoopTimerCreate(NULL, 0, 1.0, 0, 0, timerCallback, NULL);
	CFRunLoopAddTimer(CFRunLoopGetCurrent(), timerRef, kCFRunLoopCommonModes);

	CFRunLoopRun();
}
Пример #14
0
HeapTimer::HeapTimer(JSGlobalData* globalData, CFRunLoopRef runLoop)
    : m_globalData(globalData)
    , m_runLoop(runLoop)
{
    memset(&m_context, 0, sizeof(CFRunLoopTimerContext));
    m_context.info = this;
    m_timer.adoptCF(CFRunLoopTimerCreate(0, s_decade, s_decade, 0, 0, HeapTimer::timerDidFire, &m_context));
    CFRunLoopAddTimer(m_runLoop.get(), m_timer.get(), kCFRunLoopCommonModes);
}
void RunLoop::TimerBase::start(double nextFireInterval, bool repeat)
{
    if (m_timer)
        stop();
    
    CFRunLoopTimerContext context = { 0, this, 0, 0, 0 };
    CFTimeInterval repeatInterval = repeat ? nextFireInterval : 0;
    m_timer = adoptCF(CFRunLoopTimerCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + nextFireInterval, repeatInterval, 0, 0, timerFired, &context));
    CFRunLoopAddTimer(m_runLoop->m_runLoop.get(), m_timer.get(), kCFRunLoopCommonModes);
}
void DefaultGCActivityCallback::commonConstructor(Heap* heap, CFRunLoopRef runLoop)
{
    d = adoptPtr(new DefaultGCActivityCallbackPlatformData);

    memset(&d->context, 0, sizeof(CFRunLoopTimerContext));
    d->context.info = heap;
    d->runLoop = runLoop;
    d->timer.adoptCF(CFRunLoopTimerCreate(0, decade, decade, 0, 0, DefaultGCActivityCallbackPlatformData::trigger, &d->context));
    CFRunLoopAddTimer(d->runLoop.get(), d->timer.get(), kCFRunLoopCommonModes);
}
int main(int argc, char** argv) {

    CFRunLoopTimerRef timer = CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent()+1, 0,0,0, timeout, NULL);
    CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes);

    am_device_notification* notification;
    check("Subscribing to device notification",
            AMDeviceNotificationSubscribe(&onDevice, 0,0,NULL, &notification));
    CFRunLoopRun();
    return 1;
}
Пример #18
0
HeapTimer::HeapTimer(VM* vm, CFRunLoopRef runLoop)
    : m_vm(vm)
    , m_runLoop(runLoop)
{
    memset(&m_context, 0, sizeof(CFRunLoopTimerContext));
    m_context.info = &vm->apiLock();
    m_context.retain = retainAPILock;
    m_context.release = releaseAPILock;
    m_timer = adoptCF(CFRunLoopTimerCreate(0, s_decade, s_decade, 0, 0, HeapTimer::timerDidFire, &m_context));
    CFRunLoopAddTimer(m_runLoop.get(), m_timer.get(), kCFRunLoopCommonModes);
}
Пример #19
0
//  recveive - receive a packet
//    Inputs:
//	num = device to receive from (zero based)
//	buf = buffer to receive packet
//	len = buffer's size
//	timeout = time to wait, in milliseconds
//    Output:
//	number of bytes received, or -1 on error
//
int pjrc_rawhid::receive(int num, void *buf, int len, int timeout)
{
   hid_t *hid;
   buffer_t *b;
   CFRunLoopTimerRef timer=NULL;
   CFRunLoopTimerContext context;
   int ret=0, timeout_occurred=0;

   if (len < 1) return 0;
   hid = get_hid(num);
   if (!hid || !hid->open) return -1;
   if ((b = hid->first_buffer) != NULL) {
       if (len > b->len) len = b->len;
       memcpy(buf, b->buf, len);
       hid->first_buffer = b->next;
       free(b);
       return len;
   }
   memset(&context, 0, sizeof(context));
   context.info = &timeout_occurred;
   timer = CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent() +
                                (double)timeout / 1000.0, 0, 0, 0, timeout_callback, &context);
   CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopDefaultMode);
   the_correct_runloop = CFRunLoopGetCurrent();
   //qDebug("--");
   while (1) {
       //qDebug(".");
       CFRunLoopRun(); // Found the problem: somehow the input_callback does not
                       // stop this CFRunLoopRun because it is hooked to a different run loop !!!
                       // Hence the use of the "correct_runloop" variable above.
       //qDebug("  ..");

       if ((b = hid->first_buffer) != NULL) {
           if (len > b->len) len = b->len;
           memcpy(buf, b->buf, len);
           hid->first_buffer = b->next;
           free(b);
           ret = len;
           //qDebug("*************");
           break;
       }
       if (!hid->open) {
           printf("pjrc_rawhid_recv, device not open\n");
           ret = -1;
           break;
       }
       if (timeout_occurred)
            break;
   }
   CFRunLoopTimerInvalidate(timer);
   CFRelease(timer);
   return ret;
}
Пример #20
0
static void* Pt_Thread(void *p)
{
    CFTimeInterval timerInterval;
    CFRunLoopTimerContext timerContext;
    CFRunLoopTimerRef timer;
    PtThreadParams *params = (PtThreadParams*)p;
    //CFTimeInterval timeout;

    /* raise the thread's priority */
    kern_return_t error;
    thread_extended_policy_data_t extendedPolicy;
    thread_precedence_policy_data_t precedencePolicy;

    extendedPolicy.timeshare = 0;
    error = thread_policy_set(mach_thread_self(), THREAD_EXTENDED_POLICY,
                              (thread_policy_t)&extendedPolicy,
                              THREAD_EXTENDED_POLICY_COUNT);
    if (error != KERN_SUCCESS) {
        mach_error("Couldn't set thread timeshare policy", error);
    }

    precedencePolicy.importance = THREAD_IMPORTANCE;
    error = thread_policy_set(mach_thread_self(), THREAD_PRECEDENCE_POLICY,
                              (thread_policy_t)&precedencePolicy,
                              THREAD_PRECEDENCE_POLICY_COUNT);
    if (error != KERN_SUCCESS) {
        mach_error("Couldn't set thread precedence policy", error);
    }

    /* set up the timer context */
    timerContext.version = 0;
    timerContext.info = params;
    timerContext.retain = NULL;
    timerContext.release = NULL;
    timerContext.copyDescription = NULL;

    /* create a new timer */
    timerInterval = (double)params->resolution / 1000.0;
    timer = CFRunLoopTimerCreate(NULL, startTime+timerInterval, timerInterval,
                                 0, 0, Pt_CFTimerCallback, &timerContext);

    timerRunLoop = CFRunLoopGetCurrent();
    CFRunLoopAddTimer(timerRunLoop, timer, CFSTR("PtTimeMode"));

    /* run until we're told to stop by Pt_Stop() */
    CFRunLoopRunInMode(CFSTR("PtTimeMode"), LONG_TIME, false);
    
    CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), timer, CFSTR("PtTimeMode"));
    CFRelease(timer);
    free(params);

    return NULL;
}
Пример #21
0
static void ParkEventLoop() {
    // RunLoop needs at least one source, and 1e20 is pretty far into the future
    CFRunLoopTimerRef t = CFRunLoopTimerCreate(kCFAllocatorDefault, 1.0e20, 0.0, 0, 0, dummyTimer, NULL);
    CFRunLoopAddTimer(CFRunLoopGetCurrent(), t, kCFRunLoopDefaultMode);
    CFRelease(t);

    // Park this thread in the main run loop.
    int32_t result;
    do {
        result = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0e20, false);
    } while (result != kCFRunLoopRunFinished);
}
Пример #22
0
void setSharedTimerFireTime(double fireTime)
{
    ASSERT(sharedTimerFiredFunction);

    if (sharedTimer) {
        CFRunLoopTimerInvalidate(sharedTimer);
        CFRelease(sharedTimer);
    }

    CFAbsoluteTime fireDate = fireTime - kCFAbsoluteTimeIntervalSince1970;
    sharedTimer = CFRunLoopTimerCreate(0, fireDate, 0, 0, 0, timerFired, 0);
    CFRunLoopAddTimer(CFRunLoopGetCurrent(), sharedTimer, kCFRunLoopCommonModes);
}
Пример #23
0
static int run_loop_for_time(double duration)
{
    bool timed_out = false;
    CFRunLoopTimerContext timer_context = { 0, &timed_out, NULL, NULL, NULL };
    CFRunLoopTimerRef timer = CFRunLoopTimerCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + duration, 0.0, 0, 0, timeout_cb, &timer_context);
    CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes);

    CFRunLoopRun();
    CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes);
    CFRelease(timer);

    return timed_out ? -1 : 0;
}
Пример #24
0
void* os_inputmain(void* context){
    usbdevice* kb = context;
    int index = INDEX_OF(kb, keyboard);
    // Schedule async events for the device on this thread
    CFRunLoopRef runloop = CFRunLoopGetCurrent();
    int count = (IS_RGB(kb->vendor, kb->product)) ? 4 : 3;
    for(int i = 0; i < count; i++){
        CFTypeRef eventsource;
        kern_return_t res = (*kb->handles[i])->getAsyncEventSource(kb->handles[i], &eventsource);
        if(res != kIOReturnSuccess){
            ckb_err("Failed to start input thread for %s%d: %x\n", devpath, index, res);
            return 0;
        }
        if(CFGetTypeID(eventsource) == CFRunLoopSourceGetTypeID())
            CFRunLoopAddSource(runloop, (CFRunLoopSourceRef)eventsource, kCFRunLoopDefaultMode);
        else if(CFGetTypeID(eventsource) == CFRunLoopTimerGetTypeID())
            CFRunLoopAddTimer(runloop, (CFRunLoopTimerRef)eventsource, kCFRunLoopDefaultMode);
    }
    ckb_info("Starting input thread for %s%d\n", devpath, index);

    // Start getting reports
    uint8_t* urbinput[] = { malloc(8), malloc(32), malloc(MSG_SIZE) };
    (*kb->handles[0])->setInputReportCallback(kb->handles[0], urbinput[0], 8, intreport, kb, 0);
    if(IS_RGB(kb->vendor, kb->product)){
        (*kb->handles[1])->setInputReportCallback(kb->handles[1], urbinput[1], 21, intreport, kb, 0);
        (*kb->handles[2])->setInputReportCallback(kb->handles[2], urbinput[2], MSG_SIZE, intreport, kb, 0);
    } else {
        (*kb->handles[1])->setInputReportCallback(kb->handles[1], urbinput[1], 4, intreport, kb, 0);
        (*kb->handles[2])->setInputReportCallback(kb->handles[2], urbinput[2], 15, intreport, kb, 0);
    }

    // Run the run loop for up to 2ms at a time, then check for key repeats
    while(1){
        CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.002, false);
        pthread_mutex_lock(imutex(kb));
        if(!IS_CONNECTED(kb)){
            // Make sure the device hasn't disconnected
            pthread_mutex_unlock(imutex(kb));
            break;
        }
        keyretrigger(kb);
        pthread_mutex_unlock(imutex(kb));
    }

    // Clean up
    ckb_info("Stopping input thread for %s%d\n", devpath, index);
    free(urbinput[0]);
    free(urbinput[1]);
    free(urbinput[2]);
    return 0;
}
Пример #25
0
void
osx_driver_setclipboard(
		struct ts_display_t *display,
		ts_clipboard_p clipboard)
{
	//printf("%s called\n", __func__);
	loop_display = display;
	loop_clipboard = clipboard;
	loop_to = NULL;

	CFRunLoopTimerRef timer = CFRunLoopTimerCreate(NULL, 0, 0, 0, 0, osx_setclipboard_callback, NULL);

	CFRunLoopAddTimer(CFRunLoopGetMain(), timer, kCFRunLoopCommonModes);
}
Пример #26
0
int rawhid_recv(int num, void *buf, int len, int timeout)
{
   //fprintf(stderr,"rawhid_recv start len: %d\n",len);
   //fprintf(stderr,"rawhid_recv start \n");
   hid_t *hid;
	buffer_t *b;
	CFRunLoopTimerRef timer=NULL;
	CFRunLoopTimerContext context;
	int ret=0, timeout_occurred=0;
   
	if (len < 1) return 0;
	hid = get_hid(num);
	if (!hid || !hid->open) return -1;
	if ((b = hid->first_buffer) != NULL)
   {
		if (len > b->len) len = b->len;
		memcpy(buf, b->buf, len);
		hid->first_buffer = b->next;
		free(b);
     // fprintf(stderr,"rawhid_recv A len: %d\n\n",len);
		return len;
	}
	memset(&context, 0, sizeof(context));
	context.info = &timeout_occurred;
	timer = CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent() +
                                (double)timeout / 1000.0, 0, 0, 0, timeout_callback, &context);
	CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopDefaultMode);
	while (1) {
		CFRunLoopRun();
		if ((b = hid->first_buffer) != NULL) {
			if (len > b->len) len = b->len;
			memcpy(buf, b->buf, len);
			hid->first_buffer = b->next;
			free(b);
			ret = len;
			break;
		}
		if (!hid->open) {
			//printf("rawhid_recv, device not open\n");
			ret = -1;
			break;
		}
		if (timeout_occurred) break;
	}
	CFRunLoopTimerInvalidate(timer);
	CFRelease(timer);
   //fprintf(stderr,"rawhid_recv ret: %d\n",ret);
	return ret;
   
}
Пример #27
0
static void
startComplete(pid_t pid, int status, struct rusage *rusage, void *context)
{
	execCommand = 0;

	if (WIFEXITED(status)) {
		switch (WEXITSTATUS(status)) {
			case AT_CMD_SUCCESS :
				SCLog(TRUE, LOG_NOTICE, CFSTR("AppleTalk startup complete"));
				if ((curState < 0) || (curState > (int)context)) {
					// the stack is now up but we really want it down
					stopAppleTalk(NULL, (void *)curState);
				}
				return;
			case AT_CMD_ALREADY_RUNNING :
				// the stack is already up but we're not sure
				// if the configuration is correct, restart
				SCLog(TRUE, LOG_NOTICE, CFSTR("AppleTalk already running, restarting"));
				stopAppleTalk(NULL, (void *)curState);
				return;
			default :
				break;
		}
	}

	SCLog(TRUE, LOG_ERR,
	      CFSTR("AppleTalk startup failed, status = %d%s"),
	      WEXITSTATUS(status),
	      (execRetry > 1) ? " (retrying)" : "");

	// startup failed, retry
	if (--execRetry > 0) {
		CFRunLoopTimerContext	timerContext	= { 0, (void *)curState, NULL, NULL, NULL };
		CFRunLoopTimerRef	timer;

		timer = CFRunLoopTimerCreate(NULL,
					     CFAbsoluteTimeGetCurrent() + 1.0,
					     0.0,
					     0,
					     0,
					     startAppleTalk,
					     &timerContext);
		CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopDefaultMode);
		CFRelease(timer);
		return;
	}

	return;
}
Пример #28
0
static void register_timeout(void)
{
    if (idle_timer == NULL) {
        idle_timer = CFRunLoopTimerCreate(kCFAllocatorDefault,
            CFAbsoluteTimeGetCurrent() + TIMEOUT_IN_SECONDS,
            TIMEOUT_IN_SECONDS, 0, 0, idle_timer_proc, NULL);
        if (idle_timer == NULL) {
            asl_log(NULL, NULL, ASL_LEVEL_CRIT,
                "FATAL: failed to create idle timer, exiting.");
            exit(1);
        }
        CFRunLoopAddTimer(CFRunLoopGetCurrent(), idle_timer,
            kCFRunLoopDefaultMode);
    }
}
Пример #29
0
CF_EXPORT void
rwsched_tasklet_CFRunLoopAddTimer(rwsched_tasklet_ptr_t sched_tasklet,
                                  rwsched_CFRunLoopRef rl,
                                  rwsched_CFRunLoopTimerRef rwsched_timer,
                                  CFStringRef mode)
{
    // Validate input paraemters
    RW_CF_TYPE_VALIDATE(sched_tasklet, rwsched_tasklet_ptr_t);
    rwsched_instance_ptr_t instance = sched_tasklet->instance;
    RW_CF_TYPE_VALIDATE(instance, rwsched_instance_ptr_t);
    RW_CF_TYPE_VALIDATE(rwsched_timer, rwsched_CFRunLoopTimerRef);

    // Call the native CFRunLoop function
    CFRunLoopAddTimer(rl, rwsched_timer->cf_object, mode);
}
Пример #30
0
Файл: eyed.c Проект: rsms/eye
int
main(int argc, const char * argv[])
{
  int result = 0;
  FSEventStreamRef streamRef;
  Boolean startedOK;
  int flush_seconds = 3600; // When to force-flush any queued events
  
  if(argc < 2 || strcasecmp(argv[1], "--help") == 0) {
    fprintf(stderr, "usage: %s path ...\n", argv[0]);
    exit(1);
  }
  
  const char **paths = &argv[1];
  streamRef = my_FSEventStreamCreate(paths, argc-1);
  
  FSEventStreamScheduleWithRunLoop(streamRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
  
  startedOK = FSEventStreamStart(streamRef);
  if (!startedOK) {
    log_error("FSEventStreamStart(streamRef) failed");
    goto out;
  }
  
  if (flush_seconds >= 0) {
    log_debug("CFAbsoluteTimeGetCurrent() => %.3f", CFAbsoluteTimeGetCurrent());
    CFAllocatorRef allocator = kCFAllocatorDefault;
    CFAbsoluteTime fireDate = CFAbsoluteTimeGetCurrent() + /*settings->*/flush_seconds;
    CFTimeInterval interval = /*settings->*/flush_seconds;
    CFOptionFlags flags = 0;
    CFIndex order = 0;
    CFRunLoopTimerCallBack callback = (CFRunLoopTimerCallBack)timer_callback;
    CFRunLoopTimerContext context = { 0, streamRef, NULL, NULL, NULL };
    CFRunLoopTimerRef timer = CFRunLoopTimerCreate(allocator, fireDate, interval, flags, order, callback, &context);
    CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopDefaultMode);
  }
  
  // Run
  CFRunLoopRun();
  
  // Stop / Invalidate / Release
  FSEventStreamStop(streamRef);
out:
  FSEventStreamInvalidate(streamRef);
  FSEventStreamRelease(streamRef);
  
  return result;
}