Skip to content

Commit fa61594

Browse files
committed
CommandQueueMT: Reduce lock contention in cases of single flusher
1 parent e4b6e37 commit fa61594

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
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: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class CommandQueueMT {
107107

108108
static const uint32_t DEFAULT_COMMAND_MEM_SIZE_KB = 64;
109109

110+
bool unique_flusher = false;
110111
BinaryMutex mutex;
111112
LocalVector<uint8_t> command_mem;
112113
ConditionVariable sync_cond_var;
@@ -176,9 +177,17 @@ class CommandQueueMT {
176177
// invalidating the pointer due to reallocs.
177178
memcpy(cmd_backup, (char *)cmd, size);
178179

179-
uint32_t allowance_id = WorkerThreadPool::thread_enter_unlock_allowance_zone(lock);
180-
((CommandBase *)cmd_backup)->call();
181-
WorkerThreadPool::thread_exit_unlock_allowance_zone(allowance_id);
180+
if (unique_flusher) {
181+
// A single thread will pump; the lock is only needed for the command queue itself.
182+
lock.temp_unlock();
183+
((CommandBase *)cmd_backup)->call();
184+
lock.temp_relock();
185+
} else {
186+
// At least we can unlock during WTP operations.
187+
uint32_t allowance_id = WorkerThreadPool::thread_enter_unlock_allowance_zone(lock);
188+
((CommandBase *)cmd_backup)->call();
189+
WorkerThreadPool::thread_exit_unlock_allowance_zone(allowance_id);
190+
}
182191

183192
// Handle potential realloc due to the command and unlock allowance.
184193
cmd = reinterpret_cast<CommandBase *>(&command_mem[flush_read_ptr]);
@@ -266,6 +275,6 @@ class CommandQueueMT {
266275
pump_task_id = p_task_id;
267276
}
268277

269-
CommandQueueMT();
278+
CommandQueueMT(bool p_unique_flusher = false);
270279
~CommandQueueMT();
271280
};

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)