def test_local(self):
        """Test thread-local storage."""
        self.t1_cv = coro.condition_variable()
        self.t2_cv = coro.condition_variable()
        self.shared = coro.ThreadLocal()
        self.shared.x = 1
        t1 = coro.spawn(self.t1)
        t2 = coro.spawn(self.t2)

        # Let them run.
        coro.yield_slice()
        self.t1_cv.wake_one()
        # Let t1 run.
        coro.yield_slice()
        self.assertEqual(self.shared.x, 1)

        self.t2_cv.wake_one()
        # Let t2 run.
        coro.yield_slice()
        self.assertEqual(self.shared.x, 1)

        self.t1_cv.wake_one()
        self.t2_cv.wake_one()
        coro.yield_slice()

        t1.join()
        t2.join()
        self.assertEqual(self.shared.x, 1)
        del self.shared
Exemple #2
0
    def test_local(self):
        """Test thread-local storage."""
        self.t1_cv = coro.condition_variable()
        self.t2_cv = coro.condition_variable()
        self.shared = coro.ThreadLocal()
        self.shared.x = 1
        t1 = coro.spawn(self.t1)
        t2 = coro.spawn(self.t2)

        # Let them run.
        coro.yield_slice()
        self.t1_cv.wake_one()
        # Let t1 run.
        coro.yield_slice()
        self.assertEqual(self.shared.x, 1)

        self.t2_cv.wake_one()
        # Let t2 run.
        coro.yield_slice()
        self.assertEqual(self.shared.x, 1)

        self.t1_cv.wake_one()
        self.t2_cv.wake_one()
        coro.yield_slice()

        t1.join()
        t2.join()
        self.assertEqual(self.shared.x, 1)
        del self.shared
Exemple #3
0
 def __init__(self, connection_service):
     self.connection_service = connection_service
     # Local reference for convenience.
     self.transport = connection_service.transport
     self.recv_buffer = Buffer()
     self.extended_recv_buffer = {}
     self.remote_channel = Remote_Channel()
     self.window_data_added_cv = coro.condition_variable()
     self.channel_request_cv = coro.condition_variable()
     self.channel_open_cv = coro.condition_variable()
 def __init__(self, connection_service):
     self.connection_service = connection_service
     # Local reference for convenience.
     self.transport = connection_service.transport
     self.recv_buffer = Buffer()
     self.extended_recv_buffer = {}
     self.remote_channel = Remote_Channel()
     self.window_data_added_cv = coro.condition_variable()
     self.channel_request_cv = coro.condition_variable()
     self.channel_open_cv = coro.condition_variable()
Exemple #5
0
    def __init__ (self, authorizer, channel=ftp_channel, hostname=None, ip='0.0.0.0', port=21):
        self.ip = ip
        self.port = port
        self.authorizer = authorizer
        self.channel = channel
        self.thread_id = None
        # Used to signal when all the clients have exited
        self.shutdown_cv = coro.condition_variable()
        # list of ftp_channel instances
        self.clients = []
        self.session_id = 1

        if hostname is None:
            self.hostname = socket.gethostname()
        else:
            self.hostname = hostname

        # statistics
        self.total_sessions = counter()
        self.closed_sessions = counter()
        self.total_files_out = counter()
        self.total_files_in = counter()
        self.total_bytes_out = counter()
        self.total_bytes_in = counter()
        self.total_exceptions = counter()
Exemple #6
0
 def __init__(self, addr, root=None):
     Channel.__init__(self, None, root)
     self.addr = addr
     self.root = root
     self.state = self.UNCONNECTED
     self.connection_cv = coro.condition_variable()
     self.get_connected()
Exemple #7
0
 def __init__(self, lock=None):
     if lock is None:
         lock = RLock()
     self.__lock = lock
     self.acquire = lock.acquire
     self.release = lock.release
     self.__cv = coro.condition_variable()
Exemple #8
0
 def __init__(self, lock=None):
     if lock is None:
         lock = RLock()
     self.__lock = lock
     self.acquire = lock.acquire
     self.release = lock.release
     self.__cv = coro.condition_variable()
Exemple #9
0
 def __init__ (self, addr, root=None):
     Channel.__init__ (self, None, root)
     self.addr = addr
     self.root = root
     self.state = self.UNCONNECTED
     self.connection_cv = coro.condition_variable()
     self.get_connected()
