Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix race condition between creating and destroying watch

  • Loading branch information...
commit d0b268aac8607373db23f4c0a34a75b4a9912c25 1 parent 7a886ef
@luite authored
Showing with 12 additions and 4 deletions.
  1. +10 −4 cbits/c_fsevents.c
  2. +2 −0  cbits/c_fsevents.h
View
14 cbits/c_fsevents.c
@@ -31,6 +31,7 @@ void *watchRunLoop(void *vw) {
w->runLoop = rl;
FSEventStreamScheduleWithRunLoop(w->eventStream, rl, kCFRunLoopDefaultMode);
FSEventStreamStart(w->eventStream);
+ pthread_mutex_unlock(&w->mut);
CFRunLoopRun();
pthread_exit(NULL);
}
@@ -70,6 +71,8 @@ int createWatch( char** folders
w->writefd = pfds[1];
w->eventStream = es;
w->runLoop = NULL;
+ pthread_mutex_init(&w->mut, NULL);
+ pthread_mutex_lock(&w->mut);
pthread_create(&t, NULL, &watchRunLoop, (void*)w);
*fd = pfds[0];
*wp = w;
@@ -87,12 +90,15 @@ int createWatch( char** folders
}
int destroyWatch(watch* w) {
+ pthread_mutex_lock(&w->mut);
+ FSEventStreamStop(w->eventStream);
+ FSEventStreamInvalidate(w->eventStream);
+ CFRunLoopStop(w->runLoop);
+ CFRelease(w->runLoop);
FSEventStreamRelease(w->eventStream);
- if(w->runLoop != NULL) {
- CFRunLoopStop(w->runLoop);
- CFRelease(w->runLoop);
- }
close(w->writefd);
+ pthread_mutex_unlock(&w->mut);
+ pthread_mutex_destroy(&w->mut);
free(w);
}
View
2  cbits/c_fsevents.h
@@ -2,11 +2,13 @@
#define CBITS_C_FSEVENTS_H 1
#include <CoreServices/CoreServices.h>
+#include <pthread.h>
typedef struct {
FSEventStreamRef eventStream;
CFRunLoopRef runLoop;
int writefd;
+ pthread_mutex_t mut;
} watch;
Please sign in to comment.
Something went wrong with that request. Please try again.