szymanskis_mutex 0.1.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/szymanskis_mutex.rb +100 -0
  3. metadata +65 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6538dbfac64f4bdd4bd53fbb5e2807036ed382958326a2df443bf8b37b97aaf8
4
+ data.tar.gz: af0cad1545a97ebccfc7534bad9cc527e73901c51d4805772136b79e79c2c59e
5
+ SHA512:
6
+ metadata.gz: 4bc341584bb0cdbe2bb73136dba30cace5d3562e0cd1640b60503e0673b157d33611f8684e217c69c59ad3bfa486d07223a004eb80ecdfea403c2723a6fd2054
7
+ data.tar.gz: 5b045819425278d099efd33df24fbb5eaf631f98380878ca3b3eb64906528a7a43020c2bfe8d7fffa12b95c1ec4945f2b63707136b164f0c042ec6f5442d5831
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (c) 2018, Emmanuel Byrd
4
+
5
+ # Permission is hereby granted, free of charge, to any person
6
+ # obtaining a copy of this software and associated documentation
7
+ # files (the "Software"), to deal in the Software without
8
+ # restriction, including without limitation the rights to use,
9
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # copies of the Software, and to permit persons to whom the
11
+ # Software is furnished to do so, subject to the following
12
+ # conditions:
13
+
14
+ # The above copyright notice and this permission notice shall be
15
+ # included in all copies or substantial portions of the Software.
16
+
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21
+ # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
+ # OTHER DEALINGS IN THE SOFTWARE.
25
+
26
+ # Based on the Szymanski's Mutual Exclusion Algorithm
27
+ module SzymanskisMutex
28
+ # Larger seconds mean less cycles/second but may result in lower completion
29
+ SZYMANSKIS_SLEEP = 0.05
30
+
31
+ @@flags = {}
32
+ @@counter = {}
33
+
34
+ def mutual_exclusion(concern)
35
+ @@counter[concern] ||= 0
36
+ @@flags[concern] ||= {}
37
+
38
+ # Suppose @@counter += 1 is an atomic function
39
+ my_id = @@counter[concern] += 1
40
+
41
+ entry_protocol(concern, my_id)
42
+ begin
43
+ yield
44
+ ensure
45
+ exit_protocol(concern, my_id)
46
+ end
47
+ end
48
+
49
+ # 1: Standing outside waiting room
50
+ # 2: Waiting for other processes to enter
51
+ # 3: Standing in doorway
52
+ # 4: Closed entrance door
53
+ def entry_protocol(concern, id)
54
+ # Standing outside waiting room
55
+ @@flags[concern][id] = 1
56
+
57
+ # Wait for open door
58
+ sleep(SZYMANSKIS_SLEEP) while @@flags[concern].values.any? { |f| f > 2 }
59
+
60
+ # Stand in doorway
61
+ @@flags[concern][id] = 3
62
+
63
+ # Is another process waiting to enter?
64
+ if @@flags[concern].values.any? { |f| f == 1 }
65
+
66
+ # Wait for other processes to enter
67
+ @@flags[concern][id] = 2
68
+
69
+ # Wait for a process to enter and close the door
70
+ sleep(SZYMANSKIS_SLEEP) while @@flags[concern].values.all? { |f| f != 4 }
71
+ end
72
+
73
+ # Close the entrance door
74
+ @@flags[concern][id] = 4
75
+
76
+ # Wait for lower ids to finish exit protocol
77
+ while @@flags[concern].select { |i| i < id }.values.any? { |f| f != 1 }
78
+ sleep(SZYMANSKIS_SLEEP)
79
+ end
80
+ end
81
+
82
+ def exit_protocol(concern, id)
83
+ # Ensure everyone in the waiting room has realized that the door
84
+ # is supposed to be closed
85
+ while @@flags[concern].select { |i| i > id }.any? { |f| [2, 3].include?(f) }
86
+ sleep(SZYMANSKIS_SLEEP)
87
+ end
88
+
89
+ # Leave. This will reopen door if nobody is still in the waiting room
90
+ @@flags[concern].delete(id)
91
+
92
+ return unless @@flags[concern].empty?
93
+
94
+ # Prevent counter from increasing indefinitely
95
+ @@counter.delete concern
96
+ @@flags.delete concern
97
+ end
98
+
99
+ protected :entry_protocol, :exit_protocol
100
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: szymanskis_mutex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Emmanuel Byrd
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-10-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.5'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 3.5.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '3.5'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 3.5.0
33
+ description: Algorithm devised by Boleslaw Szymanski. This MutEx has linear wait and
34
+ only 5 communication variables
35
+ email: emmanuel_pajaro@hotmail.com
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - lib/szymanskis_mutex.rb
41
+ homepage: https://github.com/EByrdS/szymanskis_mutex
42
+ licenses:
43
+ - MIT
44
+ metadata: {}
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 2.7.6
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Szymanski's Mutual Exclusion Algorithm.
65
+ test_files: []