| 1 |
""" |
| 2 |
Global Interpreter Lock. |
| 3 |
""" |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
from rpython.rlib import rthread, rgil |
| 11 |
from pypy.module.thread.error import wrap_thread_error |
| 12 |
from pypy.interpreter.executioncontext import PeriodicAsyncAction |
| 13 |
from pypy.module.thread.threadlocals import OSThreadLocals |
| 14 |
|
| 15 |
class GILThreadLocals(OSThreadLocals): |
| 16 |
"""A version of OSThreadLocals that enforces a GIL.""" |
| 17 |
gil_ready = False |
| 18 |
_immutable_fields_ = ['gil_ready?'] |
| 19 |
|
| 20 |
def initialize(self, space): |
| 21 |
|
| 22 |
space.actionflag.register_periodic_action(GILReleaseAction(space), |
| 23 |
use_bytecode_counter=True) |
| 24 |
|
| 25 |
def setup_threads(self, space): |
| 26 |
"""Enable threads in the object space, if they haven't already been.""" |
| 27 |
if not self.gil_ready: |
| 28 |
|
| 29 |
|
| 30 |
rgil.allocate() |
| 31 |
self.gil_ready = True |
| 32 |
result = True |
| 33 |
else: |
| 34 |
result = False |
| 35 |
return result |
| 36 |
|
| 37 |
def threads_initialized(self): |
| 38 |
return self.gil_ready |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
class GILReleaseAction(PeriodicAsyncAction): |
| 46 |
"""An action called every sys.checkinterval bytecodes. It releases |
| 47 |
the GIL to give some other thread a chance to run. |
| 48 |
""" |
| 49 |
|
| 50 |
def perform(self, executioncontext, frame): |
| 51 |
rgil.yield_thread() |