19 |
|
|
20 |
#define DEBUG_SCRIPTVM_CORE 0 |
#define DEBUG_SCRIPTVM_CORE 0 |
21 |
|
|
22 |
|
/** |
23 |
|
* Maximum amount of VM instructions to be executed per ScriptVM::exec() call |
24 |
|
* in case loops are involved, before the script got automatically suspended |
25 |
|
* for a certain amount of time to avoid any RT instability issues. |
26 |
|
* |
27 |
|
* The following value takes a max. execution time of 300 microseconds as aimed |
28 |
|
* target, assuming an execution time of approximately 5 microseconds per |
29 |
|
* instruction this leads to the very approximate value set below. |
30 |
|
*/ |
31 |
|
#define SCRIPTVM_MAX_INSTR_PER_CYCLE_SOFT 70 |
32 |
|
|
33 |
|
/** |
34 |
|
* Absolute maximum amount of VM instructions to be executed per |
35 |
|
* ScriptVM::exec() call (even if no loops are involved), before the script got |
36 |
|
* automatically suspended for a certain amount of time to avoid any RT |
37 |
|
* instability issues. |
38 |
|
* |
39 |
|
* A distinction between "soft" and "hard" limit is done here ATM because a |
40 |
|
* script author typically expects that his script might be interrupted |
41 |
|
* automatically if he is using while() loops, however he might not be |
42 |
|
* prepared that his script might also be interrupted if no loop is involved |
43 |
|
* (i.e. on very large scripts). |
44 |
|
* |
45 |
|
* The following value takes a max. execution time of 1000 microseconds as |
46 |
|
* aimed target, assuming an execution time of approximately 5 microseconds per |
47 |
|
* instruction this leads to the very approximate value set below. |
48 |
|
*/ |
49 |
|
#define SCRIPTVM_MAX_INSTR_PER_CYCLE_HARD 210 |
50 |
|
|
51 |
|
/** |
52 |
|
* In case either SCRIPTVM_MAX_INSTR_PER_CYCLE_SOFT or |
53 |
|
* SCRIPTVM_MAX_INSTR_PER_CYCLE_HARD was exceeded when calling |
54 |
|
* ScriptVM::exec() : the amount of microseconds the respective script |
55 |
|
* execution instance should be automatically suspended by the VM. |
56 |
|
*/ |
57 |
|
#define SCRIPT_VM_FORCE_SUSPENSION_MICROSECONDS 1000 |
58 |
|
|
59 |
int InstrScript_parse(LinuxSampler::ParserContext*); |
int InstrScript_parse(LinuxSampler::ParserContext*); |
60 |
|
|
61 |
namespace LinuxSampler { |
namespace LinuxSampler { |
129 |
return max; |
return max; |
130 |
} |
} |
131 |
|
|
132 |
ScriptVM::ScriptVM() : m_eventHandler(NULL), m_parserContext(NULL) { |
ScriptVM::ScriptVM() : m_eventHandler(NULL), m_parserContext(NULL), m_autoSuspend(true) { |
133 |
m_fnMessage = new CoreVMFunction_message; |
m_fnMessage = new CoreVMFunction_message; |
134 |
m_fnExit = new CoreVMFunction_exit; |
m_fnExit = new CoreVMFunction_exit; |
135 |
m_fnWait = new CoreVMFunction_wait(this); |
m_fnWait = new CoreVMFunction_wait(this); |
309 |
return m_parserContext->execContext; |
return m_parserContext->execContext; |
310 |
} |
} |
311 |
|
|
312 |
|
void ScriptVM::setAutoSuspendEnabled(bool b) { |
313 |
|
m_autoSuspend = b; |
314 |
|
} |
315 |
|
|
316 |
|
bool ScriptVM::isAutoSuspendEnabled() const { |
317 |
|
return m_autoSuspend; |
318 |
|
} |
319 |
|
|
320 |
VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) { |
VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) { |
321 |
m_parserContext = dynamic_cast<ParserContext*>(parserContext); |
m_parserContext = dynamic_cast<ParserContext*>(parserContext); |
322 |
if (!m_parserContext) { |
if (!m_parserContext) { |
340 |
|
|
341 |
ctx->status = VM_EXEC_RUNNING; |
ctx->status = VM_EXEC_RUNNING; |
342 |
StmtFlags_t flags = STMT_SUCCESS; |
StmtFlags_t flags = STMT_SUCCESS; |
343 |
|
int instructionsCounter = 0; |
344 |
|
|
345 |
int& frameIdx = ctx->stackFrame; |
int& frameIdx = ctx->stackFrame; |
346 |
if (frameIdx < 0) { // start condition ... |
if (frameIdx < 0) { // start condition ... |
417 |
ctx->pushStack( |
ctx->pushStack( |
418 |
whileStmt->statements() |
whileStmt->statements() |
419 |
); |
); |
420 |
|
if (flags == STMT_SUCCESS && m_autoSuspend && |
421 |
|
instructionsCounter > SCRIPTVM_MAX_INSTR_PER_CYCLE_SOFT) |
422 |
|
{ |
423 |
|
flags = StmtFlags_t(STMT_SUSPEND_SIGNALLED); |
424 |
|
ctx->suspendMicroseconds = SCRIPT_VM_FORCE_SUSPENSION_MICROSECONDS; |
425 |
|
} |
426 |
} else ctx->popStack(); |
} else ctx->popStack(); |
427 |
|
break; |
428 |
} |
} |
429 |
} |
} |
430 |
|
|
431 |
|
if (flags == STMT_SUCCESS && m_autoSuspend && |
432 |
|
instructionsCounter > SCRIPTVM_MAX_INSTR_PER_CYCLE_HARD) |
433 |
|
{ |
434 |
|
flags = StmtFlags_t(STMT_SUSPEND_SIGNALLED); |
435 |
|
ctx->suspendMicroseconds = SCRIPT_VM_FORCE_SUSPENSION_MICROSECONDS; |
436 |
|
} |
437 |
|
|
438 |
|
++instructionsCounter; |
439 |
} |
} |
440 |
|
|
441 |
if (flags & STMT_SUSPEND_SIGNALLED) { |
if (flags & STMT_SUSPEND_SIGNALLED) { |