Beispiel #1
0
 def entry_point(argv):
     os.write(1, "hello world\n")
     error = rthread.set_stacksize(int(argv[1]))
     if error != 0:
         os.write(
             2,
             "set_stacksize(%d) returned %d\n" % (int(argv[1]), error))
         raise AssertionError
     # malloc a bit
     s1 = State()
     s2 = State()
     s3 = State()
     s1.x = 0x11111111
     s2.x = 0x22222222
     s3.x = 0x33333333
     # start 3 new threads
     state.ll_lock = rthread.allocate_ll_lock()
     after()
     state.count = 0
     invoke_around_extcall(before, after)
     ident1 = rthread.start_new_thread(bootstrap, ())
     ident2 = rthread.start_new_thread(bootstrap, ())
     ident3 = rthread.start_new_thread(bootstrap, ())
     # wait for the 3 threads to finish
     while True:
         if state.count == 3:
             break
         time.sleep(0.1)  # invokes before/after
     # check that the malloced structures were not overwritten
     assert s1.x == 0x11111111
     assert s2.x == 0x22222222
     assert s3.x == 0x33333333
     os.write(1, "done\n")
     return 0
Beispiel #2
0
def test_simple_tcp():
    from rpython.rlib import rthread
    sock = RSocket()
    try_ports = [1023] + range(20000, 30000, 437)
    for port in try_ports:
        print 'binding to port %d:' % (port,),
        try:
            sock.bind(INETAddress('127.0.0.1', port))
            print 'works'
            break
        except SocketError as e:   # should get a "Permission denied"
            print e
    else:
        raise e

    addr = INETAddress('127.0.0.1', port)
    assert addr.eq(sock.getsockname())
    sock.listen(1)
    s2 = RSocket(AF_INET, SOCK_STREAM)
    s2.settimeout(10.0) # test one side with timeouts so select is used, shouldn't affect test
    connected = [False] #thread-mutable list
    def connecting():
        try:
            s2.connect(addr)
            connected[0] = True
        finally:
            lock.release()
    lock = rthread.allocate_lock()
    lock.acquire(True)
    rthread.start_new_thread(connecting, ())
    print 'waiting for connection'
    fd1, addr2 = sock.accept()
    s1 = RSocket(fd=fd1)
    print 'connection accepted'
    lock.acquire(True)
    assert connected[0]
    print 'connecting side knows that the connection was accepted too'
    assert addr.eq(s2.getpeername())
    #assert addr2.eq(s2.getsockname())
    assert addr2.eq(s1.getpeername())

    s1.send('?')
    print 'sent one character'
    buf = s2.recv(100)
    assert buf == '?'
    print 'received ok'
    def sendstuff():
        s2.sendall('x'*50000)
    rthread.start_new_thread(sendstuff, ())
    buf = ''
    while len(buf) < 50000:
        data = s1.recv(50100)
        print 'recv returned %d bytes' % (len(data,))
        assert data
        buf += data
    assert buf == 'x'*50000
    print 'data received ok'
    s1.shutdown(SHUT_RDWR)
    s1.close()
    s2.close()
Beispiel #3
0
def test_simple_tcp():
    from rpython.rlib import rthread
    sock = RSocket()
    try_ports = [1023] + range(20000, 30000, 437)
    for port in try_ports:
        print 'binding to port %d:' % (port,),
        try:
            sock.bind(INETAddress('127.0.0.1', port))
            print 'works'
            break
        except SocketError as e:   # should get a "Permission denied"
            print e
    else:
        raise e

    addr = INETAddress('127.0.0.1', port)
    assert addr.eq(sock.getsockname())
    sock.listen(1)
    s2 = RSocket(AF_INET, SOCK_STREAM)
    s2.settimeout(10.0) # test one side with timeouts so select is used, shouldn't affect test
    connected = [False] #thread-mutable list
    def connecting():
        try:
            s2.connect(addr)
            connected[0] = True
        finally:
            lock.release()
    lock = rthread.allocate_lock()
    lock.acquire(True)
    rthread.start_new_thread(connecting, ())
    print 'waiting for connection'
    fd1, addr2 = sock.accept()
    s1 = RSocket(fd=fd1)
    print 'connection accepted'
    lock.acquire(True)
    assert connected[0]
    print 'connecting side knows that the connection was accepted too'
    assert addr.eq(s2.getpeername())
    #assert addr2.eq(s2.getsockname())
    assert addr2.eq(s1.getpeername())

    s1.send('?')
    print 'sent one character'
    buf = s2.recv(100)
    assert buf == '?'
    print 'received ok'
    def sendstuff():
        s2.sendall('x'*50000)
    rthread.start_new_thread(sendstuff, ())
    buf = ''
    while len(buf) < 50000:
        data = s1.recv(50100)
        print 'recv returned %d bytes' % (len(data,))
        assert data
        buf += data
    assert buf == 'x'*50000
    print 'data received ok'
    s1.shutdown(SHUT_RDWR)
    s1.close()
    s2.close()
