priority_mutex 0.0.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 +15 -0
- data/LICENSE +22 -0
- data/README.md +11 -0
- data/VERSION +1 -0
- data/lib/priority_mutex.rb +84 -0
- metadata +91 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
N2E0OTMwYzJiYmE5M2E5Y2ZjYzhlZTlkZWVlOGZiNTc5YjA1M2IwNw==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
YTJkYmZiYzM0YWE0YTg5NmE3Nzk0NGY4NTVkZGI5ZGQzZDRiMzY2NQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
Mjc4YWYyZjI1ZDlkOWJkNmM4MWZkMzU5MDVhZjZmNzM1Mjk3ODVmZmIxOWY0
|
10
|
+
YjFkYjU3N2RlODIwODMwMmQ2MDVhMzA3M2RkZjNmOGY1MzAzMTNjN2QwYWU2
|
11
|
+
ZTM4Yzk1ZWVhNDBmMTI1Mzk0MDc2NDdkZjgxMWI5YjhiN2FmZjg=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZTFhMTIzNmFmNzRmMjM2ZWQyMTM4MzQzMTQ4NDlhMWZmMTk2ODBlZjc3ZDVm
|
14
|
+
YjAwODYwMmY0MTE4OTVkYjEzMGIzMjgyZGFlYTliM2I3ZWE1OWI3ODhkNDg1
|
15
|
+
Y2ExMDQ3MzA2NmJlNWRiODI3YTYxNmY4OTIxNWFiMDViNDYzNDU=
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Mike Jarema
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# priority_mutex [](http://travis-ci.org/mikejarema/priority_mutex)
|
2
|
+
|
3
|
+
This gem implements a mutex which allows access to a shared resource as determined by a priority set by each requesting thread. Higher priority values mean higher priority.
|
4
|
+
|
5
|
+
Using a PriorityMutex, you can now allow a high priority thread to "jump in line" for a mutex ahead of other lower priority threads. Note, this does not preempt the resource from a thread which is currently using the resource.
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
## License & Notes
|
10
|
+
|
11
|
+
The MIT License - Copyright (c) 2012 [Mike Jarema](http://mikejarema.com)
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,84 @@
|
|
1
|
+
=begin
|
2
|
+
Basically the PQueue manages the incoming requests from thread to access the shared resource,
|
3
|
+
and sorting these requests by priority such that the highest priority request gains access.
|
4
|
+
|
5
|
+
The Waiter helper class allows to pause and resume threads based on when the PQueue determines
|
6
|
+
they should have access to the shared resource.
|
7
|
+
|
8
|
+
The Waiter's pause/resume impementation is borrowed from http://stackoverflow.com/a/23767859/1001980,
|
9
|
+
and the queue is specifically used to preempt any race conditions that may arise from using a
|
10
|
+
Mutex, namely if a Waiter is #resume'd before #wait'ing.
|
11
|
+
=end
|
12
|
+
|
13
|
+
require 'pqueue'
|
14
|
+
|
15
|
+
class PriorityMutex
|
16
|
+
|
17
|
+
class Waiter
|
18
|
+
attr_accessor :priority
|
19
|
+
def initialize(priority)
|
20
|
+
@priority = priority
|
21
|
+
@queue = Queue.new
|
22
|
+
end
|
23
|
+
def <=>(other)
|
24
|
+
self.priority <=> other.priority
|
25
|
+
end
|
26
|
+
def wait
|
27
|
+
@queue.deq
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
def resume
|
31
|
+
@queue.enq :go
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def initialize
|
36
|
+
@pq = PQueue.new
|
37
|
+
@pq_mutex = Mutex.new
|
38
|
+
@resource_active = false
|
39
|
+
end
|
40
|
+
|
41
|
+
def synchronize(priority = 0)
|
42
|
+
wait_for_resource_if_necessary(priority)
|
43
|
+
yield
|
44
|
+
release_resource
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def wait_for_resource_if_necessary(priority)
|
50
|
+
waiter = nil
|
51
|
+
|
52
|
+
@pq_mutex.synchronize do
|
53
|
+
if @resource_active
|
54
|
+
# So we'll need to wait, waiting happens _outside_ of this
|
55
|
+
# synchronization block to ensure that
|
56
|
+
waiter = Waiter.new(priority)
|
57
|
+
@pq.push(waiter)
|
58
|
+
|
59
|
+
else
|
60
|
+
@resource_active = true
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
if waiter
|
66
|
+
waiter.wait
|
67
|
+
@pq_mutex.synchronize { @resource_active = true }
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def release_resource
|
72
|
+
next_waiter = nil
|
73
|
+
|
74
|
+
@pq_mutex.synchronize do
|
75
|
+
@resource_active = false
|
76
|
+
next_waiter = @pq.pop
|
77
|
+
end
|
78
|
+
|
79
|
+
if next_waiter
|
80
|
+
next_waiter.resume
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: priority_mutex
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Jarema
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '10'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pqueue
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3'
|
55
|
+
description: Ruby gem implementing a Mutex which allows for preemptive queuing based
|
56
|
+
on priority.
|
57
|
+
email: mike@jarema.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- LICENSE
|
63
|
+
- README.md
|
64
|
+
- VERSION
|
65
|
+
- lib/priority_mutex.rb
|
66
|
+
homepage: http://github.com/mikejarema/priority_mutex
|
67
|
+
licenses:
|
68
|
+
- MIT
|
69
|
+
metadata: {}
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 2.2.2
|
87
|
+
signing_key:
|
88
|
+
specification_version: 4
|
89
|
+
summary: Ruby gem implementing a Mutex which allows for preemptive queuing based on
|
90
|
+
priority.
|
91
|
+
test_files: []
|