process_shared 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. data/ext/helper/extconf.rb +14 -0
  2. data/ext/helper/helper.c +70 -0
  3. data/lib/mach.rb +12 -0
  4. data/lib/mach/clock.rb +24 -0
  5. data/lib/mach/error.rb +56 -0
  6. data/lib/mach/functions.rb +342 -0
  7. data/lib/mach/host.rb +28 -0
  8. data/lib/mach/port.rb +143 -0
  9. data/lib/mach/semaphore.rb +74 -0
  10. data/lib/mach/task.rb +42 -0
  11. data/lib/mach/time_spec.rb +16 -0
  12. data/lib/process_shared.rb +17 -1
  13. data/lib/process_shared/binary_semaphore.rb +18 -7
  14. data/lib/process_shared/mach.rb +93 -0
  15. data/lib/process_shared/mach/semaphore.rb +39 -0
  16. data/lib/process_shared/posix/errno.rb +40 -0
  17. data/lib/process_shared/posix/libc.rb +78 -0
  18. data/lib/process_shared/posix/semaphore.rb +125 -0
  19. data/lib/process_shared/posix/shared_array.rb +71 -0
  20. data/lib/process_shared/posix/shared_memory.rb +82 -0
  21. data/lib/process_shared/posix/time_spec.rb +13 -0
  22. data/lib/process_shared/posix/time_val.rb +23 -0
  23. data/lib/process_shared/semaphore.rb +26 -73
  24. data/lib/process_shared/shared_array.rb +3 -1
  25. data/lib/process_shared/shared_memory.rb +10 -52
  26. data/lib/process_shared/time_spec.rb +22 -0
  27. data/spec/mach/port_spec.rb +21 -0
  28. data/spec/mach/scratch.rb +67 -0
  29. data/spec/mach/scratch2.rb +78 -0
  30. data/spec/mach/semaphore_spec.rb +60 -0
  31. data/spec/mach/task_spec.rb +31 -0
  32. data/spec/process_shared/scratch.rb +21 -0
  33. data/spec/process_shared/semaphore_spec.rb +12 -11
  34. data/spec/process_shared/shared_memory_spec.rb +1 -1
  35. metadata +46 -36
  36. data/ext/libpsem/bsem.c +0 -188
  37. data/ext/libpsem/bsem.h +0 -32
  38. data/ext/libpsem/constants.c +0 -22
  39. data/ext/libpsem/constants.h +0 -18
  40. data/ext/libpsem/extconf.rb +0 -40
  41. data/ext/libpsem/mempcpy.c +0 -7
  42. data/ext/libpsem/mempcpy.h +0 -13
  43. data/ext/libpsem/mutex.c +0 -15
  44. data/ext/libpsem/mutex.h +0 -14
  45. data/ext/libpsem/psem.c +0 -15
  46. data/ext/libpsem/psem.h +0 -45
  47. data/ext/libpsem/psem_error.c +0 -46
  48. data/ext/libpsem/psem_error.h +0 -11
  49. data/ext/libpsem/psem_posix.c +0 -160
  50. data/ext/libpsem/psem_posix.h +0 -10
  51. data/lib/process_shared/bounded_semaphore.rb +0 -46
  52. data/lib/process_shared/libc.rb +0 -36
  53. data/lib/process_shared/libpsem.bundle +0 -0
  54. data/lib/process_shared/libpsem.so +0 -0
  55. data/lib/process_shared/psem.rb +0 -113
  56. data/spec/process_shared/bounded_semaphore_spec.rb +0 -48
  57. data/spec/process_shared/libc_spec.rb +0 -9
  58. data/spec/process_shared/psem_spec.rb +0 -136
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ require 'mach'
3
+ require 'mach/port'
4
+ require 'mach/task'
5
+
6
+ module Mach
7
+ describe Task do
8
+ before :each do
9
+ @task = Task.self
10
+ end
11
+
12
+ it 'gets special ports' do
13
+ bp = @task.get_special_port(:bootstrap)
14
+ bp.must_equal @task.get_bootstrap_port
15
+ end
16
+
17
+ it 'redefines bootstrap port' do
18
+ bp = @task.get_bootstrap_port
19
+ new_bp = Port.new
20
+ bp.wont_equal(new_bp)
21
+
22
+ begin
23
+ new_bp.insert_right(:make_send)
24
+ @task.set_bootstrap_port(new_bp)
25
+ @task.get_bootstrap_port.must_equal(new_bp)
26
+ ensure
27
+ @task.set_bootstrap_port(bp)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+ require 'process_shared'
3
+
4
+ module ProcessShared
5
+ describe Semaphore2 do
6
+ it 'does stuff' do
7
+ sem = Semaphore2.new(0)
8
+ puts ProcessShared::Mach.shared_ports.inspect
9
+ fork do
10
+ puts "child: #{Process.pid}"
11
+ sleep 2
12
+ puts "child signaling sem"
13
+ sem.post
14
+ end
15
+
16
+ puts "parent waiting..."
17
+ sem.wait
18
+ puts "parent woke up"
19
+ end
20
+ end
21
+ end
@@ -58,16 +58,18 @@ module ProcessShared
58
58
  end
