semaphore_windows.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*************************************************************************/
  2. /* semaphore_windows.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "semaphore_windows.h"
  31. #if defined(WINDOWS_ENABLED)
  32. #include "os/memory.h"
  33. Error SemaphoreWindows::wait() {
  34. WaitForSingleObjectEx(semaphore, INFINITE, false);
  35. return OK;
  36. }
  37. Error SemaphoreWindows::post() {
  38. ReleaseSemaphore(semaphore, 1, NULL);
  39. return OK;
  40. }
  41. int SemaphoreWindows::get() const {
  42. long previous;
  43. switch (WaitForSingleObjectEx(semaphore, 0, false)) {
  44. case WAIT_OBJECT_0: {
  45. ERR_FAIL_COND_V(!ReleaseSemaphore(semaphore, 1, &previous), -1);
  46. return previous + 1;
  47. } break;
  48. case WAIT_TIMEOUT: {
  49. return 0;
  50. } break;
  51. default: {}
  52. }
  53. ERR_FAIL_V(-1);
  54. }
  55. Semaphore *SemaphoreWindows::create_semaphore_windows() {
  56. return memnew(SemaphoreWindows);
  57. }
  58. void SemaphoreWindows::make_default() {
  59. create_func = create_semaphore_windows;
  60. }
  61. SemaphoreWindows::SemaphoreWindows() {
  62. #ifdef UWP_ENABLED
  63. semaphore = CreateSemaphoreEx(
  64. NULL,
  65. 0,
  66. 0xFFFFFFF, //wathever
  67. NULL,
  68. 0,
  69. SEMAPHORE_ALL_ACCESS);
  70. #else
  71. semaphore = CreateSemaphore(
  72. NULL,
  73. 0,
  74. 0xFFFFFFF, //wathever
  75. NULL);
  76. #endif
  77. }
  78. SemaphoreWindows::~SemaphoreWindows() {
  79. CloseHandle(semaphore);
  80. }
  81. #endif