_mutex_pthreads_simple.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*! ========================================================================
  2. ** Extended Template and Library
  3. ** Mutex Abstraction Class Implementation
  4. **
  5. ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
  6. ** Copyright (c) 2008 Chris Moore
  7. **
  8. ** This package is free software; you can redistribute it and/or
  9. ** modify it under the terms of the GNU General Public License as
  10. ** published by the Free Software Foundation; either version 2 of
  11. ** the License, or (at your option) any later version.
  12. **
  13. ** This package is distributed in the hope that it will be useful,
  14. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. ** General Public License for more details.
  17. **
  18. ** === N O T E S ===========================================================
  19. **
  20. ** This is an internal header file, included by other ETL headers.
  21. ** You should not attempt to use it directly.
  22. **
  23. ** ========================================================================= */
  24. #ifndef __ETL__MUTEX_PTHREADS_SIMPLE_H_
  25. #define __ETL__MUTEX_PTHREADS_SIMPLE_H_
  26. #include <pthread.h>
  27. _ETL_BEGIN_NAMESPACE
  28. class mutex
  29. {
  30. pthread_mutex_t mtx;
  31. public:
  32. mutex()
  33. {
  34. pthread_mutex_init(&mtx, NULL);
  35. }
  36. ~mutex()
  37. {
  38. pthread_mutex_destroy(&mtx);
  39. }
  40. void lock_mutex()
  41. {
  42. pthread_mutex_lock(&mtx);
  43. }
  44. void unlock_mutex()
  45. {
  46. pthread_mutex_unlock(&mtx);
  47. }
  48. // Exception-safe mutex lock class
  49. class lock
  50. {
  51. mutex *_mtx;
  52. public:
  53. lock(mutex &x): _mtx(&x)
  54. {
  55. _mtx->lock_mutex();
  56. }
  57. ~lock()
  58. {
  59. _mtx->unlock_mutex();
  60. }
  61. };
  62. };
  63. _ETL_END_NAMESPACE
  64. #endif