59
59
 
60
60
  describe '#post and #wait' do
61
- it 'increments and decrements the value' do
62
- Semaphore.open(0) do |sem|
63
- 10.times do |i|
64
- sem.post
65
- sem.value.must_equal(i + 1)
66
- end
61
+ unless FFI::Platform.mac?
62
+ it 'increments and decrements the value' do
63
+ Semaphore.open(0) do |sem|
64
+ 10.times do |i|
65
+ sem.post
66
+ sem.value.must_equal(i + 1)
67
+ end
67
68
 
68
- 10.times do |i|
69
- sem.wait
70
- sem.value.must_equal(10 - i - 1)
69
+ 10.times do |i|
70
+ sem.wait
71
+ sem.value.must_equal(10 - i - 1)
72
+ end
71
73
  end
72
74
  end
73
75
  end
@@ -98,14 +100,13 @@ module ProcessShared
98
100
 
99
101
  it 'returns after waiting if another processes posts' do
100
102
  Semaphore.open(0) do |sem|
101
- start = Time.now.to_f
102
-
103
103
  pid = fork do
104
104
  sleep 0.01
105
105
  sem.post
106
106
  Kernel.exit!
107
107
  end
108
108
 
109
+ start = Time.now.to_f
109
110
  sem.try_wait(0.1)
110
111
  (Time.now.to_f - start).must be_lt(0.1)
111
112
 
@@ -1,5 +1,5 @@
1
1
  require 'spec_helper'
2
- require 'process_shared/shared_memory'
2
+ require 'process_shared'
3
3
 
4
4
  module ProcessShared
5
5
  describe SharedMemory do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: process_shared
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-21 00:00:00.000000000Z
12
+ date: 2012-02-02 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ffi
16
- requirement: &13991240 !ruby/object:Gem::Requirement
16
+ requirement: &13298820 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '1.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *13991240
24
+ version_requirements: *13298820
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &13990820 !ruby/object:Gem::Requirement
27
+ requirement: &13297880 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *13990820
35
+ version_requirements: *13297880
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake-compiler
38
- requirement: &13990280 !ruby/object:Gem::Requirement
38
+ requirement: &13297020 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *13990280
46
+ version_requirements: *13297020
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: minitest
49
- requirement: &13989780 !ruby/object:Gem::Requirement
49
+ requirement: &13296040 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *13989780
57
+ version_requirements: *13296040
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: minitest-matchers
60
- requirement: &13989260 !ruby/object:Gem::Requirement
60
+ requirement: &13295120 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,67 +65,71 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *13989260
68
+ version_requirements: *13295120
69
69
  description: FFI wrapper around portable semaphore library with mutex and condition
70
70
  vars built on top.
71
71
  email: pat@polycrystal.org
72
72
  executables: []
73
73
  extensions:
74
74
  - ext/pthread_sync_helper/extconf.rb
75
- - ext/libpsem/extconf.rb
75
+ - ext/helper/extconf.rb
76
76
  extra_rdoc_files:
77
77
  - README.rdoc
78
78
  - ChangeLog
79
79
  - COPYING
80
80
  files:
81
+ - lib/mach.rb
81
82
  - lib/scratch.rb
82
83
  - lib/process_shared.rb
84
+ - lib/mach/host.rb
85
+ - lib/mach/clock.rb
86
+ - lib/mach/semaphore.rb
87
+ - lib/mach/functions.rb
88
+ - lib/mach/time_spec.rb
89
+ - lib/mach/error.rb
90
+ - lib/mach/port.rb
91
+ - lib/mach/task.rb
83
92
  - lib/process_shared/abstract_semaphore.rb
84
93
  - lib/process_shared/posix_call.rb
94
+ - lib/process_shared/mach.rb
85
95
  - lib/process_shared/process_error.rb
86
96
  - lib/process_shared/thread.rb
97
+ - lib/process_shared/posix/errno.rb
98
+ - lib/process_shared/posix/semaphore.rb
99
+ - lib/process_shared/posix/time_spec.rb
100
+ - lib/process_shared/posix/shared_array.rb
101
+ - lib/process_shared/posix/time_val.rb
102
+ - lib/process_shared/posix/libc.rb
103
+ - lib/process_shared/posix/shared_memory.rb
87
104
  - lib/process_shared/mutex.rb
88
105
  - lib/process_shared/shared_memory_io.rb
89
106
  - lib/process_shared/semaphore.rb