Beispiel #4
0
 def entry_point(argv):
     os.write(1, "hello world\n")
     error = rthread.set_stacksize(int(argv[1]))
     if error != 0:
         os.write(2, "set_stacksize(%d) returned %d\n" % (
             int(argv[1]), error))
         raise AssertionError
     # malloc a bit
     s1 = State(); s2 = State(); s3 = State()
     s1.x = 0x11111111; s2.x = 0x22222222; s3.x = 0x33333333
     # start 3 new threads
     state.ll_lock = rthread.allocate_ll_lock()
     after()
     state.count = 0
     invoke_around_extcall(before, after)
     ident1 = rthread.start_new_thread(bootstrap, ())
     ident2 = rthread.start_new_thread(bootstrap, ())
     ident3 = rthread.start_new_thread(bootstrap, ())
     # wait for the 3 threads to finish
     while True:
         if state.count == 3:
             break
         time.sleep(0.1)      # invokes before/after
     # check that the malloced structures were not overwritten
     assert s1.x == 0x11111111
     assert s2.x == 0x22222222
     assert s3.x == 0x33333333
     os.write(1, "done\n")
     return 0
Beispiel #5
0
 def main(argv):
     print "A"
     rthread.start_new_thread(bootstrap, ())
     for i in range(2):
         _sleep(2)
         print "AAAA"
     return 9
Beispiel #6
0
 def main(argv):
     ec = EC(12)
     raw_thread_local.set(ec)
     rthread.start_new_thread(bootstrap, ())
     _sleep(2)
     print raw_thread_local.get().value
     assert raw_thread_local.get() is ec
     return 9
Beispiel #7
0
 def main(argv):
     revdb.stop_point()
     rthread.start_new_thread(bootstrap, ())
     for i in range(2):
         _sleep(2)
         revdb.stop_point()
     print "ok"
     return 9
Beispiel #8
0
        def main(argv):
            for j in range(N_THREADS):
                lock = rthread.allocate_lock()
                lock.acquire(True)
                glob.my_locks.append(lock)

            for j in range(N_THREADS):
                rthread.start_new_thread(do_random_work, ())

            for j in range(N_THREADS):
                glob.my_locks[j].acquire(True)

            print "OK"
            return 0
 def f():
     state.data = []
     state.datalen1 = 0
     state.datalen2 = 0
     state.datalen3 = 0
     state.datalen4 = 0
     state.threadlocals = my_gil_threadlocals
     state.threadlocals.setup_threads(space)
     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:
             llop.debug_print(lltype.Void, "timeout. progress: "
                              "%d of 2*N (= %f%%)" % \
                              (len(state.data), 2*N, 100*len(state.data)/(2.0*N)))
             raise ValueError("time out")
         still_waiting -= 1
         if not we_are_translated(): rgil.release()
         time.sleep(0.1)
         if not we_are_translated(): rgil.acquire()
     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)
Beispiel #10
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)
Beispiel #11
0
 def f():
     state.data = []
     state.datalen1 = 0
     state.datalen2 = 0
     state.datalen3 = 0
     state.datalen4 = 0
     state.threadlocals = my_gil_threadlocals
     state.threadlocals.setup_threads(space)
     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)
