Condition.cpp 2.4 KB

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