107
+ - lib/process_shared/time_spec.rb
90
108
  - lib/process_shared/rt.rb
109
+ - lib/process_shared/mach/semaphore.rb
91
110
  - lib/process_shared/shared_array.rb
92
111
  - lib/process_shared/binary_semaphore.rb
93
- - lib/process_shared/psem.rb
94
112
  - lib/process_shared/with_self.rb
95
113
  - lib/process_shared/define_singleton_method.rb
96
114
  - lib/process_shared/condition_variable.rb
97
- - lib/process_shared/bounded_semaphore.rb
98
- - lib/process_shared/libc.rb
99
115
  - lib/process_shared/shared_memory.rb
100
- - lib/process_shared/libpsem.bundle
101
- - lib/process_shared/libpsem.so
102
116
  - ext/pthread_sync_helper/pthread_sync_helper.c
103
117
  - ext/semaphore.c
104
- - ext/libpsem/mempcpy.c
105
- - ext/libpsem/psem_error.c
106
- - ext/libpsem/bsem.c
107
- - ext/libpsem/psem_posix.c
108
- - ext/libpsem/psem.c
109
- - ext/libpsem/mutex.c
110
- - ext/libpsem/constants.c
111
- - ext/libpsem/bsem.h
112
- - ext/libpsem/psem_error.h
113
- - ext/libpsem/mutex.h
114
- - ext/libpsem/constants.h
115
- - ext/libpsem/psem.h
116
- - ext/libpsem/psem_posix.h
117
- - ext/libpsem/mempcpy.h
118
+ - ext/helper/helper.c
118
119
  - ext/pthread_sync_helper/extconf.rb
119
- - ext/libpsem/extconf.rb
120
- - spec/process_shared/psem_spec.rb
120
+ - ext/helper/extconf.rb
121
+ - spec/mach/scratch2.rb
122
+ - spec/mach/task_spec.rb
123
+ - spec/mach/scratch.rb
124
+ - spec/mach/port_spec.rb
125
+ - spec/mach/semaphore_spec.rb
121
126
  - spec/process_shared/shared_memory_spec.rb
122
127
  - spec/process_shared/binary_semaphore_spec.rb
123
128
  - spec/process_shared/condition_variable_spec.rb
124
129
  - spec/process_shared/mutex_spec.rb
125
130
  - spec/process_shared/shared_array_spec.rb
126
- - spec/process_shared/bounded_semaphore_spec.rb
131
+ - spec/process_shared/scratch.rb
127
132
  - spec/process_shared/semaphore_spec.rb
128
- - spec/process_shared/libc_spec.rb
129
133
  - spec/spec_helper.rb
130
134
  - README.rdoc
131
135
  - ChangeLog
@@ -142,12 +146,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
142
146
  - - ! '>='
143
147
  - !ruby/object:Gem::Version
144
148
  version: '0'
149
+ segments:
150
+ - 0
151
+ hash: -4219144460365832731
145
152
  required_rubygems_version: !ruby/object:Gem::Requirement
146
153
  none: false
147
154
  requirements:
148
155
  - - ! '>='
149
156
  - !ruby/object:Gem::Version
150
157
  version: '0'
158
+ segments:
159
+ - 0
160
+ hash: -4219144460365832731
151
161
  requirements: []
152
162
  rubyforge_project:
153
163
  rubygems_version: 1.8.10
