Debian Codegraph @

ruby/lib/core/work_queue.rb

raw
1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License. You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17
18 module Qpid::Proton
19
20 # A thread-safe queue of work for multi-threaded programs.
21 #
22 # A {Container} can have multiple threads calling {Container#run}
23 # The container ensures that work associated with a single {Connection} or
24 # {Listener} is _serialized_ - two threads will never concurrently call
25 # handlers associated with the same object.
26 #
27 # To have your own code serialized in the same, add a block to the connection's
28 # {WorkQueue}. The block will be invoked as soon as it is safe to do so.
29 #
30 # A {Connection} and the objects associated with it ({Session}, {Sender},
31 # {Receiver}, {Delivery}, {Tracker}) are not thread safe, so if you have
32 # multiple threads calling {Container#run} or if you want to affect objects
33 # managed by the container from non-container threads you need to use the
34 # {WorkQueue}
35 #
36 class WorkQueue
37
38 # Error raised if work is added after the queue has been stopped.
39 class StoppedError < Qpid::Proton::StoppedError
40 def initialize() super("WorkQueue has been stopped"); end
41 end
42
43 # Add a block of code to be invoked in sequence.
44 #
45 # @yield [ ] the block will be invoked with no parameters in the appropriate thread context
46 # @note Thread Safe: may be called in any thread.
47 # @return [void]
48 # @raise [StoppedError] if the queue is closed and cannot accept more work
49 def add(&block)
50 schedule(0, &block)
51 end
52
53 # Schedule a block to be invoked at a certain time.
54 #
55 # @param at [Time] Invoke block as soon as possible after Time +at+
56 # @param at [Numeric] Invoke block after a delay of +at+ seconds from now
57 # @yield [ ] (see #add)
58 # @note (see #add)
59 # @return (see #add)
60 # @raise (see #add)
61 def schedule(at, &block)
62 raise ArgumentError, "no block" unless block_given?
63 @lock.synchronize do
64 raise @closed if @closed
65 @schedule.insert(at, block)
66 end
67 @container.send :wake
68 end
69
70 # @private
71 def initialize(container)
72 @lock = Mutex.new
73 @schedule = Schedule.new
74 @container = container
75 @closed = nil
76 end
77
78 # @private
79 def close() @lock.synchronize { @closed = StoppedError.new } end
80
81 # @private
82 def process(now)
83 while p = @lock.synchronize { @schedule.pop(now) }
84 p.call
85 end
86 end
87
88 # @private
89 def next_tick() @lock.synchronize { @schedule.next_tick } end
90
91 # @private
92 def empty?() @lock.synchronize { @schedule.empty? } end
93
94 # @private
95 def clear() @lock.synchronize { @schedule.clear } end
96 end
97 end