Tuesday, January 13, 2009

tapestry.c

#include "education.h"
#include "genotypic_limits.h"
#include "parenting.h"
#include "phenotypic_limits.h"
#include "social_influences.h"

/* This is set to true at some unknown time (i.e., when SIGTERM is received by
* the process). */
extern bool death_flag;

/* The source code that defines the pain-avoidance task has been lost! All we
* have is the binary and its interface. */
extern task_t pain_avoidance_task;

int main()
{
float pain_coefficient;
enum {
pain_avoidance,
pursuit_of_desire
} context;

/* Initialize this process. The order in which these sets of limitations are
* initialized must not be changed. */
initialize_genetypic_limits();
initialize_parenting();
initialize_education();
initialize_social_influences();
initialize_phenotypic_limits();

/* The initial context value is set to pain avoidance. This is an artifact of
* minor historic significance. It probably doesn't matter what it's set to
* because the process will adjust the value dynamically. */
context = pain_avoidance;

/* Stay alive until SIGTERM. (Hope for no SIGKILL so that we may exit
* gracefully.)
*
* This loop is a negative feedback loop that provides some level of
* homeostatic behavior for the process. Note that the loop is ultimately
* pain-driven. Experimental branch builds have been made in which this loop
* is modified to be desire-driven, but in doing so many bugs of seemingly
* non-deterministic origin are exposed throughout the system. The
* pain-driven model is well tested, and even though most developers don't
* like it, it works. So:
* DON'T MESS WITH THIS LOOP UNLESS YOU REALLY KNOW WHAT YOU'RE DOING.
* -CMB */
while (!death_flag) {

/* Get the current level of pain. Modify the system's behavior based on
* this one value. */
pain_coefficient = get_current_pain_level();
if (pain_coefficient >= TOLERABLE_PAIN_THRESHOLD &&
context != pain_avoidance) {

/* The pain coefficient has risen above its acceptable threshold.
* Suspend all tasks, and keep only the pain-avoidance task running.
*
* Merely increasing the priority of the pain avoidance task and
* decreasing the priorities of other tasks allows for insufficient
* system resources for the pain-avoidance task to result in a
* significant decrease in the pain-avoidance coefficient. All other
* tasks must be fully suspended to spare such system resources. */
suspend_all_tasks();
resume_task(pain_avoidance_task);
context = pain_avoidance;

} else if (pain_coefficient >= TOLERABLE_PAIN_THRESHOLD) {

/* The pain coefficient remains too high. Try decreasing the desire and
* vulnerability coefficients.
*
* Intuitively this seems harmless, but it's not exactly clear why this
* works. The desire and vulnerability coefficients should have no
* effect on the pain-avoidance task.
*
* TODO: Research this interdependency further, albeit in a protected
* sandbox. Testing this is dangerous and could cause the whole system
* to be blown away. */
decrement_desire_coefficient();
decrement_vulnerability_coefficient();

} else if (context != pursuit_of_desire) {

/* The pain coefficient has decreased below its acceptable threshold.
* Resume all tasks.

* Originally the behavior was to suspend the pain-avoidance task, but
* doing so caused the pain and desire coefficients to return quickly
* to intolerable values, so the pain-avoidance task is kept running.
* */
resume_all_tasks();
/* suspend_task(pain_avoidance_task); */
context = pursuit_of_desire;

} else {

/* The pain coefficient remains within its acceptable threshold.
* Increase the desire and vulnerability coefficients.
*
* FIXME: Incrementing the vulnerability coefficient seems to have no
* effect. This system cannot regain vulnerability once lost;
* vulnerability only decreases as the process ages. This is a major
* bug with no known workaround; there's a risk that other projects may
* by started/forked to create a new system without such a fundamental
* flaw if this bug cannot be fixed. This bug was introduced when the
* pain-avoidance task was set never to be suspended. The source code
* for the pain-avoidance task must be found/rewritten to
* analyze/implement a fix. */
increment_desire_coefficient();
increment_vulnerability_coefficient();
}

/* Relinquish the processor for other tasks to run for a while. */
yield_to_other_tasks();
}

/* The process exit value is a somewhat subjective. Other processes don't
* depend on it so just define it as success for now.
*
* TODO: It may someday be a useful feature to make the exit value determined
* dynamically. */
return EXIT_SUCCESS;
} /* main */

1 comment:

Karissa said...

1. Please don't blow away.
2. You and whatever it entails in a sandbox makes me smile.
3. I read on even though it looked confusing and actually caught on to this one.
4. You are very self aware.