@@ -1,188 +0,0 @@
1
- /*
2
- * Extensions atop psem. Recursive mutex, bounded semaphore.
3
- */
4
-
5
- #include <stdlib.h> /* malloc, free */
6
-
7
- #include "mempcpy.h" /* includes string.h */
8
- #include "psem.h"
9
- #include "psem_error.h"
10
- #include "bsem.h"
11
-
12
- #define MAX_NAME 128 /* This is much less the POSIX max
13
- name. Users of this library must
14
- not use longer names. */
15
-
16
- static const char bsem_lock_suffix[] = "-bsem-lock";
17
-
18
- #define MAX_LOCK_NAME (MAX_NAME + strlen(bsem_lock_suffix) + 1)
19
-
20
- /**
21
- * Assumes dest has sufficient space to hold "[MAX_NAME]-bsem-lock".
22
- */
23
- static int
24
- make_lockname(char *dest, const char *name, error_t **err)
25
- {
26
- int namelen;
27
-
28
- namelen = strlen(name);
29
- if (namelen > MAX_NAME) {
30
- error_new(err, E_SOURCE_PSEM, E_NAME_TOO_LONG);
31
- return ERROR;
32
- }
33
-
34
- *((char *) mempcpy(mempcpy(dest, name, namelen),
35
- bsem_lock_suffix,
36
- strlen(bsem_lock_suffix))) = '\0';
37
- }
38
-
39
- size_t sizeof_bsem_t = sizeof (bsem_t);
40
-
41
- bsem_t *
42
- bsem_alloc(void) {
43
- return malloc(sizeof(bsem_t));
44
- }
45
-
46
- void
47
- bsem_free(bsem_t *bsem) {
48
- free(bsem);
49
- }
50
-
51
- #define call_or_return(exp) \
52
- do { if ((exp) == ERROR) { return ERROR; } } while (0)
53
-
54
- #define bsem_lock_or_return(bsem, err) call_or_return(bsem_lock((bsem), (err)))
55
-
56
- #define bsem_unlock_or_return(bsem, err) call_or_return(bsem_unlock((bsem), (err)))
57
-
58
- int
59
- bsem_open(bsem_t *bsem, const char *name, unsigned int maxvalue, unsigned int value, error_t **err)
60
- {
61
- char lockname[MAX_LOCK_NAME];
62
-
63
- call_or_return(psem_open(&bsem->psem, name, value, err));
64
- call_or_return(make_lockname(lockname, name, err));
65
- call_or_return(psem_open(&bsem->lock, lockname, 1, err));
66
-
67
- bsem->maxvalue = maxvalue;
68
-
69
- return OK;
70
- }
71
-
72
- static int
73
- bsem_lock(bsem_t *bsem, error_t **err)
74
- {
75
- call_or_return(psem_wait(&bsem->lock, err));
76
- return OK;
77
- }
78
-
79
- static int
80
- bsem_unlock(bsem_t *bsem, error_t **err)
81
- {
82
- call_or_return(psem_post(&bsem->lock, err));
83
- return OK;
84
- }
85
-
86
- int
87
- bsem_close(bsem_t *bsem, error_t **err)
88
- {
89
- bsem_lock_or_return(bsem, err);
90
-
91
- if (psem_close(&bsem->psem, err) == ERROR) {
92
- bsem_unlock(bsem, NULL);
93
- return ERROR;
94
- }
95
-
96
- bsem_unlock_or_return(bsem, err);
97
-
98
- call_or_return(psem_close(&bsem->lock, err));
99
- return OK;
100
- }
101
-
102
- int
103
- bsem_unlink(const char *name, error_t **err)
104
- {
105
- char lockname[MAX_LOCK_NAME];
106
-
107
- call_or_return(psem_unlink(name, err));
108
- call_or_return(make_lockname(lockname, name, err));
109
- call_or_return(psem_unlink(lockname, err));
110
- return OK;
111
- }
112
-
113
- int
114
- bsem_post(bsem_t *bsem, error_t **err)
115
- {
116
- int sval;
117
-
118
- bsem_lock_or_return(bsem, err);
119
-
120
- /* FIXME: maxvalue is broken on some systems... (cygwin? mac?) */
121
- if (psem_getvalue(&bsem->psem, &sval, err) == ERROR) {
122
- bsem_unlock(bsem, err);
123
- return ERROR;
124
- }
125
-
126
- if (sval >= bsem->maxvalue) {
127
- /* ignored silently */
128
- bsem_unlock(bsem, err);
129
- return OK;
130
- }
131
-
132
- if (psem_post(&bsem->psem, err) == ERROR) {
133
- bsem_unlock(bsem, err);
134
- return ERROR;
135
- }
136
-
137
- bsem_unlock_or_return(bsem, err);
138
- return OK;
139
- }
140
-
141
- int
142
- bsem_wait(bsem_t *bsem, error_t **err)
143
- {
144
- call_or_return(psem_wait(&bsem->psem, err));
145
- return OK;
146
- }
147
-
148
- int
149
- bsem_trywait(bsem_t *bsem, error_t **err)
150
- {
151
- bsem_lock_or_return(bsem, err);
152
-
153
- if (psem_trywait(&bsem->psem, err) == ERROR) {
154
- bsem_unlock(bsem, NULL);
155
- return ERROR;
156
- }
157
-
158
- bsem_unlock_or_return(bsem, err);
159
- return OK;
160
- }
161
-
162
- int
163
- bsem_timedwait(bsem_t *bsem, float timeout_s, error_t **err)
164
- {
165
- bsem_lock_or_return(bsem, err);
166
-
167
- if (psem_timedwait(&bsem->psem, timeout_s, err) == ERROR) {
168
- bsem_unlock(bsem, NULL);
169
- return ERROR;
170
- }
171
-
172
- bsem_unlock_or_return(bsem, err);
173
- return OK;
174
- }
175
-
176
- int
177
- bsem_getvalue(bsem_t *bsem, int *sval, error_t **err)
178
- {
179
- bsem_lock_or_return(bsem, err);
180
-
181
- if (psem_getvalue(&bsem->psem, sval, err) == ERROR) {
182
- bsem_unlock(bsem, NULL);
183
- return ERROR;
184
- }
185
-
186
- bsem_unlock_or_return(bsem, err);
187
- return OK;
188
- }