123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- #include "command_queue_mt.h"
- #include "core/os/os.h"
- void CommandQueueMT::lock() {
- if (mutex)
- mutex->lock();
- }
- void CommandQueueMT::unlock() {
- if (mutex)
- mutex->unlock();
- }
- void CommandQueueMT::wait_for_flush() {
-
- OS::get_singleton()->delay_usec(1000);
- }
- CommandQueueMT::SyncSemaphore *CommandQueueMT::_alloc_sync_sem() {
- int idx = -1;
- while (true) {
- lock();
- for (int i = 0; i < SYNC_SEMAPHORES; i++) {
- if (!sync_sems[i].in_use) {
- sync_sems[i].in_use = true;
- idx = i;
- break;
- }
- }
- unlock();
- if (idx == -1) {
- wait_for_flush();
- } else {
- break;
- }
- }
- return &sync_sems[idx];
- }
- bool CommandQueueMT::dealloc_one() {
- tryagain:
- if (dealloc_ptr == write_ptr) {
-
- return false;
- }
- uint32_t size = *(uint32_t *)&command_mem[dealloc_ptr];
- if (size == 0) {
-
- dealloc_ptr = 0;
- goto tryagain;
- }
- if (size & 1) {
-
- return false;
- }
- dealloc_ptr += (size >> 1) + sizeof(uint32_t);
- return true;
- }
- CommandQueueMT::CommandQueueMT(bool p_sync) {
- read_ptr = 0;
- write_ptr = 0;
- dealloc_ptr = 0;
- mutex = Mutex::create();
- for (int i = 0; i < SYNC_SEMAPHORES; i++) {
- sync_sems[i].sem = Semaphore::create();
- sync_sems[i].in_use = false;
- }
- if (p_sync)
- sync = Semaphore::create();
- else
- sync = NULL;
- }
- CommandQueueMT::~CommandQueueMT() {
- if (sync)
- memdelete(sync);
- memdelete(mutex);
- for (int i = 0; i < SYNC_SEMAPHORES; i++) {
- memdelete(sync_sems[i].sem);
- }
- }
|