]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
double-checked-locking in tornado.ioloop.IOLoop.instance() 497/head
authorAndrej A Antonov <polymorphm@gmail.com>
Fri, 20 Apr 2012 21:34:00 +0000 (01:34 +0400)
committerAndrej A Antonov <polymorphm@gmail.com>
Fri, 20 Apr 2012 21:37:53 +0000 (01:37 +0400)
tornado/ioloop.py

index abb2de02b7c4cee45add7593a8018ab32c189579..3e14229898f57c471745bc74bc5014be188e905f 100644 (file)
@@ -104,6 +104,9 @@ class IOLoop(object):
     WRITE = _EPOLLOUT
     ERROR = _EPOLLERR | _EPOLLHUP
 
+    # Global lock for creating global IOLoop instance
+    _instance_lock = threading.Lock()
+
     def __init__(self, impl=None):
         self._impl = impl or _poll()
         if hasattr(self._impl, 'fileno'):
@@ -142,7 +145,10 @@ class IOLoop(object):
                     self.io_loop = io_loop or IOLoop.instance()
         """
         if not hasattr(IOLoop, "_instance"):
-            IOLoop._instance = IOLoop()
+            with IOLoop._instance_lock:
+                if not hasattr(IOLoop, "_instance"):
+                    # New instance after double check
+                    IOLoop._instance = IOLoop()
         return IOLoop._instance
 
     @staticmethod