Skip to content

Commit a14278a

Browse files
committed
Move #post method to different Shortcut module
1 parent 42d62af commit a14278a

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

doc/future-promise.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Futures and Promises
2+
3+
New implementation added in version 0.8 differs from previous versions and has little in common.
4+
{Future} represents a value which will become {#completed?} in future, it'll contain {#value} if {#success?} or a {#reason} if {#failed?}. It cannot be directly completed, there are implementations of abstract {Promise} class for that, so {Promise}'s only purpose is to complete a given {Future} object. They are always constructed as a Pair even in chaining methods like {#then}, {#rescue}, {#then_delay}, etc.
5+
6+
There is few {Promise} implementations:
7+
8+
- OuterPromise - only Promise used by users, can be completed by outer code. Constructed with {Concurrent::Next.promise} helper method.
9+
- Immediate - internal implementation of Promise used to represent immediate evaluation of a block. Constructed with {Concurrent::Next.future} helper method.
10+
- Delay - internal implementation of Promise used to represent delayed evaluation of a block. Constructed with {Concurrent::Next.delay} helper method.
11+
- ConnectedPromise - used internally to support {Future#with_default_executor}
12+

lib/concurrent/next.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
# TODO Dereferencable
44
# TODO document new global pool setting: no overflow, user has to buffer when there is too many tasks
5+
# TODO behaviour with Interrupt exceptions is undefined, use Signal.trap to avoid issues
56

6-
# different name just not to collide
7+
# @note different name just not to collide for now
78
module ConcurrentNext
89

910
# executors do not allocate the threads immediately so they can be constants
@@ -42,9 +43,17 @@ def executor(which)
4243
raise TypeError
4344
end
4445
end
46+
47+
module Shortcuts
48+
def post(executor = :fast, &job)
49+
ConcurrentNext.executor(executor).post &job
50+
end
51+
end
4552
end
4653

4754
extend Executors
55+
extend Executors::Shortcuts
56+
include Executors::Shortcuts
4857

4958
begin
5059
require 'jruby'
@@ -91,7 +100,7 @@ def synchronize
91100
end
92101

93102
def wait(timeout)
94-
@condition.wait @mutex, timeout
103+
synchronize { @condition.wait @mutex, timeout }
95104
end
96105

97106
def notify
@@ -118,10 +127,6 @@ class SynchronizedObject < RubySynchronizedObject
118127

119128
class Future < SynchronizedObject
120129
module Shortcuts
121-
def post(executor = :fast, &job)
122-
ConcurrentNext.executor(executor).post &job
123-
self
124-
end
125130

126131
# @return [Future]
127132
def future(executor = :fast, &block)
@@ -203,7 +208,6 @@ def value(timeout = nil)
203208
def wait(timeout = nil)
204209
synchronize do
205210
touch
206-
# TODO interruptions ?
207211
super timeout if incomplete?
208212
self
209213
end

0 commit comments

Comments
 (0)