def f(): state.data = [] state.datalen1 = 0 state.datalen2 = 0 state.datalen3 = 0 state.datalen4 = 0 state.threadlocals = gil.GILThreadLocals() state.threadlocals.setup_threads(space) thread.gc_thread_prepare() subident = thread.start_new_thread(bootstrap, ()) mainident = thread.get_ident() runme(True) still_waiting = 3000 while len(state.data) < 2*N: debug_print(len(state.data)) if not still_waiting: raise ValueError("time out") still_waiting -= 1 if not we_are_translated(): gil.before_external_call() time.sleep(0.01) if not we_are_translated(): gil.after_external_call() debug_print("leaving!") i1 = i2 = 0 for tid, i in state.data: if tid == mainident: assert i == i1; i1 += 1 elif tid == subident: assert i == i2; i2 += 1 else: assert 0 assert i1 == N + skew assert i2 == N - skew return len(state.data)
def setdict(self, space, w_dict): if not space.is_true(space.isinstance(w_dict, space.w_dict)): raise OperationError(space.w_TypeError, space.wrap("setting dictionary to a non-dict")) self.getdict() # force a dict to exist first ident = thread.get_ident() self.dicts[ident] = w_dict
def f(): state.data = [] state.threadlocals = gil.GILThreadLocals() state.threadlocals.setup_threads(space) thread.gc_thread_prepare() subident = thread.start_new_thread(bootstrap, ()) mainident = thread.get_ident() runme() still_waiting = 3000 while len(state.data) < 2*N: if not still_waiting: raise ValueError("time out") still_waiting -= 1 if not we_are_translated(): gil.before_external_call() time.sleep(0.01) if not we_are_translated(): gil.after_external_call() i1 = i2 = 0 for tid, i in state.data: if tid == mainident: assert i == i1; i1 += 1 elif tid == subident: assert i == i2; i2 += 1 else: assert 0 assert i1 == N assert i2 == N return len(state.data)
def run(space, w_callable, args): try: space.call_args(w_callable, args) except OperationError, e: if not e.match(space, space.w_SystemExit): ident = thread.get_ident() where = 'thread %d started by ' % ident e.write_unraisable(space, where, w_callable) e.clear(space)
def sleep(space, secs): # as decreed by Guido, only the main thread can be # interrupted. main_thread = space.fromcache(State).main_thread interruptible = main_thread == thread.get_ident() MAX = sys.maxint / 1000.0 # > 24 days while secs > MAX: _simple_sleep(space, MAX, interruptible) secs -= MAX _simple_sleep(space, secs, interruptible)
def get_ident(space): """Return a non-zero integer that uniquely identifies the current thread amongst other threads that exist simultaneously. This may be used to identify per-thread resources. Even though on some platforms threads identities may appear to be allocated consecutive numbers starting at 1, this behavior should not be relied upon, and the number should be seen purely as a magic cookie. A thread's identity may be reused for another thread after it exits.""" ident = thread.get_ident() return space.wrap(ident)
def setvalue(self, value): ident = thread.get_ident() if value is not None: if len(self._valuedict) == 0: self._mainthreadident = ident self._valuedict[ident] = value else: try: del self._valuedict[ident] except KeyError: pass
def getvalue(self): ident = thread.get_ident() if ident == self._mostrecentkey: result = self._mostrecentvalue else: value = self._valuedict.get(ident, None) # slow path: update the minicache self._mostrecentkey = ident self._mostrecentvalue = value result = value return result
def sleep(space, secs): if secs < 0: raise OperationError(space.w_IOError, space.wrap("Invalid argument: negative time in sleep")) # as decreed by Guido, only the main thread can be # interrupted. main_thread = space.fromcache(State).main_thread interruptible = main_thread == thread.get_ident() MAX = sys.maxint / 1000.0 # > 24 days while secs > MAX: _simple_sleep(space, MAX, interruptible) secs -= MAX _simple_sleep(space, secs, interruptible)
def sleep(space, secs): if secs < 0: raise OperationError(space.w_IOError, space.wrap("Invalid argument: negative time in sleep")) # as decreed by Guido, only the main thread can be # interrupted. main_thread = space.fromcache(State).main_thread interruptible = (main_thread == thread.get_ident()) MAX = sys.maxint / 1000.0 # > 24 days while secs > MAX: _simple_sleep(space, MAX, interruptible) secs -= MAX _simple_sleep(space, secs, interruptible)
def leave_thread(self, space): "Notification that the current thread is about to stop." try: ec = space.getexecutioncontext() while ec.thread_exit_funcs: exit_func, w_obj = ec.thread_exit_funcs.pop() exit_func(w_obj) finally: ident = thread.get_ident() try: del self._valuedict[ident] except KeyError: pass
def setvalue(self, value): ident = thread.get_ident() if value is not None: if len(self._valuedict) == 0: self._mainthreadident = ident self._valuedict[ident] = value else: try: del self._valuedict[ident] except KeyError: pass # update the minicache to prevent it from containing an outdated value self._mostrecentkey = ident self._mostrecentvalue = value
def runme(main=False): j = 0 for i in range(N + [-skew, skew][main]): state.datalen1 += 1 # try to crash if the GIL is not state.datalen2 += 1 # correctly acquired state.data.append((thread.get_ident(), i)) state.datalen3 += 1 state.datalen4 += 1 assert state.datalen1 == len(state.data) assert state.datalen2 == len(state.data) assert state.datalen3 == len(state.data) assert state.datalen4 == len(state.data) debug_print(main, i, state.datalen4) gil.do_yield_thread() assert i == j j += 1
def getdict(self, space): ident = thread.get_ident() try: w_dict = self.dicts[ident] except KeyError: # create a new dict for this thread w_dict = self.dicts[ident] = space.newdict(instance=True) # call __init__ try: w_self = space.wrap(self) w_type = space.type(w_self) w_init = space.getattr(w_type, space.wrap("__init__")) space.call_obj_args(w_init, w_self, self.initargs) except: # failed, forget w_dict and propagate the exception del self.dicts[ident] raise # ready space.threadlocals.atthreadexit(space, finish_thread, self) return w_dict
def startup(self, space): self.main_thread = thread.get_ident() globalState.startup(space)
def _ismine(self): return self.count > 0 and ll_thread.get_ident() == self.last_tid
def runme(): for i in range(N): state.data.append((thread.get_ident(), i)) state.threadlocals.yield_thread()
def PyThread_get_thread_ident(space): return ll_thread.get_ident()
return space.wrap(val) @unwrap_spec(block=bool) def acquire(self, space, block=True, w_timeout=None): # check whether we already own the lock if self.kind == RECURSIVE_MUTEX and self._ismine(): self.count += 1 return space.w_True try: got = semlock_acquire(self, space, block, w_timeout) except OSError, e: raise wrap_oserror(space, e) if got: self.last_tid = ll_thread.get_ident() self.count += 1 return space.w_True else: return space.w_False def release(self, space): if self.kind == RECURSIVE_MUTEX: if not self._ismine(): raise OperationError( space.w_AssertionError, space.wrap("attempt to release recursive lock" " not owned by thread")) if self.count > 1: self.count -= 1 return
def getvalue(self): ident = thread.get_ident() return self._valuedict.get(ident, None)
def __init__(self, space, initargs): self.space = space self.initargs = initargs.normalize() ident = thread.get_ident() self.dicts = {ident: space.newdict()}
def finish_thread(w_obj): assert isinstance(w_obj, Local) ident = thread.get_ident() del w_obj.dicts[ident]
def get_aid(): """Return the thread identifier, cast to an (opaque) address.""" return llmemory.cast_int_to_adr(ll_thread.get_ident())
def _ssl_thread_id_function(): from pypy.module.thread import ll_thread return rffi.cast(rffi.LONG, ll_thread.get_ident())
def __enter__(self): if not self.lock.acquire(False): if self.owner == ll_thread.get_ident(): raise self.operr self.lock.acquire(True) self.owner = ll_thread.get_ident()
def __init__(self, space, initargs): self.initargs = initargs ident = thread.get_ident() self.dicts = {ident: space.newdict(instance=True)}