win32-mutex 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES ADDED
@@ -0,0 +1,33 @@
1
+ == 0.3.0 - 4-May-2007
2
+ * Now pure Ruby.
3
+ * Both the Mutex.new and Mutex.open methods now accept a block, and
4
+ automatically close the associated handle at the end of the block.
5
+ * The Mutex.new method now accepts an optional third argument that controls
6
+ whether the mutex object can be inherited by other processes.
7
+ * Added a gemspec.
8
+ * Added a Rakefile, including tasks for installation and testing.
9
+ * Removed the doc/mutext.txt file. The documentation is now inlined via RDoc.
10
+ There is also some documentation in the README file.
11
+ * The mutex_test.rb script was updated and bugs were fixed.
12
+ * Now requires the windows-pr package.
13
+
14
+ == 0.2.2 - 29-May-2005
15
+ * Now Unicode friendly.
16
+ * Removed the mutex.rd file. The mutex.txt file is now rdoc friendly.
17
+ * Added some tests and tweaked the test setup.
18
+
19
+ == 0.2.1 - 1-Mar-2005
20
+ * Moved the 'examples' directory to the toplevel directory.
21
+ * Made the CHANGES and README files rdoc friendly.
22
+
23
+ == 0.2.0 - 18-Jul-2004
24
+ * Updated to use the newer allocation framework. This means that, as of this
25
+ release, this package requires Ruby 1.8.0 or later.
26
+ * Fixed a minor bug in the initialization function where an expected error
27
+ might not be raised.
28
+ * Added tests and documentation for Mutex.open(). This was in the last
29
+ version, but I forgot to document or test it.
30
+ * Moved the test.rb script to doc/examples
31
+
32
+ == 0.1.0 - 3-May-2004
33
+ * Initial release
data/MANIFEST ADDED
@@ -0,0 +1,8 @@
1
+ * CHANGES
2
+ * INSTALL
3
+ * MANIFEST
4
+ * Rakefile
5
+ * win32-mutex.gemspec
6
+ * examples/mutex_test.rb
7
+ * lib/win32/mutex.rb
8
+ * test/tc_mutex.rb
data/README ADDED
@@ -0,0 +1,52 @@
1
+ == Brief Description
2
+ Interface for Mutexes on MS Windows.
3
+
4
+ == Prerequisites
5
+ win32-ipc 0.5.0 or later
6
+
7
+ == Installation
8
+ rake test (optional)
9
+ rake install
10
+
11
+ == Synopsis
12
+ require 'win32/mutex'
13
+
14
+ # Do not leave out the 'Win32::', otherwise you're using Ruby's Mutex class.
15
+ Win32::Mutex.new(false, 'test') do |m|
16
+ # Do stuff
17
+ m.release
18
+ end
19
+
20
+ == Documentation
21
+ The mutex.rb file contains inline RDoc documentation. If you installed
22
+ this file as a gem, then you have the docs.
23
+
24
+ For an example of win32-mutex in action, look at the mutex_test.rb file
25
+ in the 'examples' directory. You can also run the 'examples' rake task.
26
+
27
+ == Notes
28
+ The Mutex class is a subclass of Win32::Ipc (win32-ipc). This package
29
+ require's the win32-ipc package internally (you don't need to explicitly
30
+ call it).
31
+
32
+ == Acknowledgements
33
+ Originally adapted from the Win32::Mutex Perl module.
34
+
35
+ == Known Bugs
36
+ None that I know of. Please log any other bug reports on the RubyForge
37
+ project page at http://www.rubyforge.net/projects/win32utils
38
+
39
+ == License
40
+ Ruby's
41
+
42
+ == Copyright
43
+ (C) 2003-2007 Daniel J. Berger, All Rights Reserved
44
+
45
+ == Warranty
46
+ This package is provided "as is" and without any express or
47
+ implied warranties, including, without limitation, the implied
48
+ warranties of merchantability and fitness for a particular purpose.
49
+
50
+ == Authors
51
+ Daniel J. Berger
52
+ Park Heesob
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rbconfig'
4
+ include Config
5
+
6
+ desc 'Install the win32-mutex package (non-gem)'
7
+ task :install do
8
+ sitelibdir = CONFIG['sitelibdir']
9
+ installdir = File.join(sitelibdir, 'win32')
10
+ file = 'lib\win32\mutex.rb'
11
+
12
+ Dir.mkdir(installdir) unless File.exists?(installdir)
13
+ FileUtils.cp(file, installdir, :verbose => true)
14
+ end
15
+
16
+ desc 'Run the example program'
17
+ task :example do
18
+ ruby '-Ilib examples/mutex_test.rb'
19
+ end
20
+
21
+ Rake::TestTask.new do |t|
22
+ t.libs << 'test'
23
+ t.verbose = true
24
+ t.warning = true
25
+ t.test_files = FileList['test/tc_mutex.rb']
26
+ end
@@ -0,0 +1,124 @@
1
+ require 'win32/ipc'
2
+
3
+ module Win32
4
+ class Mutex < Ipc
5
+
6
+ # This is the error raised if any of the Mutex methods fail.
7
+ class Error < StandardError; end
8
+
9
+ extend Windows::Synchronize
10
+ extend Windows::Error
11
+ extend Windows::Handle
12
+
13
+ VERSION = '0.3.0'
14
+
15
+ # The name of the Mutex object.
16
+ #
17
+ attr_reader :name
18
+
19
+ # Creates and returns new Mutex object. If +name+ is omitted, the
20
+ # Mutex object is created without a name, i.e. it's anonymous.
21
+ #
22
+ # If the +initial_owner+ value is true and the caller created the mutex,
23
+ # the calling thread obtains initial ownership of the mutex object.
24
+ # Otherwise, the calling thread does not obtain ownership of the mutex.
25
+ # This value is false by default.
26
+ #
27
+ # If +name+ is provided and it already exists, then it is opened
28
+ # instead, and the +initial_count+ and +max_count+ parameters are
29
+ # ignored.
30
+ #
31
+ # The +inherit+ attribute determines whether or not the mutex can
32
+ # be inherited by child processes.
33
+ #
34
+ def initialize(initial_owner=false, name=nil, inherit=true)
35
+ @initial_owner = initial_owner
36
+ @name = name
37
+ @inherit = inherit
38
+
39
+ # Used to prevent potential segfaults.
40
+ if name && !name.is_a?(String)
41
+ raise TypeError, 'name must be a string'
42
+ end
43
+
44
+ if inherit
45
+ sec = 0.chr * 12 # sizeof(SECURITY_ATTRIBUTES)
46
+ sec[0,4] = [12].pack('L')
47
+ sec[8,4] = [1].pack('L') # 1 == TRUE
48
+ else
49
+ sec = 0
50
+ end
51
+
52
+ initial = initial_owner ? 1 : 0
53
+
54
+ handle = CreateMutex(sec, initial, name)
55
+
56
+ if handle == 0 || handle == INVALID_HANDLE_VALUE
57
+ raise Error, get_last_error
58
+ end
59
+
60
+ super(handle)
61
+
62
+ if block_given?
63
+ begin
64
+ yield self
65
+ ensure
66
+ close
67
+ end
68
+ end
69
+ end
70
+
71
+ # Open an existing Mutex by +name+. The +inherit+ argument sets
72
+ # whether or not the object was opened such that a process created by the
73
+ # CreateProcess() function (a Windows API function) can inherit the
74
+ # handle. The default is true.
75
+ #
76
+ # This method is essentially identical to Mutex.new, except that the
77
+ # option for +initial_owner+ cannot be set (since it is already set).
78
+ # Also, this method will raise a Mutex::Error if the mutex doesn't
79
+ # already exist.
80
+ #
81
+ # If you want "open or create" semantics, then use Mutex.new.
82
+ #--
83
+ # The OpenMutex() call here is strictly to force an error if the user
84
+ # tries to open a mutex that doesn't already exist.
85
+ #
86
+ def self.open(name, inherit=true, &block)
87
+ if name && !name.is_a?(String)
88
+ raise TypeError, 'name must be a string'
89
+ end
90
+
91
+ bool = inherit ? 1 : 0
92
+ handle = OpenMutex(MUTEX_ALL_ACCESS, bool, name)
93
+ if handle == 0 || handle == INVALID_HANDLE_VALUE
94
+ raise Error, get_last_error
95
+ end
96
+ CloseHandle(handle)
97
+
98
+ self.new(false, name, inherit, &block)
99
+ end
100
+
101
+ # Releases ownership of the mutex.
102
+ #
103
+ def release
104
+ unless ReleaseMutex(@handle)
105
+ raise Error, get_last_error
106
+ end
107
+ end
108
+
109
+ # Returns whether or not the calling thread has initial ownership of
110
+ # the mutex object.
111
+ #
112
+ def initial_owner?
113
+ @initial_owner
114
+ end
115
+
116
+ # Returns whether or not the object was opened such that a process
117
+ # created by the CreateProcess() function (a Windows API function) can
118
+ # inherit the handle. The default is true.
119
+ #
120
+ def inheritable?
121
+ @inherit
122
+ end
123
+ end
124
+ end
data/test/tc_mutex.rb ADDED
@@ -0,0 +1,61 @@
1
+ #####################################################################
2
+ # tc_Win32::Mutex.rb
3
+ #
4
+ # Test suite for the win32-Win32::Mutex package. This test suite should
5
+ # be run via the 'rake test' task.
6
+ #####################################################################
7
+ require 'test/unit'
8
+ require 'win32/mutex'
9
+
10
+ class TC_Win32_Mutex < Test::Unit::TestCase
11
+ def setup
12
+ @mutex = Win32::Mutex.new(true, 'test')
13
+ @umutex = Win32::Mutex.new(false, "Ηελλας")
14
+ end
15
+
16
+ def test_version
17
+ assert_equal('0.3.0', Win32::Mutex::VERSION)
18
+ end
19
+
20
+ def test_open
21
+ assert_respond_to(Win32::Mutex, :open)
22
+ assert_nothing_raised{ Win32::Mutex.open("test"){} }
23
+ assert_nothing_raised{ Win32::Mutex.open("Ηελλας"){} }
24
+ assert_raises(Win32::Mutex::Error){ Win32::Mutex.open("bogus"){} }
25
+ end
26
+
27
+ def test_release
28
+ assert_respond_to(@mutex, :release)
29
+ assert_nothing_raised{ @mutex.release }
30
+ end
31
+
32
+ def test_initial_owner
33
+ assert_respond_to(@mutex, :initial_owner?)
34
+ assert_equal(true, @mutex.initial_owner?)
35
+ assert_equal(false, @umutex.initial_owner?)
36
+ end
37
+
38
+ def test_inheritable
39
+ assert_respond_to(@mutex, :inheritable?)
40
+ assert_equal(true, @mutex.inheritable?)
41
+ end
42
+
43
+ def test_wait
44
+ assert_respond_to(@mutex, :wait)
45
+ end
46
+
47
+ def test_wait_any
48
+ assert_respond_to(@mutex, :wait_any)
49
+ end
50
+
51
+ def test_wait_all
52
+ assert_respond_to(@mutex, :wait_all)
53
+ end
54
+
55
+ def teardown
56
+ @mutex.close
57
+ @umutex.close
58
+ @mutex = nil
59
+ @umutex = nil
60
+ end
61
+ end
@@ -0,0 +1,24 @@
1
+ require "rubygems"
2
+
3
+ spec = Gem::Specification.new do |gem|
4
+ gem.name = "win32-mutex"
5
+ gem.version = "0.3.0"
6
+ gem.author = "Daniel J. Berger"
7
+ gem.email = "djberg96@gmail.com"
8
+ gem.homepage = "http://www.rubyforge.org/projects/win32utils"
9
+ gem.platform = Gem::Platform::RUBY
10
+ gem.summary = "Interface to MS Windows Mutex objects."
11
+ gem.description = "Interface to MS Windows Mutex objects."
12
+ gem.test_file = "test/tc_mutex.rb"
13
+ gem.has_rdoc = true
14
+ gem.files = Dir["lib/win32/*.rb"] + Dir["test/*"] + Dir["[A-Z]*"]
15
+ gem.files.reject! { |fn| fn.include? "CVS" }
16
+ gem.require_path = "lib"
17
+ gem.extra_rdoc_files = ["README", "CHANGES", "MANIFEST"]
18
+ gem.add_dependency("win32-ipc", ">= 0.5.0")
19
+ end
20
+
21
+ if $0 == __FILE__
22
+ Gem.manage_gems
23
+ Gem::Builder.new(spec).build
24
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: win32-mutex
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.3.0
7
+ date: 2007-05-04 00:00:00 -06:00
8
+ summary: Interface to MS Windows Mutex objects.
9
+ require_paths:
10
+ - lib
11
+ email: djberg96@gmail.com
12
+ homepage: http://www.rubyforge.org/projects/win32utils
13
+ rubyforge_project:
14
+ description: Interface to MS Windows Mutex objects.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Daniel J. Berger
31
+ files:
32
+ - lib/win32/mutex.rb
33
+ - test/CVS
34
+ - test/tc_mutex.rb
35
+ - CHANGES
36
+ - CVS
37
+ - examples
38
+ - lib
39
+ - MANIFEST
40
+ - Rakefile
41
+ - README
42
+ - test
43
+ - win32-mutex.gemspec
44
+ test_files:
45
+ - test/tc_mutex.rb
46
+ rdoc_options: []
47
+
48
+ extra_rdoc_files:
49
+ - README
50
+ - CHANGES
51
+ - MANIFEST
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ requirements: []
57
+
58
+ dependencies:
59
+ - !ruby/object:Gem::Dependency
60
+ name: win32-ipc
61
+ version_requirement:
62
+ version_requirements: !ruby/object:Gem::Version::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 0.5.0
67
+ version: