command_queue_mt.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*************************************************************************/
  2. /* command_queue_mt.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "command_queue_mt.h"
  30. #include "os/os.h"
  31. void CommandQueueMT::lock() {
  32. if (mutex)
  33. mutex->lock();
  34. }
  35. void CommandQueueMT::unlock() {
  36. if (mutex)
  37. mutex->unlock();
  38. }
  39. void CommandQueueMT::wait_for_flush() {
  40. // wait one millisecond for a flush to happen
  41. OS::get_singleton()->delay_usec(1000);
  42. }
  43. CommandQueueMT::SyncSemaphore *CommandQueueMT::_alloc_sync_sem() {
  44. int idx = -1;
  45. while (true) {
  46. for (int i = 0; i < SYNC_SEMAPHORES; i++) {
  47. if (!sync_sems[i].in_use) {
  48. sync_sems[i].in_use = true;
  49. idx = i;
  50. break;
  51. }
  52. }
  53. if (idx == -1) {
  54. wait_for_flush();
  55. } else {
  56. break;
  57. }
  58. }
  59. return &sync_sems[idx];
  60. }
  61. CommandQueueMT::CommandQueueMT(bool p_sync) {
  62. read_ptr = 0;
  63. write_ptr = 0;
  64. mutex = Mutex::create();
  65. for (int i = 0; i < SYNC_SEMAPHORES; i++) {
  66. sync_sems[i].sem = Semaphore::create();
  67. sync_sems[i].in_use = false;
  68. }
  69. if (p_sync)
  70. sync = Semaphore::create();
  71. else
  72. sync = NULL;
  73. }
  74. CommandQueueMT::~CommandQueueMT() {
  75. if (sync)
  76. memdelete(sync);
  77. memdelete(mutex);
  78. for (int i = 0; i < SYNC_SEMAPHORES; i++) {
  79. memdelete(sync_sems[i].sem);
  80. }
  81. }