Exemple #1
0
/**
 * The callback for when the main thread has issued a command to the run loop
 * thread. Reads all the pending commands and executes them
 */
static void command_callback(
    CFFileDescriptorRef fdref,
    CFOptionFlags callBackTypes,
    void *context) {
  int fd = CFFileDescriptorGetNativeDescriptor(fdref);
  struct thread_env *thread_env = (struct thread_env *)context;
  struct command *to_do = NULL;
  struct command *to_free = NULL;
  clear_pipe(fd);
  do {
    to_do = *(thread_env->commands);
  } while (OSAtomicCompareAndSwapPtr(to_do, NULL, (void *)(thread_env->commands)) == false);
  while (to_do != NULL) {
    switch (to_do->type) {
      case ADD_WATCH:
        add_watch(to_do->arg, thread_env);
        break;
      case RM_WATCH:
        // hh_server doesn't need this at the moment. Shouldn't be too hard to
        // do...just need to map of paths to FSEvent streams.
        fprintf(stderr, "fsevents impl doesn't support removing watches yet\n");
        break;
    }
    to_free = to_do;
    to_do = to_do->next;
    free(to_free->arg);
    free(to_free);
  }
  // For some reason you have to re-enable this callback every bloody time
  CFFileDescriptorEnableCallBacks(fdref, kCFFileDescriptorReadCallBack);
}
	assert_errno("kevent", res >= 0);

	fprintf(stdout, "kevent.data = %ld\n", ke.data);

	return (res < 0);
}


static void
cffd_callback(CFFileDescriptorRef cffd,
	CFOptionFlags callBackTypes __attribute__((unused)),
	void *info __attribute__((unused)))
{
	int kq;

	kq = CFFileDescriptorGetNativeDescriptor(cffd);
	if (read_kevent(kq) == 0) {
		// ...
	}
 
	CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorReadCallBack);
}

void
timer()
{
	dispatch_source_t ds;
	ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
	assert(ds);
	dispatch_source_set_timer(ds, dispatch_time(0, 1*NSEC_PER_SEC), NSEC_PER_SEC, 0);
	dispatch_source_set_event_handler(ds, ^{