Exemple #10
0
 def wait_for (self, key):
     "wait on a CV with <key> (used by get_block, etc...)"
     if not self.waiting.has_key (key):
         self.waiting[key] = coro.condition_variable()
     try:
         return self.waiting[key].wait()
     finally:
         del self.waiting[key]
Exemple #11
0
    def __init__(self, client):
        self._client = client

        # It isn't necessary to hang onto the thread, but it might
        # help in debugging.
        self._thread = None

        self._res = None
        self._cv = coro.condition_variable()
Exemple #12
0
 def __init__ (self, in_flight=20):
     self.queue = coro.fifo()
     self.qset = set()
     self.requested = set()
     self.ready = {}
     self.target = G.block_db.last_block
     self.running = False
     self.live_cv = coro.condition_variable()
     self.in_flight_sem = coro.semaphore (in_flight)
Exemple #13
0
 def __init__(self, in_flight=20):
     self.queue = coro.fifo()
     self.qset = set()
     self.requested = set()
     self.ready = {}
     self.target = G.block_db.last_block
     self.running = False
     self.live_cv = coro.condition_variable()
     self.in_flight_sem = coro.semaphore(in_flight)
Exemple #14
0
    def __init__(self, client):
        self._client = client

        # It isn't necessary to hang onto the thread, but it might
        # help in debugging.
        self._thread = None

        self._res = None
        self._cv = coro.condition_variable()
Exemple #15
0
 def __init__ (self, conn):
     self.conn = conn
     self.num = channel.counter
     self.confirm_mode = False
     self.consumers = {}
     channel.counter += 1
     self._exception_handler = None
     self._next_delivery_tag = 0
     self._publish_cv = coro.condition_variable()
     self._publish_thread = coro.spawn(self._publish_thread_loop)
     self._pending_published = {}
Exemple #16
0
 def testit(family, address, block_sends, expected_buffer_result, expected_return):
     global finished
     finished = coro.condition_variable()
     s = coro.make_socket(family, socket.SOCK_STREAM)
     s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, send_buffer_size)
     coro.with_timeout(5, s.connect, (address, server.port))
     blocks = [ big_block[:size] for size in block_sends ]
     rc = coro.with_timeout(5, s.writev, blocks)
     s.close()
     if finished is not None:
         coro.with_timeout(5, finished.wait)
     self.assertEqual(expected_buffer_result, current_buffer)
     self.assertEqual(expected_return, rc)
Exemple #17
0
 def testit(family, address, block_sends, expected_buffer_result,
            expected_return):
     global finished
     finished = coro.condition_variable()
     s = coro.make_socket(family, socket.SOCK_STREAM)
     s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, send_buffer_size)
     coro.with_timeout(5, s.connect, (address, server.port))
     blocks = [big_block[:size] for size in block_sends]
     rc = coro.with_timeout(5, s.writev, blocks)
     s.close()
     if finished is not None:
         coro.with_timeout(5, finished.wait)
     self.assertEqual(expected_buffer_result, current_buffer)
     self.assertEqual(expected_return, rc)
Exemple #18
0
 def __init__ (self, auth, host, port=5672, virtual_host='/', heartbeat=0):
     self.port = port
     self.host = host
     self.auth = auth
     self.virtual_host = virtual_host
     self.heartbeat = heartbeat
     self.frame_state = 0
     self.frames = coro.fifo()
     # collect body parts here.  heh.
     self.body = []
     self.next_content_consumer = None
     self.next_properties = None
     self.consumers = {}
     self.closed_cv = coro.condition_variable()
     self.last_send = coro.now
     self.channels = {}
Exemple #19
0
 def __init__ (self, auth, host, port=5672, virtual_host='/', heartbeat=0):
     self.port = port
     self.host = host
     self.auth = auth
     self.virtual_host = virtual_host
     self.heartbeat = heartbeat
     self.frame_state = 0
     self.frames = coro.fifo()
     # collect body parts here.  heh.
     self.body = []
     self.next_content_consumer = None
     self.next_properties = None
     self.consumers = {}
     self.closed_cv = coro.condition_variable()
     self.last_send = coro.now
     self.channels = {}
 def test_cond_schedule_interrupt(self):
     """Test schedule then interrupt on condition variable."""
     c = coro.condition_variable()
     self._resume_count = 0
     threads = []
     # Spawn some threads that will block and be interrupted.
     for unused in xrange(5):
         threads.append(coro.spawn(self._cond_block, c))
     # Spawn a thread that we will not interrupt.
     no_interrupt_thread = coro.spawn(self._cond_block, c)
     coro.yield_slice()
     # Schedule all of the threads (except the no interrupt thread).
     c.wake_all()
     # Now interrupt them.
     for t in threads:
         t.shutdown()
     coro.yield_slice()
     # Verify that it ran.
     self.assertEqual(self._resume_count, 1)
