Anonymous avatar Anonymous committed 8bb706f

checking signals

Comments (0)

Files changed (5)

cherrypy/process/unix_threading/Makefile

+SHELL = /bin/bash
+
+.SUFFIXES:
+.SUFFIXES: .c .o
+
+CC = gcc
+INCLUDE = /usr/local/include/python2.5
+CFLAGS = -fno-strict-aliasing \
+	 -Wno-long-double \
+	 -no-cpp-precomp \
+	 -mno-fused-madd \
+	 -DNDEBUG -g -O3 \
+	 -Wall -Wstrict-prototypes \
+	 -I$(INCLUDE) -pg
+COMPILE = $(CC) $(CFLAGS) -c
+
+LD = $(CC)
+LDFLAGS = -undefined dynamic_lookup
+
+OBJS = _pthread_cond.o 
+LIBRARY = _pthread_cond.so
+LIBS = 
+
+all: $(LIBRARY) 
+
+$(LIBRARY): $(OBJS)
+	$(LD) $(LDFLAGS) -bundle $(OBJS) $(LIBS) -o $(LIBRARY)
+
+_pthread_cond.o: _pthread_cond.c 
+	$(COMPILE) $< -o $@
+
+# for flymake support
+check-syntax:
+	$(COMPILE) _pthread_cond.c -o /dev/null
+
+clean:
+	rm -f $(LIBRARY)
+	rm -f $(OBJS)
+

cherrypy/process/unix_threading/__init__.py

-import threading
-from unix_threading import _pthread_cond
+from _native_event import NativeEvent
 
-class NativeEvent(threading._Event):
-    def __init__(self):
-        threading._Event.__init__(self)
-        self.__cond = _pthread_cond.Condition()
-

cherrypy/process/unix_threading/_native_event.py

+import _pthread_cond
+
+class NativeEvent(object):
+    def __init__(self):
+        self.__cond = _pthread_cond.Condition()
+        self.__flag = False
+
+    def isSet(self):
+        return self.__flag
+
+    def set(self):
+        self.__cond.acquire()
+        try:
+            self.__flag = True
+            self.__cond.notifyAll()
+        finally:
+            self.__cond.release()
+
+    def clear(self):
+        self.__cond.acquire()
+        try:
+            self.__flag = False
+        finally:
+            self.__cond.release()
+
+    def wait(self, timeout=None):
+        self.__cond.acquire()
+        try:
+            if not self.__flag:
+                self.__cond.wait(timeout)
+        finally:
+            self.__cond.release()

cherrypy/process/unix_threading/_pthread_cond.c

   // for now timed waits are not supported (it's ignored)
   int err = 0;
   while( !self->set && err != EINVAL ) {
+    /*    struct timespec timeout = {0};
+    timeout.tv_sec = time(0) + 4;
+
+    Py_BEGIN_ALLOW_THREADS;
+    err = pthread_cond_timedwait( &self->cond, &self->lock, &timeout );
+    Py_END_ALLOW_THREADS;
+    */
     Py_BEGIN_ALLOW_THREADS;
     err = pthread_cond_wait( &self->cond, &self->lock );
     Py_END_ALLOW_THREADS;
+    if( PyErr_CheckSignals() ) {
+      return NULL;
+    }
   }
   if( err != 0 ) {
     PyErr_SetFromErrno( PyExc_OSError );

cherrypy/process/unix_threading/test.py

+#! /usr/bin/env python
+import select
+import sys
+import os
+import threading
+from _native_event import NativeEvent
+import time
+
+"""
+def test(event_class):
+    events = []
+    def wait_thread():
+        e = event_class()
+        events.append(e)
+        for i in range(100):
+            e.wait()
+    threads = []
+    for _ in range(100):
+        t = threading.Thread(target=wait_thread)
+        t.start()
+        threads.append(t)
+    time.sleep(1)
+    for i in range(100):
+        for e in events:
+            e.set()
+            e.clear()
+    for t in threads:
+        t.join()
+
+if __name__ == '__main__':
+    if eval(sys.argv[1]) == 0:
+        test(threading.Event)
+    else:
+        test(unix_threading.NativeEvent)
+"""
+import unix_threading._pthread_cond
+print unix_threading._pthread_cond.__file__
+print os.getpid()
+def test():
+    ev = NativeEvent()
+    def proc():
+        time.sleep(10)
+        ev.set()
+
+    t = threading.Thread(target=proc)
+    t.start()
+    
+    try:
+        ev.wait()
+    except KeyboardInterrupt:
+        print 'hi'
+
+test()
+
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.