Skip to content

Commit 6ec154d

Browse files
committed
Implemented module-level configuration.
* Implemented a module-level Configuration object * Removed the $GLOBAL_THREAD_POOL global variable * Added a global_thread_pool accessor to the module config * Renamed NullThreadPool to PerThreadExecutor * Moved PerThreadExecutor into its own file * Updated UsesGlobalThreadPool to use new module config * Updated all tests to use new module config * Updated all tests with new name of PerThreadExecutor
1 parent efb1a4c commit 6ec154d

14 files changed

+106
-87
lines changed

lib/concurrent.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'concurrent/version'
2+
require 'concurrent/configuration'
23

34
require 'concurrent/atomic'
45
require 'concurrent/count_down_latch'
@@ -30,10 +31,11 @@
3031
require 'concurrent/timer_task'
3132
require 'concurrent/utilities'
3233

33-
require 'concurrent/global_thread_pool'
3434
require 'concurrent/cached_thread_pool'
3535
require 'concurrent/fixed_thread_pool'
3636
require 'concurrent/immediate_executor'
37+
require 'concurrent/per_thread_executor'
38+
require 'concurrent/uses_global_thread_pool'
3739

3840
# Modern concurrency tools for Ruby. Inspired by Erlang, Clojure, Scala, Haskell,
3941
# F#, C#, Java, and classic concurrency patterns.

lib/concurrent/agent.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require 'observer'
33

44
require 'concurrent/dereferenceable'
5-
require 'concurrent/global_thread_pool'
5+
require 'concurrent/uses_global_thread_pool'
66
require 'concurrent/utilities'
77

88
module Concurrent

lib/concurrent/configuration.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'concurrent/per_thread_executor'
2+
3+
module Concurrent
4+
class << self
5+
attr_accessor :configuration
6+
end
7+
8+
def self.configure
9+
yield(configuration)
10+
end
11+
12+
class Configuration
13+
attr_accessor :global_thread_pool
14+
15+
def initialize
16+
@global_thread_pool = Concurrent::PerThreadExecutor.new
17+
end
18+
end
19+
20+
# create the default configuration on load
21+
self.configuration = Configuration.new
22+
end

lib/concurrent/future.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'thread'
22

33
require 'concurrent/obligation'
4-
require 'concurrent/global_thread_pool'
4+
require 'concurrent/uses_global_thread_pool'
55
require 'concurrent/safe_task_executor'
66

77
module Concurrent

lib/concurrent/global_thread_pool.rb

Lines changed: 0 additions & 42 deletions
This file was deleted.

lib/concurrent/per_thread_executor.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module Concurrent
2+
3+
class PerThreadExecutor
4+
5+
def self.post(*args)
6+
Thread.new(*args) do
7+
Thread.current.abort_on_exception = false
8+
yield(*args)
9+
end
10+
return true
11+
end
12+
13+
def post(*args, &block)
14+
return PerThreadExecutor.post(*args, &block)
15+
end
16+
17+
def <<(block)
18+
PerThreadExecutor.post(&block)
19+
return self
20+
end
21+
end
22+
end

lib/concurrent/promise.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'thread'
22

3-
require 'concurrent/global_thread_pool'
3+
require 'concurrent/uses_global_thread_pool'
44
require 'concurrent/obligation'
55

66
module Concurrent
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'concurrent/configuration'
2+
3+
module Concurrent
4+
5+
module UsesGlobalThreadPool
6+
7+
def self.included(base)
8+
class << base
9+
def thread_pool
10+
@thread_pool || Concurrent.configuration.global_thread_pool
11+
end
12+
def thread_pool=(pool)
13+
if pool == Concurrent.configuration.global_thread_pool
14+
@thread_pool = nil
15+
else
16+
@thread_pool = pool
17+
end
18+
end
19+
end
20+
end
21+
end
22+
end

spec/concurrent/agent_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module Concurrent
1818
end
1919

2020
before(:each) do
21-
Agent.thread_pool = NullThreadPool.new
21+
Agent.thread_pool = PerThreadExecutor.new
2222
end
2323

2424
context 'behavior' do

spec/concurrent/async_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Concurrent
55
describe Async do
66

77
before(:each) do
8-
Concurrent::Future.thread_pool = Concurrent::NullThreadPool.new
8+
Concurrent::Future.thread_pool = Concurrent::PerThreadExecutor.new
99
end
1010

1111
let(:async_class) do

0 commit comments

Comments
 (0)