Signal.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  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 "Signal.h"
  25. #ifdef WIN32
  26. #include <windows.h>
  27. #else
  28. #include <pthread.h>
  29. #endif
  30. #ifdef WIN32
  31. Signal::Signal() :
  32. event_(0)
  33. {
  34. event_ = CreateEvent(0, FALSE, FALSE, 0);
  35. }
  36. Signal::~Signal()
  37. {
  38. CloseHandle((HANDLE)event_);
  39. event_ = 0;
  40. }
  41. void Signal::Set()
  42. {
  43. SetEvent((HANDLE)event_);
  44. }
  45. void Signal::Wait()
  46. {
  47. WaitForSingleObject((HANDLE)event_, INFINITE);
  48. }
  49. #else
  50. Signal::Signal() :
  51. mutex_(new pthread_mutex_t),
  52. event_(new pthread_cond_t)
  53. {
  54. pthread_mutex_init((pthread_mutex_t*)mutex_, 0);
  55. pthread_cond_init((pthread_cond_t*)event_, 0);
  56. }
  57. Signal::~Signal()
  58. {
  59. pthread_cond_t* cond = (pthread_cond_t*)event_;
  60. pthread_mutex_t* mutex = (pthread_mutex_t*)mutex_;
  61. pthread_cond_destroy(cond);
  62. pthread_mutex_destroy(mutex);
  63. delete cond;
  64. delete mutex;
  65. event_ = 0;
  66. mutex_ = 0;
  67. }
  68. void Signal::Set()
  69. {
  70. pthread_cond_signal((pthread_cond_t*)event_);
  71. }
  72. void Signal::Wait()
  73. {
  74. pthread_cond_t* cond = (pthread_cond_t*)event_;
  75. pthread_mutex_t* mutex = (pthread_mutex_t*)mutex_;
  76. pthread_mutex_lock(mutex);
  77. pthread_cond_wait(cond, mutex);
  78. pthread_mutex_unlock(mutex);
  79. }
  80. #endif