2008-07-23 09:02:47 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2012-05-11 13:08:27 +00:00
|
|
|
*
|
2008-07-23 09:02:47 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2012-05-11 13:08:27 +00:00
|
|
|
*
|
2008-07-23 09:02:47 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2014-02-18 01:34:18 +00:00
|
|
|
*
|
2008-07-23 09:02:47 +00:00
|
|
|
*/
|
|
|
|
|
2012-05-11 13:08:27 +00:00
|
|
|
#ifndef COMMON_COROUTINES_H
|
|
|
|
#define COMMON_COROUTINES_H
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
#include "common/scummsys.h"
|
2012-06-07 15:51:58 +00:00
|
|
|
#include "common/util.h" // for SCUMMVM_CURRENT_FUNCTION
|
2012-05-11 13:08:27 +00:00
|
|
|
#include "common/list.h"
|
|
|
|
#include "common/singleton.h"
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2012-05-11 13:08:27 +00:00
|
|
|
namespace Common {
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2009-04-09 17:07:24 +00:00
|
|
|
/**
|
2012-05-11 13:08:27 +00:00
|
|
|
* @defgroup Coroutine support for simulating multi-threading.
|
2009-04-09 17:07:24 +00:00
|
|
|
*
|
2008-07-24 08:59:17 +00:00
|
|
|
* The following is loosely based on an article by Simon Tatham:
|
|
|
|
* <http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html>.
|
|
|
|
* However, many improvements and tweaks have been made, in particular
|
|
|
|
* by taking advantage of C++ features not available in C.
|
|
|
|
*/
|
2009-04-09 17:07:24 +00:00
|
|
|
//@{
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2012-05-11 13:08:27 +00:00
|
|
|
#define CoroScheduler (Common::CoroutineScheduler::instance())
|
|
|
|
|
2010-10-30 00:33:11 +00:00
|
|
|
|
|
|
|
// Enable this macro to enable some debugging support in the coroutine code.
|
2012-05-13 08:19:40 +00:00
|
|
|
//#define COROUTINE_DEBUG
|
2010-10-30 00:33:11 +00:00
|
|
|
|
2008-07-24 08:59:17 +00:00
|
|
|
/**
|
|
|
|
* The core of any coroutine context which captures the 'state' of a coroutine.
|
2009-04-09 17:07:24 +00:00
|
|
|
* Private use only.
|
2008-07-23 09:02:47 +00:00
|
|
|
*/
|
|
|
|
struct CoroBaseContext {
|
|
|
|
int _line;
|
|
|
|
int _sleep;
|
|
|
|
CoroBaseContext *_subctx;
|
2012-05-13 08:19:40 +00:00
|
|
|
#ifdef COROUTINE_DEBUG
|
2010-10-30 00:33:11 +00:00
|
|
|
const char *_funcName;
|
|
|
|
#endif
|
2012-06-07 15:50:20 +00:00
|
|
|
/**
|
|
|
|
* Creates a coroutine context
|
|
|
|
*/
|
2010-10-30 00:33:11 +00:00
|
|
|
CoroBaseContext(const char *func);
|
2012-06-07 15:50:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor for coroutine context
|
|
|
|
*/
|
2012-05-13 08:51:41 +00:00
|
|
|
virtual ~CoroBaseContext();
|
2008-07-23 09:02:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef CoroBaseContext *CoroContext;
|
|
|
|
|
2009-04-09 17:07:24 +00:00
|
|
|
|
2012-05-11 13:08:27 +00:00
|
|
|
/** This is a special constant that can be temporarily used as a parameter to call coroutine-ised
|
2012-05-30 22:06:59 +00:00
|
|
|
* methods from code that haven't yet been converted to being a coroutine, so code at least
|
2012-05-25 13:36:18 +00:00
|
|
|
* compiles correctly. Be aware, though, that an error will occur if a coroutine that was passed
|
|
|
|
* the nullContext tries to sleep or yield control.
|
2012-05-11 13:08:27 +00:00
|
|
|
*/
|
2008-12-01 20:35:36 +00:00
|
|
|
extern CoroContext nullContext;
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper class which holds a pointer to a pointer to a CoroBaseContext.
|
|
|
|
* The interesting part is the destructor, which kills the context being held,
|
2008-07-24 08:59:17 +00:00
|
|
|
* but ONLY if the _sleep val of that context is zero. This way, a coroutine
|
|
|
|
* can just 'return' w/o having to worry about freeing the allocated context
|
|
|
|
* (in Simon Tatham's original code, one had to use a special macro to
|
|
|
|
* return from a coroutine).
|
2008-07-23 09:02:47 +00:00
|
|
|
*/
|
2008-07-24 08:59:17 +00:00
|
|
|
class CoroContextHolder {
|
2008-07-23 09:02:47 +00:00
|
|
|
CoroContext &_ctx;
|
2008-07-24 08:59:17 +00:00
|
|
|
public:
|
2009-04-09 17:07:24 +00:00
|
|
|
CoroContextHolder(CoroContext &ctx) : _ctx(ctx) {
|
|
|
|
assert(ctx);
|
|
|
|
assert(ctx->_sleep >= 0);
|
|
|
|
ctx->_sleep = 0;
|
|
|
|
}
|
2008-07-24 08:59:17 +00:00
|
|
|
~CoroContextHolder() {
|
|
|
|
if (_ctx && _ctx->_sleep == 0) {
|
|
|
|
delete _ctx;
|
2018-04-05 18:25:28 +00:00
|
|
|
_ctx = nullptr;
|
2008-07-24 08:59:17 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-23 09:02:47 +00:00
|
|
|
};
|
|
|
|
|
2012-05-11 13:08:27 +00:00
|
|
|
/** Methods that have been converted to being a coroutine should have this as the first parameter */
|
|
|
|
#define CORO_PARAM Common::CoroContext &coroParam
|
2008-07-24 08:59:17 +00:00
|
|
|
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2009-04-09 17:07:24 +00:00
|
|
|
/**
|
|
|
|
* Begin the declaration of a coroutine context.
|
|
|
|
* This allows declaring variables which are 'persistent' during the
|
|
|
|
* lifetime of the coroutine. An example use would be:
|
|
|
|
*
|
|
|
|
* CORO_BEGIN_CONTEXT;
|
|
|
|
* int var;
|
|
|
|
* char *foo;
|
|
|
|
* CORO_END_CONTEXT(_ctx);
|
|
|
|
*
|
|
|
|
* It is not possible to initialize variables here, due to the way this
|
|
|
|
* macro is implemented. Furthermore, to use the variables declared in
|
|
|
|
* the coroutine context, you have to access them via the context variable
|
|
|
|
* name that was specified as parameter to CORO_END_CONTEXT, e.g.
|
|
|
|
* _ctx->var = 0;
|
|
|
|
*
|
|
|
|
* @see CORO_END_CONTEXT
|
2010-10-31 22:33:13 +00:00
|
|
|
*
|
|
|
|
* @note We declare a variable 'DUMMY' to allow the user to specify an 'empty'
|
|
|
|
* context, and so compilers won't complain about ";" following the macro.
|
2009-04-09 17:07:24 +00:00
|
|
|
*/
|
2010-10-30 00:33:11 +00:00
|
|
|
#define CORO_BEGIN_CONTEXT \
|
2012-05-11 13:08:27 +00:00
|
|
|
struct CoroContextTag : Common::CoroBaseContext { \
|
2013-09-21 19:45:06 +00:00
|
|
|
CoroContextTag() : CoroBaseContext(SCUMMVM_CURRENT_FUNCTION) { DUMMY = 0; } \
|
2010-10-31 22:33:13 +00:00
|
|
|
int DUMMY
|
2009-04-09 17:07:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* End the declaration of a coroutine context.
|
2012-06-07 15:51:58 +00:00
|
|
|
* @param x name of the coroutine context
|
2009-04-09 17:07:24 +00:00
|
|
|
* @see CORO_BEGIN_CONTEXT
|
|
|
|
*/
|
2008-07-23 09:02:47 +00:00
|
|
|
#define CORO_END_CONTEXT(x) } *x = (CoroContextTag *)coroParam
|
|
|
|
|
2009-04-09 17:07:24 +00:00
|
|
|
/**
|
2009-05-24 15:17:42 +00:00
|
|
|
* Begin the code section of a coroutine.
|
2012-06-07 15:51:58 +00:00
|
|
|
* @param x name of the coroutine context
|
2009-04-09 17:07:24 +00:00
|
|
|
* @see CORO_BEGIN_CODE
|
|
|
|
*/
|
2008-07-23 09:02:47 +00:00
|
|
|
#define CORO_BEGIN_CODE(x) \
|
2012-06-07 15:51:58 +00:00
|
|
|
if (&coroParam == &Common::nullContext) assert(!Common::nullContext); \
|
|
|
|
if (!x) { coroParam = x = new CoroContextTag(); } \
|
2013-09-21 06:48:11 +00:00
|
|
|
x->DUMMY = 0; \
|
2012-06-07 15:51:58 +00:00
|
|
|
Common::CoroContextHolder tmpHolder(coroParam); \
|
2019-10-20 23:17:58 +00:00
|
|
|
switch (coroParam->_line) { default: break; case 0:;
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2009-04-09 17:07:24 +00:00
|
|
|
/**
|
|
|
|
* End the code section of a coroutine.
|
|
|
|
* @see CORO_END_CODE
|
|
|
|
*/
|
2008-07-23 09:02:47 +00:00
|
|
|
#define CORO_END_CODE \
|
2012-06-07 15:51:58 +00:00
|
|
|
if (&coroParam == &Common::nullContext) { \
|
|
|
|
delete Common::nullContext; \
|
|
|
|
Common::nullContext = NULL; \
|
|
|
|
} \
|
|
|
|
}
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2009-04-09 17:07:24 +00:00
|
|
|
/**
|
|
|
|
* Sleep for the specified number of scheduler cycles.
|
|
|
|
*/
|
2012-06-07 15:51:58 +00:00
|
|
|
#define CORO_SLEEP(delay) \
|
|
|
|
do { \
|
|
|
|
coroParam->_line = __LINE__; \
|
|
|
|
coroParam->_sleep = delay; \
|
|
|
|
assert(&coroParam != &Common::nullContext); \
|
|
|
|
return; case __LINE__:; \
|
|
|
|
} while (0)
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2012-05-11 13:08:27 +00:00
|
|
|
#define CORO_GIVE_WAY do { CoroScheduler.giveWay(); CORO_SLEEP(1); } while (0)
|
|
|
|
#define CORO_RESCHEDULE do { CoroScheduler.reschedule(); CORO_SLEEP(1); } while (0)
|
2008-12-01 20:35:36 +00:00
|
|
|
|
2009-04-09 17:07:24 +00:00
|
|
|
/**
|
2011-06-20 15:23:30 +00:00
|
|
|
* Stop the currently running coroutine and all calling coroutines.
|
|
|
|
*
|
|
|
|
* This sets _sleep to -1 rather than 0 so that the context doesn't get
|
|
|
|
* deleted by CoroContextHolder, since we want CORO_INVOKE_ARGS to
|
|
|
|
* propogate the _sleep value and return immediately (the scheduler will
|
|
|
|
* then delete the entire coroutine's state, including all subcontexts).
|
2009-04-09 17:07:24 +00:00
|
|
|
*/
|
2008-12-01 20:35:36 +00:00
|
|
|
#define CORO_KILL_SELF() \
|
2012-06-07 15:51:58 +00:00
|
|
|
do { if (&coroParam != &Common::nullContext) { coroParam->_sleep = -1; } return; } while (0)
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2010-10-30 00:33:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This macro is to be used in conjunction with CORO_INVOKE_ARGS and
|
|
|
|
* similar macros for calling coroutines-enabled subroutines.
|
|
|
|
*/
|
|
|
|
#define CORO_SUBCTX coroParam->_subctx
|
|
|
|
|
2009-04-09 17:07:24 +00:00
|
|
|
/**
|
|
|
|
* Invoke another coroutine.
|
|
|
|
*
|
2011-06-20 15:23:30 +00:00
|
|
|
* If the subcontext still exists after the coroutine is invoked, it has
|
|
|
|
* either yielded/slept or killed itself, and so we copy the _sleep value
|
|
|
|
* to our own context and return (execution will continue at the case
|
|
|
|
* statement below, where we loop and call the coroutine again).
|
|
|
|
* If the subcontext is null, the coroutine ended normally, and we can
|
|
|
|
* simply break out of the loop and continue execution.
|
2010-10-30 00:33:54 +00:00
|
|
|
*
|
2012-06-07 15:51:58 +00:00
|
|
|
* @param subCoro name of the coroutine-enabled function to invoke
|
|
|
|
* @param ARGS list of arguments to pass to subCoro
|
2010-10-30 00:33:54 +00:00
|
|
|
*
|
|
|
|
* @note ARGS must be surrounded by parentheses, and the first argument
|
|
|
|
* in this list must always be CORO_SUBCTX. For example, the
|
|
|
|
* regular function call
|
|
|
|
* myFunc(a, b);
|
|
|
|
* becomes the following:
|
|
|
|
* CORO_INVOKE_ARGS(myFunc, (CORO_SUBCTX, a, b));
|
2009-04-09 17:07:24 +00:00
|
|
|
*/
|
2012-06-07 15:51:58 +00:00
|
|
|
#define CORO_INVOKE_ARGS(subCoro, ARGS) \
|
|
|
|
do { \
|
|
|
|
coroParam->_line = __LINE__; \
|
|
|
|
coroParam->_subctx = 0; \
|
|
|
|
do { \
|
|
|
|
subCoro ARGS; \
|
|
|
|
if (!coroParam->_subctx) break; \
|
|
|
|
coroParam->_sleep = coroParam->_subctx->_sleep; \
|
|
|
|
assert(&coroParam != &Common::nullContext); \
|
|
|
|
return; case __LINE__:; \
|
|
|
|
} while (1); \
|
|
|
|
} while (0)
|
2009-04-09 17:07:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoke another coroutine. Similar to CORO_INVOKE_ARGS,
|
|
|
|
* but allows specifying a return value which is returned
|
|
|
|
* if invoked coroutine yields (thus causing the current
|
|
|
|
* coroutine to yield, too).
|
|
|
|
*/
|
2012-06-07 15:51:58 +00:00
|
|
|
#define CORO_INVOKE_ARGS_V(subCoro, RESULT, ARGS) \
|
|
|
|
do { \
|
|
|
|
coroParam->_line = __LINE__; \
|
|
|
|
coroParam->_subctx = 0; \
|
|
|
|
do { \
|
|
|
|
subCoro ARGS; \
|
|
|
|
if (!coroParam->_subctx) break; \
|
|
|
|
coroParam->_sleep = coroParam->_subctx->_sleep; \
|
|
|
|
assert(&coroParam != &Common::nullContext); \
|
|
|
|
return RESULT; case __LINE__:; \
|
|
|
|
} while (1); \
|
|
|
|
} while (0)
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2009-04-09 17:07:24 +00:00
|
|
|
/**
|
|
|
|
* Convenience wrapper for CORO_INVOKE_ARGS for invoking a coroutine
|
|
|
|
* with no parameters.
|
|
|
|
*/
|
2008-07-23 09:02:47 +00:00
|
|
|
#define CORO_INVOKE_0(subCoroutine) \
|
2012-06-07 15:51:58 +00:00
|
|
|
CORO_INVOKE_ARGS(subCoroutine, (CORO_SUBCTX))
|
2009-04-09 17:07:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience wrapper for CORO_INVOKE_ARGS for invoking a coroutine
|
|
|
|
* with one parameter.
|
|
|
|
*/
|
2008-07-23 09:02:47 +00:00
|
|
|
#define CORO_INVOKE_1(subCoroutine, a0) \
|
2012-06-07 15:51:58 +00:00
|
|
|
CORO_INVOKE_ARGS(subCoroutine, (CORO_SUBCTX, a0))
|
2009-04-09 17:07:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience wrapper for CORO_INVOKE_ARGS for invoking a coroutine
|
|
|
|
* with two parameters.
|
|
|
|
*/
|
2008-07-23 09:02:47 +00:00
|
|
|
#define CORO_INVOKE_2(subCoroutine, a0,a1) \
|
2012-06-07 15:51:58 +00:00
|
|
|
CORO_INVOKE_ARGS(subCoroutine, (CORO_SUBCTX, a0, a1))
|
2009-04-09 17:07:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience wrapper for CORO_INVOKE_ARGS for invoking a coroutine
|
|
|
|
* with three parameters.
|
|
|
|
*/
|
2008-12-01 20:35:36 +00:00
|
|
|
#define CORO_INVOKE_3(subCoroutine, a0,a1,a2) \
|
2012-06-07 15:51:58 +00:00
|
|
|
CORO_INVOKE_ARGS(subCoroutine, (CORO_SUBCTX, a0, a1, a2))
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2012-05-11 13:08:27 +00:00
|
|
|
/**
|
|
|
|
* Convenience wrapper for CORO_INVOKE_ARGS for invoking a coroutine
|
|
|
|
* with four parameters.
|
|
|
|
*/
|
|
|
|
#define CORO_INVOKE_4(subCoroutine, a0,a1,a2,a3) \
|
2012-06-07 15:51:58 +00:00
|
|
|
CORO_INVOKE_ARGS(subCoroutine, (CORO_SUBCTX, a0, a1, a2, a3))
|
2012-05-11 13:08:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// the size of process specific info
|
2012-06-07 15:51:58 +00:00
|
|
|
#define CORO_PARAM_SIZE 32
|
2012-05-11 13:08:27 +00:00
|
|
|
|
|
|
|
// the maximum number of processes
|
2012-06-07 15:51:58 +00:00
|
|
|
#define CORO_NUM_PROCESS 100
|
|
|
|
#define CORO_MAX_PROCESSES 100
|
2012-05-12 14:19:04 +00:00
|
|
|
#define CORO_MAX_PID_WAITING 5
|
2012-05-11 13:08:27 +00:00
|
|
|
|
|
|
|
#define CORO_INFINITE 0xffffffff
|
|
|
|
#define CORO_INVALID_PID_VALUE 0
|
|
|
|
|
2012-05-25 13:36:18 +00:00
|
|
|
/** Coroutine parameter for methods converted to coroutines */
|
2012-05-11 13:08:27 +00:00
|
|
|
typedef void (*CORO_ADDR)(CoroContext &, const void *);
|
|
|
|
|
|
|
|
/** process structure */
|
|
|
|
struct PROCESS {
|
2012-06-07 15:51:58 +00:00
|
|
|
PROCESS *pNext; ///< pointer to next process in active or free list
|
|
|
|
PROCESS *pPrevious; ///< pointer to previous process in active or free list
|
2012-05-11 13:08:27 +00:00
|
|
|
|
2012-06-07 15:51:58 +00:00
|
|
|
CoroContext state; ///< the state of the coroutine
|
|
|
|
CORO_ADDR coroAddr; ///< the entry point of the coroutine
|
2012-05-11 13:08:27 +00:00
|
|
|
|
2012-06-07 15:51:58 +00:00
|
|
|
int sleepTime; ///< number of scheduler cycles to sleep
|
|
|
|
uint32 pid; ///< process ID
|
|
|
|
uint32 pidWaiting[CORO_MAX_PID_WAITING]; ///< Process ID(s) process is currently waiting on
|
|
|
|
char param[CORO_PARAM_SIZE]; ///< process specific info
|
2012-05-11 13:08:27 +00:00
|
|
|
};
|
|
|
|
typedef PROCESS *PPROCESS;
|
|
|
|
|
|
|
|
|
|
|
|
/** Event structure */
|
|
|
|
struct EVENT {
|
|
|
|
uint32 pid;
|
|
|
|
bool manualReset;
|
|
|
|
bool signalled;
|
2012-06-09 07:14:21 +00:00
|
|
|
bool pulsing;
|
2012-05-11 13:08:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates and manages "processes" (really coroutines).
|
|
|
|
*/
|
2012-06-07 15:51:58 +00:00
|
|
|
class CoroutineScheduler : public Singleton<CoroutineScheduler> {
|
2012-05-11 13:08:27 +00:00
|
|
|
public:
|
|
|
|
/** Pointer to a function of the form "void function(PPROCESS)" */
|
|
|
|
typedef void (*VFPTRPP)(PROCESS *);
|
|
|
|
|
|
|
|
private:
|
2012-06-07 16:11:38 +00:00
|
|
|
friend class Singleton<CoroutineScheduler>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
CoroutineScheduler();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor
|
|
|
|
*/
|
|
|
|
~CoroutineScheduler();
|
|
|
|
|
2012-05-11 13:08:27 +00:00
|
|
|
|
|
|
|
/** list of all processes */
|
|
|
|
PROCESS *processList;
|
|
|
|
|
|
|
|
/** active process list - also saves scheduler state */
|
|
|
|
PROCESS *active;
|
|
|
|
|
|
|
|
/** pointer to free process list */
|
|
|
|
PROCESS *pFreeProcesses;
|
|
|
|
|
|
|
|
/** the currently active process */
|
|
|
|
PROCESS *pCurrent;
|
|
|
|
|
|
|
|
/** Auto-incrementing process Id */
|
|
|
|
int pidCounter;
|
|
|
|
|
|
|
|
/** Event list */
|
|
|
|
Common::List<EVENT *> _events;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// diagnostic process counters
|
|
|
|
int numProcs;
|
|
|
|
int maxProcs;
|
|
|
|
|
2012-06-07 15:50:20 +00:00
|
|
|
/**
|
|
|
|
* Checks both the active and free process list to insure all the links are valid,
|
|
|
|
* and that no processes have been lost
|
|
|
|
*/
|
2012-06-07 15:51:58 +00:00
|
|
|
void checkStack();
|
2012-05-11 13:08:27 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called from killProcess() to enable other resources
|
|
|
|
* a process may be allocated to be released.
|
|
|
|
*/
|
|
|
|
VFPTRPP pRCfunction;
|
|
|
|
|
|
|
|
PROCESS *getProcess(uint32 pid);
|
|
|
|
EVENT *getEvent(uint32 pid);
|
|
|
|
public:
|
2012-06-07 15:50:20 +00:00
|
|
|
/**
|
|
|
|
* Kills all processes and places them on the free list.
|
|
|
|
*/
|
2012-05-11 13:08:27 +00:00
|
|
|
void reset();
|
|
|
|
|
2012-06-07 15:51:58 +00:00
|
|
|
#ifdef DEBUG
|
2012-06-07 15:50:20 +00:00
|
|
|
/**
|
|
|
|
* Shows the maximum number of process used at once.
|
|
|
|
*/
|
2012-05-11 13:08:27 +00:00
|
|
|
void printStats();
|
2012-06-07 15:51:58 +00:00
|
|
|
#endif
|
2012-05-11 13:08:27 +00:00
|
|
|
|
2012-06-07 15:50:20 +00:00
|
|
|
/**
|
|
|
|
* Give all active processes a chance to run
|
|
|
|
*/
|
2012-05-11 13:08:27 +00:00
|
|
|
void schedule();
|
2012-05-29 11:51:10 +00:00
|
|
|
|
2012-06-07 15:50:20 +00:00
|
|
|
/**
|
|
|
|
* Reschedules all the processes to run again this tick
|
|
|
|
*/
|
2012-05-11 13:08:27 +00:00
|
|
|
void rescheduleAll();
|
2012-05-29 11:51:10 +00:00
|
|
|
|
2012-06-07 15:50:20 +00:00
|
|
|
/**
|
|
|
|
* If the specified process has already run on this tick, make it run
|
|
|
|
* again on the current tick.
|
|
|
|
*/
|
2018-04-05 18:25:28 +00:00
|
|
|
void reschedule(PPROCESS pReSchedProc = nullptr);
|
2012-05-29 11:51:10 +00:00
|
|
|
|
2012-06-07 15:50:20 +00:00
|
|
|
/**
|
|
|
|
* Moves the specified process to the end of the dispatch queue
|
|
|
|
* allowing it to run again within the current game cycle.
|
2012-06-07 15:51:58 +00:00
|
|
|
* @param pGiveProc Which process
|
2012-06-07 15:50:20 +00:00
|
|
|
*/
|
2018-04-05 18:25:28 +00:00
|
|
|
void giveWay(PPROCESS pReSchedProc = nullptr);
|
2012-05-29 11:51:10 +00:00
|
|
|
|
2012-06-07 15:50:20 +00:00
|
|
|
/**
|
|
|
|
* Continously makes a given process wait for another process to finish or event to signal.
|
|
|
|
*
|
2012-06-07 15:51:58 +00:00
|
|
|
* @param pid Process/Event identifier
|
|
|
|
* @param duration Duration in milliseconds
|
|
|
|
* @param expired If specified, set to true if delay period expired
|
2012-06-07 15:50:20 +00:00
|
|
|
*/
|
2018-04-05 18:25:28 +00:00
|
|
|
void waitForSingleObject(CORO_PARAM, int pid, uint32 duration, bool *expired = nullptr);
|
2012-05-29 11:51:10 +00:00
|
|
|
|
2012-06-07 15:50:20 +00:00
|
|
|
/**
|
|
|
|
* Continously makes a given process wait for given prcesses to finished or events to be set
|
|
|
|
*
|
2012-06-07 15:51:58 +00:00
|
|
|
* @param nCount Number of Id's being passed
|
|
|
|
* @param evtList List of pids to wait for
|
|
|
|
* @param bWaitAll Specifies whether all or any of the processes/events
|
|
|
|
* @param duration Duration in milliseconds
|
|
|
|
* @param expired Set to true if delay period expired
|
2012-06-07 15:50:20 +00:00
|
|
|
*/
|
2012-06-07 15:51:58 +00:00
|
|
|
void waitForMultipleObjects(CORO_PARAM, int nCount, uint32 *pidList, bool bWaitAll,
|
2018-04-05 18:25:28 +00:00
|
|
|
uint32 duration, bool *expired = nullptr);
|
2012-05-11 13:08:27 +00:00
|
|
|
|
2012-06-07 15:50:20 +00:00
|
|
|
/**
|
|
|
|
* Make the active process sleep for the given duration in milliseconds
|
|
|
|
*
|
2012-06-07 15:51:58 +00:00
|
|
|
* @param duration Duration in milliseconds
|
|
|
|
* @remarks This duration won't be precise, since it relies on the frequency the
|
2012-06-07 15:50:20 +00:00
|
|
|
* scheduler is called.
|
|
|
|
*/
|
2012-05-29 11:51:10 +00:00
|
|
|
void sleep(CORO_PARAM, uint32 duration);
|
2012-06-07 15:51:58 +00:00
|
|
|
|
2012-06-07 15:50:20 +00:00
|
|
|
/**
|
|
|
|
* Creates a new process.
|
|
|
|
*
|
2012-06-07 15:51:58 +00:00
|
|
|
* @param pid process identifier
|
|
|
|
* @param coroAddr Coroutine start address
|
|
|
|
* @param pParam Process specific info
|
|
|
|
* @param sizeParam Size of process specific info
|
2012-06-07 15:50:20 +00:00
|
|
|
*/
|
2012-05-11 13:08:27 +00:00
|
|
|
PROCESS *createProcess(uint32 pid, CORO_ADDR coroAddr, const void *pParam, int sizeParam);
|
2012-06-07 15:50:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new process with an auto-incrementing Process Id.
|
|
|
|
*
|
2012-06-07 15:51:58 +00:00
|
|
|
* @param coroAddr Coroutine start address
|
|
|
|
* @param pParam Process specific info
|
|
|
|
* @param sizeParam Size of process specific info
|
2012-06-07 15:50:20 +00:00
|
|
|
*/
|
2012-05-11 13:08:27 +00:00
|
|
|
uint32 createProcess(CORO_ADDR coroAddr, const void *pParam, int sizeParam);
|
2012-06-07 15:50:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new process with an auto-incrementing Process Id, and a single pointer parameter.
|
|
|
|
*
|
2012-06-07 15:51:58 +00:00
|
|
|
* @param coroAddr Coroutine start address
|
|
|
|
* @param pParam Process specific info
|
2012-06-07 15:50:20 +00:00
|
|
|
*/
|
2012-05-11 13:08:27 +00:00
|
|
|
uint32 createProcess(CORO_ADDR coroAddr, const void *pParam);
|
2012-05-29 11:51:10 +00:00
|
|
|
|
2012-06-07 15:50:20 +00:00
|
|
|
/**
|
|
|
|
* Kills the specified process.
|
|
|
|
*
|
2012-06-07 15:51:58 +00:00
|
|
|
* @param pKillProc Which process to kill
|
2012-06-07 15:50:20 +00:00
|
|
|
*/
|
2012-05-11 13:08:27 +00:00
|
|
|
void killProcess(PROCESS *pKillProc);
|
|
|
|
|
2012-06-07 15:50:20 +00:00
|
|
|
/**
|
|
|
|
* Returns a pointer to the currently running process.
|
|
|
|
*/
|
2012-05-11 13:08:27 +00:00
|
|
|
PROCESS *getCurrentProcess();
|
2012-05-29 11:51:10 +00:00
|
|
|
|
2012-06-07 15:50:20 +00:00
|
|
|
/**
|
|
|
|
* Returns the process identifier of the currently running process.
|
|
|
|
*/
|
2012-05-11 13:08:27 +00:00
|
|
|
int getCurrentPID() const;
|
2012-05-29 11:51:10 +00:00
|
|
|
|
2012-06-07 15:50:20 +00:00
|
|
|
/**
|
|
|
|
* Kills any process matching the specified PID. The current
|
|
|
|
* process cannot be killed.
|
|
|
|
*
|
2012-06-07 15:51:58 +00:00
|
|
|
* @param pidKill Process identifier of process to kill
|
|
|
|
* @param pidMask Mask to apply to process identifiers before comparison
|
|
|
|
* @return The number of processes killed is returned.
|
2012-06-07 15:50:20 +00:00
|
|
|
*/
|
2012-05-11 13:08:27 +00:00
|
|
|
int killMatchingProcess(uint32 pidKill, int pidMask = -1);
|
|
|
|
|
2012-06-07 15:50:20 +00:00
|
|
|
/**
|
|
|
|
* Set pointer to a function to be called by killProcess().
|
|
|
|
*
|
|
|
|
* May be called by a resource allocator, the function supplied is
|
|
|
|
* called by killProcess() to allow the resource allocator to free
|
|
|
|
* resources allocated to the dying process.
|
|
|
|
*
|
2012-06-07 15:51:58 +00:00
|
|
|
* @param pFunc Function to be called by killProcess()
|
2012-06-07 15:50:20 +00:00
|
|
|
*/
|
2012-05-11 13:08:27 +00:00
|
|
|
void setResourceCallback(VFPTRPP pFunc);
|
|
|
|
|
|
|
|
/* Event methods */
|
2012-06-07 15:50:20 +00:00
|
|
|
/**
|
|
|
|
* Creates a new event (semaphore) object
|
|
|
|
*
|
2012-06-07 15:51:58 +00:00
|
|
|
* @param bManualReset Events needs to be manually reset. Otherwise,
|
2012-06-07 15:50:20 +00:00
|
|
|
* events will be automatically reset after a
|
|
|
|
* process waits on the event finishes
|
2012-06-07 15:51:58 +00:00
|
|
|
* @param bInitialState Specifies whether the event is signalled or not
|
2012-06-07 15:50:20 +00:00
|
|
|
* initially
|
|
|
|
*/
|
2012-05-11 13:08:27 +00:00
|
|
|
uint32 createEvent(bool bManualReset, bool bInitialState);
|
2012-05-29 11:51:10 +00:00
|
|
|
|
2012-06-07 15:50:20 +00:00
|
|
|
/**
|
|
|
|
* Destroys the given event
|
2012-06-07 15:51:58 +00:00
|
|
|
* @param pidEvent Event Process Id
|
2012-06-07 15:50:20 +00:00
|
|
|
*/
|
2012-05-11 13:08:27 +00:00
|
|
|
void closeEvent(uint32 pidEvent);
|
2012-05-29 11:51:10 +00:00
|
|
|
|
2012-06-07 15:50:20 +00:00
|
|
|
/**
|
|
|
|
* Sets the event
|
2012-06-07 15:51:58 +00:00
|
|
|
* @param pidEvent Event Process Id
|
2012-06-07 15:50:20 +00:00
|
|
|
*/
|
2012-05-11 13:08:27 +00:00
|
|
|
void setEvent(uint32 pidEvent);
|
2012-05-29 11:51:10 +00:00
|
|
|
|
2012-06-07 15:50:20 +00:00
|
|
|
/**
|
|
|
|
* Resets the event
|
2012-06-07 15:51:58 +00:00
|
|
|
* @param pidEvent Event Process Id
|
2012-06-07 15:50:20 +00:00
|
|
|
*/
|
2012-05-11 13:08:27 +00:00
|
|
|
void resetEvent(uint32 pidEvent);
|
2012-05-29 11:51:10 +00:00
|
|
|
|
2012-06-07 15:50:20 +00:00
|
|
|
/**
|
|
|
|
* Temporarily sets a given event to true, and then runs all waiting
|
|
|
|
* processes,allowing any processes waiting on the event to be fired. It
|
|
|
|
* then immediately resets the event again.
|
|
|
|
*
|
2012-06-07 15:51:58 +00:00
|
|
|
* @param pidEvent Event Process Id
|
2012-06-07 15:50:20 +00:00
|
|
|
*
|
2012-06-07 15:51:58 +00:00
|
|
|
* @remarks Should not be run inside of another process
|
2012-06-07 15:50:20 +00:00
|
|
|
*/
|
2012-05-11 13:08:27 +00:00
|
|
|
void pulseEvent(uint32 pidEvent);
|
|
|
|
};
|
|
|
|
|
2009-04-09 17:07:24 +00:00
|
|
|
//@}
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2012-05-11 13:08:27 +00:00
|
|
|
} // end of namespace Common
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2012-06-07 15:51:58 +00:00
|
|
|
#endif // COMMON_COROUTINES_H
|