Condition.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/Condition.h"
  24. #ifdef _WIN32
  25. #include <windows.h>
  26. #else
  27. #include <pthread.h>
  28. #endif
  29. namespace Atomic
  30. {
  31. #ifdef _WIN32
  32. Condition::Condition() :
  33. event_(0)
  34. {
  35. event_ = CreateEvent(0, FALSE, FALSE, 0);
  36. }
  37. Condition::~Condition()
  38. {
  39. CloseHandle((HANDLE)event_);
  40. event_ = 0;
  41. }
  42. void Condition::Set()
  43. {
  44. SetEvent((HANDLE)event_);
  45. }
  46. void Condition::Wait()
  47. {
  48. WaitForSingleObject((HANDLE)event_, INFINITE);
  49. }
  50. #else
  51. Condition::Condition() :
  52. mutex_(new pthread_mutex_t),
  53. event_(new pthread_cond_t)
  54. {
  55. pthread_mutex_init((pthread_mutex_t*)mutex_, 0);
  56. pthread_cond_init((pthread_cond_t*)event_, 0);
  57. }
  58. Condition::~Condition()
  59. {
  60. pthread_cond_t* cond = (pthread_cond_t*)event_;
  61. pthread_mutex_t* mutex = (pthread_mutex_t*)mutex_;
  62. pthread_cond_destroy(cond);
  63. pthread_mutex_destroy(mutex);
  64. delete cond;
  65. delete mutex;
  66. event_ = 0;
  67. mutex_ = 0;
  68. }
  69. void Condition::Set()
  70. {
  71. pthread_cond_signal((pthread_cond_t*)event_);
  72. }
  73. void Condition::Wait()
  74. {
  75. pthread_cond_t* cond = (pthread_cond_t*)event_;
  76. pthread_mutex_t* mutex = (pthread_mutex_t*)mutex_;
  77. pthread_mutex_lock(mutex);
  78. pthread_cond_wait(cond, mutex);
  79. pthread_mutex_unlock(mutex);
  80. }
  81. #endif
  82. }