mirror of
https://github.com/NeoFlock/neonucleus.git
synced 2025-09-24 09:03:32 +02:00
new call cost system
TODO: change all the ctrls from latency to amount per tick.
This commit is contained in:
parent
3b733e0708
commit
3546fe1093
@ -95,7 +95,7 @@ bool nn_invokeComponentMethod(nn_component *component, const char *name) {
|
|||||||
if(strcmp(method.name, name) == 0) {
|
if(strcmp(method.name, name) == 0) {
|
||||||
nn_callCost(component->computer, NN_CALL_COST);
|
nn_callCost(component->computer, NN_CALL_COST);
|
||||||
if(!method.direct) {
|
if(!method.direct) {
|
||||||
nn_busySleep(NN_INDIRECT_CALL_LATENCY);
|
nn_triggerIndirect(component->computer);
|
||||||
}
|
}
|
||||||
method.method(component->statePtr, method.userdata, component, component->computer);
|
method.method(component->statePtr, method.userdata, component, component->computer);
|
||||||
return true;
|
return true;
|
||||||
@ -105,3 +105,13 @@ bool nn_invokeComponentMethod(nn_component *component, const char *name) {
|
|||||||
// no such method
|
// no such method
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void nn_simulateBufferedIndirect(nn_component *component, double amount, double amountPerTick) {
|
||||||
|
double maximum = 100.0;
|
||||||
|
double x = amount / amountPerTick * maximum;
|
||||||
|
component->indirectBufferProgress += x;
|
||||||
|
if(component->indirectBufferProgress >= maximum) {
|
||||||
|
component->indirectBufferProgress = 0;
|
||||||
|
nn_triggerIndirect(component->computer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -25,6 +25,7 @@ typedef struct nn_componentTable {
|
|||||||
typedef struct nn_component {
|
typedef struct nn_component {
|
||||||
nn_address address;
|
nn_address address;
|
||||||
int slot;
|
int slot;
|
||||||
|
float indirectBufferProgress;
|
||||||
nn_componentTable *table;
|
nn_componentTable *table;
|
||||||
void *statePtr;
|
void *statePtr;
|
||||||
nn_computer *computer;
|
nn_computer *computer;
|
||||||
|
@ -219,24 +219,29 @@ bool nn_isUser(nn_computer *computer, const char *name) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void nn_setCallBudget(nn_computer *computer, size_t callBudget) {
|
void nn_setCallBudget(nn_computer *computer, double callBudget) {
|
||||||
computer->callBudget = callBudget;
|
computer->callBudget = callBudget;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t nn_getCallBudget(nn_computer *computer) {
|
double nn_getCallBudget(nn_computer *computer) {
|
||||||
return computer->callBudget;
|
return computer->callBudget;
|
||||||
}
|
}
|
||||||
|
|
||||||
void nn_callCost(nn_computer *computer, size_t cost) {
|
void nn_callCost(nn_computer *computer, double cost) {
|
||||||
computer->callCost += cost;
|
computer->callCost += cost;
|
||||||
|
if(computer->callCost >= computer->callBudget) nn_triggerIndirect(computer);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t nn_getCallCost(nn_computer *computer) {
|
double nn_getCallCost(nn_computer *computer) {
|
||||||
return computer->callCost;
|
return computer->callCost;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool nn_isOverworked(nn_computer *computer) {
|
bool nn_isOverworked(nn_computer *computer) {
|
||||||
return computer->callCost >= computer->callBudget;
|
return computer->state == NN_STATE_OVERWORKED;
|
||||||
|
}
|
||||||
|
|
||||||
|
void nn_triggerIndirect(nn_computer *computer) {
|
||||||
|
computer->state = NN_STATE_OVERWORKED;
|
||||||
}
|
}
|
||||||
|
|
||||||
int nn_getState(nn_computer *computer) {
|
int nn_getState(nn_computer *computer) {
|
||||||
|
@ -40,8 +40,8 @@ typedef struct nn_computer {
|
|||||||
double temperature;
|
double temperature;
|
||||||
double temperatureCoefficient;
|
double temperatureCoefficient;
|
||||||
double roomTemperature;
|
double roomTemperature;
|
||||||
size_t callCost;
|
double callCost;
|
||||||
size_t callBudget;
|
double callBudget;
|
||||||
} nn_computer;
|
} nn_computer;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -712,9 +712,6 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int state = nn_tickComputer(computer);
|
int state = nn_tickComputer(computer);
|
||||||
if(nn_isOverworked(computer)) {
|
|
||||||
printf("Machine overworked.\n");
|
|
||||||
}
|
|
||||||
if(state == NN_STATE_SWITCH) {
|
if(state == NN_STATE_SWITCH) {
|
||||||
nn_architecture *nextArch = nn_getNextArchitecture(computer);
|
nn_architecture *nextArch = nn_getNextArchitecture(computer);
|
||||||
printf("Next architecture: %s\n", nextArch->archName);
|
printf("Next architecture: %s\n", nextArch->archName);
|
||||||
|
@ -69,7 +69,6 @@ extern "C" {
|
|||||||
#define NN_CALL_HEAT 0.05
|
#define NN_CALL_HEAT 0.05
|
||||||
#define NN_CALL_COST 1
|
#define NN_CALL_COST 1
|
||||||
#define NN_LABEL_SIZE 128
|
#define NN_LABEL_SIZE 128
|
||||||
#define NN_INDIRECT_CALL_LATENCY 0.0005
|
|
||||||
|
|
||||||
typedef struct nn_guard nn_guard;
|
typedef struct nn_guard nn_guard;
|
||||||
typedef atomic_size_t nn_refc;
|
typedef atomic_size_t nn_refc;
|
||||||
@ -235,11 +234,12 @@ const char *nn_addUser(nn_computer *computer, const char *name);
|
|||||||
void nn_deleteUser(nn_computer *computer, const char *name);
|
void nn_deleteUser(nn_computer *computer, const char *name);
|
||||||
const char *nn_indexUser(nn_computer *computer, size_t idx);
|
const char *nn_indexUser(nn_computer *computer, size_t idx);
|
||||||
bool nn_isUser(nn_computer *computer, const char *name);
|
bool nn_isUser(nn_computer *computer, const char *name);
|
||||||
void nn_setCallBudget(nn_computer *computer, size_t callBudget);
|
void nn_setCallBudget(nn_computer *computer, double callBudget);
|
||||||
size_t nn_getCallBudget(nn_computer *computer);
|
double nn_getCallBudget(nn_computer *computer);
|
||||||
void nn_callCost(nn_computer *computer, size_t cost);
|
void nn_callCost(nn_computer *computer, double cost);
|
||||||
size_t nn_getCallCost(nn_computer *computer);
|
double nn_getCallCost(nn_computer *computer);
|
||||||
bool nn_isOverworked(nn_computer *computer);
|
bool nn_isOverworked(nn_computer *computer);
|
||||||
|
void nn_triggerIndirect(nn_computer *computer);
|
||||||
|
|
||||||
/* The memory returned can be freed with nn_free() */
|
/* The memory returned can be freed with nn_free() */
|
||||||
char *nn_serializeProgram(nn_computer *computer, size_t *len);
|
char *nn_serializeProgram(nn_computer *computer, size_t *len);
|
||||||
@ -277,6 +277,9 @@ void nn_unlockComputer(nn_computer *computer);
|
|||||||
/// The architecture is returned by getNextArchitecture.
|
/// The architecture is returned by getNextArchitecture.
|
||||||
#define NN_STATE_SWITCH 6
|
#define NN_STATE_SWITCH 6
|
||||||
|
|
||||||
|
/// The machine is overworked.
|
||||||
|
#define NN_STATE_OVERWORKED 7
|
||||||
|
|
||||||
int nn_getState(nn_computer *computer);
|
int nn_getState(nn_computer *computer);
|
||||||
void nn_setState(nn_computer *computer, int state);
|
void nn_setState(nn_computer *computer, int state);
|
||||||
|
|
||||||
@ -346,6 +349,7 @@ const char *nn_methodDoc(nn_componentTable *table, const char *methodName);
|
|||||||
|
|
||||||
/* Returns false if the method does not exist */
|
/* Returns false if the method does not exist */
|
||||||
bool nn_invokeComponentMethod(nn_component *component, const char *name);
|
bool nn_invokeComponentMethod(nn_component *component, const char *name);
|
||||||
|
void nn_simulateBufferedIndirect(nn_component *component, double amount, double amountPerTick);
|
||||||
void nn_resetCall(nn_computer *computer);
|
void nn_resetCall(nn_computer *computer);
|
||||||
void nn_addArgument(nn_computer *computer, nn_value arg);
|
void nn_addArgument(nn_computer *computer, nn_value arg);
|
||||||
void nn_return(nn_computer *computer, nn_value val);
|
void nn_return(nn_computer *computer, nn_value val);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user