/src/EXTMonitor.cpp

http://github.com/digego/extempore · C++ · 114 lines · 59 code · 21 blank · 34 comment · 2 complexity · 6c26457c8a33b9c5f0ff95751dd8539f MD5 · raw file

  1. /*
  2. * Copyright (c) 2011, Andrew Sorensen
  3. *
  4. * All rights reserved.
  5. *
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. *
  17. * Neither the name of the authors nor other contributors may be used to endorse
  18. * or promote products derived from this software without specific prior written
  19. * permission.
  20. *
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLEXTD. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. */
  35. #include <iostream>
  36. #include "EXTMonitor.h"
  37. #define _EXTMONITOR_DEBUG_
  38. namespace extemp
  39. {
  40. EXTMonitor::EXTMonitor(std::string _name) :
  41. mutex(_name),
  42. name(_name)
  43. {
  44. initialised = false;
  45. init();
  46. }
  47. EXTMonitor::~EXTMonitor()
  48. {
  49. destroy();
  50. }
  51. void EXTMonitor::init()
  52. {
  53. if (! initialised)
  54. {
  55. mutex.init();
  56. condition.init();
  57. initialised = true;
  58. }
  59. }
  60. void EXTMonitor::destroy()
  61. {
  62. if (initialised)
  63. {
  64. initialised = false;
  65. mutex.destroy();
  66. condition.destroy();
  67. }
  68. }
  69. int EXTMonitor::lock()
  70. {
  71. int result = mutex.lock();
  72. return result;
  73. }
  74. int EXTMonitor::unlock()
  75. {
  76. int result = mutex.unlock();
  77. return result;
  78. }
  79. int EXTMonitor::wait()
  80. {
  81. int result = condition.wait(&mutex);
  82. return result;
  83. }
  84. int EXTMonitor::signal()
  85. {
  86. int result = condition.signal(&mutex);
  87. return result;
  88. }
  89. bool EXTMonitor::isOwnedByCurrentThread()
  90. {
  91. return mutex.isOwnedByCurrentThread();
  92. }
  93. }