/src/EXTCondition.cpp

http://github.com/digego/extempore · C++ · 150 lines · 87 code · 28 blank · 35 comment · 5 complexity · 2feab6e8e2f5c46318b03c1d410ae8eb 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. #ifdef EXT_BOOST
  37. #else
  38. #include "pthread.h"
  39. #endif
  40. #include "EXTCondition.h"
  41. #include "EXTMutex.h"
  42. #define EXTCONDITION_DEBUG
  43. namespace extemp
  44. {
  45. EXTCondition::EXTCondition()
  46. {
  47. initialised = false;
  48. }
  49. EXTCondition::~EXTCondition()
  50. {
  51. destroy();
  52. }
  53. int EXTCondition::init()
  54. {
  55. #ifdef EXT_BOOST
  56. //boost_cond = boost::condition_variable;
  57. int result = 0;
  58. #else
  59. int result = pthread_cond_init(&pthread_cond, NULL);
  60. #endif
  61. initialised = ! result;
  62. #ifdef _EXTCONDITION_DEBUG_
  63. if (result)
  64. {
  65. std::cerr << "Error initialising condition: " << result << std::endl;
  66. }
  67. #endif
  68. return result;
  69. }
  70. void EXTCondition::destroy()
  71. {
  72. int result = 0;
  73. if (initialised)
  74. {
  75. initialised = false;
  76. #ifdef EXT_BOOST
  77. result = 0;
  78. #else
  79. result = pthread_cond_destroy(&pthread_cond);
  80. #endif
  81. }
  82. #ifdef _EXTCONDITION_DEBUG_
  83. if (result)
  84. {
  85. std::cerr << "Error destroying condition: " << result << std::endl;
  86. }
  87. #endif
  88. }
  89. int EXTCondition::wait(EXTMutex *aimeMutex)
  90. {
  91. #ifdef EXT_BOOST
  92. boost::recursive_mutex::scoped_lock lock(aimeMutex->bmutex);
  93. boost_cond.wait(lock); //aimeMutex->block); // bmutex.scoped_lock); //lock);
  94. int result = 0;
  95. #else
  96. int result = pthread_cond_wait(&pthread_cond, &aimeMutex->pthread_mutex);
  97. #endif
  98. #ifdef _EXTCONDITION_DEBUG_
  99. if (result)
  100. {
  101. std::cerr << "Error waiting on condition: " << result << std::endl;
  102. }
  103. #endif
  104. return result;
  105. }
  106. int EXTCondition::signal(EXTMutex* aimeMutex)
  107. {
  108. #ifdef EXT_BOOST
  109. boost_cond.notify_one();
  110. int result = 0;
  111. #else
  112. int result = pthread_cond_signal(&pthread_cond);
  113. #endif
  114. #ifdef _EXTCONDITION_DEBUG_
  115. if (result)
  116. {
  117. std::cerr << "Error signalling condition: " << result << std::endl;
  118. }
  119. #endif
  120. return result;
  121. }
  122. }