Debian Codegraph @

pypy/module/thread/gil.py

raw symbols SCIP
1 """
2 Global Interpreter Lock.
3 """
4
5 # This module adds a global lock to an object space.
6 # If multiple threads try to execute simultaneously in this space,
7 # all but one will be blocked. The other threads get a chance to run
8 # from time to time, using the periodic action GILReleaseAction.
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 # add the GIL-releasing callback as an action on the space
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 # Note: this is a quasi-immutable read by module/pypyjit/interp_jit
29 # It must be changed (to True) only if it was really False before
30 rgil.allocate()
31 self.gil_ready = True
32 result = True
33 else:
34 result = False # already set up
35 return result
36
37 def threads_initialized(self):
38 return self.gil_ready
39
40 ## def reinit_threads(self, space):
41 ## "Called in the child process after a fork()"
42 ## OSThreadLocals.reinit_threads(self, space)
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()