Acquire and release the GIL around select callbacks.
[mLib-python] / sig.pyx
1 # -*-pyrex-*-
2 #
3 # $Id$
4 #
5 # In-band signals
6 #
7 # (c) 2005 Straylight/Edgeware
8 #
9
10 #----- Licensing notice -----------------------------------------------------
11 #
12 # This file is part of the Python interface to mLib.
13 #
14 # mLib/Python is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
18 #
19 # mLib/Python is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with mLib/Python; if not, write to the Free Software Foundation,
26 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27
28 import signal
29
30 cdef class SelSignal:
31 cdef sig s
32 cdef int _activep
33 cdef readonly int signal
34 cdef _signalled
35 def __new__(me, int sig, signalledproc = None, *hunoz, **hukairz):
36 if sig < 0 or sig >= signal.NSIG:
37 raise ValueError, 'signal number out of range'
38 me.signal = sig
39 me._signalledproc = _checkcallable(signalledproc, 'signalled proc')
40 me._activep = 0
41 def __dealloc__(me):
42 if me._activep:
43 sig_remove(&me.s)
44 property activep:
45 def __get__(me):
46 return _tobool(me._activep)
47 property signalledproc:
48 def __get__(me):
49 return me._signalled
50 def __set__(me, proc):
51 me._signalled = _checkcallable(proc, 'signalled proc')
52 def __del__(me):
53 me._signalled = None
54 def enable(me):
55 if me._activep:
56 raise ValueError, 'already enabled'
57 sig_add(&me.s, me.signal, _sigfunc, <void *>me)
58 me._enabled()
59 return me
60 def disable(me):
61 if not me._activep:
62 raise ValueError, 'already disabled'
63 sig_remove(&me.s)
64 me._disabled()
65 return me
66 cdef _enabled(me):
67 me._activep = 1
68 me.enabled()
69 cdef _disabled(me):
70 me._activep = 0
71 me.disabled()
72 def enabled(me):
73 pass
74 def disabled(me):
75 pass
76 def signalled(me):
77 return _maybecall(me._signalled, ())
78
79 cdef void _sigfunc2(void *arg):
80 cdef SelSignal s
81 s = <SelSignal>arg
82 s.signalled()
83 cdef void _sigfunc(int sig, void *arg):
84 PyEval_AcquireLock()
85 _sigfunc2(arg)
86 PyEval_ReleaseLock()
87
88 sig_init(&_sel)
89
90 #----- That's all, folks ----------------------------------------------------