Skip to content

Commit 352150c

Browse files
committed
CommandQueueMT: Release the lock during commands for single-flusher usages
1 parent cb3af5a commit 352150c

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

core/templates/command_queue_mt.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030

3131
#include "command_queue_mt.h"
3232

33-
CommandQueueMT::CommandQueueMT() {
33+
CommandQueueMT::CommandQueueMT(bool p_unique_flusher) :
34+
unique_flusher(p_unique_flusher) {
3435
command_mem.reserve(DEFAULT_COMMAND_MEM_SIZE_KB * 1024);
3536
}
3637

core/templates/command_queue_mt.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class CommandQueueMT {
105105

106106
static const uint32_t DEFAULT_COMMAND_MEM_SIZE_KB = 64;
107107

108+
bool unique_flusher = false;
108109
BinaryMutex mutex;
109110
LocalVector<uint8_t> command_mem;
110111
ConditionVariable sync_cond_var;
@@ -154,29 +155,38 @@ class CommandQueueMT {
154155
}
155156

156157
void _flush() {
158+
MutexLock lock(mutex);
159+
157160
if (unlikely(flush_read_ptr)) {
158161
// Re-entrant call.
159162
return;
160163
}
161164

162-
MutexLock lock(mutex);
163-
164165
while (flush_read_ptr < command_mem.size()) {
165166
uint64_t size = *(uint64_t *)&command_mem[flush_read_ptr];
166167
flush_read_ptr += 8;
167168
CommandBase *cmd = reinterpret_cast<CommandBase *>(&command_mem[flush_read_ptr]);
168-
uint32_t allowance_id = WorkerThreadPool::thread_enter_unlock_allowance_zone(lock);
169-
cmd->call();
170-
WorkerThreadPool::thread_exit_unlock_allowance_zone(allowance_id);
169+
170+
if (unique_flusher) {
171+
// A single thread will pump; the lock is only needed for the command queue itself.
172+
lock.temp_unlock();
173+
cmd->call();
174+
lock.temp_relock();
175+
} else {
176+
// At least we can unlock during WTP operations.
177+
uint32_t allowance_id = WorkerThreadPool::thread_enter_unlock_allowance_zone(lock);
178+
cmd->call();
179+
WorkerThreadPool::thread_exit_unlock_allowance_zone(allowance_id);
180+
}
171181

172182
// Handle potential realloc due to the command and unlock allowance.
173183
cmd = reinterpret_cast<CommandBase *>(&command_mem[flush_read_ptr]);
174184

175185
if (unlikely(cmd->sync)) {
176186
sync_head++;
177-
lock.~MutexLock(); // Give an opportunity to awaiters right away.
187+
lock.temp_unlock(); // Give an opportunity to awaiters right away.
178188
sync_cond_var.notify_all();
179-
new (&lock) MutexLock(mutex);
189+
lock.temp_relock();
180190
// Handle potential realloc happened during unlock.
181191
cmd = reinterpret_cast<CommandBase *>(&command_mem[flush_read_ptr]);
182192
}
@@ -252,6 +262,6 @@ class CommandQueueMT {
252262
pump_task_id = p_task_id;
253263
}
254264

255-
CommandQueueMT();
265+
CommandQueueMT(bool p_unique_flusher);
256266
~CommandQueueMT();
257267
};

modules/betsy/image_compress_betsy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Error _betsy_compress_s3tc(Image *r_img, Image::UsedChannels p_channels);
103103
class BetsyCompressor : public Object {
104104
GDSOFTCLASS(BetsyCompressor, Object);
105105

106-
mutable CommandQueueMT command_queue;
106+
mutable CommandQueueMT command_queue = CommandQueueMT(true);
107107
bool exit = false;
108108
WorkerThreadPool::TaskID task_id = WorkerThreadPool::INVALID_TASK_ID;
109109

servers/rendering/rendering_server_default.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class RenderingServerDefault : public RenderingServer {
7474
uint64_t print_frame_profile_ticks_from = 0;
7575
uint32_t print_frame_profile_frame_count = 0;
7676

77-
mutable CommandQueueMT command_queue;
77+
mutable CommandQueueMT command_queue = CommandQueueMT(true);
7878

7979
Thread::ID server_thread = Thread::MAIN_ID;
8080
WorkerThreadPool::TaskID server_task_id = WorkerThreadPool::INVALID_TASK_ID;

0 commit comments

Comments
 (0)