2
0

command_queue_mt.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*************************************************************************/
  2. /* command_queue_mt.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "command_queue_mt.h"
  31. #include "core/os/os.h"
  32. void CommandQueueMT::lock() {
  33. mutex.lock();
  34. }
  35. void CommandQueueMT::unlock() {
  36. mutex.unlock();
  37. }
  38. void CommandQueueMT::wait_for_flush() {
  39. // wait one millisecond for a flush to happen
  40. OS::get_singleton()->delay_usec(1000);
  41. }
  42. CommandQueueMT::SyncSemaphore *CommandQueueMT::_alloc_sync_sem() {
  43. int idx = -1;
  44. while (true) {
  45. lock();
  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. unlock();
  54. if (idx == -1) {
  55. wait_for_flush();
  56. } else {
  57. break;
  58. }
  59. }
  60. return &sync_sems[idx];
  61. }
  62. bool CommandQueueMT::dealloc_one() {
  63. tryagain:
  64. if (dealloc_ptr == write_ptr) {
  65. // The queue is empty
  66. return false;
  67. }
  68. uint32_t size = *(uint32_t *)&command_mem[dealloc_ptr];
  69. if (size == 0) {
  70. // End of command buffer wrap down
  71. dealloc_ptr = 0;
  72. goto tryagain;
  73. }
  74. if (size & 1) {
  75. // Still used, nothing can be deallocated
  76. return false;
  77. }
  78. dealloc_ptr += (size >> 1) + 8;
  79. return true;
  80. }
  81. CommandQueueMT::CommandQueueMT(bool p_sync) {
  82. if (p_sync) {
  83. sync = memnew(Semaphore);
  84. }
  85. }
  86. CommandQueueMT::~CommandQueueMT() {
  87. if (sync) {
  88. memdelete(sync);
  89. }
  90. memfree(command_mem);
  91. }