senotrusov-ruby-threading-toolkit 1.0.2 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
@@ -22,6 +22,7 @@ require "ruby-toolkit"
|
|
22
22
|
require 'ruby-threading-toolkit/condition_variable.rb'
|
23
23
|
require 'ruby-threading-toolkit/mutexed_accessors.rb'
|
24
24
|
require 'ruby-threading-toolkit/smart_queue.rb'
|
25
|
+
require 'ruby-threading-toolkit/persistent_queue.rb'
|
25
26
|
require 'ruby-threading-toolkit/thread.rb'
|
26
27
|
require 'ruby-threading-toolkit/thread_lock.rb'
|
27
28
|
require 'ruby-threading-toolkit/threadsafe_sequence.rb'
|
@@ -0,0 +1,42 @@
|
|
1
|
+
|
2
|
+
# Copyright 2006-2009 Stanislav Senotrusov <senotrusov@gmail.com>
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
|
16
|
+
|
17
|
+
class PersistentQueue < SmartQueue
|
18
|
+
attr_accessor :storage
|
19
|
+
|
20
|
+
def restore
|
21
|
+
@mutex.synchronize do
|
22
|
+
if @storage.exist?
|
23
|
+
data = Marshal.restore(@storage.read)
|
24
|
+
@storage.delete
|
25
|
+
|
26
|
+
@regular = data[:regular]
|
27
|
+
@priority = data[:priority]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def store_and_close
|
33
|
+
@mutex.synchronize do
|
34
|
+
@storage.write Marshal.dump({:regular => @regular, :priority => @priority})
|
35
|
+
|
36
|
+
# This will simply raise exception on trying to push/pop from/to queue
|
37
|
+
@regular = nil
|
38
|
+
@priority = nil
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
@@ -15,29 +15,20 @@
|
|
15
15
|
|
16
16
|
|
17
17
|
class SmartQueue
|
18
|
-
def initialize
|
18
|
+
def initialize
|
19
19
|
@mutex = Mutex.new
|
20
20
|
|
21
21
|
@arrived = ConditionVariable.new
|
22
22
|
@shifted = ConditionVariable.new
|
23
23
|
|
24
|
-
@
|
25
|
-
|
26
|
-
if dumped && (dumped = Marshal.restore(dumped))
|
27
|
-
@regular = dumped[:regular]
|
28
|
-
@priority = dumped[:priority]
|
29
|
-
else
|
30
|
-
@regular = []
|
31
|
-
@priority = []
|
32
|
-
end
|
24
|
+
@regular = []
|
25
|
+
@priority = []
|
33
26
|
|
34
27
|
@regular_limit = nil
|
35
28
|
|
36
29
|
@stop_list = {}
|
37
30
|
end
|
38
31
|
|
39
|
-
attr_accessor :persistent
|
40
|
-
|
41
32
|
def release_blocked thread
|
42
33
|
@mutex.synchronize do
|
43
34
|
# Cleaning up @stop_list right here, so, at maximum, it holds only one dead thread.
|
@@ -50,22 +41,13 @@ class SmartQueue
|
|
50
41
|
end
|
51
42
|
end
|
52
43
|
|
53
|
-
def to_storage
|
54
|
-
@mutex.synchronize do
|
55
|
-
dump = Marshal.dump({:regular => @regular, :priority => @priority})
|
56
|
-
@regular.clear
|
57
|
-
@priority.clear
|
58
|
-
return dump
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
44
|
def regular_limit= limit
|
63
45
|
@mutex.synchronize do
|
64
46
|
@regular_limit = limit
|
65
47
|
end
|
66
48
|
end
|
67
49
|
|
68
|
-
def
|
50
|
+
def regular_enqueue action, data
|
69
51
|
@mutex.synchronize do
|
70
52
|
@shifted.if_wait_while(@mutex) { @regular_limit && @regular.length >= @regular_limit && !@stop_list.include?(Thread.current) }
|
71
53
|
|
@@ -73,20 +55,36 @@ class SmartQueue
|
|
73
55
|
@stop_list.delete(Thread.current)
|
74
56
|
end
|
75
57
|
|
76
|
-
@regular.
|
58
|
+
@regular.__send__(action, data)
|
77
59
|
@arrived.signal
|
78
60
|
end
|
79
61
|
true
|
80
62
|
end
|
81
63
|
|
82
|
-
def
|
64
|
+
def push message
|
65
|
+
regular_enqueue :push, message
|
66
|
+
end
|
67
|
+
|
68
|
+
def concat messages
|
69
|
+
regular_enqueue :concat, messages
|
70
|
+
end
|
71
|
+
|
72
|
+
def priority_enqueue action, data
|
83
73
|
@mutex.synchronize do
|
84
|
-
@priority.
|
74
|
+
@priority.__send__(action, data)
|
85
75
|
@arrived.signal
|
86
76
|
end
|
87
77
|
true
|
88
78
|
end
|
89
79
|
|
80
|
+
def priority_push message
|
81
|
+
priority_enqueue :push, message
|
82
|
+
end
|
83
|
+
|
84
|
+
def priority_concat messages
|
85
|
+
priority_enqueue :concat, messages
|
86
|
+
end
|
87
|
+
|
90
88
|
def shift
|
91
89
|
@mutex.synchronize do
|
92
90
|
@arrived.if_wait_while(@mutex) { @priority.empty? && @regular.empty? && !@stop_list.include?(Thread.current) }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: senotrusov-ruby-threading-toolkit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stanislav Senotrusov
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-08-09 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -46,6 +46,7 @@ files:
|
|
46
46
|
- LICENSE
|
47
47
|
- lib/ruby-threading-toolkit
|
48
48
|
- lib/ruby-threading-toolkit/thread_lock.rb
|
49
|
+
- lib/ruby-threading-toolkit/persistent_queue.rb
|
49
50
|
- lib/ruby-threading-toolkit/threadsafe_sequence_loop.rb
|
50
51
|
- lib/ruby-threading-toolkit/condition_variable.rb
|
51
52
|
- lib/ruby-threading-toolkit/smart_queue.rb
|