From 1fd9ce285c8144fe69c36585f141f16bfa745af7 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Thu, 5 Oct 2017 17:14:38 +0000 Subject: [PATCH] added experimental code that implements singal callback in semaphore --- moo/kernel/Process.moo | 24 +++++++++++++++++++--- moo/kernel/test-002.moo | 44 ++++++++++++++++++++++++++++++++++++----- moo/kernel/test-003.moo | 31 +---------------------------- moo/lib/exec.c | 4 ++-- moo/lib/moo.h | 3 ++- 5 files changed, 65 insertions(+), 41 deletions(-) diff --git a/moo/kernel/Process.moo b/moo/kernel/Process.moo index deaaae7..7eb4390 100644 --- a/moo/kernel/Process.moo +++ b/moo/kernel/Process.moo @@ -60,12 +60,21 @@ class Semaphore(Object) ioHandle := nil, ioMask := 0. + var (#get,#set) signalAction := nil. var(#get,#set) _group := nil. ## ================================================================== method(#primitive) signal. - method(#primitive) wait. + method(#primitive) _wait. + + method wait + { + | k | + k := self _wait. + if (self.signalAction notNil) { self.signalAction value: self }. + ^k + } ## ================================================================== @@ -246,7 +255,15 @@ method(#class,#abstract) xxx. => method(#class) xxx { self subclassResponsibilit } } - method(#primitive) wait. + method(#primitive) _wait. + + method wait + { + | r | + r := self wait. + if (r signalAction notNil) { r signalAction value: r }. + ^r + } method waitWithTimeout: seconds { @@ -267,7 +284,8 @@ method(#class,#abstract) xxx. => method(#class) xxx { self subclassResponsibilit ## if the internal semaphore has been signaled, ## arrange to return nil to indicate timeout. - if (r == s) { r := nil }. + if (r == s) { r := nil } + elsif (r signalAction notNil) { r signalAction value: r }. ## nullify the membership s _group: nil. diff --git a/moo/kernel/test-002.moo b/moo/kernel/test-002.moo index 2e3349d..2b2ef8f 100644 --- a/moo/kernel/test-002.moo +++ b/moo/kernel/test-002.moo @@ -7,8 +7,6 @@ class MyObject(Object) { -## TODO: support import in extend?? - var(#class) a := 100. method(#class) proc1 @@ -36,7 +34,6 @@ class MyObject(Object) sempq deleteAt: 50. sempq deleteAt: 100. - a := -100. [sempq size > 0] whileTrue: [ | sem b | @@ -85,6 +82,27 @@ class MyObject(Object) ^%( v, p ) ## v must be 2000, p must be 6000 } +(* + method(#class) test_sem_sig + { + | s | + s := Semaphore new. + s signalAction: [:sem | 'SIGNAL ACTION............' dump. ]. + [ Processor sleepFor: 1. s signal ] fork. + s wait. + } + + method(#class) test_semgrp + { + | sg | + sg := SemaphoreGroup new. + sg add: s1 withAction: []. + sg add: s2 withAction: []. + sg add: s3 withAction: []. + sg wait. + } +*) + method(#class) main { | tc limit | @@ -94,7 +112,9 @@ class MyObject(Object) [ self proc1 == 100 ], [ Processor sleepFor: 2. self proc1 == 200 ], [ self test_semaphore_heap == true ], - [ self test_mutex = #(2000 6000) ] + [ self test_mutex = #(2000 6000) ], + ####[ self test_sem_sig ], + [ a == 300 ] ). limit := tc size. @@ -105,5 +125,19 @@ class MyObject(Object) System log(System.Log.INFO, idx asString, (if (tb value) { ' PASS' } else { ' FAIL' }), S'\n'). ]. } - } + + +(* +s1 := TcpSocket new. + +s1 onEvent: #connected do: [ + s1 write: C'GET / HTTP/1.0\n\r'. +] +s1 onEvent: #written do: [ +] + +s1 on: #read do: +s1 connectTo: '1.2.3.4:45'. + +*) diff --git a/moo/kernel/test-003.moo b/moo/kernel/test-003.moo index db713a4..28fb155 100644 --- a/moo/kernel/test-003.moo +++ b/moo/kernel/test-003.moo @@ -8,36 +8,7 @@ ## MAIN ################################################################# -## TODO: use #define to define a class or use #class to define a class. -## use #extend to extend a class -## using #class for both feels confusing. - -extend Apex -{ - -} - -extend SmallInteger -{ - method getTrue: anInteger - { - ^anInteger + 9999. - } - - method inc - { - ^self + 1. - } -} - -class TestObject(Object) -{ - var(#class) Q, R. - var(#classinst) a1, a2. -} - - -class MyObject(TestObject) +class MyObject(Object) { var(#classinst) t1, t2. diff --git a/moo/lib/exec.c b/moo/lib/exec.c index adf4dde..72507e2 100644 --- a/moo/lib/exec.c +++ b/moo/lib/exec.c @@ -4243,8 +4243,8 @@ static pf_t pftab[] = { "Process__terminate", { pf_process_terminate, 0, 0 } }, { "Semaphore_signal", { pf_semaphore_signal, 0, 0 } }, - { "Semaphore_wait", { pf_semaphore_wait, 0, 0 } }, - { "SemaphoreGroup_wait", { pf_semaphore_group_wait, 0, 0 } }, + { "Semaphore__wait", { pf_semaphore_wait, 0, 0 } }, + { "SemaphoreGroup__wait", { pf_semaphore_group_wait, 0, 0 } }, { "SmallInteger_asCharacter", { pf_smooi_as_character, 0, 0 } }, { "SmallInteger_asError", { pf_smooi_as_error, 0, 0 } }, diff --git a/moo/lib/moo.h b/moo/lib/moo.h index 2ca74bd..330c885 100644 --- a/moo/lib/moo.h +++ b/moo/lib/moo.h @@ -748,7 +748,7 @@ struct moo_context_t typedef struct moo_process_t moo_process_t; typedef struct moo_process_t* moo_oop_process_t; -#define MOO_SEMAPHORE_NAMED_INSTVARS 10 +#define MOO_SEMAPHORE_NAMED_INSTVARS 11 typedef struct moo_semaphore_t moo_semaphore_t; typedef struct moo_semaphore_t* moo_oop_semaphore_t; @@ -814,6 +814,7 @@ struct moo_semaphore_t moo_oop_t io_handle; moo_oop_t io_mask; /* SmallInteger */ + moo_oop_t signal_action; moo_oop_semaphore_group_t group; /* nil or belonging semaphore group */ };