OFFIS DCMTK  Version 3.6.0
syncpwin.h
Go to the documentation of this file.
1 // Copyright (C) 2009, Vaclav Haisman. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without modifica-
4 // tion, are permitted provided that the following conditions are met:
5 //
6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 //
9 // 2. Redistributions in binary form must reproduce the above copyright notice,
10 // this list of conditions and the following disclaimer in the documentation
11 // and/or other materials provided with the distribution.
12 //
13 // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
14 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
15 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
16 // APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
17 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
18 // DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
19 // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 
24 #include "dcmtk/ofstd/ofcast.h"
25 
31 
32 namespace log4cplus { namespace thread {
33 
34 
35 #define LOG4CPLUS_THROW_RTE(msg) \
36  do { detail::syncprims_throw_exception (msg, __FILE__, __LINE__); } while (0)
37 
38 //
39 //
40 //
41 
42 inline
43 Mutex::Mutex ()
44 {
45  InitializeCriticalSection (&cs);
46 }
47 
48 
49 inline
50 Mutex::~Mutex ()
51 {
52  DeleteCriticalSection (&cs);
53 }
54 
55 
56 inline
57 void
58 Mutex::lock () const
59 {
60  EnterCriticalSection (&cs);
61 }
62 
63 
64 inline
65 void
66 Mutex::unlock () const
67 {
68  LeaveCriticalSection (&cs);
69 }
70 
71 
72 //
73 //
74 //
75 
76 inline
77 Semaphore::Semaphore (unsigned max, unsigned initial)
78 {
79  sem = CreateSemaphore (0, initial, max, 0);
80  if (! sem)
81  LOG4CPLUS_THROW_RTE ("Semaphore::Semaphore");
82 }
83 
84 
85 inline
86 Semaphore::~Semaphore ()
87 {
88  try
89  {
90  if (! CloseHandle (sem))
91  LOG4CPLUS_THROW_RTE ("Semaphore::~Semaphore");
92  }
93  catch (...)
94  { }
95 }
96 
97 
98 inline
99 void
100 Semaphore::unlock () const
101 {
102  DWORD ret = ReleaseSemaphore (sem, 1, 0);
103  if (! ret)
104  LOG4CPLUS_THROW_RTE ("Semaphore::unlock");
105 }
106 
107 
108 inline
109 void
110 Semaphore::lock () const
111 {
112  DWORD ret = WaitForSingleObject (sem, INFINITE);
113  if (ret != WAIT_OBJECT_0)
114  LOG4CPLUS_THROW_RTE ("Semaphore::lock");
115 }
116 
117 
118 //
119 //
120 //
121 
122 inline
123 ManualResetEvent::ManualResetEvent (bool sig)
124 {
125  ev = CreateEvent (0, true, sig, 0);
126  if (! ev)
127  LOG4CPLUS_THROW_RTE ("ManualResetEvent::ManualResetEvent");
128 }
129 
130 
131 inline
132 ManualResetEvent::~ManualResetEvent ()
133 {
134  try
135  {
136  if (! CloseHandle (ev))
137  LOG4CPLUS_THROW_RTE ("ManualResetEvent::~ManualResetEvent");
138  }
139  catch (...)
140  { }
141 }
142 
143 
144 inline
145 void
146 ManualResetEvent::signal () const
147 {
148  if (! SetEvent (ev))
149  LOG4CPLUS_THROW_RTE ("ManualResetEVent::signal");
150 }
151 
152 
153 inline
154 void
155 ManualResetEvent::wait () const
156 {
157  DWORD ret = WaitForSingleObject (ev, INFINITE);
158  if (ret != WAIT_OBJECT_0)
159  LOG4CPLUS_THROW_RTE ("ManualResetEvent::wait");
160 }
161 
162 
163 inline
164 bool
165 ManualResetEvent::timed_wait (unsigned long msec) const
166 {
167  DWORD ret = WaitForSingleObject (ev, OFstatic_cast(DWORD, msec));
168  switch(ret)
169  {
170  case WAIT_OBJECT_0:
171  return true;
172 
173  case WAIT_TIMEOUT:
174  return false;
175 
176  default:
177  LOG4CPLUS_THROW_RTE ("ManualResetEvent::timed_wait");
178  // Silence warnings about not returning any value from function
179  // returning bool.
180  return false;
181  }
182 }
183 
184 
185 inline
186 void
187 ManualResetEvent::reset () const
188 {
189  if (! ResetEvent (ev))
190  LOG4CPLUS_THROW_RTE ("ManualResetEvent::reset");
191 }
192 
193 
194 #undef LOG4CPLUS_THROW_RTE
195 
196 
197 } } // namespace log4cplus { namespace thread {


Generated on Thu Dec 20 2012 for OFFIS DCMTK Version 3.6.0 by Doxygen 1.8.2