Author: mattip <[email protected]>
Branch: issue1430
Changeset: r71149:87914f035951
Date: 2014-05-01 22:22 +0300
http://bitbucket.org/pypy/pypy/changeset/87914f035951/

Log:    try a different approach

diff --git a/rpython/rlib/rsocket.py b/rpython/rlib/rsocket.py
--- a/rpython/rlib/rsocket.py
+++ b/rpython/rlib/rsocket.py
@@ -20,7 +20,6 @@
 from rpython.rlib.rarithmetic import intmask, r_uint
 from rpython.rtyper.lltypesystem import lltype, rffi
 from rpython.rtyper.lltypesystem.rffi import sizeof, offsetof
-from rpython.rlib import rthread
 INVALID_SOCKET = _c.INVALID_SOCKET
 
 def mallocbuf(buffersize):
@@ -30,8 +29,6 @@
 constants = _c.constants
 locals().update(constants) # Define constants from _c
 
-ll_locks = {}
-
 if _c.WIN32:
     from rpython.rlib import rwin32
     def rsocket_startup():
@@ -41,12 +38,9 @@
             assert res == 0
         finally:
             lltype.free(wsadata, flavor='raw')
-        ll_locks['gethostbyname'] = rthread.allocate_lock()
-        ll_locks['gethostbyaddr'] = rthread.allocate_lock()
 else:
     def rsocket_startup():
-        ll_locks['gethostbyname'] = rthread.allocate_lock()
-        ll_locks['gethostbyaddr'] = rthread.allocate_lock()
+        pass
 
 
 def ntohs(x):
@@ -1130,18 +1124,18 @@
         paddr = h_addr_list[i]
     return (rffi.charp2str(hostent.c_h_name), aliases, address_list)
 
-def gethostbyname_ex(name):
+def gethostbyname_ex(name, lock):
     # XXX use gethostbyname_r() if available instead of locks
     addr = gethostbyname(name)
-    with ll_locks['gethostbyname']:
+    with lock:
         hostent = _c.gethostbyname(name)
         return gethost_common(name, hostent, addr)
 
-def gethostbyaddr(ip):
+def gethostbyaddr(ip, lock):
     # XXX use gethostbyaddr_r() if available, instead of locks
     addr = makeipaddr(ip)
     assert isinstance(addr, IPAddress)
-    with ll_locks['gethostbyaddr']:
+    with lock:
         p, size = addr.lock_in_addr()
         try:
             hostent = _c.gethostbyaddr(p, size, addr.family)
diff --git a/rpython/rlib/test/test_rsocket.py 
b/rpython/rlib/test/test_rsocket.py
--- a/rpython/rlib/test/test_rsocket.py
+++ b/rpython/rlib/test/test_rsocket.py
@@ -3,6 +3,14 @@
 from rpython.rlib.rsocket import *
 import socket as cpy_socket
 
+class DummyLock(object):
+    def __enter__(self):
+        pass
+
+    def __exit__(self, *args):
+        pass
+
+
 def setup_module(mod):
     rsocket_startup()
 
@@ -47,7 +55,7 @@
 
 def test_gethostbyname_ex():
     for host in ["localhost", "127.0.0.1"]:
-        name, aliases, address_list = gethostbyname_ex(host)
+        name, aliases, address_list = gethostbyname_ex(host, DummyLock())
         allnames = [name] + aliases
         for n in allnames:
             assert isinstance(n, str)
@@ -67,8 +75,9 @@
     domain = 'google.com'
     result = [0] * nthreads
     threads = [None] * nthreads
+    lock = threading.Lock()
     def lookup_name(i):
-        name, aliases, address_list = gethostbyname_ex(domain)
+        name, aliases, address_list = gethostbyname_ex(domain, lock)
         if name == domain:
             result[i] += 1
     for i in range(nthreads):
@@ -82,11 +91,12 @@
     import threading
     nthreads = 10
     ip = '8.8.8.8'
-    domain = gethostbyaddr(ip)[0]
+    lock = threading.Lock()
+    domain = gethostbyaddr(ip, lock)[0]
     result = [0] * nthreads
     threads = [None] * nthreads
     def lookup_addr(ip, i):
-        name, aliases, address_list = gethostbyaddr(ip)
+        name, aliases, address_list = gethostbyaddr(ip, lock)
         if name == domain:
             result[i] += 1
     for i in range(nthreads):
@@ -110,7 +120,7 @@
             with py.test.raises(ipv6):
                 gethostbyaddr(host)
             continue
-        name, aliases, address_list = gethostbyaddr(host)
+        name, aliases, address_list = gethostbyaddr(host, DummyLock())
         allnames = [name] + aliases
         for n in allnames:
             assert isinstance(n, str)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to