@@ -33,23 +33,39 @@ def initialize
33
33
#
34
34
# @raise [ArgumentError] if no task is given
35
35
def post ( executor , *args , &task )
36
- return nil if task . nil?
36
+ posts [ [ executor , args , task ] ]
37
+ true
38
+ end
39
+
40
+ # As {#post} but allows to submit multiple tasks at once, it's guaranteed that they will not
41
+ # be interleaved by other tasks.
42
+ #
43
+ # @param [Array<Array(Executor, Array<Object>, Proc)>] posts array of triplets where
44
+ # first is a {Executor}, second is array of args for task, third is a task (Proc)
45
+ def posts ( posts )
46
+ # if can_overflow?
47
+ # raise ArgumentError, 'SerializedExecution does not support thread-pools which can overflow'
48
+ # end
49
+
50
+ return nil if posts . empty?
37
51
38
- job = Job . new executor , args , task
52
+ jobs = posts . map { | executor , args , task | Job . new executor , args , task }
39
53
40
54
begin
41
55
@mutex . lock
42
- post = if @being_executed
43
- @stash << job
44
- false
45
- else
46
- @being_executed = true
47
- end
56
+ job_to_post = if @being_executed
57
+ @stash . push ( *jobs )
58
+ nil
59
+ else
60
+ @being_executed = true
61
+ @stash . push ( *jobs [ 1 ..-1 ] )
62
+ jobs . first
63
+ end
48
64
ensure
49
65
@mutex . unlock
50
66
end
51
67
52
- call_job job if post
68
+ call_job job_to_post if job_to_post
53
69
true
54
70
end
55
71
@@ -98,7 +114,7 @@ class SerializedExecutionDelegator < SimpleDelegator
98
114
include SerialExecutor
99
115
100
116
def initialize ( executor )
101
- @executor = executor
117
+ @executor = executor
102
118
@serializer = SerializedExecution . new
103
119
super ( executor )
104
120
end
0 commit comments