Beispiel #12
0
def start_new_thread(space, w_callable, w_args, w_kwargs=None):
    """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.isinstance_w(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.isinstance_w(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:
            ident = rthread.start_new_thread(bootstrapper.bootstrap, ())
        except Exception:
            bootstrapper.release()     # normally called by the new thread
            raise
    except rthread.error:
        raise wrap_thread_error(space, "can't start new thread")
    return space.wrap(ident)
Beispiel #13
0
def start_new_thread(space, w_callable, w_args, w_kwargs=None):
    """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.isinstance_w(w_args, space.w_tuple):
        raise oefmt(space.w_TypeError, "2nd arg must be a tuple")
    if w_kwargs is not None and not space.isinstance_w(w_kwargs, space.w_dict):
        raise oefmt(space.w_TypeError, "optional 3rd arg must be a dictionary")
    if not space.is_true(space.callable(w_callable)):
        raise oefmt(space.w_TypeError, "first arg must be callable")

    args = Arguments.frompacked(space, w_args, w_kwargs)
    bootstrapper.acquire(space, w_callable, args)
    try:
        try:
            ident = rthread.start_new_thread(bootstrapper.bootstrap, ())
        except Exception:
            bootstrapper.release()  # normally called by the new thread
            raise
    except rthread.error:
        raise wrap_thread_error(space, "can't start new thread")
    return space.newint(ident)
Beispiel #14
0
 def new_thread():
     ident = rthread.start_new_thread(bootstrap, ())
     time.sleep(0.5)  # enough time to start, hopefully
     return ident
Beispiel #15
0
def new_thread(fn):
    bootstrapper.aquire(fn)
    ident = rthread.start_new_thread(bootstrap, ())
    return nil
Beispiel #16
0
 def new_thread():
     ident = rthread.start_new_thread(bootstrap, ())
     time.sleep(0.5)    # enough time to start, hopefully
     return ident
Beispiel #17
0
def new_thread(fn):
    bootstrapper.aquire(fn)
    ident = rthread.start_new_thread(bootstrap, ())
    return nil
Beispiel #18
0
    addr = INETAddress('127.0.0.1', port)
    assert addr.eq(sock.getsockname())
    sock.listen(1)
    s2 = RSocket(AF_INET, SOCK_STREAM)
    s2.settimeout(10.0) # test one side with timeouts so select is used, shouldn't affect test
    connected = [False] #thread-mutable list
    def connecting():
        try:
            s2.connect(addr)
            connected[0] = True
        finally:
            lock.release()
    lock = rthread.allocate_lock()
    lock.acquire(True)
    rthread.start_new_thread(connecting, ())
    print 'waiting for connection'
    fd1, addr2 = sock.accept()
    s1 = RSocket(fd=fd1)
    print 'connection accepted'
    lock.acquire(True)
    assert connected[0]
    print 'connecting side knows that the connection was accepted too'
    assert addr.eq(s2.getpeername())
    #assert addr2.eq(s2.getsockname())
    assert addr2.eq(s1.getpeername())

    s1.send('?')
    print 'sent one character'
    buf = s2.recv(100)
    assert buf == '?'
Beispiel #19
0
    s2 = RSocket(AF_INET, SOCK_STREAM)
    s2.settimeout(
        10.0
    )  # test one side with timeouts so select is used, shouldn't affect test
    connected = [False]  #thread-mutable list

    def connecting():
        try:
            s2.connect(addr)
            connected[0] = True
        finally:
            lock.release()

    lock = rthread.allocate_lock()
    lock.acquire(True)
    rthread.start_new_thread(connecting, ())
    print 'waiting for connection'
    fd1, addr2 = sock.accept()
    s1 = RSocket(fd=fd1)
    print 'connection accepted'
    lock.acquire(True)
    assert connected[0]
    print 'connecting side knows that the connection was accepted too'
    assert addr.eq(s2.getpeername())
    #assert addr2.eq(s2.getsockname())
    assert addr2.eq(s1.getpeername())

    s1.send('?')
    print 'sent one character'
    buf = s2.recv(100)
    assert buf == '?'