Commits

briancurtin  committed 88507db

Add some thread specific stuff, simply loop and make sure a callback occurs on the interval.

  • Participants
  • Parent commits 5ab028c

Comments (0)

Files changed (2)

+syntax: glob
+*.so
+*.test
+*.swp
+build/*
 #include <fcntl.h>
 #include <sys/types.h>
 #include <sys/event.h>
+#include <sys/time.h>
 
 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
-#endif
+#endif /*DARWIN */
 
 #ifdef LINUX
 #endif
     unsigned int event_flags;
     struct kevent events[1];
     struct kevent data[1];
-#endif
+#endif /* DARWIN */
 } watcher_object;
 
 #ifdef DARWIN
 void *kqueue_thread(void *arg)
 {
+    PyGILState_STATE state;
+    int count;
+    struct timespec timeout;
+    timeout.tv_sec = 0;
+    timeout.tv_nsec = 100000000; /* 100 milliseconds */
+
+    watcher_object *parent = (watcher_object*)arg;
+
     while(1) {
-        if(die == 1)
+        if(die == 1) {
             goto exit;
-        sleep(1);
+        }
+
+        /*count = kevent(parent->kq, parent->events, 1, parent->data,
+                       1, &timeout); */
+       
+        state = PyGILState_Ensure();
+        PyObject_CallObject(parent->callback, NULL);
+        PyGILState_Release(state);
+        sleep(5);
     }
 exit:
     pthread_exit(NULL);
     }
     
     /* Open a file specifically for event notifications */
+    Py_BEGIN_ALLOW_THREADS
     obj->fd = open(path, O_EVTONLY);
+    Py_END_ALLOW_THREADS
+
     if(obj->fd <= 0) {
         PyErr_Format(watcher_error, "unable to open file");
         return NULL;
     }
-#endif
+#endif /* DARWIN */
 
     return (PyObject*)obj;
 }
         Py_RETURN_FALSE;
 
 #ifdef DARWIN
-    if(pthread_create(&self->thread_id, NULL, kqueue_thread, NULL)) {
+    /* vnode_events = NOTE_DELETE |  NOTE_WRITE | NOTE_EXTEND |
+                   NOTE_ATTRIB | NOTE_LINK | NOTE_RENAME | NOTE_REVOKE;
+    EV_SET(&events[0], self->fd, EVFILT_VNODE, EV_ADD | EV_CLEAR,
+           vnode_events, 0, NULL); */
+    if(pthread_create(&self->thread_id, NULL, kqueue_thread,
+                      (void*)self)) {
         PyErr_Format(watcher_error, "unable to start kqueue thread");
         return NULL;
     }
 {
     PyObject *module, *dict;
 
+    /* Initialize and acquire the GIL before we do anything else. */
+    PyEval_InitThreads();
+
 #if PY_MAJOR_VERSION == 3
     module = PyModule_Create(&_watchermodule);
 #else