From: Andrej A Antonov Date: Fri, 20 Apr 2012 21:34:00 +0000 (+0400) Subject: double-checked-locking in tornado.ioloop.IOLoop.instance() X-Git-Tag: v2.3.0~41^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=840392fa34b61cea923149f813df6c3d72d9aedd;p=thirdparty%2Ftornado.git double-checked-locking in tornado.ioloop.IOLoop.instance() --- diff --git a/tornado/ioloop.py b/tornado/ioloop.py index abb2de02b..3e1422989 100644 --- a/tornado/ioloop.py +++ b/tornado/ioloop.py @@ -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