You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

507 lines
9.8 KiB

  1. // -*- C++ -*-
  2. // Copyright (C) 2009-2010, Vaclav Haisman. All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without modifica-
  5. // tion, are permitted provided that the following conditions are met:
  6. //
  7. // 1. Redistributions of source code must retain the above copyright notice,
  8. // this list of conditions and the following disclaimer.
  9. //
  10. // 2. Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. //
  14. // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  15. // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16. // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  17. // APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  18. // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  19. // DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  20. // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. //! @file
  25. //! This file contains implementations of synchronization
  26. //! primitives using the POSIX threads. It does not contain any
  27. //! include guards because it is only a fragment to be included by
  28. //! syncprims.h.
  29. #include "log4cplus/thread/threads.h"
  30. #include <limits>
  31. #include <algorithm>
  32. namespace log4cplus { namespace thread { namespace impl {
  33. struct PthreadMutexAttr
  34. {
  35. PthreadMutexAttr ()
  36. {
  37. int ret = pthread_mutexattr_init (&attr);
  38. if (ret != 0)
  39. LOG4CPLUS_THROW_RTE ("PthreadMutexAttr::PthreadMutexAttr");
  40. }
  41. ~PthreadMutexAttr ()
  42. {
  43. try
  44. {
  45. int ret = pthread_mutexattr_destroy (&attr);
  46. if (ret != 0)
  47. LOG4CPLUS_THROW_RTE ("PthreadMutexAttr::~PthreadMutexAttr");
  48. }
  49. catch (...)
  50. { }
  51. }
  52. void
  53. set_type (log4cplus::thread::Mutex::Type t)
  54. {
  55. int mutex_type;
  56. switch (t)
  57. {
  58. case log4cplus::thread::Mutex::RECURSIVE:
  59. mutex_type = PTHREAD_MUTEX_RECURSIVE;
  60. break;
  61. default:
  62. mutex_type = PTHREAD_MUTEX_DEFAULT;
  63. }
  64. int ret = pthread_mutexattr_settype (&attr, mutex_type);
  65. if (ret != 0)
  66. LOG4CPLUS_THROW_RTE ("PthreadMutexAttr::set_type");
  67. }
  68. pthread_mutexattr_t attr;
  69. };
  70. //
  71. //
  72. //
  73. inline
  74. Mutex::Mutex (log4cplus::thread::Mutex::Type t)
  75. {
  76. PthreadMutexAttr attr;
  77. attr.set_type (t);
  78. int ret = pthread_mutex_init (&mtx, &attr.attr);
  79. if (ret != 0)
  80. LOG4CPLUS_THROW_RTE ("Mutex::Mutex");
  81. }
  82. inline
  83. Mutex::~Mutex ()
  84. {
  85. try
  86. {
  87. int ret = pthread_mutex_destroy (&mtx);
  88. if (ret != 0)
  89. LOG4CPLUS_THROW_RTE ("Mutex::~Mutex");
  90. }
  91. catch (...)
  92. { }
  93. }
  94. inline
  95. void
  96. Mutex::lock () const
  97. {
  98. int ret = pthread_mutex_lock (&mtx);
  99. if (ret != 0)
  100. LOG4CPLUS_THROW_RTE ("Mutex::lock");
  101. }
  102. inline
  103. void
  104. Mutex::unlock () const
  105. {
  106. int ret = pthread_mutex_unlock (&mtx);
  107. if (ret != 0)
  108. LOG4CPLUS_THROW_RTE ("Mutex::unlock");
  109. }
  110. //
  111. //
  112. //
  113. inline
  114. Semaphore::Semaphore (unsigned max, unsigned initial)
  115. {
  116. unsigned const sem_value_max =
  117. #if defined (SEM_VALUE_MAX)
  118. SEM_VALUE_MAX
  119. #else
  120. (std::numeric_limits<int>::max) ()
  121. #endif
  122. ;
  123. unsigned const limited_max = (std::min) (max, sem_value_max);
  124. unsigned const limited_initial = (std::min) (initial, limited_max);
  125. int ret = 0;
  126. #if defined (LOG4CPLUS_USE_NAMED_POSIX_SEMAPHORE)
  127. std::ostringstream oss;
  128. char size_check[2 * (static_cast<int>(sizeof (std::ptrdiff_t))
  129. - static_cast<int>(sizeof (this))) + 1];
  130. (void)size_check;
  131. oss << getpid () << "-" << reinterpret_cast<std::ptrdiff_t>(this);
  132. std::string name (oss.str ());
  133. sem = sem_open (name.c_str (), O_CREAT, S_IRWXU | S_IRWXG, limited_max);
  134. ret = sem == SEM_FAILED;
  135. if (ret != 0)
  136. LOG4CPLUS_THROW_RTE ("Semaphore::Semaphore");
  137. try
  138. {
  139. // Unlink the semaphore early to simulate anonymous semaphore.
  140. ret = sem_unlink (name.c_str ());
  141. if (ret != 0)
  142. LOG4CPLUS_THROW_RTE ("Semaphore::Semaphore");
  143. }
  144. catch (std::runtime_error const &)
  145. {
  146. ret = sem_close (sem);
  147. if (ret != 0)
  148. LOG4CPLUS_THROW_RTE ("Semaphore::~Semaphore");
  149. throw;
  150. }
  151. #else
  152. ret = sem_init (&sem, 0, limited_max);
  153. if (ret != 0)
  154. LOG4CPLUS_THROW_RTE ("Semaphore::Semaphore");
  155. #endif
  156. try
  157. {
  158. for (unsigned i = limited_initial; i < limited_max; ++i)
  159. lock ();
  160. }
  161. catch (std::runtime_error const &)
  162. {
  163. #if defined (LOG4CPLUS_USE_NAMED_POSIX_SEMAPHORE)
  164. ret = sem_close (sem);
  165. #else
  166. ret = sem_destroy (&sem);
  167. #endif
  168. if (ret != 0)
  169. LOG4CPLUS_THROW_RTE ("Semaphore::~Semaphore");
  170. throw;
  171. }
  172. }
  173. inline
  174. Semaphore::~Semaphore ()
  175. {
  176. try
  177. {
  178. int ret = 0;
  179. #if defined (LOG4CPLUS_USE_NAMED_POSIX_SEMAPHORE)
  180. ret = sem_close (sem);
  181. #else
  182. ret = sem_destroy (&sem);
  183. #endif
  184. if (ret != 0)
  185. LOG4CPLUS_THROW_RTE ("Semaphore::~Semaphore");
  186. }
  187. catch (...)
  188. { }
  189. }
  190. inline
  191. void
  192. Semaphore::unlock () const
  193. {
  194. #if defined (LOG4CPLUS_USE_NAMED_POSIX_SEMAPHORE)
  195. int ret = sem_post (sem);
  196. #else
  197. int ret = sem_post (&sem);
  198. #endif
  199. if (ret != 0)
  200. LOG4CPLUS_THROW_RTE ("Semaphore::unlock");
  201. }
  202. inline
  203. void
  204. Semaphore::lock () const
  205. {
  206. #if defined (LOG4CPLUS_USE_NAMED_POSIX_SEMAPHORE)
  207. int ret = sem_wait (sem);
  208. #else
  209. int ret = sem_wait (&sem);
  210. #endif
  211. if (ret != 0)
  212. LOG4CPLUS_THROW_RTE ("Semaphore::lock");
  213. }
  214. //
  215. //
  216. //
  217. inline
  218. FairMutex::FairMutex ()
  219. : sem (1, 1)
  220. { }
  221. inline
  222. FairMutex::~FairMutex ()
  223. { }
  224. inline
  225. void
  226. FairMutex::lock () const
  227. {
  228. sem.lock ();
  229. }
  230. inline
  231. void
  232. FairMutex::unlock () const
  233. {
  234. sem.unlock ();
  235. }
  236. //
  237. //
  238. //
  239. inline
  240. ManualResetEvent::ManualResetEvent (bool sig)
  241. : mtx (log4cplus::thread::Mutex::DEFAULT)
  242. , sigcount (0)
  243. , signaled (sig)
  244. {
  245. int ret = pthread_cond_init (&cv, 0);
  246. if (ret != 0)
  247. LOG4CPLUS_THROW_RTE ("ManualResetEvent::ManualResetEvent");
  248. }
  249. inline
  250. ManualResetEvent::~ManualResetEvent ()
  251. {
  252. try
  253. {
  254. int ret = pthread_cond_destroy (&cv);
  255. if (ret != 0)
  256. LOG4CPLUS_THROW_RTE ("ManualResetEvent::~ManualResetEvent");
  257. }
  258. catch (...)
  259. { }
  260. }
  261. inline
  262. void
  263. ManualResetEvent::signal () const
  264. {
  265. MutexGuard mguard (mtx);
  266. signaled = true;
  267. sigcount += 1;
  268. int ret = pthread_cond_broadcast (&cv);
  269. if (ret != 0)
  270. LOG4CPLUS_THROW_RTE ("ManualResetEVent::signal");
  271. }
  272. inline
  273. void
  274. ManualResetEvent::wait () const
  275. {
  276. MutexGuard mguard (mtx);
  277. if (! signaled)
  278. {
  279. unsigned prev_count = sigcount;
  280. do
  281. {
  282. int ret = pthread_cond_wait (&cv, &mtx.mtx);
  283. if (ret != 0)
  284. {
  285. mguard.unlock ();
  286. mguard.detach ();
  287. LOG4CPLUS_THROW_RTE ("ManualResetEvent::wait");
  288. }
  289. }
  290. while (prev_count == sigcount);
  291. }
  292. }
  293. inline
  294. bool
  295. ManualResetEvent::timed_wait (unsigned long msec) const
  296. {
  297. MutexGuard mguard (mtx);
  298. if (! signaled)
  299. {
  300. helpers::Time const wakeup_time (helpers::Time::gettimeofday ()
  301. + helpers::Time (msec / 1000, (msec % 1000) * 1000));
  302. struct timespec const ts = {wakeup_time.sec (),
  303. wakeup_time.usec () * 1000};
  304. unsigned prev_count = sigcount;
  305. do
  306. {
  307. int ret = pthread_cond_timedwait (&cv, &mtx.mtx, &ts);
  308. switch (ret)
  309. {
  310. case 0:
  311. break;
  312. case ETIMEDOUT:
  313. return false;
  314. default:
  315. mguard.unlock ();
  316. mguard.detach ();
  317. LOG4CPLUS_THROW_RTE ("ManualResetEvent::timed_wait");
  318. }
  319. }
  320. while (prev_count == sigcount);
  321. }
  322. return true;
  323. }
  324. inline
  325. void
  326. ManualResetEvent::reset () const
  327. {
  328. MutexGuard mguard (mtx);
  329. signaled = false;
  330. }
  331. //
  332. //
  333. //
  334. #if defined (LOG4CPLUS_POOR_MANS_SHAREDMUTEX)
  335. #include "log4cplus/thread/impl/syncprims-pmsm.h"
  336. #else
  337. inline
  338. SharedMutex::SharedMutex ()
  339. {
  340. int ret = pthread_rwlock_init (&rwl, 0);
  341. if (ret != 0)
  342. LOG4CPLUS_THROW_RTE ("SharedMutex::SharedMutex");
  343. }
  344. inline
  345. SharedMutex::~SharedMutex ()
  346. {
  347. try
  348. {
  349. int ret = pthread_rwlock_destroy (&rwl);
  350. if (ret != 0)
  351. LOG4CPLUS_THROW_RTE ("SharedMutex::~SharedMutex");
  352. }
  353. catch (...)
  354. { }
  355. }
  356. inline
  357. void
  358. SharedMutex::rdlock () const
  359. {
  360. int ret;
  361. do
  362. {
  363. ret = pthread_rwlock_rdlock (&rwl);
  364. switch (ret)
  365. {
  366. case EAGAIN:
  367. // The read lock could not be acquired because the maximum
  368. // number of read locks for rwlock has been exceeded.
  369. log4cplus::thread::yield ();
  370. // Fall through.
  371. case 0:
  372. break;
  373. default:
  374. LOG4CPLUS_THROW_RTE ("SharedMutex::rdlock");
  375. }
  376. }
  377. while (ret != 0);
  378. }
  379. inline
  380. void
  381. SharedMutex::rdunlock () const
  382. {
  383. unlock ();
  384. }
  385. inline
  386. void
  387. SharedMutex::wrlock () const
  388. {
  389. int ret = pthread_rwlock_wrlock (&rwl);
  390. if (ret != 0)
  391. LOG4CPLUS_THROW_RTE ("SharedMutex::wrlock");
  392. }
  393. inline
  394. void
  395. SharedMutex::wrunlock () const
  396. {
  397. unlock ();
  398. }
  399. inline
  400. void
  401. SharedMutex::unlock () const
  402. {
  403. int ret = pthread_rwlock_unlock (&rwl);
  404. if (ret != 0)
  405. LOG4CPLUS_THROW_RTE ("SharedMutex::unlock");
  406. }
  407. #endif
  408. } } } // namespace log4cplus { namespace thread { namespace impl {