Commit fa3600c1 authored by Jim Fulton's avatar Jim Fulton

Clean up thunk management logic.

Allow thunks to take args.

Don't hold trigger lock when calling thunks.
parent 397b8b5f
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
# #
############################################################################## ##############################################################################
from __future__ import with_statement
import asyncore import asyncore
import os import os
import socket import socket
...@@ -95,14 +97,15 @@ class _triggerbase(object): ...@@ -95,14 +97,15 @@ class _triggerbase(object):
def _close(self): # see close() above; subclass must supply def _close(self): # see close() above; subclass must supply
raise NotImplementedError raise NotImplementedError
def pull_trigger(self, thunk=None): def pull_trigger(self, *thunk):
if thunk: if thunk:
self.lock.acquire() with self.lock:
try:
self.thunks.append(thunk) self.thunks.append(thunk)
finally: try:
self.lock.release()
self._physical_pull() self._physical_pull()
except Exception:
if not self._closed:
raise
# Subclass must supply _physical_pull, which does whatever the OS # Subclass must supply _physical_pull, which does whatever the OS
# needs to do to provoke the "write" end of the trigger. # needs to do to provoke the "write" end of the trigger.
...@@ -114,18 +117,19 @@ class _triggerbase(object): ...@@ -114,18 +117,19 @@ class _triggerbase(object):
self.recv(8192) self.recv(8192)
except socket.error: except socket.error:
return return
self.lock.acquire()
try: while 1:
for thunk in self.thunks: with self.lock:
if self.thunks:
thunk = self.thunks.pop(0)
else:
return
try: try:
thunk() thunk[0](*thunk[1:])
except: except:
nil, t, v, tbinfo = asyncore.compact_traceback() nil, t, v, tbinfo = asyncore.compact_traceback()
print ('exception in trigger thunk:' print ('exception in trigger thunk:'
' (%s:%s %s)' % (t, v, tbinfo)) ' (%s:%s %s)' % (t, v, tbinfo))
self.thunks = []
finally:
self.lock.release()
def __repr__(self): def __repr__(self):
return '<select-trigger (%s) at %x>' % (self.kind, positive_id(self)) return '<select-trigger (%s) at %x>' % (self.kind, positive_id(self))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment