# Path: evergreen/locks.py
# class Condition(object):
# 
#     def __init__(self, lock=None):
#         if lock is None:
#             lock = RLock()
#         self._lock = lock
#         self._waiters = []
# 
#         # Export the lock's acquire() and release() methods
#         self.acquire = lock.acquire
#         self.release = lock.release
#         # If the lock defines _release_save() and/or _acquire_restore(),
#         # these override the default implementations (which just call
#         # release() and acquire() on the lock).  Ditto for _is_owned().
#         try:
#             self._release_save = lock._release_save
#         except AttributeError:
#             pass
#         try:
#             self._acquire_restore = lock._acquire_restore
#         except AttributeError:
#             pass
#         try:
#             self._is_owned = lock._is_owned
#         except AttributeError:
#             pass
# 
#     def wait(self, timeout=None):
#         if not self._is_owned():
#             raise RuntimeError('cannot wait on un-acquired lock')
#         waiter = Semaphore()
#         waiter.acquire()
#         self._waiters.append(waiter)
#         saved_state = self._release_save()
#         try:
#             return waiter.acquire(timeout=timeout)
#         finally:
#             self._acquire_restore(saved_state)
# 
#     def wait_for(self, predicate, timeout=None):
#         endtime = None
#         waittime = timeout
#         result = predicate()
#         if not result:
#             self.wait(waittime)
#             result = predicate()
#         return result
# 
#     def notify(self, n=1):
#         if not self._is_owned():
#             raise RuntimeError('cannot wait on un-acquired lock')
#         _waiters = self._waiters
#         waiters = _waiters[:n]
#         for waiter in waiters:
#             waiter.release()
#             try:
#                 _waiters.remove(waiter)
#             except ValueError:
#                 pass
# 
#     def notify_all(self):
#         self.notify(len(self._waiters))
# 
#     def _acquire_restore(self, state):
#         self._lock.acquire()
# 
#     def _release_save(self):
#         self._lock.release()
# 
#     def _is_owned(self):
#         # Return True if lock is owned by current_thread.
#         # This method is called only if __lock doesn't have _is_owned().
#         if self._lock.acquire(False):
#             self._lock.release()
#             return False
#         else:
#             return True
# 
#     def __enter__(self):
#         return self._lock.__enter__()
# 
#     def __exit__(self, *args):
#         return self._lock.__exit__(*args)
# 
# class Lock(Semaphore):
# 
#     def __init__(self):
#         super(Lock, self).__init__(value=1)


import heapq
from collections import deque
from six.moves import queue as __queue__
from evergreen.locks import Condition, Lock
#
# This file is part of Evergreen. See the NOTICE for more information.
#

from __future__ import absolute_import




__all__ = ['Empty', 'Full', 'Queue', 'PriorityQueue', 'LifoQueue']

Empty = __queue__.Empty
Full = __queue__.Full


class Queue(object):
    """Create a queue object with a given maximum size.

    If maxsize is <= 0, the queue size is infinite.
    """
    def __init__(self, maxsize=0):
        self.maxsize = maxsize
        self._init(maxsize)
        # mutex must be held whenever the queue is mutating.  All methods
        # that acquire mutex must release it before returning.  mutex
        # is shared between the three conditions, so acquiring and
        # releasing the conditions also acquires and releases mutex.

# Path: evergreen/locks.py
# class Condition(object):
# 
#     def __init__(self, lock=None):
#         if lock is None:
#             lock = RLock()
#         self._lock = lock
#         self._waiters = []
# 
#         # Export the lock's acquire() and release() methods
#         self.acquire = lock.acquire
#         self.release = lock.release
#         # If the lock defines _release_save() and/or _acquire_restore(),
#         # these override the default implementations (which just call
#         # release() and acquire() on the lock).  Ditto for _is_owned().
#         try:
#             self._release_save = lock._release_save
#         except AttributeError:
#             pass
#         try:
#             self._acquire_restore = lock._acquire_restore
#         except AttributeError:
#             pass
#         try:
#             self._is_owned = lock._is_owned
#         except AttributeError:
#             pass
# 
#     def wait(self, timeout=None):
#         if not self._is_owned():
#             raise RuntimeError('cannot wait on un-acquired lock')
#         waiter = Semaphore()
#         waiter.acquire()
#         self._waiters.append(waiter)
#         saved_state = self._release_save()
#         try:
#             return waiter.acquire(timeout=timeout)
#         finally:
#             self._acquire_restore(saved_state)
# 
#     def wait_for(self, predicate, timeout=None):
#         endtime = None
#         waittime = timeout
#         result = predicate()
#         if not result:
#             self.wait(waittime)
#             result = predicate()
#         return result
# 
#     def notify(self, n=1):
#         if not self._is_owned():
#             raise RuntimeError('cannot wait on un-acquired lock')
#         _waiters = self._waiters
#         waiters = _waiters[:n]
#         for waiter in waiters:
#             waiter.release()
#             try:
#                 _waiters.remove(waiter)
#             except ValueError:
#                 pass
# 
#     def notify_all(self):
#         self.notify(len(self._waiters))
# 
#     def _acquire_restore(self, state):
#         self._lock.acquire()
# 
#     def _release_save(self):
#         self._lock.release()
# 
#     def _is_owned(self):
#         # Return True if lock is owned by current_thread.
#         # This method is called only if __lock doesn't have _is_owned().
#         if self._lock.acquire(False):
#             self._lock.release()
#             return False
#         else:
#             return True
# 
#     def __enter__(self):
#         return self._lock.__enter__()
# 
#     def __exit__(self, *args):
#         return self._lock.__exit__(*args)
# 
# class Lock(Semaphore):
# 
#     def __init__(self):
#         super(Lock, self).__init__(value=1)


# Path: evergreen/queue.py
import heapq
from collections import deque
from six.moves import queue as __queue__
from evergreen.locks import Condition, Lock

#
# This file is part of Evergreen. See the NOTICE for more information.
#

from __future__ import absolute_import




__all__ = ['Empty', 'Full', 'Queue', 'PriorityQueue', 'LifoQueue']

Empty = __queue__.Empty
Full = __queue__.Full


class Queue(object):
    """Create a queue object with a given maximum size.

    If maxsize is <= 0, the queue size is infinite.
    """
    def __init__(self, maxsize=0):
        self.maxsize = maxsize
        self._init(maxsize)
        # mutex must be held whenever the queue is mutating.  All methods
        # that acquire mutex must release it before returning.  mutex
        # is shared between the three conditions, so acquiring and
        # releasing the conditions also acquires and releases mutex.

        self.mutex = Lock()