Skip to content

Commit bd615b4

Browse files
committed
CommandQueueMT: Reduce lock contention in cases of single flusher
1 parent 8fb61ce commit bd615b4

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
@@ -111,6 +111,7 @@ class CommandQueueMT {
111111

112112
static const uint32_t DEFAULT_COMMAND_MEM_SIZE_KB = 64;
113113

114+
bool unique_flusher = false;
114115
BinaryMutex mutex;
115116
LocalVector<uint8_t> command_mem;
116117
ConditionVariable sync_cond_var;
@@ -179,9 +180,17 @@ class CommandQueueMT {
179180
// invalidating the pointer due to reallocs.
180181
memcpy(cmd_backup, (char *)cmd, size);
181182

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

186195
// Handle potential realloc due to the command and unlock allowance.
187196
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)