From 2d352939d4b4799b6681f3c870aac21fd7d7fa25 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Sat, 13 Feb 2016 17:39:11 +0000 Subject: [PATCH] handled process termination --- stix/lib/exec.c | 71 ++++++++++++++++++++++++++++++++++++++++----- stix/lib/main.c | 5 +++- stix/lib/stix-prv.h | 1 + 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/stix/lib/exec.c b/stix/lib/exec.c index dc71962..e3b44c7 100644 --- a/stix/lib/exec.c +++ b/stix/lib/exec.c @@ -237,6 +237,7 @@ printf ("ADDED FIRST NEW PROCESS - %d\n", (int)1); #if defined(STIX_DEBUG_PROCESSOR) printf ("TOO MANY PROCESS\n"); #endif + stix->errnum = STIX_EPFULL; return -1; } @@ -256,9 +257,60 @@ printf ("ADDED NEW PROCESS - %d\n", (int)tally + 1); return 0; } +static void remove_terminated_process (stix_t* stix, stix_oop_process_t proc) +{ +/* TODO: + * can a main process be killed? + * can the only process be killed? if so, terminate VM??? */ + + if (proc->state == STIX_SMOOI_TO_OOP(1)) + { + stix_ooi_t tally; + + tally = STIX_OOP_TO_SMOOI(stix->processor->tally); + STIX_ASSERT (tally >= 2); /* the main process must not reach here */ + + /* the state must be alive */ + if ((stix_oop_t)proc->prev != stix->_nil) proc->prev->next = proc->next; + else stix->processor->head = proc->next; + if ((stix_oop_t)proc->next != stix->_nil) proc->next->prev = proc->prev; + else stix->processor->tail = proc->prev; + + proc->state = STIX_SMOOI_TO_OOP(-1); /* killed */ + proc->sp = STIX_SMOOI_TO_OOP(-1); /* invalidate the process stack */ + + tally--; + stix->processor->tally = STIX_SMOOI_TO_OOP(tally); +/* TODO: allow the last process to be killed like this??? */ + if (tally <= 0) + { + stix->processor->active = stix->nil_process; + } + else + { + stix_oop_process_t new_proc; + + if ((stix_oop_t)proc->next == stix->_nil) + new_proc = stix->processor->head; + else new_proc = proc->next; + + switch_process (stix, new_proc); + } + + proc->prev = (stix_oop_process_t)stix->_nil; + proc->next = (stix_oop_process_t)stix->_nil; + } +} + static int schedule_process (stix_t* stix, stix_oop_process_t proc) { - if (proc->state == STIX_SMOOI_TO_OOP(0)) + if (proc->state == STIX_SMOOI_TO_OOP(-1)) + { + /* the process is terminated already */ + stix->errnum = STIX_EINVAL; /* TODO: more specialized error code? */ + return -1; + } + else if (proc->state == STIX_SMOOI_TO_OOP(0)) { /* the process is not scheduled at all. it must not exist in the * process list of the process scheduler. */ @@ -1479,8 +1531,9 @@ static int prim_processor_schedule (stix_t* stix, stix_ooi_t nargs) return 0; } - if (!schedule_process (stix, (stix_oop_process_t)arg)) + if (schedule_process (stix, (stix_oop_process_t)arg) <= -1) { +printf ("PROCESS SCHEDULE FAILURE...\n"); /* TODO: Can this be a soft failure? */ return (stix->errnum == STIX_EPFULL)? 0: -1; } @@ -2851,7 +2904,8 @@ printf ("<> SP=%d\n", (int)stix->sp); if (stix->processor->active->initial_context == stix->active_context) { /* TODO: terminate a process... */ -printf ("TERMINATING XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); +printf ("TERMINATING A PROCESS RETURNING\n"); + remove_terminated_process (stix, stix->processor->active); } else { @@ -2862,7 +2916,7 @@ printf ("TERMINATING XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); if (stix->active_context->sender == stix->_nil) { - /* the sending context of the intial context has been set to nil. + /* the sender of the intial context is nil. * use this fact to tell an initial context from a normal context. */ STIX_ASSERT (stix->active_context->receiver_or_source == stix->_nil); #if defined(STIX_DEBUG_EXEC_001) @@ -2870,10 +2924,12 @@ printf ("<<>>\n"); #endif printf ("TERMINATING SP.... %ld\n", (long int)stix->sp); + #if defined(STIX_USE_PROCSTK) - /* nothing special */ + /* TODO: */ #else - STIX_ASSERT (stix->sp == 0); + /* the stack contains the final return value so the stack pointer must be 0. */ + STIX_ASSERT (stix->sp == 0); #endif goto done; } @@ -2889,7 +2945,8 @@ printf ("TERMINATING SP.... %ld\n", (long int)stix->sp); if (stix->active_context == stix->processor->active->initial_context) { /* TODO: terminate the process. can this happen? */ -printf ("TERMINATE A PROCESS............\n"); +printf ("TERMINATE A PROCESS RETURNING FROM BLOCK\n"); + remove_terminated_process (stix, stix->processor->active); /* **************************************** */ } else diff --git a/stix/lib/main.c b/stix/lib/main.c index 9d82d55..1240702 100644 --- a/stix/lib/main.c +++ b/stix/lib/main.c @@ -449,7 +449,7 @@ int main (int argc, char* argv[]) stix_setoption (stix, STIX_SYMTAB_SIZE, &tab_size); tab_size = 5000; stix_setoption (stix, STIX_SYSDIC_SIZE, &tab_size); - tab_size = 60; + tab_size = 600; stix_setoption (stix, STIX_PROCSTK_SIZE, &tab_size); } @@ -599,6 +599,9 @@ printf ("%p\n", a); { printf ("ERROR: cannot execute code - %d\n", stix_geterrnum(stix)); stix_close (stix); +#if defined(USE_LTDL) + lt_dlexit (); +#endif return -1; } diff --git a/stix/lib/stix-prv.h b/stix/lib/stix-prv.h index 28dd3cb..bf19870 100644 --- a/stix/lib/stix-prv.h +++ b/stix/lib/stix-prv.h @@ -57,6 +57,7 @@ #define STIX_DEBUG_GC_001 /*#define STIX_DEBUG_GC_002*/ #define STIX_DEBUG_COMP_001 +/*#define STIX_DEBUG_COMP_002*/ /*#define STIX_DEBUG_EXEC_001*/ /*#define STIX_DEBUG_EXEC_002*/ #define STIX_PROFILE_EXEC