# Path: evergreen/event.py
# class Event(object):
# 
#     def __init__(self):
#         self._cond = Condition(Lock())
#         self._flag = False
# 
#     def is_set(self):
#         return self._flag
# 
#     def set(self):
#         self._cond.acquire()
#         try:
#             self._flag = True
#             self._cond.notify_all()
#         finally:
#             self._cond.release()
# 
#     def clear(self):
#         self._cond.acquire()
#         try:
#             self._flag = False
#         finally:
#             self._cond.release()
# 
#     def wait(self, timeout=None):
#         self._cond.acquire()
#         try:
#             signaled = self._flag
#             if not signaled:
#                 signaled = self._cond.wait(timeout)
#             return signaled
#         finally:
#             self._cond.release()


import six
import evergreen
from evergreen.event import Event
from fibers import Fiber
def task(func):
    """Decorator to run the decorated function as a Task
    """
    def task_wrapper(*args, **kwargs):
        return spawn(func, *args, **kwargs)
    return task_wrapper


class TaskExit(BaseException):
    pass


# Helper to generate new task names
_counter = 0
def _newname(template="Task-%d"):
    global _counter
    _counter = _counter + 1
    return template % _counter


class Task(Fiber):

    def __init__(self, target=None, name=None, args=(), kwargs={}):
        super(Task, self).__init__(target=self.__run, parent=evergreen.current.loop.task)
        self._name = str(name or _newname())
        self._target = target
        self._args = args
        self._kwargs = kwargs
        self._started = False
        self._running = False

# Path: evergreen/event.py
# class Event(object):
# 
#     def __init__(self):
#         self._cond = Condition(Lock())
#         self._flag = False
# 
#     def is_set(self):
#         return self._flag
# 
#     def set(self):
#         self._cond.acquire()
#         try:
#             self._flag = True
#             self._cond.notify_all()
#         finally:
#             self._cond.release()
# 
#     def clear(self):
#         self._cond.acquire()
#         try:
#             self._flag = False
#         finally:
#             self._cond.release()
# 
#     def wait(self, timeout=None):
#         self._cond.acquire()
#         try:
#             signaled = self._flag
#             if not signaled:
#                 signaled = self._cond.wait(timeout)
#             return signaled
#         finally:
#             self._cond.release()


# Path: evergreen/tasks.py
import six
import evergreen
from evergreen.event import Event
from fibers import Fiber

def task(func):
    """Decorator to run the decorated function as a Task
    """
    def task_wrapper(*args, **kwargs):
        return spawn(func, *args, **kwargs)
    return task_wrapper


class TaskExit(BaseException):
    pass


# Helper to generate new task names
_counter = 0
def _newname(template="Task-%d"):
    global _counter
    _counter = _counter + 1
    return template % _counter


class Task(Fiber):

    def __init__(self, target=None, name=None, args=(), kwargs={}):
        super(Task, self).__init__(target=self.__run, parent=evergreen.current.loop.task)
        self._name = str(name or _newname())
        self._target = target
        self._args = args
        self._kwargs = kwargs
        self._started = False
        self._running = False

        self._exit_event = Event()