blib/lib/IPC/Mmap/Win32.pm untested

filter: Statementstmt 0.0% 0 / 37 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 72 CC 5 SCAR 24.3
CC
5
Cov
0%
new
30.0
lock
2.0
unlock
2.0
CRAP 11.3
1 #/**
2 # Concrete implementation of the IPC::Mmap class for Win32.
3 # <p>
4 # Permission is granted to use this software under the same terms as Perl itself.
5 # Refer to the <a href='http://perldoc.perl.org/perlartistic.html'>Perl Artistic License</a>
6 # for details.
7 #
8 # @author D. Arnold
9 # @since 2006-05-01
10 # @self $self
11 #*/
12 package IPC::Mmap::Win32;
13 #
14 # define and export Win32 equivalents for MMAP_* and
15 # PROT_* flags
16 #
17 use Carp;
18 use Win32::MMF;
19 use Win32::MMF qw(
20 CreateFile OpenFile CloseHandle CreateFileMapping OpenFileMapping
21 MapViewOfFile UnmapViewOfFile ClaimNamespace ReleaseNamespace UseNamespace
22 CreateSemaphore WaitForSingleObject ReleaseSemaphore);
23  
24 use IPC::Mmap;
25 use IPC::Mmap qw(MAP_ANON MAP_ANONYMOUS MAP_FILE MAP_PRIVATE MAP_SHARED
26 PROT_READ PROT_WRITE);
27 use base qw(IPC::Mmap);
28  
29 our $VERSION = '0.22_02';
30  
31 #/**
32 # Constructor. mmap()'s using Win32::MMF::UseNameSpace,
33 # Win32::MMF::ClaimNameSpace, Win32::MMF::MapViewOfFile,
34 # and Win32::MMF::CreateSemaphore methods.
35 #
36 # @param $filename
37 # @param $length optional
38 # @param $protflags optional
39 # @param $mmapflags optional
40 #
41 # @return the IPC::Mmap::Win32 object on success; undef on failure
42 #*/
43 sub new {
44 my ($class, $file, $length, $prot, $mmap) = @_;
45  
46 croak 'Filename required for Win32'
47 if (! $file) || (ref $file);
48 #
49 # if no length given, Win32::MMF defaults to 128K
50 #
51 $length = 128 * 1024 unless $length;
52 #
53 # if MMAP_FILE set, then we creat the swapfile from
54 # the filename, and use it as the namespace too
55 #
56 my $self = {
57 _file => $file,
58 _maxlen => $length,
59 _swapfile => (($mmap & MAP_FILE) ? $file : undef),
60 _fh => 0, # swap file handle
61 _ns => 0, # namespace handle
62 _addr => 0, # view address
63 _semaphore => 0, # semaphore used for exclusive locking
64 _access => $prot, # enforced in module
65 _slop => 0 # dummy value; needed on POSIX
66 };
67 #
68 # try the file first; if not found, create it
69 #
70 ($self->{_fh}, $self->{_ns}) = ClaimNamespace($self->{_swapfile}, $file, $length)
71 unless $self->{_ns} = UseNamespace($file);
72 #
73 # set default view to the namespace
74 #
75 $self->{_addr} = MapViewOfFile($self->{_ns}, 0, $length)
76 or croak "Can't map view of file";
77 #
78 # create semaphore object for the view
79 #
80 my @sem = split(/[\/\\]/, $file);
81 $self->{_semaphore} = CreateSemaphore(1, 1, $sem[-1] . '.lock')
82 or croak("Can not create semaphore!");
83  
84 return bless $self, $class;
85 }
86  
87 #/**
88 # Locks the mmap'ed region. Implemented using Win32::MMF::WaitForSingleObject().
89 # <i>May</i> be sufficient for multithread locking.
90 #*/
91 sub lock {
92 return WaitForSingleObject($_[0]->{_semaphore}, 0x7FFFFFFF);
93 }
94  
95 #/**
96 # Unlocks the mmap'ed region. Implemented using Win32::MMF::ReleaseSemaphore().
97 #*/
98 sub unlock {
99 return ReleaseSemaphore($_[0]->{_semaphore}, 1);
100 }
101 #/**
102 # Unmap the mmap()ed region.
103 #*/
104 sub close {
105 my $self = shift;
106  
107 # unmap existing views
108 UnmapViewOfFile($self->{_addr});
109  
110 # close namespace and swap file
111 ReleaseNamespace($self->{_swap}, $self->{_ns});
112 }
113 #
114 # unmap and close the file
115 # NOTE: we need a ref count for multithreaded environments ?;
116 # for now we'll just bypass DESTROY
117 #
118 sub oldDESTROY {
119 my $self = shift;
120  
121 # unmap existing views
122 UnmapViewOfFile($self->{_addr});
123  
124 # close namespace and swap file
125 ReleaseNamespace($self->{_swap}, $self->{_ns});
126 }
127  
128 1;