lock.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJPP_LOCK_HPP__
  20. #define __PJPP_LOCK_HPP__
  21. #include <pj++/types.hpp>
  22. #include <pj/lock.h>
  23. #include <pj++/pool.hpp>
  24. //////////////////////////////////////////////////////////////////////////////
  25. // Lock object.
  26. //
  27. class Pj_Lock : public Pj_Object
  28. {
  29. public:
  30. //
  31. // Constructor.
  32. //
  33. explicit Pj_Lock(pj_lock_t *lock)
  34. : lock_(lock)
  35. {
  36. }
  37. //
  38. // Destructor.
  39. //
  40. ~Pj_Lock()
  41. {
  42. if (lock_)
  43. pj_lock_destroy(lock_);
  44. }
  45. //
  46. // Get pjlib compatible lock object.
  47. //
  48. pj_lock_t *pj_lock_t_()
  49. {
  50. return lock_;
  51. }
  52. //
  53. // acquire lock.
  54. //
  55. pj_status_t acquire()
  56. {
  57. return pj_lock_acquire(lock_);
  58. }
  59. //
  60. // release lock,.
  61. //
  62. pj_status_t release()
  63. {
  64. return pj_lock_release(lock_);
  65. }
  66. protected:
  67. pj_lock_t *lock_;
  68. };
  69. //////////////////////////////////////////////////////////////////////////////
  70. // Null lock object.
  71. //
  72. class Pj_Null_Lock : public Pj_Lock
  73. {
  74. public:
  75. //
  76. // Default constructor.
  77. //
  78. explicit Pj_Null_Lock(Pj_Pool *pool, const char *name = NULL)
  79. : Pj_Lock(NULL)
  80. {
  81. pj_lock_create_null_mutex(pool->pool_(), name, &lock_);
  82. }
  83. };
  84. //////////////////////////////////////////////////////////////////////////////
  85. // Simple mutex lock object.
  86. //
  87. class Pj_Simple_Mutex_Lock : public Pj_Lock
  88. {
  89. public:
  90. //
  91. // Default constructor.
  92. //
  93. explicit Pj_Simple_Mutex_Lock(Pj_Pool *pool, const char *name = NULL)
  94. : Pj_Lock(NULL)
  95. {
  96. pj_lock_create_simple_mutex(pool->pool_(), name, &lock_);
  97. }
  98. };
  99. //////////////////////////////////////////////////////////////////////////////
  100. // Recursive mutex lock object.
  101. //
  102. class Pj_Recursive_Mutex_Lock : public Pj_Lock
  103. {
  104. public:
  105. //
  106. // Default constructor.
  107. //
  108. explicit Pj_Recursive_Mutex_Lock(Pj_Pool *pool, const char *name = NULL)
  109. : Pj_Lock(NULL)
  110. {
  111. pj_lock_create_recursive_mutex(pool->pool_(), name, &lock_);
  112. }
  113. };
  114. //////////////////////////////////////////////////////////////////////////////
  115. // Semaphore lock object.
  116. //
  117. class Pj_Semaphore_Lock : public Pj_Lock
  118. {
  119. public:
  120. //
  121. // Default constructor.
  122. //
  123. explicit Pj_Semaphore_Lock(Pj_Pool *pool,
  124. unsigned max=PJ_MAXINT32,
  125. unsigned initial=0,
  126. const char *name=NULL)
  127. : Pj_Lock(NULL)
  128. {
  129. pj_lock_create_semaphore(pool->pool_(), name, initial, max, &lock_);
  130. }
  131. };
  132. #endif /* __PJPP_LOCK_HPP__ */