async.cc 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. ** © 2014 by Philipp Dunkel <pip@pipobscure.com>
  3. ** Licensed under MIT License.
  4. */
  5. void async_propagate(uv_async_t *async) {
  6. if (!async->data) return;
  7. FSEvents *fse = (FSEvents *)async->data;
  8. CFIndex idx, cnt;
  9. fse_event *event;
  10. char pathbuf[1024];
  11. const char *pathptr = NULL;
  12. uv_mutex_lock(&fse->mutex);
  13. cnt = fse->events.size();
  14. for (idx=0; idx<cnt; idx++) {
  15. event = fse->events.at(idx);
  16. if (event == NULL) continue;
  17. pathptr = CFStringGetCStringPtr(event->path, kCFStringEncodingUTF8);
  18. if (!pathptr) CFStringGetCString(event->path, pathbuf, 1024, kCFStringEncodingUTF8);
  19. fse->emitEvent(pathptr ? pathptr : pathbuf, event->flags, event->id);
  20. delete event;
  21. }
  22. if (cnt>0) fse->events.clear();
  23. uv_mutex_unlock(&fse->mutex);
  24. }
  25. void FSEvents::asyncStart() {
  26. if (async.data == this) return;
  27. async.data = this;
  28. uv_async_init(uv_default_loop(), &async, (uv_async_cb) async_propagate);
  29. }
  30. void FSEvents::asyncTrigger() {
  31. if (async.data != this) return;
  32. uv_async_send(&async);
  33. }
  34. void FSEvents::asyncStop() {
  35. if (async.data != this) return;
  36. async.data = NULL;
  37. uv_close((uv_handle_t *) &async, NULL);
  38. }