rbgo 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 97e2bc03c10cecfd43a53f1b5a322bb834ba96585db1d604efc7fb13ac3f3728
4
- data.tar.gz: a32cbd76dca6ade5900239d6168034e9678193eac871f98fc4a0d28a5bc025c3
3
+ metadata.gz: 83acff2fb4a9fbfa2d789ae9cc43db9b8e08fa6ad9e09c47822549945f0324d8
4
+ data.tar.gz: fc88b7550a6af76531668b20b7e7287d2db4ce73436bea75acb7ee05ddcecd02
5
5
  SHA512:
6
- metadata.gz: ace96944fbbd9437ade60d4540eb1fdc4b277445eaff47fa11d5176202ae986f6f58b0e5c15c3e4cfd3e40d92c01e074b03d54a6e94d39249a4332bb7a962cfe
7
- data.tar.gz: 578f17172ef9fddaf97244b1cb5e49b22f1ac0e4730e7c1c02753fde9d356b4038809eb3bf12dfd11cc3e82505543dda6c3c8e81ff656807bfdd56eafab6a163
6
+ metadata.gz: ac2d5d12d5ba6832f715c2a1df977c9ef435b94ee1f4d5a4259279f1dd8d871ded805ed5a70698fd8c186065355213db9bb003963bfde908a93caf01036b2652
7
+ data.tar.gz: cda8846388ff6c7853490765111f9e49ac80e73c028ed44ec8ed72e93dd538b476f5888d2f649d07740470e42b72308904f42948c052ed77ea49e9a00e9d0491
data/README.md CHANGED
@@ -29,7 +29,46 @@ select_chan(
29
29
  on_write(chan: ch3, obj: 'world'){
30
30
  #do when write success
31
31
  }
32
- ){ puts 'call default block' }
32
+ ){ puts 'call default block' }
33
+
34
+
35
+ timeout_ch = Chan.after(4)
36
+ job_ch1 = Chan.perform do
37
+ #do some job
38
+ sleep
39
+ end
40
+
41
+ job_ch2 = Chan.perform(timeout: 3) do
42
+ #do some job
43
+ sleep
44
+ end
45
+
46
+
47
+ select_chan(
48
+ on_read(chan: job_ch1){
49
+ p :job_ch1
50
+ },
51
+ on_read(chan: job_ch2){
52
+ p :job_ch2
53
+ },
54
+ on_read(chan: timeout_ch){
55
+ p 'timeout'
56
+ }
57
+ )
58
+
59
+ # Chan is Enumerable
60
+
61
+ ch = Chan.new
62
+
63
+ go do
64
+ 10.times do
65
+ ch << Time.now
66
+ end
67
+ end
68
+
69
+ ch.each do|obj|
70
+ p obj
71
+ end
33
72
  ```
34
73
  # go
35
74
  create lightweight routine like golang
@@ -3,8 +3,25 @@ require 'thread'
3
3
  module Rbgo
4
4
  module Channel
5
5
  module Chan
6
+ include Enumerable
7
+
8
+ def each
9
+ if block_given?
10
+ loop do
11
+ obj, ok = pop
12
+ if ok
13
+ yield obj
14
+ else
15
+ break
16
+ end
17
+ end
18
+ else
19
+ enum_for(:each)
20
+ end
21
+ end
6
22
 
7
23
  def self.new(max = 0)
24
+ max = max.to_i
8
25
  if max <= 0
9
26
  NonBufferChan.new
10
27
  else
@@ -12,6 +29,45 @@ module Rbgo
12
29
  end
13
30
  end
14
31
 
32
+ def self.after(seconds)
33
+ ch = new
34
+ CoRun::Routine.new(new_thread: true, queue_tag: :none) do
35
+ sleep seconds
36
+ ch << Time.now rescue nil
37
+ ch.close
38
+ end
39
+ ch
40
+ end
41
+
42
+ def self.tick(every_seconds)
43
+ ch = new
44
+ CoRun::Routine.new(new_thread: true, queue_tag: :none) do
45
+ loop do
46
+ sleep every_seconds
47
+ begin
48
+ ch.enq(Time.now, true)
49
+ rescue ThreadError
50
+ end
51
+ end
52
+ end
53
+ ch
54
+ end
55
+
56
+ def self.perform(timeout: nil, &blk)
57
+ ch = new
58
+ CoRun::Routine.new(new_thread: false, queue_tag: :default) do
59
+ begin
60
+ Timeout::timeout(timeout) do
61
+ blk.call
62
+ end
63
+ rescue Timeout::Error
64
+ ensure
65
+ ch.close
66
+ end
67
+ end
68
+ ch
69
+ end
70
+
15
71
  private
16
72
 
17
73
  attr_accessor :ios
@@ -183,38 +239,37 @@ module Rbgo
183
239
  #
184
240
  #
185
241
  class BufferChan < SizedQueue
242
+ alias_method :queue_max, :max
243
+ alias_method :queue_max=, :max=
186
244
  include Chan
187
- include Enumerable
188
-
189
- def each
190
- if block_given?
191
- loop do
192
- begin
193
- yield pop(true)
194
- rescue ThreadError
195
- return
196
- end
197
- end
198
- else
199
- enum_for(:each)
200
- end
201
- end
202
-
245
+
203
246
  def initialize(max)
204
247
  super(max)
205
- @mutex = Mutex.new
206
-
248
+ @mutex = Mutex.new
249
+ @cond = ConditionVariable.new
207
250
  self.ios = []
208
251
  self.register_mutex = Mutex.new
209
252
  end
210
253
 
211
254
  def push(obj, nonblock = false)
212
- super(obj, nonblock)
213
- notify
214
- self
215
- rescue ThreadError
216
- raise ClosedQueueError.new if closed?
217
- raise
255
+ @mutex.synchronize do
256
+ begin
257
+ if nonblock
258
+ super(obj, true)
259
+ else
260
+ while length == queue_max && !closed?
261
+ @cond.wait(@mutex)
262
+ end
263
+ super(obj, false)
264
+ end
265
+ notify
266
+ @cond.broadcast
267
+ self
268
+ rescue ThreadError
269
+ raise ClosedQueueError.new if closed?
270
+ raise
271
+ end
272
+ end
218
273
  end
219
274
 
220
275
  def pop(nonblock = false)
@@ -223,8 +278,17 @@ module Rbgo
223
278
  ok = true
224
279
  ok = false if empty? && closed?
225
280
  begin
226
- res = super(nonblock)
281
+ if nonblock
282
+ res = super(true)
283
+ else
284
+ while empty? && !closed?
285
+ @cond.wait(@mutex)
286
+ end
287
+ ok = false if closed?
288
+ res = super(false)
289
+ end
227
290
  notify
291
+ @cond.broadcast
228
292
  rescue ThreadError
229
293
  raise unless closed?
230
294
  ok = false
@@ -243,6 +307,7 @@ module Rbgo
243
307
  @mutex.synchronize do
244
308
  super
245
309
  notify
310
+ @cond.broadcast
246
311
  self
247
312
  end
248
313
  end
data/lib/rbgo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rbgo
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbgo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wang Yin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-26 00:00:00.000000000 Z
11
+ date: 2019-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: system