process_shared 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,8 +4,9 @@ Concurrency primitives that may be used in a cross-process way to
4
4
  coordinate share memory between processes.
5
5
 
6
6
  A small C library (libpsem) is compiled to provide portable access to
7
- semaphores. This library is then accessed using FFI to implement Ruby
8
- classes ProcessShared::Semaphore, ProcessShared::BoundedSemaphore,
7
+ semaphores (based on http://pyprocessing.berlios.de/). This library
8
+ is then accessed using FFI to implement Ruby classes
9
+ ProcessShared::Semaphore, ProcessShared::BoundedSemaphore,
9
10
  ProcessShared::Mutex, and ProcessShared::SharedMemory.
10
11
 
11
12
  This is an incomplete work in progress.
@@ -3,7 +3,8 @@ require 'mkmf'
3
3
  $objs = []
4
4
 
5
5
  # posix semaphores
6
- if have_func('sem_open', 'semaphore.h')
6
+ if have_func('sem_open', 'semaphore.h') ||
7
+ ($libs << '-lpthread' && have_func('sem_open', 'semaphore.h'))
7
8
  have_func('floorf', 'math.h') or abort("Missing required floorf() in math.h")
8
9
  have_library('m', 'floorf')
9
10
 
@@ -12,12 +13,15 @@ if have_func('sem_open', 'semaphore.h')
12
13
  end
13
14
 
14
15
  have_library('rt', 'sem_open')
16
+ else
17
+ abort('Win32 or platform without sem_open not supported (yet?)')
15
18
  end
16
19
 
17
20
  c_sources = ['psem.c', 'psem_error.c', 'psem_posix.c', 'bsem.c', 'constants.c']
18
21
  $objs += ['psem.o', 'psem_error.o', 'bsem.o', 'constants.o']
19
22
 
20
- depend_rules <<-END
23
+ if respond_to? :depend_rules
24
+ depend_rules <<-END
21
25
  psem.c: psem.h psem_posix.c
22
26
  psem_error.c: psem_error.h
23
27
 
@@ -31,6 +35,6 @@ mempcpy.c: mempcpy.h
31
35
 
32
36
  libpsem.o: #{$objs.join(' ')}
33
37
  END
34
-
38
+ end
35
39
 
36
40
  create_makefile('libpsem')
@@ -5,6 +5,8 @@
5
5
  * Portable semaphore interface focusing on cross-process use.
6
6
  */
7
7
 
8
+ #include <stdlib.h> /* size_t */
9
+
8
10
  #ifdef HAVE_SEM_OPEN
9
11
  #include "psem_posix.h"
10
12
  #endif
@@ -1,6 +1,25 @@
1
1
  require 'ffi'
2
2
 
3
+ if VERSION =~ /^1.8/
4
+ require 'process_shared/define_singleton_method'
5
+
6
+ module ProcessShared
7
+ module PSem
8
+ extend DefineSingletonMethod
9
+ end
10
+
11
+ module RT
12
+ extend DefineSingletonMethod
13
+ end
14
+
15
+ module LibC
16
+ extend DefineSingletonMethod
17
+ end
18
+ end
19
+ end
20
+
3
21
  require 'process_shared/semaphore'
4
22
  require 'process_shared/bounded_semaphore'
5
23
  require 'process_shared/mutex'
6
24
  require 'process_shared/shared_memory'
25
+
@@ -0,0 +1,14 @@
1
+ module ProcessShared
2
+ module DefineSingletonMethod
3
+ # This method was added in Ruby 1.9.x. Include this module for
4
+ # backwards compatibility.
5
+ #
6
+ # This isn't exactly compatible with the method in 1.9 which can
7
+ # take a Proc, Method, or a block. This only accepts a block.
8
+ def define_singleton_method(sym, &block)
9
+ eigenclass = class << self; self; end
10
+ eigenclass.send(:define_method, sym, &block)
11
+ end
12
+ end
13
+ end
14
+
@@ -20,6 +20,10 @@ module ProcessShared
20
20
 
21
21
  # @return [Mutex]
22
22
  def lock
23
+ if locked_by == ::Process.pid
24
+ raise ProcessError, "already locked by this process #{::Process.pid}"
25
+ end
26
+
23
27
  @sem.wait
24
28
  self.locked_by = ::Process.pid
25
29
  self
@@ -37,7 +41,7 @@ module ProcessShared
37
41
  def sleep(timeout = nil)
38
42
  unlock
39
43
  begin
40
- timeout ? sleep(timeout) : sleep
44
+ timeout ? Kernel.sleep(timeout) : Kernel.sleep
41
45
  ensure
42
46
  lock
43
47
  end
@@ -62,6 +66,7 @@ module ProcessShared
62
66
  raise ProcessError, "lock is held by #{p} not #{::Process.pid}"
63
67
  end
64
68
 
69
+ self.locked_by = 0
65
70
  @sem.post
66
71
  self
67
72
  end
@@ -70,5 +70,13 @@ module ProcessShared
70
70
 
71
71
  ::Process.wait(pid)
72
72
  end
73
+
74
+ it 'raises exception when locked twice by same process' do
75
+ mutex = Mutex.new
76
+
77
+ mutex.lock
78
+ proc { mutex.lock }.must_raise(ProcessError)
79
+ mutex.unlock
80
+ end
73
81
  end
74
82
  end
@@ -1,8 +1,11 @@
1
+ require 'rubygems' if VERSION =~ /^1.8/
1
2
  gem 'minitest'
2
3
  require 'minitest/spec'
3
4
  require 'minitest/autorun'
4
5
  require 'minitest/matchers'
5
6
 
7
+ require 'process_shared'
8
+
6
9
  class RangeMatcher
7
10
  def initialize(operator, limit)
8
11
  @operator = operator.to_sym
metadata CHANGED
@@ -1,72 +1,91 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: process_shared
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.1
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Patrick Mahoney
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2011-12-12 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
17
+
18
+ date: 2011-12-13 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
15
21
  name: ffi
16
- requirement: &19463720 !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
17
24
  none: false
18
- requirements:
25
+ requirements:
19
26
  - - ~>
20
- - !ruby/object:Gem::Version
21
- version: '1.0'
27
+ - !ruby/object:Gem::Version
28
+ hash: 15
29
+ segments:
30
+ - 1
31
+ - 0
32
+ version: "1.0"
22
33
  type: :runtime
23
- prerelease: false
24
- version_requirements: *19463720
25
- - !ruby/object:Gem::Dependency
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
26
36
  name: rake-compiler
27
- requirement: &19462580 !ruby/object:Gem::Requirement
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
28
39
  none: false
29
- requirements:
30
- - - ! '>='
31
- - !ruby/object:Gem::Version
32
- version: '0'
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
33
47
  type: :development
34
- prerelease: false
35
- version_requirements: *19462580
36
- - !ruby/object:Gem::Dependency
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
37
50
  name: minitest
38
- requirement: &19461120 !ruby/object:Gem::Requirement
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
39
53
  none: false
40
- requirements:
41
- - - ! '>='
42
- - !ruby/object:Gem::Version
43
- version: '0'
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
44
61
  type: :development
45
- prerelease: false
46
- version_requirements: *19461120
47
- - !ruby/object:Gem::Dependency
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
48
64
  name: minitest-matchers
49
- requirement: &19459840 !ruby/object:Gem::Requirement
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
50
67
  none: false
51
- requirements:
52
- - - ! '>='
53
- - !ruby/object:Gem::Version
54
- version: '0'
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
55
75
  type: :development
56
- prerelease: false
57
- version_requirements: *19459840
58
- description: FFI wrapper around portable semaphore library with mutex and condition
59
- vars built on top.
76
+ version_requirements: *id004
77
+ description: FFI wrapper around portable semaphore library with mutex and condition vars built on top.
60
78
  email: pat@polycrystal.org
61
79
  executables: []
62
- extensions:
80
+
81
+ extensions:
63
82
  - ext/pthread_sync_helper/extconf.rb
64
83
  - ext/libpsem/extconf.rb
65
- extra_rdoc_files:
84
+ extra_rdoc_files:
66
85
  - README.rdoc
67
86
  - ChangeLog
68
87
  - COPYING
69
- files:
88
+ files:
70
89
  - lib/scratch.rb
71
90
  - lib/process_shared.rb
72
91
  - lib/process_shared/abstract_semaphore.rb
@@ -78,6 +97,7 @@ files:
78
97
  - lib/process_shared/rt.rb
79
98
  - lib/process_shared/psem.rb
80
99
  - lib/process_shared/with_self.rb
100
+ - lib/process_shared/define_singleton_method.rb
81
101
  - lib/process_shared/condition_variable.rb
82
102
  - lib/process_shared/bounded_semaphore.rb
83
103
  - lib/process_shared/libc.rb
@@ -114,26 +134,36 @@ files:
114
134
  - COPYING
115
135
  homepage: https://github.com/pmahoney/process_shared
116
136
  licenses: []
137
+
117
138
  post_install_message:
118
139
  rdoc_options: []
119
- require_paths:
140
+
141
+ require_paths:
120
142
  - lib
121
- required_ruby_version: !ruby/object:Gem::Requirement
143
+ required_ruby_version: !ruby/object:Gem::Requirement
122
144
  none: false
123
- requirements:
124
- - - ! '>='
125
- - !ruby/object:Gem::Version
126
- version: '0'
127
- required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ hash: 3
149
+ segments:
150
+ - 0
151
+ version: "0"
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
153
  none: false
129
- requirements:
130
- - - ! '>='
131
- - !ruby/object:Gem::Version
132
- version: '0'
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ hash: 3
158
+ segments:
159
+ - 0
160
+ version: "0"
133
161
  requirements: []
162
+
134
163
  rubyforge_project:
135
164
  rubygems_version: 1.8.10
136
165
  signing_key:
137
166
  specification_version: 3
138
167
  summary: process-shared synchronization primitives
139
168
  test_files: []
169
+