Ejemplo n.º 1
0
def start_new_thread(space, w_callable, w_args, w_kwargs=NoneNotWrapped):
    """Start a new thread and return its identifier.  The thread will call the
function with positional arguments from the tuple args and keyword arguments
taken from the optional dictionary kwargs.  The thread exits when the
function returns; the return value is ignored.  The thread will also exit
when the function raises an unhandled exception; a stack trace will be
printed unless the exception is SystemExit."""
    setup_threads(space)
    if not space.is_true(space.isinstance(w_args, space.w_tuple)):
        raise OperationError(space.w_TypeError,
                             space.wrap("2nd arg must be a tuple"))
    if w_kwargs is not None and not space.is_true(
            space.isinstance(w_kwargs, space.w_dict)):
        raise OperationError(
            space.w_TypeError,
            space.wrap("optional 3rd arg must be a dictionary"))
    if not space.is_true(space.callable(w_callable)):
        raise OperationError(space.w_TypeError,
                             space.wrap("first arg must be callable"))

    args = Arguments.frompacked(space, w_args, w_kwargs)
    bootstrapper.acquire(space, w_callable, args)
    try:
        try:
            thread.gc_thread_prepare()  # (this has no effect any more)
            ident = thread.start_new_thread(bootstrapper.bootstrap, ())
        except Exception, e:
            bootstrapper.release()  # normally called by the new thread
            raise
    except thread.error:
        raise wrap_thread_error(space, "can't start new thread")
    return space.wrap(ident)
Ejemplo n.º 2
0
def start_new_thread(space, w_callable, w_args, w_kwargs=NoneNotWrapped):
    """Start a new thread and return its identifier.  The thread will call the
function with positional arguments from the tuple args and keyword arguments
taken from the optional dictionary kwargs.  The thread exits when the
function returns; the return value is ignored.  The thread will also exit
when the function raises an unhandled exception; a stack trace will be
printed unless the exception is SystemExit."""
    setup_threads(space)
    if not space.is_true(space.isinstance(w_args, space.w_tuple)):
        raise OperationError(space.w_TypeError,
                             space.wrap("2nd arg must be a tuple"))
    if w_kwargs is not None and not space.is_true(
            space.isinstance(w_kwargs, space.w_dict)):
        raise OperationError(
            space.w_TypeError,
            space.wrap("optional 3rd arg must be a dictionary"))
    if not space.is_true(space.callable(w_callable)):
        raise OperationError(space.w_TypeError,
                             space.wrap("first arg must be callable"))

    args = Arguments.frompacked(space, w_args, w_kwargs)
    boot = Bootstrapper()
    boot.space = space
    boot.w_callable = w_callable
    boot.args = args
    try:
        ident = thread.start_new_thread(Bootstrapper.bootstrap, (boot, ))
    except thread.error:
        raise wrap_thread_error(space, "can't start new thread")
    return space.wrap(ident)
Ejemplo n.º 3
0
def stack_size(space, size=0):
    """stack_size([size]) -> size

Return the thread stack size used when creating new threads.  The
optional size argument specifies the stack size (in bytes) to be used
for subsequently created threads, and must be 0 (use platform or
configured default) or a positive integer value of at least 32,768 (32k).
If changing the thread stack size is unsupported, a ThreadError
exception is raised.  If the specified size is invalid, a ValueError
exception is raised, and the stack size is unmodified.  32k bytes
is currently the minimum supported stack size value to guarantee
sufficient stack space for the interpreter itself.

Note that some platforms may have particular restrictions on values for
the stack size, such as requiring a minimum stack size larger than 32kB or
requiring allocation in multiples of the system memory page size
- platform documentation should be referred to for more information
(4kB pages are common; using multiples of 4096 for the stack size is
the suggested approach in the absence of more specific information)."""
    if size < 0:
        raise OperationError(space.w_ValueError,
                             space.wrap("size must be 0 or a positive value"))
    old_size = thread.get_stacksize()
    error = thread.set_stacksize(size)
    if error == -1:
        raise OperationError(space.w_ValueError,
                             space.wrap("size not valid: %d bytes" % size))
    if error == -2:
        raise wrap_thread_error(space, "setting stack size not supported")
    return space.wrap(old_size)
Ejemplo n.º 4
0
def stack_size(space, size=0):
    """stack_size([size]) -> size

Return the thread stack size used when creating new threads.  The
optional size argument specifies the stack size (in bytes) to be used
for subsequently created threads, and must be 0 (use platform or
configured default) or a positive integer value of at least 32,768 (32k).
If changing the thread stack size is unsupported, a ThreadError
exception is raised.  If the specified size is invalid, a ValueError
exception is raised, and the stack size is unmodified.  32k bytes
is currently the minimum supported stack size value to guarantee
sufficient stack space for the interpreter itself.

Note that some platforms may have particular restrictions on values for
the stack size, such as requiring a minimum stack size larger than 32kB or
requiring allocation in multiples of the system memory page size
- platform documentation should be referred to for more information
(4kB pages are common; using multiples of 4096 for the stack size is
the suggested approach in the absence of more specific information)."""
    if size < 0:
        raise OperationError(space.w_ValueError,
                             space.wrap("size must be 0 or a positive value"))
    old_size = thread.get_stacksize()
    error = thread.set_stacksize(size)
    if error == -1:
        raise operationerrfmt(space.w_ValueError, "size not valid: %d bytes",
                              size)
    if error == -2:
        raise wrap_thread_error(space, "setting stack size not supported")
    return space.wrap(old_size)
