2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* command_queue_mt.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2020-01-01 11:16:22 +01:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* 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 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifndef COMMAND_QUEUE_MT_H
|
|
|
|
#define COMMAND_QUEUE_MT_H
|
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/os/memory.h"
|
|
|
|
#include "core/os/mutex.h"
|
|
|
|
#include "core/os/semaphore.h"
|
|
|
|
#include "core/simple_type.h"
|
|
|
|
#include "core/typedefs.h"
|
2019-02-16 03:28:02 +01:00
|
|
|
|
2017-10-13 19:13:52 +02: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-02 21:50:52 +02:00
|
|
|
#define _COMMA_13 ,
|
2019-06-16 04:45:24 +02:00
|
|
|
#define _COMMA_14 ,
|
|
|
|
#define _COMMA_15 ,
|
2017-10-13 19:13:52 +02:00
|
|
|
|
2018-02-21 17:30:55 +01:00
|
|
|
// 1-based comma separated list of ITEMs
|
2017-10-13 19:13:52 +02:00
|
|
|
#define COMMA_SEP_LIST(ITEM, LENGTH) _COMMA_SEP_LIST_##LENGTH(ITEM)
|
2019-06-16 04:45:24 +02: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-02 21:50:52 +02:00
|
|
|
#define _COMMA_SEP_LIST_13(ITEM) \
|
|
|
|
_COMMA_SEP_LIST_12(ITEM) \
|
|
|
|
, ITEM(13)
|
2017-10-13 19:13:52 +02: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-21 17:30:55 +01:00
|
|
|
// 1-based semicolon separated list of ITEMs
|
2017-10-13 19:13:52 +02:00
|
|
|
#define SEMIC_SEP_LIST(ITEM, LENGTH) _SEMIC_SEP_LIST_##LENGTH(ITEM)
|
2019-06-16 04:45:24 +02: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-02 21:50:52 +02:00
|
|
|
#define _SEMIC_SEP_LIST_13(ITEM) \
|
|
|
|
_SEMIC_SEP_LIST_12(ITEM); \
|
|
|
|
ITEM(13)
|
2017-10-13 19:13:52 +02: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-21 17:30:55 +01:00
|
|
|
// 1-based space separated list of ITEMs
|
2017-10-13 19:13:52 +02:00
|
|
|
#define SPACE_SEP_LIST(ITEM, LENGTH) _SPACE_SEP_LIST_##LENGTH(ITEM)
|
2019-06-16 04:45:24 +02: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-02 21:50:52 +02:00
|
|
|
#define _SPACE_SEP_LIST_13(ITEM) \
|
|
|
|
_SPACE_SEP_LIST_12(ITEM) \
|
|
|
|
ITEM(13)
|
2017-10-13 19:13:52 +02: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) class P##N
|
|
|
|
#define PARAM_DECL(N) typename GetSimpleTypeT<P##N>::type_t p##N
|
|
|
|
|
|
|
|
#define DECL_CMD(N) \
|
|
|
|
template <class T, class M COMMA(N) COMMA_SEP_LIST(TYPE_PARAM, N)> \
|
|
|
|
struct Command##N : public CommandBase { \
|
|
|
|
T *instance; \
|
|
|
|
M method; \
|
|
|
|
SEMIC_SEP_LIST(PARAM_DECL, N); \
|
|
|
|
virtual void call() { \
|
|
|
|
(instance->*method)(COMMA_SEP_LIST(ARG, N)); \
|
|
|
|
} \
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DECL_CMD_RET(N) \
|
|
|
|
template <class T, class M, COMMA_SEP_LIST(TYPE_PARAM, N) COMMA(N) class R> \
|
|
|
|
struct CommandRet##N : public SyncCommand { \
|
|
|
|
R *ret; \
|
|
|
|
T *instance; \
|
|
|
|
M method; \
|
|
|
|
SEMIC_SEP_LIST(PARAM_DECL, N); \
|
|
|
|
virtual void call() { \
|
|
|
|
*ret = (instance->*method)(COMMA_SEP_LIST(ARG, N)); \
|
|
|
|
} \
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DECL_CMD_SYNC(N) \
|
|
|
|
template <class T, class M COMMA(N) COMMA_SEP_LIST(TYPE_PARAM, N)> \
|
|
|
|
struct CommandSync##N : public SyncCommand { \
|
|
|
|
T *instance; \
|
|
|
|
M method; \
|
|
|
|
SEMIC_SEP_LIST(PARAM_DECL, N); \
|
|
|
|
virtual void call() { \
|
|
|
|
(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
|
|
|
|
|
|
|
|
#define DECL_PUSH(N) \
|
|
|
|
template <class T, class M COMMA(N) COMMA_SEP_LIST(TYPE_PARAM, N)> \
|
|
|
|
void push(T *p_instance, M p_method COMMA(N) COMMA_SEP_LIST(PARAM, N)) { \
|
|
|
|
CMD_TYPE(N) *cmd = allocate_and_lock<CMD_TYPE(N)>(); \
|
|
|
|
cmd->instance = p_instance; \
|
|
|
|
cmd->method = p_method; \
|
|
|
|
SEMIC_SEP_LIST(CMD_ASSIGN_PARAM, N); \
|
|
|
|
unlock(); \
|
2020-05-10 12:56:01 +02:00
|
|
|
if (sync) \
|
|
|
|
sync->post(); \
|
2017-10-13 19:13:52 +02: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 <class T, class M, COMMA_SEP_LIST(TYPE_PARAM, N) COMMA(N) class R> \
|
|
|
|
void push_and_ret(T *p_instance, M p_method, COMMA_SEP_LIST(PARAM, N) COMMA(N) R *r_ret) { \
|
|
|
|
SyncSemaphore *ss = _alloc_sync_sem(); \
|
|
|
|
CMD_RET_TYPE(N) *cmd = allocate_and_lock<CMD_RET_TYPE(N)>(); \
|
|
|
|
cmd->instance = p_instance; \
|
|
|
|
cmd->method = p_method; \
|
|
|
|
SEMIC_SEP_LIST(CMD_ASSIGN_PARAM, N); \
|
|
|
|
cmd->ret = r_ret; \
|
|
|
|
cmd->sync_sem = ss; \
|
|
|
|
unlock(); \
|
2020-05-10 12:56:01 +02:00
|
|
|
if (sync) \
|
|
|
|
sync->post(); \
|
2020-03-03 09:26:42 +01:00
|
|
|
ss->sem.wait(); \
|
2019-02-16 03:28:02 +01:00
|
|
|
ss->in_use = false; \
|
2017-10-13 19:13:52 +02: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 <class T, class 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)) { \
|
|
|
|
SyncSemaphore *ss = _alloc_sync_sem(); \
|
|
|
|
CMD_SYNC_TYPE(N) *cmd = allocate_and_lock<CMD_SYNC_TYPE(N)>(); \
|
|
|
|
cmd->instance = p_instance; \
|
|
|
|
cmd->method = p_method; \
|
|
|
|
SEMIC_SEP_LIST(CMD_ASSIGN_PARAM, N); \
|
|
|
|
cmd->sync_sem = ss; \
|
|
|
|
unlock(); \
|
2020-05-10 12:56:01 +02:00
|
|
|
if (sync) \
|
|
|
|
sync->post(); \
|
2020-03-03 09:26:42 +01:00
|
|
|
ss->sem.wait(); \
|
2019-02-16 03:28:02 +01:00
|
|
|
ss->in_use = false; \
|
2017-10-13 19:13:52 +02:00
|
|
|
}
|
|
|
|
|
2019-06-16 04:45:24 +02:00
|
|
|
#define MAX_CMD_PARAMS 15
|
2017-10-13 19:13:52 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
class CommandQueueMT {
|
|
|
|
struct SyncSemaphore {
|
2020-03-03 09:26:42 +01:00
|
|
|
Semaphore sem;
|
2020-05-12 17:01:17 +02:00
|
|
|
bool in_use = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CommandBase {
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual void call() = 0;
|
2020-05-12 17:01:17 +02:00
|
|
|
virtual void post() {}
|
|
|
|
virtual ~CommandBase() {}
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-10-13 19:13:52 +02:00
|
|
|
struct SyncCommand : public CommandBase {
|
|
|
|
SyncSemaphore *sync_sem;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-10-13 19:13:52 +02:00
|
|
|
virtual void post() {
|
2020-03-03 09:26:42 +01:00
|
|
|
sync_sem->sem.post();
|
2017-03-05 16:44:50 +01:00
|
|
|
}
|
2016-06-03 16:11:10 +02:00
|
|
|
};
|
|
|
|
|
2017-10-13 19:13:52 +02:00
|
|
|
DECL_CMD(0)
|
2019-06-16 04:45:24 +02:00
|
|
|
SPACE_SEP_LIST(DECL_CMD, 15)
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/* comands that return */
|
2017-10-13 19:13:52 +02:00
|
|
|
DECL_CMD_RET(0)
|
2019-06-16 04:45:24 +02:00
|
|
|
SPACE_SEP_LIST(DECL_CMD_RET, 15)
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-10-13 19:13:52 +02:00
|
|
|
/* commands that don't return but sync */
|
|
|
|
DECL_CMD_SYNC(0)
|
2019-06-16 04:45:24 +02:00
|
|
|
SPACE_SEP_LIST(DECL_CMD_SYNC, 15)
|
2016-06-03 16:11:10 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
/***** BASE *******/
|
|
|
|
|
2016-03-09 00:00:52 +01:00
|
|
|
enum {
|
2020-09-24 18:03:19 +02:00
|
|
|
DEFAULT_COMMAND_MEM_SIZE_KB = 256,
|
2017-03-05 16:44:50 +01:00
|
|
|
SYNC_SEMAPHORES = 8
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2020-09-24 18:03:19 +02:00
|
|
|
uint8_t *command_mem = nullptr;
|
2020-09-24 18:55:38 +02:00
|
|
|
uint32_t read_ptr_and_epoch = 0;
|
|
|
|
uint32_t write_ptr_and_epoch = 0;
|
2020-05-12 17:01:17 +02:00
|
|
|
uint32_t dealloc_ptr = 0;
|
2020-09-24 18:03:19 +02:00
|
|
|
uint32_t command_mem_size = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
SyncSemaphore sync_sems[SYNC_SEMAPHORES];
|
2020-02-26 11:28:13 +01:00
|
|
|
Mutex mutex;
|
2020-05-12 17:01:17 +02:00
|
|
|
Semaphore *sync = nullptr;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
|
|
|
T *allocate() {
|
2014-02-10 02:10:30 +01:00
|
|
|
// alloc size is size+T+safeguard
|
2019-02-16 03:28:02 +01:00
|
|
|
uint32_t alloc_size = ((sizeof(T) + 8 - 1) & ~(8 - 1)) + 8;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-09-24 18:55:38 +02:00
|
|
|
// Assert that the buffer is big enough to hold at least two messages.
|
|
|
|
ERR_FAIL_COND_V(alloc_size * 2 + sizeof(uint32_t) > command_mem_size, nullptr);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
tryagain:
|
2020-09-24 18:55:38 +02:00
|
|
|
uint32_t write_ptr = write_ptr_and_epoch >> 1;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-10-13 19:13:52 +02:00
|
|
|
if (write_ptr < dealloc_ptr) {
|
|
|
|
// behind dealloc_ptr, check that there is room
|
|
|
|
if ((dealloc_ptr - write_ptr) <= alloc_size) {
|
|
|
|
// There is no more room, try to deallocate something
|
|
|
|
if (dealloc_one()) {
|
|
|
|
goto tryagain;
|
|
|
|
}
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2017-10-13 19:13:52 +02:00
|
|
|
}
|
2019-07-01 12:59:42 +02:00
|
|
|
} else {
|
2017-10-13 19:13:52 +02:00
|
|
|
// ahead of dealloc_ptr, check that there is room
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-09-24 18:03:19 +02:00
|
|
|
if ((command_mem_size - write_ptr) < alloc_size + sizeof(uint32_t)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
// no room at the end, wrap down;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-10-13 19:13:52 +02:00
|
|
|
if (dealloc_ptr == 0) { // don't want write_ptr to become dealloc_ptr
|
|
|
|
|
|
|
|
// There is no more room, try to deallocate something
|
|
|
|
if (dealloc_one()) {
|
|
|
|
goto tryagain;
|
|
|
|
}
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2017-10-13 19:13:52 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
// if this happens, it's a bug
|
2020-09-24 18:03:19 +02:00
|
|
|
ERR_FAIL_COND_V((command_mem_size - write_ptr) < 8, nullptr);
|
2017-03-24 21:45:31 +01:00
|
|
|
// zero means, wrap to beginning
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t *p = (uint32_t *)&command_mem[write_ptr];
|
2020-09-24 18:55:38 +02:00
|
|
|
*p = 1;
|
|
|
|
write_ptr_and_epoch = 0 | (1 & ~write_ptr_and_epoch); // Invert epoch.
|
|
|
|
// See if we can get the thread to run and clear up some more space while we wait.
|
|
|
|
// This is required if alloc_size * 2 + 4 > COMMAND_MEM_SIZE
|
|
|
|
if (sync) {
|
|
|
|
sync->post();
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
goto tryagain;
|
|
|
|
}
|
|
|
|
}
|
2017-10-13 19:13:52 +02:00
|
|
|
// Allocate the size and the 'in use' bit.
|
|
|
|
// First bit used to mark if command is still in use (1)
|
|
|
|
// or if it has been destroyed and can be deallocated (0).
|
2019-02-16 03:28:02 +01:00
|
|
|
uint32_t size = (sizeof(T) + 8 - 1) & ~(8 - 1);
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t *p = (uint32_t *)&command_mem[write_ptr];
|
2019-02-16 03:28:02 +01:00
|
|
|
*p = (size << 1) | 1;
|
|
|
|
write_ptr += 8;
|
2014-02-10 02:10:30 +01:00
|
|
|
// allocate the command
|
2017-03-05 16:44:50 +01:00
|
|
|
T *cmd = memnew_placement(&command_mem[write_ptr], T);
|
2019-02-16 03:28:02 +01:00
|
|
|
write_ptr += size;
|
2020-09-24 18:55:38 +02:00
|
|
|
write_ptr_and_epoch = (write_ptr << 1) | (write_ptr_and_epoch & 1);
|
2014-02-10 02:10:30 +01:00
|
|
|
return cmd;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
|
|
|
T *allocate_and_lock() {
|
2014-02-10 02:10:30 +01:00
|
|
|
lock();
|
2017-03-05 16:44:50 +01:00
|
|
|
T *ret;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
while ((ret = allocate<T>()) == nullptr) {
|
2014-02-10 02:10:30 +01:00
|
|
|
unlock();
|
2016-03-09 00:00:52 +01:00
|
|
|
// sleep a little until fetch happened and some room is made
|
2014-02-10 02:10:30 +01:00
|
|
|
wait_for_flush();
|
|
|
|
lock();
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-10-13 19:13:52 +02:00
|
|
|
bool flush_one(bool p_lock = true) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_lock) {
|
2020-05-10 12:56:01 +02:00
|
|
|
lock();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
tryagain:
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
// tried to read an empty queue
|
2020-09-24 18:55:38 +02:00
|
|
|
if (read_ptr_and_epoch == write_ptr_and_epoch) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_lock) {
|
2020-05-10 12:56:01 +02:00
|
|
|
unlock();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
2019-05-30 10:28:03 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-09-24 18:55:38 +02:00
|
|
|
uint32_t read_ptr = read_ptr_and_epoch >> 1;
|
2017-10-13 19:13:52 +02:00
|
|
|
uint32_t size_ptr = read_ptr;
|
|
|
|
uint32_t size = *(uint32_t *)&command_mem[read_ptr] >> 1;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (size == 0) {
|
2020-09-24 18:55:38 +02:00
|
|
|
*(uint32_t *)&command_mem[read_ptr] = 0; // clear in-use bit.
|
2014-02-10 02:10:30 +01:00
|
|
|
//end of ringbuffer, wrap
|
2020-09-24 18:55:38 +02:00
|
|
|
read_ptr_and_epoch = 0 | (1 & ~read_ptr_and_epoch); // Invert epoch.
|
2014-02-10 02:10:30 +01:00
|
|
|
goto tryagain;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2019-02-16 03:28:02 +01:00
|
|
|
read_ptr += 8;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
CommandBase *cmd = reinterpret_cast<CommandBase *>(&command_mem[read_ptr]);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-10-13 19:13:52 +02:00
|
|
|
read_ptr += size;
|
|
|
|
|
2020-09-24 18:55:38 +02:00
|
|
|
read_ptr_and_epoch = (read_ptr << 1) | (read_ptr_and_epoch & 1);
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_lock) {
|
2020-05-10 12:56:01 +02:00
|
|
|
unlock();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
cmd->call();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_lock) {
|
2020-05-10 12:56:01 +02:00
|
|
|
lock();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-10-13 19:13:52 +02:00
|
|
|
cmd->post();
|
|
|
|
cmd->~CommandBase();
|
|
|
|
*(uint32_t *)&command_mem[size_ptr] &= ~1;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_lock) {
|
2020-05-10 12:56:01 +02:00
|
|
|
unlock();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
return true;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void lock();
|
|
|
|
void unlock();
|
|
|
|
void wait_for_flush();
|
2017-03-05 16:44:50 +01:00
|
|
|
SyncSemaphore *_alloc_sync_sem();
|
2017-10-13 19:13:52 +02:00
|
|
|
bool dealloc_one();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
public:
|
|
|
|
/* NORMAL PUSH COMMANDS */
|
2017-10-13 19:13:52 +02:00
|
|
|
DECL_PUSH(0)
|
2019-06-16 04:45:24 +02:00
|
|
|
SPACE_SEP_LIST(DECL_PUSH, 15)
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-10-13 19:13:52 +02:00
|
|
|
/* PUSH AND RET COMMANDS */
|
|
|
|
DECL_PUSH_AND_RET(0)
|
2019-06-16 04:45:24 +02:00
|
|
|
SPACE_SEP_LIST(DECL_PUSH_AND_RET, 15)
|
2017-06-09 05:23:50 +02:00
|
|
|
|
2017-10-13 19:13:52 +02:00
|
|
|
/* PUSH AND RET SYNC COMMANDS*/
|
|
|
|
DECL_PUSH_AND_SYNC(0)
|
2019-06-16 04:45:24 +02:00
|
|
|
SPACE_SEP_LIST(DECL_PUSH_AND_SYNC, 15)
|
2016-06-03 16:11:10 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void wait_and_flush_one() {
|
|
|
|
ERR_FAIL_COND(!sync);
|
|
|
|
sync->wait();
|
2016-03-09 00:00:52 +01:00
|
|
|
flush_one();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void flush_all() {
|
2016-01-04 04:00:33 +01:00
|
|
|
//ERR_FAIL_COND(sync);
|
2014-02-10 02:10:30 +01:00
|
|
|
lock();
|
2020-05-14 16:41:43 +02:00
|
|
|
while (flush_one(false)) {
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
unlock();
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
CommandQueueMT(bool p_sync);
|
|
|
|
~CommandQueueMT();
|
|
|
|
};
|
|
|
|
|
2017-10-13 19:13:52 +02: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 11:10:34 +01:00
|
|
|
#endif // COMMAND_QUEUE_MT_H
|