Exemple #21
0
 def __init__(self, read_only=True):
     from __main__ import G
     self.read_only = read_only
     self.blocks = {}
     self.prev = {}
     self.block_num = {ZERO_NAME: -1}
     self.num_block = {}
     self.last_block = 0
     self.new_block_cv = coro.condition_variable()
     self.file = None
     metadata_path = os.path.join(G.args.base, self.metadata_path)
     if os.path.isfile(metadata_path):
         f = open(metadata_path, 'rb')
         start_scan = self.load_metadata(f)
         f.close()
     else:
         start_scan = 0
     self.scan_block_chain(start_scan)
     coro.spawn(self.metadata_thread)
 def test_cond_interrupt_schedule(self):
     """Test interrupt then schedule on condition variable."""
     c = coro.condition_variable()
     self._resume_count = 0
     threads = []
     # Spawn some threads that will block and be interrupted.
     for unused in xrange(5):
         threads.append(coro.spawn(self._cond_block, c))
     # Spawn a thread that we will not interrupt.
     no_interrupt_thread = coro.spawn(self._cond_block, c)
     coro.yield_slice()
     # Cause an interrupt on these threads.
     for t in threads:
         t.shutdown()
     # Now try to get the non-interrupted thread to run.
     c.wake_all()
     coro.yield_slice()
     # Verify that it ran.
     self.assertEqual(self._resume_count, 1)
Exemple #23
0
 def test_cond_schedule_interrupt(self):
     """Test schedule then interrupt on condition variable."""
     c = coro.condition_variable()
     self._resume_count = 0
     threads = []
     # Spawn some threads that will block and be interrupted.
     for unused in xrange(5):
         threads.append(coro.spawn(self._cond_block, c))
     # Spawn a thread that we will not interrupt.
     no_interrupt_thread = coro.spawn(self._cond_block, c)
     coro.yield_slice()
     # Schedule all of the threads (except the no interrupt thread).
     c.wake_all()
     # Now interrupt them.
     for t in threads:
         t.shutdown()
     coro.yield_slice()
     # Verify that it ran.
     self.assertEqual(self._resume_count, 1)
Exemple #24
0
 def __init__ (self, read_only=True):
     from __main__ import G
     self.read_only = read_only
     self.blocks = {}
     self.prev = {}
     self.block_num = {ZERO_NAME: -1}
     self.num_block = {}
     self.last_block = 0
     self.new_block_cv = coro.condition_variable()
     self.file = None
     metadata_path = os.path.join (G.args.base, self.metadata_path)
     if os.path.isfile (metadata_path):
         f = open (metadata_path, 'rb')
         start_scan = self.load_metadata (f)
         f.close()
     else:
         start_scan = 0
     self.scan_block_chain (start_scan)
     coro.spawn (self.metadata_thread)
Exemple #25
0
 def test_cond_interrupt_schedule(self):
     """Test interrupt then schedule on condition variable."""
     c = coro.condition_variable()
     self._resume_count = 0
     threads = []
     # Spawn some threads that will block and be interrupted.
     for unused in xrange(5):
         threads.append(coro.spawn(self._cond_block, c))
     # Spawn a thread that we will not interrupt.
     no_interrupt_thread = coro.spawn(self._cond_block, c)
     coro.yield_slice()
     # Cause an interrupt on these threads.
     for t in threads:
         t.shutdown()
     # Now try to get the non-interrupted thread to run.
     c.wake_all()
     coro.yield_slice()
     # Verify that it ran.
     self.assertEqual(self._resume_count, 1)
