2014-02-10 09:10:30 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* command_queue_mt.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 07:50:27 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
#ifndef COMMAND_QUEUE_MT_H
|
|
|
|
#define COMMAND_QUEUE_MT_H
|
|
|
|
|
2023-12-29 08:27:17 +08:00
|
|
|
#include "core/object/worker_thread_pool.h"
|
2024-04-16 23:45:56 +08:00
|
|
|
#include "core/os/condition_variable.h"
|
2018-09-12 00:13:45 +08:00
|
|
|
#include "core/os/memory.h"
|
|
|
|
#include "core/os/mutex.h"
|
2021-06-09 23:09:31 +08:00
|
|
|
#include "core/string/print_string.h"
|
|
|
|
#include "core/templates/local_vector.h"
|
2020-11-08 06:33:38 +08:00
|
|
|
#include "core/templates/simple_type.h"
|
2018-09-12 00:13:45 +08:00
|
|
|
#include "core/typedefs.h"
|
2019-02-16 10:28:02 +08:00
|
|
|
|
2017-10-14 01:13:52 +08:00
|
|
|
#define COMMA(N) _COMMA_##N
|
|
|
|
#define _COMMA_0
|
|
|
|
#define _COMMA_1 ,
|
|
|
|
#define _COMMA_2 ,
|
|
|
|
#define _COMMA_3 ,
|
|
|
|
#define _COMMA_4 ,
|
|
|
|
#define _COMMA_5 ,
|
|
|
|
#define _COMMA_6 ,
|
|
|
|
#define _COMMA_7 ,
|
|
|
|
#define _COMMA_8 ,
|
|
|
|
#define _COMMA_9 ,
|
|
|
|
#define _COMMA_10 ,
|
|
|
|
#define _COMMA_11 ,
|
|
|
|
#define _COMMA_12 ,
|
2018-07-03 03:50:52 +08:00
|
|
|
#define _COMMA_13 ,
|
2019-06-16 10:45:24 +08:00
|
|
|
#define _COMMA_14 ,
|
|
|
|
#define _COMMA_15 ,
|
2017-10-14 01:13:52 +08:00
|
|
|
|
2018-02-22 00:30:55 +08:00
|
|
|
// 1-based comma separated list of ITEMs
|
2017-10-14 01:13:52 +08:00
|
|
|
#define COMMA_SEP_LIST(ITEM, LENGTH) _COMMA_SEP_LIST_##LENGTH(ITEM)
|
2019-06-16 10:45:24 +08:00
|
|
|
#define _COMMA_SEP_LIST_15(ITEM) \
|
|
|
|
_COMMA_SEP_LIST_14(ITEM) \
|
|
|
|
, ITEM(15)
|
|
|
|
#define _COMMA_SEP_LIST_14(ITEM) \
|
|
|
|
_COMMA_SEP_LIST_13(ITEM) \
|
|
|
|
, ITEM(14)
|
2018-07-03 03:50:52 +08:00
|
|
|
#define _COMMA_SEP_LIST_13(ITEM) \
|
|
|
|
_COMMA_SEP_LIST_12(ITEM) \
|
|
|
|
, ITEM(13)
|
2017-10-14 01:13:52 +08:00
|
|
|
#define _COMMA_SEP_LIST_12(ITEM) \
|
|
|
|
_COMMA_SEP_LIST_11(ITEM) \
|
|
|
|
, ITEM(12)
|
|
|
|
#define _COMMA_SEP_LIST_11(ITEM) \
|
|
|
|
_COMMA_SEP_LIST_10(ITEM) \
|
|
|
|
, ITEM(11)
|
|
|
|
#define _COMMA_SEP_LIST_10(ITEM) \
|
|
|
|
_COMMA_SEP_LIST_9(ITEM) \
|
|
|
|
, ITEM(10)
|
|
|
|
#define _COMMA_SEP_LIST_9(ITEM) \
|
|
|
|
_COMMA_SEP_LIST_8(ITEM) \
|
|
|
|
, ITEM(9)
|
|
|
|
#define _COMMA_SEP_LIST_8(ITEM) \
|
|
|
|
_COMMA_SEP_LIST_7(ITEM) \
|
|
|
|
, ITEM(8)
|
|
|
|
#define _COMMA_SEP_LIST_7(ITEM) \
|
|
|
|
_COMMA_SEP_LIST_6(ITEM) \
|
|
|
|
, ITEM(7)
|
|
|
|
#define _COMMA_SEP_LIST_6(ITEM) \
|
|
|
|
_COMMA_SEP_LIST_5(ITEM) \
|
|
|
|
, ITEM(6)
|
|
|
|
#define _COMMA_SEP_LIST_5(ITEM) \
|
|
|
|
_COMMA_SEP_LIST_4(ITEM) \
|
|
|
|
, ITEM(5)
|
|
|
|
#define _COMMA_SEP_LIST_4(ITEM) \
|
|
|
|
_COMMA_SEP_LIST_3(ITEM) \
|
|
|
|
, ITEM(4)
|
|
|
|
#define _COMMA_SEP_LIST_3(ITEM) \
|
|
|
|
_COMMA_SEP_LIST_2(ITEM) \
|
|
|
|
, ITEM(3)
|
|
|
|
#define _COMMA_SEP_LIST_2(ITEM) \
|
|
|
|
_COMMA_SEP_LIST_1(ITEM) \
|
|
|
|
, ITEM(2)
|
|
|
|
#define _COMMA_SEP_LIST_1(ITEM) \
|
|
|
|
_COMMA_SEP_LIST_0(ITEM) \
|
|
|
|
ITEM(1)
|
|
|
|
#define _COMMA_SEP_LIST_0(ITEM)
|
|
|
|
|
2018-02-22 00:30:55 +08:00
|
|
|
// 1-based semicolon separated list of ITEMs
|
2017-10-14 01:13:52 +08:00
|
|
|
#define SEMIC_SEP_LIST(ITEM, LENGTH) _SEMIC_SEP_LIST_##LENGTH(ITEM)
|
2019-06-16 10:45:24 +08:00
|
|
|
#define _SEMIC_SEP_LIST_15(ITEM) \
|
|
|
|
_SEMIC_SEP_LIST_14(ITEM); \
|
|
|
|
ITEM(15)
|
|
|
|
#define _SEMIC_SEP_LIST_14(ITEM) \
|
|
|
|
_SEMIC_SEP_LIST_13(ITEM); \
|
|
|
|
ITEM(14)
|
2018-07-03 03:50:52 +08:00
|
|
|
#define _SEMIC_SEP_LIST_13(ITEM) \
|
|
|
|
_SEMIC_SEP_LIST_12(ITEM); \
|
|
|
|
ITEM(13)
|
2017-10-14 01:13:52 +08:00
|
|
|
#define _SEMIC_SEP_LIST_12(ITEM) \
|
|
|
|
_SEMIC_SEP_LIST_11(ITEM); \
|
|
|
|
ITEM(12)
|
|
|
|
#define _SEMIC_SEP_LIST_11(ITEM) \
|
|
|
|
_SEMIC_SEP_LIST_10(ITEM); \
|
|
|
|
ITEM(11)
|
|
|
|
#define _SEMIC_SEP_LIST_10(ITEM) \
|
|
|
|
_SEMIC_SEP_LIST_9(ITEM); \
|
|
|
|
ITEM(10)
|
|
|
|
#define _SEMIC_SEP_LIST_9(ITEM) \
|
|
|
|
_SEMIC_SEP_LIST_8(ITEM); \
|
|
|
|
ITEM(9)
|
|
|
|
#define _SEMIC_SEP_LIST_8(ITEM) \
|
|
|
|
_SEMIC_SEP_LIST_7(ITEM); \
|
|
|
|
ITEM(8)
|
|
|
|
#define _SEMIC_SEP_LIST_7(ITEM) \
|
|
|
|
_SEMIC_SEP_LIST_6(ITEM); \
|
|
|
|
ITEM(7)
|
|
|
|
#define _SEMIC_SEP_LIST_6(ITEM) \
|
|
|
|
_SEMIC_SEP_LIST_5(ITEM); \
|
|
|
|
ITEM(6)
|
|
|
|
#define _SEMIC_SEP_LIST_5(ITEM) \
|
|
|
|
_SEMIC_SEP_LIST_4(ITEM); \
|
|
|
|
ITEM(5)
|
|
|
|
#define _SEMIC_SEP_LIST_4(ITEM) \
|
|
|
|
_SEMIC_SEP_LIST_3(ITEM); \
|
|
|
|
ITEM(4)
|
|
|
|
#define _SEMIC_SEP_LIST_3(ITEM) \
|
|
|
|
_SEMIC_SEP_LIST_2(ITEM); \
|
|
|
|
ITEM(3)
|
|
|
|
#define _SEMIC_SEP_LIST_2(ITEM) \
|
|
|
|
_SEMIC_SEP_LIST_1(ITEM); \
|
|
|
|
ITEM(2)
|
|
|
|
#define _SEMIC_SEP_LIST_1(ITEM) \
|
|
|
|
_SEMIC_SEP_LIST_0(ITEM) \
|
|
|
|
ITEM(1)
|
|
|
|
#define _SEMIC_SEP_LIST_0(ITEM)
|
|
|
|
|
2018-02-22 00:30:55 +08:00
|
|
|
// 1-based space separated list of ITEMs
|
2017-10-14 01:13:52 +08:00
|
|
|
#define SPACE_SEP_LIST(ITEM, LENGTH) _SPACE_SEP_LIST_##LENGTH(ITEM)
|
2019-06-16 10:45:24 +08:00
|
|
|
#define _SPACE_SEP_LIST_15(ITEM) \
|
|
|
|
_SPACE_SEP_LIST_14(ITEM) \
|
|
|
|
ITEM(15)
|
|
|
|
#define _SPACE_SEP_LIST_14(ITEM) \
|
|
|
|
_SPACE_SEP_LIST_13(ITEM) \
|
|
|
|
ITEM(14)
|
2018-07-03 03:50:52 +08:00
|
|
|
#define _SPACE_SEP_LIST_13(ITEM) \
|
|
|
|
_SPACE_SEP_LIST_12(ITEM) \
|
|
|
|
ITEM(13)
|
2017-10-14 01:13:52 +08:00
|
|
|
#define _SPACE_SEP_LIST_12(ITEM) \
|
|
|
|
_SPACE_SEP_LIST_11(ITEM) \
|
|
|
|
ITEM(12)
|
|
|
|
#define _SPACE_SEP_LIST_11(ITEM) \
|
|
|
|
_SPACE_SEP_LIST_10(ITEM) \
|
|
|
|
ITEM(11)
|
|
|
|
#define _SPACE_SEP_LIST_10(ITEM) \
|
|
|
|
_SPACE_SEP_LIST_9(ITEM) \
|
|
|
|
ITEM(10)
|
|
|
|
#define _SPACE_SEP_LIST_9(ITEM) \
|
|
|
|
_SPACE_SEP_LIST_8(ITEM) \
|
|
|
|
ITEM(9)
|
|
|
|
#define _SPACE_SEP_LIST_8(ITEM) \
|
|
|
|
_SPACE_SEP_LIST_7(ITEM) \
|
|
|
|
ITEM(8)
|
|
|
|
#define _SPACE_SEP_LIST_7(ITEM) \
|
|
|
|
_SPACE_SEP_LIST_6(ITEM) \
|
|
|
|
ITEM(7)
|
|
|
|
#define _SPACE_SEP_LIST_6(ITEM) \
|
|
|
|
_SPACE_SEP_LIST_5(ITEM) \
|
|
|
|
ITEM(6)
|
|
|
|
#define _SPACE_SEP_LIST_5(ITEM) \
|
|
|
|
_SPACE_SEP_LIST_4(ITEM) \
|
|
|
|
ITEM(5)
|
|
|
|
#define _SPACE_SEP_LIST_4(ITEM) \
|
|
|
|
_SPACE_SEP_LIST_3(ITEM) \
|
|
|
|
ITEM(4)
|
|
|
|
#define _SPACE_SEP_LIST_3(ITEM) \
|
|
|
|
_SPACE_SEP_LIST_2(ITEM) \
|
|
|
|
ITEM(3)
|
|
|
|
#define _SPACE_SEP_LIST_2(ITEM) \
|
|
|
|
_SPACE_SEP_LIST_1(ITEM) \
|
|
|
|
ITEM(2)
|
|
|
|
#define _SPACE_SEP_LIST_1(ITEM) \
|
|
|
|
_SPACE_SEP_LIST_0(ITEM) \
|
|
|
|
ITEM(1)
|
|
|
|
#define _SPACE_SEP_LIST_0(ITEM)
|
|
|
|
|
|
|
|
#define ARG(N) p##N
|
|
|
|
#define PARAM(N) P##N p##N
|
|
|
|
#define TYPE_PARAM(N) typename P##N
|
2024-04-24 17:41:58 +08:00
|
|
|
#define PARAM_DECL(N) GetSimpleTypeT<P##N> p##N
|
2017-10-14 01:13:52 +08:00
|
|
|
|
|
|
|
#define DECL_CMD(N) \
|
|
|
|
template <typename T, typename M COMMA(N) COMMA_SEP_LIST(TYPE_PARAM, N)> \
|
|
|
|
struct Command##N : public CommandBase { \
|
|
|
|
T *instance; \
|
|
|
|
M method; \
|
|
|
|
SEMIC_SEP_LIST(PARAM_DECL, N); \
|
2022-04-05 18:40:26 +08:00
|
|
|
virtual void call() override { \
|
2017-10-14 01:13:52 +08:00
|
|
|
(instance->*method)(COMMA_SEP_LIST(ARG, N)); \
|
|
|
|
} \
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DECL_CMD_RET(N) \
|
|
|
|
template <typename T, typename M, COMMA_SEP_LIST(TYPE_PARAM, N) COMMA(N) typename R> \
|
|
|
|
struct CommandRet##N : public SyncCommand { \
|
|
|
|
R *ret; \
|
|
|
|
T *instance; \
|
|
|
|
M method; \
|
|
|
|
SEMIC_SEP_LIST(PARAM_DECL, N); \
|
2022-04-05 18:40:26 +08:00
|
|
|
virtual void call() override { \
|
2017-10-14 01:13:52 +08:00
|
|
|
*ret = (instance->*method)(COMMA_SEP_LIST(ARG, N)); \
|
|
|
|
} \
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DECL_CMD_SYNC(N) \
|
|
|
|
template <typename T, typename M COMMA(N) COMMA_SEP_LIST(TYPE_PARAM, N)> \
|
|
|
|
struct CommandSync##N : public SyncCommand { \
|
|
|
|
T *instance; \
|
|
|
|
M method; \
|
|
|
|
SEMIC_SEP_LIST(PARAM_DECL, N); \
|
2022-04-05 18:40:26 +08:00
|
|
|
virtual void call() override { \
|
2017-10-14 01:13:52 +08:00
|
|
|
(instance->*method)(COMMA_SEP_LIST(ARG, N)); \
|
|
|
|
} \
|
|
|
|
};
|
|
|
|
|
|
|
|
#define TYPE_ARG(N) P##N
|
|
|
|
#define CMD_TYPE(N) Command##N<T, M COMMA(N) COMMA_SEP_LIST(TYPE_ARG, N)>
|
|
|
|
#define CMD_ASSIGN_PARAM(N) cmd->p##N = p##N
|
|
|
|
|
2024-04-04 16:18:08 +08:00
|
|
|
#define DECL_PUSH(N) \
|
|
|
|
template <typename T, typename M COMMA(N) COMMA_SEP_LIST(TYPE_PARAM, N)> \
|
|
|
|
void push(T *p_instance, M p_method COMMA(N) COMMA_SEP_LIST(PARAM, N)) { \
|
2024-04-16 23:45:56 +08:00
|
|
|
MutexLock mlock(mutex); \
|
|
|
|
CMD_TYPE(N) *cmd = allocate<CMD_TYPE(N)>(); \
|
2024-04-04 16:18:08 +08:00
|
|
|
cmd->instance = p_instance; \
|
|
|
|
cmd->method = p_method; \
|
|
|
|
SEMIC_SEP_LIST(CMD_ASSIGN_PARAM, N); \
|
|
|
|
if (pump_task_id != WorkerThreadPool::INVALID_TASK_ID) { \
|
|
|
|
WorkerThreadPool::get_singleton()->notify_yield_over(pump_task_id); \
|
|
|
|
} \
|
2017-10-14 01:13:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define CMD_RET_TYPE(N) CommandRet##N<T, M, COMMA_SEP_LIST(TYPE_ARG, N) COMMA(N) R>
|
|
|
|
|
|
|
|
#define DECL_PUSH_AND_RET(N) \
|
|
|
|
template <typename T, typename M, COMMA_SEP_LIST(TYPE_PARAM, N) COMMA(N) typename R> \
|
|
|
|
void push_and_ret(T *p_instance, M p_method, COMMA_SEP_LIST(PARAM, N) COMMA(N) R *r_ret) { \
|
2024-04-16 23:45:56 +08:00
|
|
|
MutexLock mlock(mutex); \
|
|
|
|
CMD_RET_TYPE(N) *cmd = allocate<CMD_RET_TYPE(N)>(); \
|
2017-10-14 01:13:52 +08:00
|
|
|
cmd->instance = p_instance; \
|
|
|
|
cmd->method = p_method; \
|
|
|
|
SEMIC_SEP_LIST(CMD_ASSIGN_PARAM, N); \
|
|
|
|
cmd->ret = r_ret; \
|
2024-04-04 16:18:08 +08:00
|
|
|
if (pump_task_id != WorkerThreadPool::INVALID_TASK_ID) { \
|
|
|
|
WorkerThreadPool::get_singleton()->notify_yield_over(pump_task_id); \
|
|
|
|
} \
|
2024-04-16 23:45:56 +08:00
|
|
|
sync_tail++; \
|
|
|
|
_wait_for_sync(mlock); \
|
2017-10-14 01:13:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define CMD_SYNC_TYPE(N) CommandSync##N<T, M COMMA(N) COMMA_SEP_LIST(TYPE_ARG, N)>
|
|
|
|
|
|
|
|
#define DECL_PUSH_AND_SYNC(N) \
|
|
|
|
template <typename T, typename M COMMA(N) COMMA_SEP_LIST(TYPE_PARAM, N)> \
|
|
|
|
void push_and_sync(T *p_instance, M p_method COMMA(N) COMMA_SEP_LIST(PARAM, N)) { \
|
2024-04-16 23:45:56 +08:00
|
|
|
MutexLock mlock(mutex); \
|
|
|
|
CMD_SYNC_TYPE(N) *cmd = allocate<CMD_SYNC_TYPE(N)>(); \
|
2017-10-14 01:13:52 +08:00
|
|
|
cmd->instance = p_instance; \
|
|
|
|
cmd->method = p_method; \
|
|
|
|
SEMIC_SEP_LIST(CMD_ASSIGN_PARAM, N); \
|
2024-04-04 16:18:08 +08:00
|
|
|
if (pump_task_id != WorkerThreadPool::INVALID_TASK_ID) { \
|
|
|
|
WorkerThreadPool::get_singleton()->notify_yield_over(pump_task_id); \
|
|
|
|
} \
|
2024-04-16 23:45:56 +08:00
|
|
|
sync_tail++; \
|
|
|
|
_wait_for_sync(mlock); \
|
2017-10-14 01:13:52 +08:00
|
|
|
}
|
|
|
|
|
2019-06-16 10:45:24 +08:00
|
|
|
#define MAX_CMD_PARAMS 15
|
2017-10-14 01:13:52 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
class CommandQueueMT {
|
|
|
|
struct CommandBase {
|
2024-04-16 23:45:56 +08:00
|
|
|
bool sync = false;
|
2014-02-10 09:10:30 +08:00
|
|
|
virtual void call() = 0;
|
2024-04-23 18:01:12 +08:00
|
|
|
virtual ~CommandBase() = default;
|
2014-02-10 09:10:30 +08:00
|
|
|
};
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2017-10-14 01:13:52 +08:00
|
|
|
struct SyncCommand : public CommandBase {
|
2024-04-16 23:45:56 +08:00
|
|
|
virtual void call() override {}
|
|
|
|
SyncCommand() {
|
|
|
|
sync = true;
|
2017-01-15 23:38:54 +08:00
|
|
|
}
|
2016-06-03 22:11:10 +08:00
|
|
|
};
|
|
|
|
|
2017-10-14 01:13:52 +08:00
|
|
|
DECL_CMD(0)
|
2019-06-16 10:45:24 +08:00
|
|
|
SPACE_SEP_LIST(DECL_CMD, 15)
|
2014-02-10 09:10:30 +08:00
|
|
|
|
Fix various typos with codespell
Found via `codespell -q 3 -S ./thirdparty,*.po,./DONORS.md -L ackward,ang,ans,ba,beng,cas,childs,childrens,dof,doubleclick,fave,findn,hist,inout,leapyear,lod,nd,numer,ois,ony,paket,seeked,sinc,switchs,te,uint`
2021-07-07 23:17:32 +08:00
|
|
|
// Commands that return.
|
2017-10-14 01:13:52 +08:00
|
|
|
DECL_CMD_RET(0)
|
2019-06-16 10:45:24 +08:00
|
|
|
SPACE_SEP_LIST(DECL_CMD_RET, 15)
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-10-14 01:13:52 +08:00
|
|
|
/* commands that don't return but sync */
|
|
|
|
DECL_CMD_SYNC(0)
|
2019-06-16 10:45:24 +08:00
|
|
|
SPACE_SEP_LIST(DECL_CMD_SYNC, 15)
|
2016-06-03 22:11:10 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
/***** BASE *******/
|
|
|
|
|
2024-05-09 01:17:08 +08:00
|
|
|
static const uint32_t DEFAULT_COMMAND_MEM_SIZE_KB = 64;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2024-04-16 23:45:56 +08:00
|
|
|
BinaryMutex mutex;
|
2021-06-09 23:09:31 +08:00
|
|
|
LocalVector<uint8_t> command_mem;
|
2024-04-16 23:45:56 +08:00
|
|
|
ConditionVariable sync_cond_var;
|
|
|
|
uint32_t sync_head = 0;
|
|
|
|
uint32_t sync_tail = 0;
|
2024-05-09 00:56:16 +08:00
|
|
|
uint32_t sync_awaiters = 0;
|
2024-04-04 16:18:08 +08:00
|
|
|
WorkerThreadPool::TaskID pump_task_id = WorkerThreadPool::INVALID_TASK_ID;
|
2023-12-29 08:27:17 +08:00
|
|
|
uint64_t flush_read_ptr = 0;
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
template <typename T>
|
|
|
|
T *allocate() {
|
|
|
|
// alloc size is size+T+safeguard
|
2021-06-09 23:09:31 +08:00
|
|
|
uint32_t alloc_size = ((sizeof(T) + 8 - 1) & ~(8 - 1));
|
|
|
|
uint64_t size = command_mem.size();
|
|
|
|
command_mem.resize(size + alloc_size + 8);
|
|
|
|
*(uint64_t *)&command_mem[size] = alloc_size;
|
|
|
|
T *cmd = memnew_placement(&command_mem[size + 8], T);
|
2014-02-10 09:10:30 +08:00
|
|
|
return cmd;
|
|
|
|
}
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2024-05-09 00:56:16 +08:00
|
|
|
_FORCE_INLINE_ void _prevent_sync_wraparound() {
|
|
|
|
bool safe_to_reset = !sync_awaiters;
|
|
|
|
bool already_sync_to_latest = sync_head == sync_tail;
|
|
|
|
if (safe_to_reset && already_sync_to_latest) {
|
|
|
|
sync_head = 0;
|
|
|
|
sync_tail = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 23:09:31 +08:00
|
|
|
void _flush() {
|
2024-04-10 15:57:33 +08:00
|
|
|
if (unlikely(flush_read_ptr)) {
|
|
|
|
// Re-entrant call.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-18 20:54:58 +08:00
|
|
|
MutexLock lock(mutex);
|
2024-04-16 23:45:56 +08:00
|
|
|
|
2023-12-29 08:27:17 +08:00
|
|
|
while (flush_read_ptr < command_mem.size()) {
|
|
|
|
uint64_t size = *(uint64_t *)&command_mem[flush_read_ptr];
|
|
|
|
flush_read_ptr += 8;
|
|
|
|
CommandBase *cmd = reinterpret_cast<CommandBase *>(&command_mem[flush_read_ptr]);
|
2024-07-18 20:54:58 +08:00
|
|
|
uint32_t allowance_id = WorkerThreadPool::thread_enter_unlock_allowance_zone(lock);
|
2023-12-29 08:27:17 +08:00
|
|
|
cmd->call();
|
2024-07-18 20:54:58 +08:00
|
|
|
WorkerThreadPool::thread_exit_unlock_allowance_zone(allowance_id);
|
2024-07-10 00:41:24 +08:00
|
|
|
|
|
|
|
// Handle potential realloc due to the command and unlock allowance.
|
|
|
|
cmd = reinterpret_cast<CommandBase *>(&command_mem[flush_read_ptr]);
|
|
|
|
|
2024-04-16 23:45:56 +08:00
|
|
|
if (unlikely(cmd->sync)) {
|
|
|
|
sync_head++;
|
2024-07-18 20:54:58 +08:00
|
|
|
lock.~MutexLock(); // Give an opportunity to awaiters right away.
|
2024-04-16 23:45:56 +08:00
|
|
|
sync_cond_var.notify_all();
|
2024-07-18 20:54:58 +08:00
|
|
|
new (&lock) MutexLock(mutex);
|
2024-07-10 00:41:24 +08:00
|
|
|
// Handle potential realloc happened during unlock.
|
|
|
|
cmd = reinterpret_cast<CommandBase *>(&command_mem[flush_read_ptr]);
|
2023-12-29 08:27:17 +08:00
|
|
|
}
|
|
|
|
|
2024-04-23 18:01:12 +08:00
|
|
|
cmd->~CommandBase();
|
|
|
|
|
2023-12-29 08:27:17 +08:00
|
|
|
flush_read_ptr += size;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2021-06-09 23:09:31 +08:00
|
|
|
command_mem.clear();
|
2023-12-29 08:27:17 +08:00
|
|
|
flush_read_ptr = 0;
|
2024-05-09 00:56:16 +08:00
|
|
|
|
|
|
|
_prevent_sync_wraparound();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2024-04-16 23:45:56 +08:00
|
|
|
_FORCE_INLINE_ void _wait_for_sync(MutexLock<BinaryMutex> &p_lock) {
|
2024-05-09 00:56:16 +08:00
|
|
|
sync_awaiters++;
|
2024-04-16 23:45:56 +08:00
|
|
|
uint32_t sync_head_goal = sync_tail;
|
|
|
|
do {
|
|
|
|
sync_cond_var.wait(p_lock);
|
2024-05-09 00:56:16 +08:00
|
|
|
} while (sync_head < sync_head_goal);
|
|
|
|
sync_awaiters--;
|
|
|
|
_prevent_sync_wraparound();
|
2024-04-16 23:45:56 +08:00
|
|
|
}
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2024-04-23 18:01:23 +08:00
|
|
|
void _no_op() {}
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
public:
|
|
|
|
/* NORMAL PUSH COMMANDS */
|
2017-10-14 01:13:52 +08:00
|
|
|
DECL_PUSH(0)
|
2019-06-16 10:45:24 +08:00
|
|
|
SPACE_SEP_LIST(DECL_PUSH, 15)
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2017-10-14 01:13:52 +08:00
|
|
|
/* PUSH AND RET COMMANDS */
|
|
|
|
DECL_PUSH_AND_RET(0)
|
2019-06-16 10:45:24 +08:00
|
|
|
SPACE_SEP_LIST(DECL_PUSH_AND_RET, 15)
|
2017-06-09 11:23:50 +08:00
|
|
|
|
2017-10-14 01:13:52 +08:00
|
|
|
/* PUSH AND RET SYNC COMMANDS*/
|
|
|
|
DECL_PUSH_AND_SYNC(0)
|
2019-06-16 10:45:24 +08:00
|
|
|
SPACE_SEP_LIST(DECL_PUSH_AND_SYNC, 15)
|
2016-06-03 22:11:10 +08:00
|
|
|
|
2021-02-10 00:19:03 +08:00
|
|
|
_FORCE_INLINE_ void flush_if_pending() {
|
2021-06-09 23:09:31 +08:00
|
|
|
if (unlikely(command_mem.size() > 0)) {
|
|
|
|
_flush();
|
2021-02-10 00:19:03 +08:00
|
|
|
}
|
|
|
|
}
|
2024-04-23 18:01:23 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
void flush_all() {
|
2021-06-09 23:09:31 +08:00
|
|
|
_flush();
|
|
|
|
}
|
|
|
|
|
2024-04-23 18:01:23 +08:00
|
|
|
void sync() {
|
|
|
|
push_and_sync(this, &CommandQueueMT::_no_op);
|
|
|
|
}
|
|
|
|
|
2021-06-09 23:09:31 +08:00
|
|
|
void wait_and_flush() {
|
2024-04-04 16:18:08 +08:00
|
|
|
ERR_FAIL_COND(pump_task_id == WorkerThreadPool::INVALID_TASK_ID);
|
|
|
|
WorkerThreadPool::get_singleton()->wait_for_task_completion(pump_task_id);
|
2021-06-09 23:09:31 +08:00
|
|
|
_flush();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2024-04-04 16:18:08 +08:00
|
|
|
void set_pump_task_id(WorkerThreadPool::TaskID p_task_id) {
|
2024-07-18 20:54:58 +08:00
|
|
|
MutexLock lock(mutex);
|
2024-04-04 16:18:08 +08:00
|
|
|
pump_task_id = p_task_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandQueueMT();
|
2014-02-10 09:10:30 +08:00
|
|
|
~CommandQueueMT();
|
|
|
|
};
|
|
|
|
|
2017-10-14 01:13:52 +08:00
|
|
|
#undef ARG
|
|
|
|
#undef PARAM
|
|
|
|
#undef TYPE_PARAM
|
|
|
|
#undef PARAM_DECL
|
|
|
|
#undef DECL_CMD
|
|
|
|
#undef DECL_CMD_RET
|
|
|
|
#undef DECL_CMD_SYNC
|
|
|
|
#undef TYPE_ARG
|
|
|
|
#undef CMD_TYPE
|
|
|
|
#undef CMD_ASSIGN_PARAM
|
|
|
|
#undef DECL_PUSH
|
|
|
|
#undef CMD_RET_TYPE
|
|
|
|
#undef DECL_PUSH_AND_RET
|
|
|
|
#undef CMD_SYNC_TYPE
|
|
|
|
#undef DECL_CMD_SYNC
|
|
|
|
|
2020-03-25 18:10:34 +08:00
|
|
|
#endif // COMMAND_QUEUE_MT_H
|