Ejemplo n.º 5
0
 def disable_signals(self, space):
     ec = self.getvalue()
     new = ec._signals_enabled - 1
     if new < 0:
         raise wrap_thread_error(space,
             "cannot disable signals in thread not enabled for signals")
     ec._signals_enabled = new
Ejemplo n.º 6
0
 def __init__(self, space):
     self.rlock_count = 0
     self.rlock_owner = 0
     try:
         self.lock = rthread.allocate_lock()
     except rthread.error:
         raise wrap_thread_error(space, "cannot allocate lock")
Ejemplo n.º 7
0
    def setup_threads(self, space):
        """Enable threads in the object space, if they haven't already been."""
        if self.GIL is None:
            try:
                self.GIL = thread.allocate_lock_NOAUTO()
            except thread.error:
                raise wrap_thread_error(space, "can't allocate GIL")
            self.enter_thread(space)   # setup the main thread
            # add the GIL-releasing callback as an action on the space
            space.pending_actions.append(GILReleaseAction(self))
            result = True
        else:
            result = False      # already set up

        # add the GIL-releasing callback around external function calls.
        #
        # XXX we assume a single space, but this is not quite true during
        # testing; for example, if you run the whole of test_lock you get
        # a deadlock caused by the first test's space being reused by
        # test_lock_again after the global state was cleared by
        # test_compile_lock.  As a workaround, we repatch these global
        # fields systematically.
        spacestate.GIL = self.GIL
        invoke_around_extcall(before_external_call, after_external_call)
        return result
Ejemplo n.º 8
0
def start_new_thread(space, w_callable, w_args, w_kwargs=NoneNotWrapped):
    """Start a new thread and return its identifier.  The thread will call the
function with positional arguments from the tuple args and keyword arguments
taken from the optional dictionary kwargs.  The thread exits when the
function returns; the return value is ignored.  The thread will also exit
when the function raises an unhandled exception; a stack trace will be
printed unless the exception is SystemExit."""
    setup_threads(space)
    if not space.is_true(space.isinstance(w_args, space.w_tuple)): 
        raise OperationError(space.w_TypeError, 
                space.wrap("2nd arg must be a tuple")) 
    if w_kwargs is not None and not space.is_true(space.isinstance(w_kwargs, space.w_dict)): 
        raise OperationError(space.w_TypeError, 
                space.wrap("optional 3rd arg must be a dictionary")) 
    if not space.is_true(space.callable(w_callable)):
        raise OperationError(space.w_TypeError, 
                space.wrap("first arg must be callable"))

    args = Arguments.frompacked(space, w_args, w_kwargs)
    bootstrapper.acquire(space, w_callable, args)
    try:
        try:
            thread.gc_thread_prepare()
            ident = thread.start_new_thread(bootstrapper.bootstrap, ())
        except Exception, e:
            bootstrapper.release()     # normally called by the new thread
            raise
    except thread.error:
        raise wrap_thread_error(space, "can't start new thread")
    return space.wrap(ident)
Ejemplo n.º 9
0
    def setup_threads(self, space):
        """Enable threads in the object space, if they haven't already been."""
        if self.GIL is None:
            try:
                self.GIL = thread.allocate_lock_NOAUTO()
            except thread.error:
                raise wrap_thread_error(space, "can't allocate GIL")
            self.enter_thread(space)  # setup the main thread
            # add the GIL-releasing callback as an action on the space
            space.pending_actions.append(GILReleaseAction(self))
            result = True
        else:
            result = False  # already set up

        # add the GIL-releasing callback around external function calls.
        #
        # XXX we assume a single space, but this is not quite true during
        # testing; for example, if you run the whole of test_lock you get
        # a deadlock caused by the first test's space being reused by
        # test_lock_again after the global state was cleared by
        # test_compile_lock.  As a workaround, we repatch these global
        # fields systematically.
        spacestate.GIL = self.GIL
        invoke_around_extcall(before_external_call, after_external_call)
        return result
Ejemplo n.º 10
0
 def disable_signals(self, space):
     ec = self.get_ec()
     assert ec is not None
     new = ec._signals_enabled - 1
     if new < 0:
         raise wrap_thread_error(space,
             "cannot disable signals in thread not enabled for signals")
     ec._signals_enabled = new