Exemple #26
0
 def __init__(self, default_size=1024, data=None):
     """circular_fifo(default_size = 1024, data = None) -> circular_fifo_object
     Default size is the number of slots you want to start with.
     data can be a list of elements to seed the fifo.
     """
     self.cv = coro.condition_variable()
     if data:
         # Note that we need an extra element because
         # we need somewhere for tail to point to.
         if len(data) <= default_size:
             default_size = len(data) + 1
         self._fifo = copy.copy(data)
         self._fifo.extend([None] * (default_size - len(data)))
         self._tail = len(data)
         self._head = 0
         self._fifo_size = default_size
     else:
         self._fifo = [None] * default_size
         self._tail = 0
         self._head = 0
         self._fifo_size = default_size
Exemple #27
0
 def __init__(self, default_size = 1024, data = None):
     """circular_fifo(default_size = 1024, data = None) -> circular_fifo_object
     Default size is the number of slots you want to start with.
     data can be a list of elements to seed the fifo.
     """
     self.cv = coro.condition_variable()
     if data:
         # Note that we need an extra element because
         # we need somewhere for tail to point to.
         if len(data) <= default_size:
             default_size = len(data)+1
         self._fifo = copy.copy(data)
         self._fifo.extend([None]*(default_size - len(data)))
         self._tail = len(data)
         self._head = 0
         self._fifo_size = default_size
     else:
         self._fifo = [None]*default_size
         self._tail = 0
         self._head = 0
         self._fifo_size = default_size
Exemple #28
0
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import coro

wait1 = coro.condition_variable()
wait2 = coro.condition_variable()

def func1():
    print "func1 entering into wait."
    wait1.wait()
    print "func1 broke out of wait."

def func1_2():
    print "func1_2 entering into wait."
    wait1.wait()
    print "func1_2 broke out of wait."

def func2():
    print "func2 entering into wait."
    wait2.wait()
Exemple #29
0
 def __init__(self):
     self.q = []
     self.cv = coro.condition_variable()
Exemple #30
0
 def __init__(self, *args):
     LockSmith.__init__(self, *args)
     # Threads waiting to acquire the global lock wait on global_cv.
     self.global_cv = coro.condition_variable()
     # Threads waiting for the global lock to be released wait on global_finished_cv.
     self.global_finished_cv = coro.condition_variable()
Exemple #31
0
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import coro

wait1 = coro.condition_variable()
wait2 = coro.condition_variable()


def func1():
    print "func1 entering into wait."
    wait1.wait()
    print "func1 broke out of wait."


def func1_2():
    print "func1_2 entering into wait."
    wait1.wait()
    print "func1_2 broke out of wait."

Exemple #32
0
def log (subject, *data):
    W ('log: %s %r\n' % (subject, data))

G = GlobalState()

p = argparse.ArgumentParser (description='Pull a copy of the blockchain from another node.')
p.add_argument ('connect', help="connect to this address", metavar='IP:PORT')
p.add_argument ('-i', '--inflight', help='number of blocks in flight.', type=int, default=20)
p.add_argument ('-b', '--base', help='data directory', default='/usr/local/caesure', metavar='PATH')
p.add_argument ('-g', '--getblocks', action='store_true', help='use getblocks rather than getheaders')

G.args = p.parse_args()
G.args.packet = False
G.log = log
G.verbose = False
G.block_db = BlockDB()

block_fifo = coro.fifo()
# tried to use inverted_semaphore here, couldn't get it to work.
in_flight = 0
in_flight_cv = coro.condition_variable()

import coro.backdoor
coro.spawn (coro.backdoor.serve, unix_path='/tmp/chainpuller.bd')

coro.spawn (go, G)
coro.spawn (rate_thread)
coro.spawn (write_thread)
coro.event_loop()
Exemple #33
0
 def __init__(self):
     self.q = []
     self.cv = coro.condition_variable()
Exemple #34
0
 def __init__(self, *args):
     LockSmith.__init__(self, *args)
     # Threads waiting to acquire the global lock wait on global_cv.
     self.global_cv = coro.condition_variable()
     # Threads waiting for the global lock to be released wait on global_finished_cv.
     self.global_finished_cv = coro.condition_variable()
Exemple #35
0
 def __init__ (self):
     self.cv = coro.condition_variable()
     self.done = False
Exemple #36
0
 def __init__(self):
     self.__cond = coro.condition_variable()
     self.__flag = False
Exemple #37
0
 def __init__(self):
     self.cv = coro.condition_variable()
     self.done = False
Exemple #38
0
 def __init__(self):
     self.__cond = coro.condition_variable()
     self.__flag = False