diff --git a/src/component.c b/src/component.c index 8c45753..181f9a1 100644 --- a/src/component.c +++ b/src/component.c @@ -84,6 +84,10 @@ bool nn_invokeComponentMethod(nn_component *component, const char *name) { for(size_t i = 0; i < table->methodCount; i++) { nn_method method = table->methods[i]; if(strcmp(method.name, name) == 0) { + nn_callCost(component->computer, NN_CALL_COST); + if(!method.direct) { + nn_busySleep(NN_INDIRECT_CALL_LATENCY); + } method.method(component->statePtr, method.userdata, component, component->computer); return true; } diff --git a/src/neonucleus.h b/src/neonucleus.h index 39b8718..bca577d 100644 --- a/src/neonucleus.h +++ b/src/neonucleus.h @@ -62,6 +62,7 @@ #define NN_CALL_HEAT 0.05 #define NN_CALL_COST 1 #define NN_LABEL_SIZE 128 +#define NN_INDIRECT_CALL_LATENCY 0.05 typedef struct nn_guard nn_guard; typedef struct nn_universe nn_universe; @@ -144,6 +145,8 @@ void nn_deleteGuard(nn_guard *guard); double nn_realTime(); double nn_realTimeClock(void *_); +/* Will busy-loop until the time passes. This is meant for computed latencies in components. */ +void nn_busySleep(double t); typedef double nn_clock_t(void *_); diff --git a/src/testLuaArch.c b/src/testLuaArch.c index 9a83239..809389a 100644 --- a/src/testLuaArch.c +++ b/src/testLuaArch.c @@ -205,6 +205,12 @@ static int testLuaArch_computer_setArchitecture(lua_State *L) { return 0; } +static int testLuaArch_computer_isOverworked(lua_State *L) { + nn_computer *c = testLuaArch_getComputer(L); + lua_pushboolean(L, nn_isOverworked(c)); + return 1; +} + static int testLuaArch_computer_isOverheating(lua_State *L) { nn_computer *c = testLuaArch_getComputer(L); lua_pushboolean(L, nn_isOverheating(c)); @@ -426,6 +432,8 @@ void testLuaArch_loadEnv(lua_State *L) { lua_setfield(L, computer, "getArchitectures"); lua_pushcfunction(L, testLuaArch_computer_setArchitecture); lua_setfield(L, computer, "setArchitecture"); + lua_pushcfunction(L, testLuaArch_computer_isOverworked); + lua_setfield(L, computer, "isOverworked"); lua_pushcfunction(L, testLuaArch_computer_isOverheating); lua_setfield(L, computer, "isOverheating"); lua_pushcfunction(L, testLuaArch_computer_getTemperature); diff --git a/src/utils.c b/src/utils.c index fe2539c..d59e065 100644 --- a/src/utils.c +++ b/src/utils.c @@ -59,3 +59,8 @@ double nn_realTime() { double nn_realTimeClock(void *_) { return nn_realTime(); } + +void nn_busySleep(double t) { + double deadline = nn_realTime() + t; + while(nn_realTime() < deadline) {} +}