Example #1
0
 def do_random_work():
     thread_index = glob.n_threads
     glob.n_threads += 1
     ops = ops_by_thread[thread_index]
     nextop = 0
     while nextop < len(ops):
         op = ops[nextop]
         nextop += 1
         if op == OP_YIELD:
             rgil.yield_thread()
             check("after yield", nextop)
         elif op == OP_RELEASE_AND_ACQUIRE:
             rgil.release()
             check("after release_gil", nextop, expected=False)
             rgil.acquire()
             check("after acquire_gil", nextop)
         else:
             arg = ops[nextop]
             nextop += 1
             if op == OP_BUSY:
                 end_time = time.time() + arg * 1e-6
                 while time.time() < end_time:
                     pass
                 check("after busy work", nextop)
             else:
                 time.sleep(arg * 1e-6)
                 check("after time.sleep()", nextop)
     finish_lock = glob.my_locks[thread_index]
     finish_lock.release()
Example #2
0
 def main(argv):
     rgil.release()
     # don't have the GIL here
     rgil.acquire()
     rgil.yield_thread()
     print "OK"  # there is also a release/acquire pair here
     return 0
Example #3
0
 def main(argv):
     check('1')
     rgil.release()
     # don't have the GIL here
     check('2', False)
     rgil.acquire()
     check('3')
     rgil.yield_thread()
     check('4')
     print "OK"  # there is also a release/acquire pair here
     check('5')
     return 0
Example #4
0
def test_semlock_release(space):
    # trigger the setup() code in time.moduledef
    space.getbuiltinmodule('time')
    sem_name = '/test8'
    _handle = create_semaphore(space, sem_name, 1, 1)
    try:
        w_lock = W_SemLock(space, _handle, 0, 1)
        created = []
        successful = []
        N_THREADS = 16

        def run(space):
            w_sentinel = allocate_lock(space)
            yield_thread()
            w_sentinel.descr_lock_acquire(space)  # releases GIL
            try:
                yield_thread()
                created.append(w_sentinel)
                w_got = w_lock.acquire(
                    space, w_timeout=space.newfloat(5.))  # releases GIL
                if space.is_true(w_got):
                    yield_thread()
                    w_lock.release(space)
                    successful.append(w_sentinel)
                w_sentinel.descr_lock_release(space)
            except:
                import traceback
                traceback.print_exc()
                w_sentinel.descr_lock_release(space)
                raise

        w_run = space.wrap(interp2app(run))

        w_lock.acquire(space)
        for _ in range(N_THREADS):
            start_new_thread(space, w_run, space.newtuple([]))  # releases GIL
        deadline = time.time() + 5.
        while len(created) < N_THREADS:
            assert time.time() < deadline
            yield_thread()
        w_lock.release(space)

        for w_sentinel in created:
            # Join thread
            w_sentinel.descr_lock_acquire(space)  # releases GIL
            w_sentinel.descr_lock_release(space)
        assert len(successful) == N_THREADS
    finally:
        delete_semaphore(_handle)
Example #5
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)
         rgil.yield_thread()
         assert i == j
         j += 1
Example #6
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)
         rgil.yield_thread()
         assert i == j
         j += 1
Example #7
0
def test_semlock_release(space):
    sem_name = '/test7'
    _handle = create_semaphore(space, sem_name, 1, 1)
    w_lock = W_SemLock(space, _handle, 0, 1)
    created = []
    successful = []
    N_THREADS = 16

    def run(space):
        w_sentinel = allocate_lock(space)
        yield_thread()
        w_sentinel.descr_lock_acquire(space)  # releases GIL
        yield_thread()
        created.append(w_sentinel)
        w_got = w_lock.acquire(space,
                               w_timeout=space.newfloat(5.))  # releases GIL
        if space.is_true(w_got):
            yield_thread()
            w_lock.release(space)
            successful.append(w_sentinel)
        w_sentinel.descr_lock_release(space)

    w_run = space.wrap(interp2app(run))

    w_lock.acquire(space)
    for _ in range(N_THREADS):
        start_new_thread(space, w_run, space.newtuple([]))  # releases GIL
    deadline = time.time() + 5.
    while len(created) < N_THREADS:
        assert time.time() < deadline
        yield_thread()
    w_lock.release(space)

    for w_sentinel in created:
        # Join thread
        w_sentinel.descr_lock_acquire(space)  # releases GIL
        w_sentinel.descr_lock_release(space)
    assert len(successful) == N_THREADS
Example #8
0
 def run(space):
     w_sentinel = allocate_lock(space)
     yield_thread()
     w_sentinel.descr_lock_acquire(space)  # releases GIL
     yield_thread()
     created.append(w_sentinel)
     w_got = w_lock.acquire(
         space, w_timeout=space.newfloat(5.))  # releases GIL
     if space.is_true(w_got):
         yield_thread()
         w_lock.release(space)
         successful.append(w_sentinel)
     w_sentinel.descr_lock_release(space)
 def run(space):
     w_sentinel = _set_sentinel(space)
     yield_thread()
     w_sentinel.descr_lock_acquire(space)  # releases GIL
     try:
         yield_thread()
         created.append(w_sentinel)
         w_got = w_lock.acquire(
             space, w_timeout=space.newfloat(5.))  # releases GIL
         if space.is_true(w_got):
             yield_thread()
             w_lock.release(space)
             successful.append(w_sentinel)
     except:
         import traceback
         traceback.print_exc()
         raise
Example #10
0
def yield_thread():
    rgil.yield_thread()
    return nil
Example #11
0
def yield_thread():
    rgil.yield_thread()
    return nil
Example #12
0
 def perform(self, executioncontext, frame):
     rgil.yield_thread()