asynchronous 3.0.1 → 4.0.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/Gemfile.lock +1 -13
- data/README.md +36 -87
- data/VERSION +1 -1
- data/asynchronous.gemspec +0 -4
- data/examples/async_patterns.rb +17 -14
- data/examples/bootstrap.rb +2 -0
- data/examples/no_zombie_test.rb +5 -2
- data/examples/parallelism.rb +35 -0
- data/lib/async.rb +1 -0
- data/lib/asynchronous.rb +4 -9
- data/lib/asynchronous/concurrency.rb +33 -67
- data/lib/asynchronous/error.rb +8 -0
- data/lib/asynchronous/kernel.rb +18 -17
- data/lib/asynchronous/parallelism.rb +102 -106
- metadata +12 -45
- data/dump/async.rb +0 -4
- data/dump/shared_memory.rb +0 -138
- data/examples/array_of_value_with_native_threads.rb +0 -23
- data/examples/ruby_worker.rb +0 -54
- data/examples/shared_memory1.rb +0 -19
- data/examples/shared_memory2.rb +0 -29
- data/lib/asynchronous/clean_class.rb +0 -14
- data/lib/asynchronous/shared_memory.rb +0 -214
@@ -1,23 +0,0 @@
|
|
1
|
-
require_relative "../lib/asynchronous"
|
2
|
-
|
3
|
-
|
4
|
-
async1= async :OS do
|
5
|
-
1000000*5
|
6
|
-
end
|
7
|
-
|
8
|
-
async2= async :OS do
|
9
|
-
|
10
|
-
var = ("sup" * 1000000)
|
11
|
-
puts "the superHuge String length in the pipe is: #{var.length}"
|
12
|
-
|
13
|
-
var
|
14
|
-
end
|
15
|
-
|
16
|
-
|
17
|
-
async3= async :OS do
|
18
|
-
1000000*5.0
|
19
|
-
end
|
20
|
-
|
21
|
-
puts async1
|
22
|
-
|
23
|
-
puts [ async3, async2[0..5], async1 ]
|
data/examples/ruby_worker.rb
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
require_relative "../lib/asynchronous"
|
2
|
-
|
3
|
-
shared_memory.ruby_worker_jobs= Array.new
|
4
|
-
shared_memory.static_variables.push :ruby_worker_jobs
|
5
|
-
|
6
|
-
class Worker
|
7
|
-
|
8
|
-
def self.do_some_hard_work! *args
|
9
|
-
|
10
|
-
puts "heavy db load job here with params #{args.inspect}"
|
11
|
-
#some brutal DB work here
|
12
|
-
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.add_job *array
|
16
|
-
shared_memory.ruby_worker_jobs.push array
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
|
22
|
-
async :OS do
|
23
|
-
|
24
|
-
loop do
|
25
|
-
|
26
|
-
unless shared_memory.ruby_worker_jobs.empty?
|
27
|
-
|
28
|
-
puts "job to do again..."
|
29
|
-
job= shared_memory.ruby_worker_jobs.pop
|
30
|
-
|
31
|
-
model_name = job.shift
|
32
|
-
method_name = job.shift
|
33
|
-
|
34
|
-
model_name.__send__(method_name,*job)
|
35
|
-
|
36
|
-
puts "everything is done, so... nothing to do here... *sigh*"
|
37
|
-
|
38
|
-
else
|
39
|
-
sleep 0.1
|
40
|
-
end
|
41
|
-
|
42
|
-
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
loop do
|
50
|
-
|
51
|
-
sleep 6
|
52
|
-
Worker.add_job Worker,:do_some_hard_work!,{random: Random.srand}
|
53
|
-
|
54
|
-
end
|
data/examples/shared_memory1.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
require_relative "../lib/asynchronous"
|
2
|
-
|
3
|
-
SharedMemory.test_value= 0
|
4
|
-
async :OS do
|
5
|
-
|
6
|
-
loop do
|
7
|
-
SharedMemory.test_value += 1
|
8
|
-
sleep 3
|
9
|
-
end
|
10
|
-
|
11
|
-
end
|
12
|
-
|
13
|
-
loop do
|
14
|
-
if shared_memory.test_value >= 10
|
15
|
-
Process.exit
|
16
|
-
end
|
17
|
-
puts SharedMemory.test_value
|
18
|
-
sleep 1
|
19
|
-
end
|
data/examples/shared_memory2.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
require_relative "../lib/asynchronous"
|
2
|
-
|
3
|
-
shared_memory.test_value = Array.new
|
4
|
-
shared_memory.ready_state = Hash.new
|
5
|
-
times_value= 5
|
6
|
-
|
7
|
-
times_value.times do
|
8
|
-
|
9
|
-
# remember! IO pipes cant be made too fast!
|
10
|
-
# this does not mean the Shared memory cant handle the speed
|
11
|
-
|
12
|
-
var= async :OS do
|
13
|
-
|
14
|
-
shared_memory.test_value.push $$
|
15
|
-
shared_memory.ready_state[$$]= true
|
16
|
-
|
17
|
-
nil
|
18
|
-
end
|
19
|
-
shared_memory.ready_state[var.asynchronous_get_pid] ||= false
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
|
24
|
-
while shared_memory.ready_state.values.include?(false)
|
25
|
-
sleep 0.5
|
26
|
-
end
|
27
|
-
|
28
|
-
puts shared_memory.test_value.inspect
|
29
|
-
puts "#{times_value} OS thread should made this much shared memory update: #{times_value} / and it's #{shared_memory.test_value.count}"
|
@@ -1,214 +0,0 @@
|
|
1
|
-
module Asynchronous
|
2
|
-
|
3
|
-
class << self
|
4
|
-
attr_accessor :global_mutex
|
5
|
-
end
|
6
|
-
self.global_mutex = false
|
7
|
-
|
8
|
-
class << self
|
9
|
-
attr_accessor :memory_allocation_size
|
10
|
-
end
|
11
|
-
self.memory_allocation_size= 16384
|
12
|
-
|
13
|
-
module Global
|
14
|
-
|
15
|
-
def self.mutex
|
16
|
-
@@mutex
|
17
|
-
end
|
18
|
-
|
19
|
-
@@mutex = ProcessShared::Mutex.new
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
|
24
|
-
class MemoryObj < Asynchronous::CleanClass
|
25
|
-
|
26
|
-
@data = nil
|
27
|
-
@mutex = nil
|
28
|
-
|
29
|
-
def initialize(obj)
|
30
|
-
|
31
|
-
@data= ::ProcessShared::SharedMemory.new(
|
32
|
-
::Asynchronous.memory_allocation_size)
|
33
|
-
|
34
|
-
@mutex= ::ProcessShared::Mutex.new
|
35
|
-
|
36
|
-
@data.write_object(obj)
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
def asynchronous_set_value obj
|
41
|
-
return @data.write_object obj
|
42
|
-
end
|
43
|
-
alias :asynchronous_set_value= :asynchronous_set_value
|
44
|
-
|
45
|
-
def asynchronous_get_value
|
46
|
-
return @data.read_object
|
47
|
-
end
|
48
|
-
|
49
|
-
def method_missing(method, *args, &block)
|
50
|
-
|
51
|
-
#::Kernel.puts "method: #{method}, #{args}, #{block}"
|
52
|
-
|
53
|
-
new_value= nil
|
54
|
-
original_value= nil
|
55
|
-
return_value= nil
|
56
|
-
mutex_obj= nil
|
57
|
-
|
58
|
-
if ::Asynchronous.global_mutex == true
|
59
|
-
mutex_obj= ::Asynchronous::Global.mutex
|
60
|
-
else
|
61
|
-
mutex_obj= @mutex
|
62
|
-
end
|
63
|
-
|
64
|
-
mutex_obj.synchronize do
|
65
|
-
|
66
|
-
new_value= asynchronous_get_value
|
67
|
-
|
68
|
-
begin
|
69
|
-
original_value= new_value.dup
|
70
|
-
rescue ::TypeError
|
71
|
-
original_value= new_value
|
72
|
-
end
|
73
|
-
|
74
|
-
return_value= new_value.__send__(method,*args,&block)
|
75
|
-
|
76
|
-
unless new_value == original_value
|
77
|
-
asynchronous_set_value new_value
|
78
|
-
end
|
79
|
-
|
80
|
-
end
|
81
|
-
|
82
|
-
return return_value
|
83
|
-
|
84
|
-
end
|
85
|
-
|
86
|
-
end
|
87
|
-
|
88
|
-
class SharedMemory < Asynchronous::CleanClass
|
89
|
-
class << self
|
90
|
-
def method_missing(method, *args)
|
91
|
-
|
92
|
-
case true
|
93
|
-
|
94
|
-
when method.to_s.include?('=')
|
95
|
-
|
96
|
-
@@static_variables ||= ::Asynchronous::MemoryObj.new(Array.new.push(:static_variables))
|
97
|
-
if @@static_variables.include?(method.to_s.sub!('=','').to_sym)
|
98
|
-
$stdout.puts "Warning! static varieble cant be changed without removing from the Asynchronous.static_variables array collection (sym)"
|
99
|
-
return nil
|
100
|
-
else
|
101
|
-
|
102
|
-
begin
|
103
|
-
self.class_variable_get("@@#{method.to_s.sub('=','')}").asynchronous_set_value= args[0]
|
104
|
-
rescue ::NameError
|
105
|
-
self.class_variable_set(
|
106
|
-
"@@#{method.to_s.sub('=','')}",
|
107
|
-
::Asynchronous::MemoryObj.new(args[0]))
|
108
|
-
end
|
109
|
-
|
110
|
-
end
|
111
|
-
|
112
|
-
else
|
113
|
-
begin
|
114
|
-
self.class_variable_get("@@#{method.to_s}")
|
115
|
-
rescue ::NameError
|
116
|
-
return nil
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
end
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
def self.static_variables
|
125
|
-
SharedMemory.static_variables
|
126
|
-
end
|
127
|
-
|
128
|
-
end
|
129
|
-
|
130
|
-
SharedMemory ||= Asynchronous::SharedMemory
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
=begin
|
135
|
-
|
136
|
-
|
137
|
-
# The C void type; only useful for function return types
|
138
|
-
:void => Type::VOID,
|
139
|
-
|
140
|
-
# C boolean type
|
141
|
-
:bool => Type::BOOL,
|
142
|
-
|
143
|
-
# C nul-terminated string
|
144
|
-
:string => Type::STRING,
|
145
|
-
|
146
|
-
# C signed char
|
147
|
-
:char => Type::CHAR,
|
148
|
-
# C unsigned char
|
149
|
-
:uchar => Type::UCHAR,
|
150
|
-
|
151
|
-
# C signed short
|
152
|
-
:short => Type::SHORT,
|
153
|
-
# C unsigned short
|
154
|
-
:ushort => Type::USHORT,
|
155
|
-
|
156
|
-
# C signed int
|
157
|
-
:int => Type::INT,
|
158
|
-
# C unsigned int
|
159
|
-
:uint => Type::UINT,
|
160
|
-
|
161
|
-
# C signed long
|
162
|
-
:long => Type::LONG,
|
163
|
-
|
164
|
-
# C unsigned long
|
165
|
-
:ulong => Type::ULONG,
|
166
|
-
|
167
|
-
# C signed long long integer
|
168
|
-
:long_long => Type::LONG_LONG,
|
169
|
-
|
170
|
-
# C unsigned long long integer
|
171
|
-
:ulong_long => Type::ULONG_LONG,
|
172
|
-
|
173
|
-
# C single precision float
|
174
|
-
:float => Type::FLOAT,
|
175
|
-
|
176
|
-
# C double precision float
|
177
|
-
:double => Type::DOUBLE,
|
178
|
-
|
179
|
-
# C long double
|
180
|
-
:long_double => Type::LONGDOUBLE,
|
181
|
-
|
182
|
-
# Native memory address
|
183
|
-
:pointer => Type::POINTER,
|
184
|
-
|
185
|
-
# 8 bit signed integer
|
186
|
-
:int8 => Type::INT8,
|
187
|
-
# 8 bit unsigned integer
|
188
|
-
:uint8 => Type::UINT8,
|
189
|
-
|
190
|
-
# 16 bit signed integer
|
191
|
-
:int16 => Type::INT16,
|
192
|
-
# 16 bit unsigned integer
|
193
|
-
:uint16 => Type::UINT16,
|
194
|
-
|
195
|
-
# 32 bit signed integer
|
196
|
-
:int32 => Type::INT32,
|
197
|
-
# 32 bit unsigned integer
|
198
|
-
:uint32 => Type::UINT32,
|
199
|
-
|
200
|
-
# 64 bit signed integer
|
201
|
-
:int64 => Type::INT64,
|
202
|
-
# 64 bit unsigned integer
|
203
|
-
:uint64 => Type::UINT64,
|
204
|
-
|
205
|
-
:buffer_in => Type::BUFFER_IN,
|
206
|
-
:buffer_out => Type::BUFFER_OUT,
|
207
|
-
:buffer_inout => Type::BUFFER_INOUT,
|
208
|
-
|
209
|
-
# Used in function prototypes to indicate the arguments are variadic
|
210
|
-
:varargs => Type::VARARGS,
|
211
|
-
|
212
|
-
|
213
|
-
=end
|
214
|
-
|