Ejemplo n.º 11
0
 def disable_signals(self, space):
     ec = self.get_ec()
     assert ec is not None
     new = ec._signals_enabled - 1
     if new < 0:
         raise wrap_thread_error(space,
             "cannot disable signals in thread not enabled for signals")
     ec._signals_enabled = new
Ejemplo n.º 12
0
Archivo: os_lock.py Proyecto: Mu-L/pypy
 def __init__(self, space, w_active=None):
     self.rlock_count = 0
     self.rlock_owner = 0
     self.w_active = w_active  # dictionary 'threading._active'
     try:
         self.lock = rthread.allocate_lock()
     except rthread.error:
         raise wrap_thread_error(space, "cannot allocate lock")
Ejemplo n.º 13
0
    def descr_lock_release(self, space):
        """Release the lock, allowing another thread that is blocked waiting for
the lock to acquire the lock.  The lock must be in the locked state,
but it needn't be locked by the same thread that unlocks it."""
        try:
            self.lock.release()
        except rthread.error:
            raise wrap_thread_error(space, "release unlocked lock")
Ejemplo n.º 14
0
    def descr_lock_release(self, space):
        """Release the lock, allowing another thread that is blocked waiting for
the lock to acquire the lock.  The lock must be in the locked state,
but it needn't be locked by the same thread that unlocks it."""
        try:
            self.lock.release()
        except thread.error:
            raise wrap_thread_error(space, "release unlocked lock")
Ejemplo n.º 15
0
 def acquire_restore_w(self, space, w_saved_state):
     """For internal use by `threading.Condition`."""
     # saved_state is the value returned by release_save()
     w_count, w_owner = space.unpackiterable(w_saved_state, 2)
     count = space.int_w(w_count)
     owner = space.int_w(w_owner)
     r = True
     if not self.lock.acquire(False):
         r = self.lock.acquire(True)
     if not r:
         raise wrap_thread_error(space, "coult not acquire lock")
     assert self.rlock_count == 0
     self.rlock_owner = owner
     self.rlock_count = count
Ejemplo n.º 16
0
    def setup_threads(self, space):
        """Enable threads in the object space, if they haven't already been."""
        if not self.gil_ready:
            if not thread.gil_allocate():
                raise wrap_thread_error(space, "can't allocate GIL")
            self.gil_ready = True
            result = True
        else:
            result = False  # already set up

        # add the GIL-releasing callback around external function calls.
        #
        # XXX we assume a single space, but this is not quite true during
        # testing; for example, if you run the whole of test_lock you get
        # a deadlock caused by the first test's space being reused by
        # test_lock_again after the global state was cleared by
        # test_compile_lock.  As a workaround, we repatch these global
        # fields systematically.
        invoke_around_extcall(before_external_call, after_external_call)
        return result
Ejemplo n.º 17
0
    def setup_threads(self, space):
        """Enable threads in the object space, if they haven't already been."""
        if not self.gil_ready:
            if not thread.gil_allocate():
                raise wrap_thread_error(space, "can't allocate GIL")
            self.gil_ready = True
            result = True
        else:
            result = False      # already set up

        # add the GIL-releasing callback around external function calls.
        #
        # XXX we assume a single space, but this is not quite true during
        # testing; for example, if you run the whole of test_lock you get
        # a deadlock caused by the first test's space being reused by
        # test_lock_again after the global state was cleared by
        # test_compile_lock.  As a workaround, we repatch these global
        # fields systematically.
        invoke_around_extcall(before_external_call, after_external_call)
        return result
Ejemplo n.º 18
0
 def __init__(self, space):
     self.space = space
     try:
         self.lock = rthread.allocate_lock()
     except rthread.error:
         raise wrap_thread_error(space, "out of resources")
Ejemplo n.º 19
0
 def __init__(self, space):
     self.space = space
     try:
         self.lock = thread.allocate_lock()
     except thread.error:
         raise wrap_thread_error(space, "out of resources")
Ejemplo n.º 20
0
 def setup(space):
     if bootstrapper.lock is None:
         try:
             bootstrapper.lock = thread.allocate_lock()
         except thread.error:
             raise wrap_thread_error(space, "can't allocate bootstrap lock")
Ejemplo n.º 21
0
Archivo: gil.py Proyecto: charred/pypy
 def _initialize_gil(self, space):
     if not rthread.gil_allocate():
         raise wrap_thread_error(space, "can't allocate GIL")
Ejemplo n.º 22
0
 def setup(space):
     if bootstrapper.lock is None:
         try:
             bootstrapper.lock = thread.allocate_lock()
         except thread.error:
             raise wrap_thread_error(space, "can't allocate bootstrap lock")
Ejemplo n.º 23
0
Archivo: os_lock.py Proyecto: Mu-L/pypy
def try_release(space, lock):
    try:
        lock.release()
    except rthread.error:
        raise wrap_thread_error(space, "release unlocked lock")