00001 // Copyright (C) 2009, Vaclav Haisman. All rights reserved. 00002 // 00003 // Redistribution and use in source and binary forms, with or without modifica- 00004 // tion, are permitted provided that the following conditions are met: 00005 // 00006 // 1. Redistributions of source code must retain the above copyright notice, 00007 // this list of conditions and the following disclaimer. 00008 // 00009 // 2. Redistributions in binary form must reproduce the above copyright notice, 00010 // this list of conditions and the following disclaimer in the documentation 00011 // and/or other materials provided with the distribution. 00012 // 00013 // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 00014 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 00015 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00016 // APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 00017 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- 00018 // DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 00019 // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 00020 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00021 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00022 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00023 00024 #include "dcmtk/ofstd/ofcast.h" 00025 00031 00032 namespace log4cplus { namespace thread { 00033 00034 00035 #define LOG4CPLUS_THROW_RTE(msg) \ 00036 do { detail::syncprims_throw_exception (msg, __FILE__, __LINE__); } while (0) 00037 00038 // 00039 // 00040 // 00041 00042 inline 00043 Mutex::Mutex () 00044 { 00045 InitializeCriticalSection (&cs); 00046 } 00047 00048 00049 inline 00050 Mutex::~Mutex () 00051 { 00052 DeleteCriticalSection (&cs); 00053 } 00054 00055 00056 inline 00057 void 00058 Mutex::lock () const 00059 { 00060 EnterCriticalSection (&cs); 00061 } 00062 00063 00064 inline 00065 void 00066 Mutex::unlock () const 00067 { 00068 LeaveCriticalSection (&cs); 00069 } 00070 00071 00072 // 00073 // 00074 // 00075 00076 inline 00077 Semaphore::Semaphore (unsigned max, unsigned initial) 00078 { 00079 sem = CreateSemaphore (0, initial, max, 0); 00080 if (! sem) 00081 LOG4CPLUS_THROW_RTE ("Semaphore::Semaphore"); 00082 } 00083 00084 00085 inline 00086 Semaphore::~Semaphore () 00087 { 00088 try 00089 { 00090 if (! CloseHandle (sem)) 00091 LOG4CPLUS_THROW_RTE ("Semaphore::~Semaphore"); 00092 } 00093 catch (...) 00094 { } 00095 } 00096 00097 00098 inline 00099 void 00100 Semaphore::unlock () const 00101 { 00102 DWORD ret = ReleaseSemaphore (sem, 1, 0); 00103 if (! ret) 00104 LOG4CPLUS_THROW_RTE ("Semaphore::unlock"); 00105 } 00106 00107 00108 inline 00109 void 00110 Semaphore::lock () const 00111 { 00112 DWORD ret = WaitForSingleObject (sem, INFINITE); 00113 if (ret != WAIT_OBJECT_0) 00114 LOG4CPLUS_THROW_RTE ("Semaphore::lock"); 00115 } 00116 00117 00118 // 00119 // 00120 // 00121 00122 inline 00123 ManualResetEvent::ManualResetEvent (bool sig) 00124 { 00125 ev = CreateEvent (0, true, sig, 0); 00126 if (! ev) 00127 LOG4CPLUS_THROW_RTE ("ManualResetEvent::ManualResetEvent"); 00128 } 00129 00130 00131 inline 00132 ManualResetEvent::~ManualResetEvent () 00133 { 00134 try 00135 { 00136 if (! CloseHandle (ev)) 00137 LOG4CPLUS_THROW_RTE ("ManualResetEvent::~ManualResetEvent"); 00138 } 00139 catch (...) 00140 { } 00141 } 00142 00143 00144 inline 00145 void 00146 ManualResetEvent::signal () const 00147 { 00148 if (! SetEvent (ev)) 00149 LOG4CPLUS_THROW_RTE ("ManualResetEVent::signal"); 00150 } 00151 00152 00153 inline 00154 void 00155 ManualResetEvent::wait () const 00156 { 00157 DWORD ret = WaitForSingleObject (ev, INFINITE); 00158 if (ret != WAIT_OBJECT_0) 00159 LOG4CPLUS_THROW_RTE ("ManualResetEvent::wait"); 00160 } 00161 00162 00163 inline 00164 bool 00165 ManualResetEvent::timed_wait (unsigned long msec) const 00166 { 00167 DWORD ret = WaitForSingleObject (ev, OFstatic_cast(DWORD, msec)); 00168 switch(ret) 00169 { 00170 case WAIT_OBJECT_0: 00171 return true; 00172 00173 case WAIT_TIMEOUT: 00174 return false; 00175 00176 default: 00177 LOG4CPLUS_THROW_RTE ("ManualResetEvent::timed_wait"); 00178 // Silence warnings about not returning any value from function 00179 // returning bool. 00180 return false; 00181 } 00182 } 00183 00184 00185 inline 00186 void 00187 ManualResetEvent::reset () const 00188 { 00189 if (! ResetEvent (ev)) 00190 LOG4CPLUS_THROW_RTE ("ManualResetEvent::reset"); 00191 } 00192 00193 00194 #undef LOG4CPLUS_THROW_RTE 00195 00196 00197 } } // namespace log4cplus { namespace thread {