rubysl-sync 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2351a3ab15577c7df19f0e26645761adbec86dce
4
+ data.tar.gz: 6e955d443a1e77a08e2136906761d98afb8925c9
5
+ SHA512:
6
+ metadata.gz: 2aa0cb37b52d749dfce2f165aec61a5db5d60a3c8396262cf95400a48669729e2a7f6a7895a76c0a817360ea549df13c9e577f8b352d13dd1f9752cbceb2cbdc
7
+ data.tar.gz: 77fadf07d279c6d5ca22fd1f6f87229d881bdc169eec12f03c58bbe32ba2bb1492ace6812bf73af9880eb9db21bf05781c64d7b4e4f2a4d1bb029b9bb577de50
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ env:
3
+ - RUBYLIB=lib
4
+ script: bundle exec mspec
5
+ rvm:
6
+ - 1.8.7
7
+ - rbx-nightly-18mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rubysl-sync.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ Copyright (c) 2013, Brian Shirai
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ 3. Neither the name of the library nor the names of its contributors may be
13
+ used to endorse or promote products derived from this software without
14
+ specific prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT,
20
+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23
+ OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Rubysl::Sync
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rubysl-sync'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rubysl-sync
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,2 @@
1
+ require "rubysl/sync/sync"
2
+ require "rubysl/sync/version"
@@ -0,0 +1,312 @@
1
+ #
2
+ # sync.rb - 2 phase lock with counter
3
+ # $Release Version: 1.0$
4
+ # $Revision: 22457 $
5
+ # $Date: 2009-02-19 08:41:12 -0800 (Thu, 19 Feb 2009) $
6
+ # by Keiju ISHITSUKA(keiju@ishitsuka.com)
7
+ #
8
+ # --
9
+ # Sync_m, Synchronizer_m
10
+ # Usage:
11
+ # obj.extend(Sync_m)
12
+ # or
13
+ # class Foo
14
+ # include Sync_m
15
+ # :
16
+ # end
17
+ #
18
+ # Sync_m#sync_mode
19
+ # Sync_m#sync_locked?, locked?
20
+ # Sync_m#sync_shared?, shared?
21
+ # Sync_m#sync_exclusive?, sync_exclusive?
22
+ # Sync_m#sync_try_lock, try_lock
23
+ # Sync_m#sync_lock, lock
24
+ # Sync_m#sync_unlock, unlock
25
+ #
26
+ # Sync, Synchronicer:
27
+ # include Sync_m
28
+ # Usage:
29
+ # sync = Sync.new
30
+ #
31
+ # Sync#mode
32
+ # Sync#locked?
33
+ # Sync#shared?
34
+ # Sync#exclusive?
35
+ # Sync#try_lock(mode) -- mode = :EX, :SH, :UN
36
+ # Sync#lock(mode) -- mode = :EX, :SH, :UN
37
+ # Sync#unlock
38
+ # Sync#synchronize(mode) {...}
39
+ #
40
+ #
41
+
42
+ unless defined? Thread
43
+ fail "Thread not available for this ruby interpreter"
44
+ end
45
+
46
+ module Sync_m
47
+ RCS_ID='-$Header$-'
48
+
49
+ # lock mode
50
+ UN = :UN
51
+ SH = :SH
52
+ EX = :EX
53
+
54
+ # exceptions
55
+ class Err < StandardError
56
+ def Err.Fail(*opt)
57
+ Thread.critical = false
58
+ fail self, sprintf(self::Message, *opt)
59
+ end
60
+
61
+ class UnknownLocker < Err
62
+ Message = "Thread(%s) not locked."
63
+ def UnknownLocker.Fail(th)
64
+ super(th.inspect)
65
+ end
66
+ end
67
+
68
+ class LockModeFailer < Err
69
+ Message = "Unknown lock mode(%s)"
70
+ def LockModeFailer.Fail(mode)
71
+ if mode.id2name
72
+ mode = id2name
73
+ end
74
+ super(mode)
75
+ end
76
+ end
77
+ end
78
+
79
+ def Sync_m.define_aliases(cl)
80
+ cl.module_eval %q{
81
+ alias locked? sync_locked?
82
+ alias shared? sync_shared?
83
+ alias exclusive? sync_exclusive?
84
+ alias lock sync_lock
85
+ alias unlock sync_unlock
86
+ alias try_lock sync_try_lock
87
+ alias synchronize sync_synchronize
88
+ }
89
+ end
90
+
91
+ def Sync_m.append_features(cl)
92
+ super
93
+ unless cl.instance_of?(Module)
94
+ # do nothing for Modules
95
+ # make aliases and include the proper module.
96
+ define_aliases(cl)
97
+ end
98
+ end
99
+
100
+ def Sync_m.extend_object(obj)
101
+ super
102
+ obj.sync_extended
103
+ end
104
+
105
+ def sync_extended
106
+ unless (defined? locked? and
107
+ defined? shared? and
108
+ defined? exclusive? and
109
+ defined? lock and
110
+ defined? unlock and
111
+ defined? try_lock and
112
+ defined? synchronize)
113
+ Sync_m.define_aliases(class<<self;self;end)
114
+ end
115
+ sync_initialize
116
+ end
117
+
118
+ # accessing
119
+ def sync_locked?
120
+ sync_mode != UN
121
+ end
122
+
123
+ def sync_shared?
124
+ sync_mode == SH
125
+ end
126
+
127
+ def sync_exclusive?
128
+ sync_mode == EX
129
+ end
130
+
131
+ # locking methods.
132
+ def sync_try_lock(mode = EX)
133
+ return unlock if mode == UN
134
+
135
+ Thread.critical = true
136
+ ret = sync_try_lock_sub(mode)
137
+ Thread.critical = false
138
+ ret
139
+ end
140
+
141
+ def sync_lock(m = EX)
142
+ return unlock if m == UN
143
+
144
+ until (Thread.critical = true; sync_try_lock_sub(m))
145
+ if sync_sh_locker[Thread.current]
146
+ sync_upgrade_waiting.push [Thread.current, sync_sh_locker[Thread.current]]
147
+ sync_sh_locker.delete(Thread.current)
148
+ else
149
+ sync_waiting.push Thread.current
150
+ end
151
+ Thread.stop
152
+ end
153
+ Thread.critical = false
154
+ self
155
+ end
156
+
157
+ def sync_unlock(m = EX)
158
+ Thread.critical = true
159
+ if sync_mode == UN
160
+ Thread.critical = false
161
+ Err::UnknownLocker.Fail(Thread.current)
162
+ end
163
+
164
+ m = sync_mode if m == EX and sync_mode == SH
165
+
166
+ runnable = false
167
+ case m
168
+ when UN
169
+ Thread.critical = false
170
+ Err::UnknownLocker.Fail(Thread.current)
171
+
172
+ when EX
173
+ if sync_ex_locker == Thread.current
174
+ if (self.sync_ex_count = sync_ex_count - 1) == 0
175
+ self.sync_ex_locker = nil
176
+ if sync_sh_locker.include?(Thread.current)
177
+ self.sync_mode = SH
178
+ else
179
+ self.sync_mode = UN
180
+ end
181
+ runnable = true
182
+ end
183
+ else
184
+ Err::UnknownLocker.Fail(Thread.current)
185
+ end
186
+
187
+ when SH
188
+ if (count = sync_sh_locker[Thread.current]).nil?
189
+ Err::UnknownLocker.Fail(Thread.current)
190
+ else
191
+ if (sync_sh_locker[Thread.current] = count - 1) == 0
192
+ sync_sh_locker.delete(Thread.current)
193
+ if sync_sh_locker.empty? and sync_ex_count == 0
194
+ self.sync_mode = UN
195
+ runnable = true
196
+ end
197
+ end
198
+ end
199
+ end
200
+
201
+ if runnable
202
+ if sync_upgrade_waiting.size > 0
203
+ for k, v in sync_upgrade_waiting
204
+ sync_sh_locker[k] = v
205
+ end
206
+ wait = sync_upgrade_waiting
207
+ self.sync_upgrade_waiting = []
208
+ Thread.critical = false
209
+
210
+ for w, v in wait
211
+ w.run
212
+ end
213
+ else
214
+ wait = sync_waiting
215
+ self.sync_waiting = []
216
+ Thread.critical = false
217
+ for w in wait
218
+ w.run
219
+ end
220
+ end
221
+ end
222
+
223
+ Thread.critical = false
224
+ self
225
+ end
226
+
227
+ def sync_synchronize(mode = EX)
228
+ begin
229
+ sync_lock(mode)
230
+ yield
231
+ ensure
232
+ sync_unlock
233
+ end
234
+ end
235
+
236
+ attr :sync_mode, true
237
+
238
+ attr :sync_waiting, true
239
+ attr :sync_upgrade_waiting, true
240
+ attr :sync_sh_locker, true
241
+ attr :sync_ex_locker, true
242
+ attr :sync_ex_count, true
243
+
244
+ private
245
+
246
+ def sync_initialize
247
+ @sync_mode = UN
248
+ @sync_waiting = []
249
+ @sync_upgrade_waiting = []
250
+ @sync_sh_locker = Hash.new
251
+ @sync_ex_locker = nil
252
+ @sync_ex_count = 0
253
+ end
254
+
255
+ def initialize(*args)
256
+ sync_initialize
257
+ super
258
+ end
259
+
260
+ def sync_try_lock_sub(m)
261
+ case m
262
+ when SH
263
+ case sync_mode
264
+ when UN
265
+ self.sync_mode = m
266
+ sync_sh_locker[Thread.current] = 1
267
+ ret = true
268
+ when SH
269
+ count = 0 unless count = sync_sh_locker[Thread.current]
270
+ sync_sh_locker[Thread.current] = count + 1
271
+ ret = true
272
+ when EX
273
+ # in EX mode, lock will upgrade to EX lock
274
+ if sync_ex_locker == Thread.current
275
+ self.sync_ex_count = sync_ex_count + 1
276
+ ret = true
277
+ else
278
+ ret = false
279
+ end
280
+ end
281
+ when EX
282
+ if sync_mode == UN or
283
+ sync_mode == SH && sync_sh_locker.size == 1 && sync_sh_locker.include?(Thread.current)
284
+ self.sync_mode = m
285
+ self.sync_ex_locker = Thread.current
286
+ self.sync_ex_count = 1
287
+ ret = true
288
+ elsif sync_mode == EX && sync_ex_locker == Thread.current
289
+ self.sync_ex_count = sync_ex_count + 1
290
+ ret = true
291
+ else
292
+ ret = false
293
+ end
294
+ else
295
+ Thread.critical = false
296
+ Err::LockModeFailer.Fail mode
297
+ end
298
+ return ret
299
+ end
300
+ end
301
+ Synchronizer_m = Sync_m
302
+
303
+ class Sync
304
+ #Sync_m.extend_class self
305
+ include Sync_m
306
+
307
+ def initialize
308
+ super
309
+ end
310
+
311
+ end
312
+ Synchronizer = Sync
@@ -0,0 +1,5 @@
1
+ module RubySL
2
+ module Sync
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
data/lib/sync.rb ADDED
@@ -0,0 +1 @@
1
+ require "rubysl/sync"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ require './lib/rubysl/sync/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "rubysl-sync"
6
+ spec.version = RubySL::Sync::VERSION
7
+ spec.authors = ["Brian Shirai"]
8
+ spec.email = ["brixen@gmail.com"]
9
+ spec.description = %q{Ruby standard library sync.}
10
+ spec.summary = %q{Ruby standard library sync.}
11
+ spec.homepage = "https://github.com/rubysl/rubysl-sync"
12
+ spec.license = "BSD"
13
+
14
+ spec.files = `git ls-files`.split($/)
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.3"
20
+ spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_development_dependency "mspec", "~> 1.5"
22
+ spec.add_development_dependency "rubysl-prettyprint", "~> 1.0"
23
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubysl-sync
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Shirai
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubysl-prettyprint
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ description: Ruby standard library sync.
70
+ email:
71
+ - brixen@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .travis.yml
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - lib/rubysl/sync.rb
83
+ - lib/rubysl/sync/sync.rb
84
+ - lib/rubysl/sync/version.rb
85
+ - lib/sync.rb
86
+ - rubysl-sync.gemspec
87
+ homepage: https://github.com/rubysl/rubysl-sync
88
+ licenses:
89
+ - BSD
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.0.7
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Ruby standard library sync.
111
+ test_files: []
112
+ has_rdoc: