process_shared 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
data/lib/process_shared.rb
CHANGED
@@ -2,6 +2,9 @@ require 'process_shared/psem'
|
|
2
2
|
require 'process_shared/semaphore'
|
3
3
|
|
4
4
|
module ProcessShared
|
5
|
+
# BoundedSemaphore is identical to Semaphore except that its value
|
6
|
+
# is not permitted to rise above a maximum. When the value is at
|
7
|
+
# the maximum, calls to #post will have no effect.
|
5
8
|
class BoundedSemaphore < Semaphore
|
6
9
|
# With no associated block, open is a synonym for
|
7
10
|
# Semaphore.new. If the optional code block is given, it will be
|
data/lib/process_shared/mutex.rb
CHANGED
@@ -4,12 +4,27 @@ require 'process_shared/shared_memory'
|
|
4
4
|
require 'process_shared/process_error'
|
5
5
|
|
6
6
|
module ProcessShared
|
7
|
+
# This Mutex class is implemented as a BoundedSemaphore with a
|
8
|
+
# maximum value of 1. Additionally, the locking process is tracked,
|
9
|
+
# and ProcessError is raised if either #unlock is called by a
|
10
|
+
# process different from the locking process, or if #lock is called
|
11
|
+
# while the process already holds the lock (i.e. the mutex is not
|
12
|
+
# re-entrant). This tracking is not without performance cost, of
|
13
|
+
# course (current implementation uses an additional BoundedSemaphore
|
14
|
+
# and SharedMemory segment).
|
15
|
+
#
|
16
|
+
# The API is intended to be identical to the ::Mutex in the core
|
17
|
+
# Ruby library.
|
18
|
+
#
|
19
|
+
# TODO: the core Ruby api has no #close method, but this Mutex must
|
20
|
+
# release its BoundedSemaphore and SharedMemory resources. For now,
|
21
|
+
# we rely on the object finalizers of those objects...
|
7
22
|
class Mutex
|
8
|
-
include WithSelf
|
23
|
+
# include WithSelf
|
9
24
|
|
10
|
-
def self.open(&block)
|
11
|
-
|
12
|
-
end
|
25
|
+
# def self.open(&block)
|
26
|
+
# new.with_self(&block)
|
27
|
+
# end
|
13
28
|
|
14
29
|
def initialize
|
15
30
|
@internal_sem = BoundedSemaphore.new(1)
|
@@ -73,6 +88,8 @@ module ProcessShared
|
|
73
88
|
|
74
89
|
# Acquire the lock, yield the block, then ensure the lock is
|
75
90
|
# unlocked.
|
91
|
+
#
|
92
|
+
# @return [Object] the result of the block
|
76
93
|
def synchronize
|
77
94
|
lock
|
78
95
|
begin
|
@@ -44,7 +44,8 @@ module ProcessShared
|
|
44
44
|
psem_post(sem, err)
|
45
45
|
end
|
46
46
|
|
47
|
-
# Get the current value of the semaphore.
|
47
|
+
# Get the current value of the semaphore. Raises Errno::NOTSUP on
|
48
|
+
# platforms that don't support this (e.g. Mac OS X).
|
48
49
|
#
|
49
50
|
# @return [Integer] the current value of the semaphore.
|
50
51
|
def value
|
@@ -53,6 +54,11 @@ module ProcessShared
|
|
53
54
|
int.get_int(0)
|
54
55
|
end
|
55
56
|
|
57
|
+
# Release the resources associated with this semaphore. Calls to
|
58
|
+
# other methods are undefined after #close has been called.
|
59
|
+
#
|
60
|
+
# Close must be called when the semaphore is no longer needed. An
|
61
|
+
# object finalizer will close the semaphore as a last resort.
|
56
62
|
def close
|
57
63
|
psem_close(sem, err)
|
58
64
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: process_shared
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Patrick Mahoney
|