Example #1
0
 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)
Example #2
0
 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
Example #3
0
 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)
Example #4
0
 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
Example #5
0
 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)
Example #6
0
 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)
Example #7
0
 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)
Example #8
0
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)
Example #9
0
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)
Example #10
0
 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
Example #11
0
 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
Example #12
0
 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
Example #13
0
 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
Example #14
0
 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)
Example #15
0
 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)
Example #16
0
 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
Example #17
0
 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
Example #18
0
 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
Example #19
0
 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
Example #20
0
 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
Example #21
0
 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
Example #22
0
 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
Example #23
0
 def startup(self, space):
     self.main_thread = thread.get_ident()
     globalState.startup(space)
Example #24
0
 def _ismine(self):
     return self.count > 0 and ll_thread.get_ident() == self.last_tid
Example #25
0
 def runme():
     for i in range(N):
         state.data.append((thread.get_ident(), i))
         state.threadlocals.yield_thread()
Example #26
0
def PyThread_get_thread_ident(space):
    return ll_thread.get_ident()
Example #27
0
        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
Example #28
0
 def _ismine(self):
     return self.count > 0 and ll_thread.get_ident() == self.last_tid
Example #29
0
 def getvalue(self):
     ident = thread.get_ident()
     return self._valuedict.get(ident, None)
Example #30
0
 def __init__(self, space, initargs):
     self.space = space
     self.initargs = initargs.normalize()
     ident = thread.get_ident()
     self.dicts = {ident: space.newdict()}
Example #31
0
 def __init__(self, space, initargs):
     self.space = space
     self.initargs = initargs.normalize()
     ident = thread.get_ident()
     self.dicts = {ident: space.newdict()}
Example #32
0
        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
Example #33
0
def finish_thread(w_obj):
    assert isinstance(w_obj, Local)
    ident = thread.get_ident()
    del w_obj.dicts[ident]
Example #34
0
 def getvalue(self):
     ident = thread.get_ident()
     return self._valuedict.get(ident, None)
Example #35
0
 def get_aid():
     """Return the thread identifier, cast to an (opaque) address."""
     return llmemory.cast_int_to_adr(ll_thread.get_ident())
Example #36
0
 def startup(self, space):
     self.main_thread = thread.get_ident()
     globalState.startup(space)
Example #37
0
def _ssl_thread_id_function():
    from pypy.module.thread import ll_thread
    return rffi.cast(rffi.LONG, ll_thread.get_ident())
Example #38
0
def finish_thread(w_obj):
    assert isinstance(w_obj, Local)
    ident = thread.get_ident()
    del w_obj.dicts[ident]
Example #39
0
def _ssl_thread_id_function():
    from pypy.module.thread import ll_thread
    return rffi.cast(rffi.LONG, ll_thread.get_ident())
Example #40
0
 def runme():
     for i in range(N):
         state.data.append((thread.get_ident(), i))
         state.threadlocals.yield_thread()
Example #41
0
def PyThread_get_thread_ident(space):
    return ll_thread.get_ident()
Example #42
0
 def get_aid():
     """Return the thread identifier, cast to an (opaque) address."""
     return llmemory.cast_int_to_adr(ll_thread.get_ident())
Example #43
0
 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()
Example #44
0
 def __init__(self, space, initargs):
     self.initargs = initargs
     ident = thread.get_ident()
     self.dicts = {ident: space.newdict(instance=True)}
Example #45
0
 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()
Example #46
0
 def __init__(self, space, initargs):
     self.initargs = initargs
     ident = thread.get_ident()
     self.dicts = {ident: space.newdict(instance=True)}