Mutex 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* -*-c++-*- OpenThreads library, Copyright (C) 2002 - 2007 The Open Thread Group
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. //
  14. // Mutex - C++ mutex class
  15. // ~~~~~
  16. //
  17. #ifndef _OPENTHREADS_MUTEX_
  18. #define _OPENTHREADS_MUTEX_
  19. #include <OpenThreads/Exports>
  20. namespace OpenThreads {
  21. /**
  22. * @class Mutex
  23. * @brief This class provides an object-oriented thread mutex interface.
  24. */
  25. class OPENTHREAD_EXPORT_DIRECTIVE Mutex {
  26. friend class Condition;
  27. public:
  28. enum MutexType
  29. {
  30. MUTEX_NORMAL,
  31. MUTEX_RECURSIVE
  32. };
  33. /**
  34. * Constructor
  35. */
  36. Mutex(MutexType type=MUTEX_NORMAL);
  37. /**
  38. * Destructor
  39. */
  40. virtual ~Mutex();
  41. MutexType getMutexType() const { return _mutexType; }
  42. /**
  43. * Lock the mutex
  44. *
  45. * @return 0 if normal, -1 if errno set, errno code otherwise.
  46. */
  47. virtual int lock();
  48. /**
  49. * Unlock the mutex
  50. *
  51. * @return 0 if normal, -1 if errno set, errno code otherwise.
  52. */
  53. virtual int unlock();
  54. /**
  55. * Test if mutex can be locked.
  56. *
  57. * @return 0 if normal, -1 if errno set, errno code otherwise.
  58. */
  59. virtual int trylock();
  60. private:
  61. /**
  62. * Private copy constructor, to prevent tampering.
  63. */
  64. Mutex(const Mutex &/*m*/) {};
  65. /**
  66. * Private copy assignment, to prevent tampering.
  67. */
  68. Mutex &operator=(const Mutex &/*m*/) {return *(this);};
  69. /**
  70. * Implementation-specific private data.
  71. */
  72. void *_prvData;
  73. MutexType _mutexType;
  74. };
  75. }
  76. #endif // _OPENTHREADS_MUTEX_