From 55a43371d5358d01e9bae26db54b9e7291989f90 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Mon, 29 Feb 2016 15:27:10 +0000 Subject: [PATCH] added some code for implementing semaphore --- stix/kernel/Number.st | 37 ------------------------- stix/kernel/Process.st | 1 + stix/kernel/Stix.st | 14 ++++++++-- stix/lib/exec.c | 61 +++++++++++++++++++++++++++++++++--------- stix/lib/gc.c | 5 ++++ stix/lib/stix-prv.h | 2 +- stix/lib/stix.h | 3 +++ 7 files changed, 71 insertions(+), 52 deletions(-) delete mode 100644 stix/kernel/Number.st diff --git a/stix/kernel/Number.st b/stix/kernel/Number.st deleted file mode 100644 index 5c1978a..0000000 --- a/stix/kernel/Number.st +++ /dev/null @@ -1,37 +0,0 @@ -#class Number(Magnitude) -{ - #method add: aNumber - { - - } - - #method + aNumber - { - - } - - #method - aNumber - { - - } - - #method * aNumber - { - - } - - #method = aNumber - { - - } - - #method < aNumber - { - - } - - #method > aNumber - { - - } -} diff --git a/stix/kernel/Process.st b/stix/kernel/Process.st index 9d11bd8..a8a2c04 100644 --- a/stix/kernel/Process.st +++ b/stix/kernel/Process.st @@ -88,6 +88,7 @@ #method initialize { + count := 0. } #method signal diff --git a/stix/kernel/Stix.st b/stix/kernel/Stix.st index 00a9e85..cab4f06 100644 --- a/stix/kernel/Stix.st +++ b/stix/kernel/Stix.st @@ -199,13 +199,23 @@ self > 0 ifTrue: [^1]. ^0. } + } -#class SmallInteger(Number) + +#class Integer(Number) +{ + #method timesRepeat: aBlock + { + 1 to: self by: 1 do: [ :count | aBlock value ]. + } +} + +#class SmallInteger(Integer) { } -#class(#liword) LargeInteger(Number) +#class(#liword) LargeInteger(Integer) { } diff --git a/stix/lib/exec.c b/stix/lib/exec.c index 8378425..caf0199 100644 --- a/stix/lib/exec.c +++ b/stix/lib/exec.c @@ -160,7 +160,7 @@ printf ("PROCESS %p SIZE => %ld\n", proc, (long int)STIX_OBJ_GET_SIZE(proc)); return proc; } -static void switch_to_process (stix_t* stix, stix_oop_process_t proc) +static void switch_to_process (stix_t* stix, stix_oop_process_t proc, int new_state_for_old_active) { /* the new process must not be the currently active process */ STIX_ASSERT (stix->processor->active != proc); @@ -183,7 +183,8 @@ printf ("ACTUAL PROCESS SWITCHING BF...%d %p\n", (int)stix->ip, stix->active_con * it is the suspended context of the process to be suspended */ STIX_ASSERT ((stix_oop_t)stix->processor->active != stix->_nil); stix->processor->active->current_context = stix->active_context; - stix->processor->active->state = STIX_SMOOI_TO_OOP(PROCESS_STATE_RUNNABLE); + /*stix->processor->active->state = STIX_SMOOI_TO_OOP(PROCESS_STATE_RUNNABLE);*/ + stix->processor->active->state = STIX_SMOOI_TO_OOP(new_state_for_old_active); /* activate the given process */ proc->state = STIX_SMOOI_TO_OOP(PROCESS_STATE_RUNNING); @@ -219,7 +220,7 @@ static STIX_INLINE void switch_to_next_runnable_process (stix_t* stix) stix_oop_process_t nrp; nrp = find_next_runnable_process (stix); - if (nrp != stix->processor->active) switch_to_process (stix, nrp); + if (nrp != stix->processor->active) switch_to_process (stix, nrp, PROCESS_STATE_RUNNABLE); } static STIX_INLINE int chain_into_processor (stix_t* stix, stix_oop_process_t proc) @@ -306,7 +307,7 @@ static void terminate_process (stix_t* stix, stix_oop_process_t proc) } else { - switch_to_process (stix, nrp); + switch_to_process (stix, nrp, PROCESS_STATE_TERMINATED); } } else @@ -331,6 +332,7 @@ static void terminate_process (stix_t* stix, stix_oop_process_t proc) static void resume_process (stix_t* stix, stix_oop_process_t proc) { +printf ("TO RESUME PROCESS = %p %d\n", proc, STIX_OOP_TO_SMOOI(proc->state)); if (proc->state == STIX_SMOOI_TO_OOP(PROCESS_STATE_SUSPENDED)) { /* SUSPENED ---> RUNNING */ @@ -340,7 +342,7 @@ static void resume_process (stix_t* stix, stix_oop_process_t proc) chain_into_processor (stix, proc); /* TODO: error check */ - proc->current_context = proc->initial_context; + /*proc->current_context = proc->initial_context;*/ proc->state = STIX_SMOOI_TO_OOP(PROCESS_STATE_RUNNABLE); /*switch_to_process (stix, proc);*/ @@ -351,7 +353,7 @@ static void resume_process (stix_t* stix, stix_oop_process_t proc) /* RUNNABLE ---> RUNNING */ /* TODO: should i allow this? */ STIX_ASSERT (stix->processor->active != proc); - switch_to_process (stix, proc); + switch_to_process (stix, proc, PROCESS_STATE_RUNNABLE); } #endif } @@ -370,17 +372,19 @@ static void suspend_process (stix_t* stix, stix_oop_process_t proc) nrp = find_next_runnable_process (stix); unchain_from_processor (stix, proc); - proc->state = STIX_SMOOI_TO_OOP(PROCESS_STATE_SUSPENDED); - proc->current_context = stix->active_context; - +printf ("TO SUSPEND...%p %d\n", proc, (int)STIX_OOP_TO_SMOOI(proc->state)); if (nrp == proc) { /* no runnable process after suspension */ STIX_ASSERT (stix->processor->active == stix->nil_process); +printf ("NO RUNNABLE PROCESS AFTER SUPSENDISION\n"); + + proc->state = STIX_SMOOI_TO_OOP(PROCESS_STATE_SUSPENDED); + proc->current_context = stix->active_context; } else { - switch_to_process (stix, nrp); + switch_to_process (stix, nrp, PROCESS_STATE_SUSPENDED); } } else @@ -404,10 +408,28 @@ static void yield_process (stix_t* stix, stix_oop_process_t proc) nrp = find_next_runnable_process (stix); /* if there are more than 1 runnable processes, the next * runnable process must be different from proc */ - if (nrp != proc) switch_to_process (stix, proc); + if (nrp != proc) + { + switch_to_process (stix, nrp, PROCESS_STATE_RUNNABLE); + } } } +static int async_signal_semaphore (stix_t* stix, stix_oop_semaphore_t sem) +{ + if (stix->sem_count > STIX_COUNTOF(stix->sem_list)) + { + /* TOO MANY ASYNC SEMAPHORES.. */ + /* TODO: PROPER ERROR HANDLING */ + stix->errnum = STIX_ESYSMEM; + return -1; + } + + stix->sem_list[stix->sem_count] = sem; + stix->sem_count++; + return 0; +} + static void signal_semaphore (stix_t* stix, stix_oop_semaphore_t sem) { stix_oop_process_t proc; @@ -415,12 +437,15 @@ static void signal_semaphore (stix_t* stix, stix_oop_semaphore_t sem) if ((stix_oop_t)sem->waiting_head == stix->_nil) { +printf ("signal semaphore...1111\n"); + /* no process is waiting on this semaphore */ count = STIX_OOP_TO_SMOOI(sem->count); count++; sem->count = STIX_SMOOI_TO_OOP(count); } else { +printf ("signal semaphore...2222\n"); proc = sem->waiting_head; sem->waiting_head = proc->sem_next; if ((stix_oop_t)sem->waiting_head == stix->_nil) @@ -439,14 +464,18 @@ static void await_semaphore (stix_t* stix, stix_oop_semaphore_t sem) count = STIX_OOP_TO_SMOOI(sem->count); if (count > 0) { + /* it's already signalled */ count--; sem->count = STIX_SMOOI_TO_OOP(count); +printf (">>>>>>>>>>>>>> AWAIT SEMAPHORE - NO SUSPENDING ...................\n"); } else { + /* not signaled. need to wait */ proc = stix->processor->active; STIX_ASSERT ((stix_oop_t)proc->sem_next == stix->_nil); + /* add the active process to the semaphore's waiting list */ if ((stix_oop_t)sem->waiting_tail == stix->_nil) { STIX_ASSERT ((stix_oop_t)sem->waiting_head == stix->_nil); @@ -459,7 +488,8 @@ static void await_semaphore (stix_t* stix, stix_oop_semaphore_t sem) } sem->waiting_tail = proc; - suspend_process (stix, proc); +printf (">>>>>>>>>>>>>> AWAIT SEMAPHORE - SUEPENDING PROCESS...............\n"); + suspend_process (stix, proc); /* suspend the active process */ STIX_ASSERT (stix->processor->active != proc); } } @@ -2496,6 +2526,7 @@ int stix_execute (stix_t* stix) { /* no more process in the system */ STIX_ASSERT (stix->processor->tally = STIX_SMOOI_TO_OOP(0)); +printf ("NO MORE RUNNABLE PROCESS...\n"); break; } /* @@ -2505,6 +2536,12 @@ IDLEING WAITING FOR PROCESS WAKE-UP... } */ + while (stix->sem_count > 0) + { + --stix->sem_count; + signal_semaphore (stix, stix->sem_list[stix->sem_count]); + } + /* TODO: implement different process switching scheme - time-slice or clock based??? */ if (!stix->proc_switched) { switch_to_next_runnable_process (stix); } stix->proc_switched = 0; diff --git a/stix/lib/gc.c b/stix/lib/gc.c index e0070d0..0583858 100644 --- a/stix/lib/gc.c +++ b/stix/lib/gc.c @@ -339,6 +339,11 @@ printf ("STARTING GC curheap base %p ptr %p newheap base %p ptr %p\n", stix->processor = (stix_oop_process_scheduler_t) stix_moveoop (stix, (stix_oop_t)stix->processor); stix->nil_process = (stix_oop_process_t) stix_moveoop (stix, (stix_oop_t)stix->nil_process); + for (i = 0; i < stix->sem_count; i++) + { + stix->sem_list[i] = stix_moveoop (stix, stix->sem_list[i]); + } + for (i = 0; i < stix->tmp_count; i++) { *stix->tmp_stack[i] = stix_moveoop (stix, *stix->tmp_stack[i]); diff --git a/stix/lib/stix-prv.h b/stix/lib/stix-prv.h index 114d2ae..a0a8f1b 100644 --- a/stix/lib/stix-prv.h +++ b/stix/lib/stix-prv.h @@ -54,7 +54,7 @@ /* this is for gc debugging */ /*#define STIX_DEBUG_PROCESSOR*/ -#define STIX_DEBUG_GC_001 +/*#define STIX_DEBUG_GC_001*/ /*#define STIX_DEBUG_GC_002*/ #define STIX_DEBUG_COMP_001 /*#define STIX_DEBUG_COMP_002*/ diff --git a/stix/lib/stix.h b/stix/lib/stix.h index 1757db6..d17f1b9 100644 --- a/stix/lib/stix.h +++ b/stix/lib/stix.h @@ -766,6 +766,9 @@ struct stix_t stix_oop_process_scheduler_t processor; /* instance of ProcessScheduler */ stix_oop_process_t nil_process; /* instance of Process */ + stix_oop_semaphore_t sem_list[256]; /* TODO: make it dynamic */ + stix_oow_t sem_count; + stix_oop_t* tmp_stack[256]; /* stack for temporaries */ stix_oow_t tmp_count;