added experimental code that implements singal callback in semaphore
This commit is contained in:
		| @ -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.  | ||||
|  | ||||
| @ -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'. | ||||
|  | ||||
| *) | ||||
|  | ||||
| @ -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. | ||||
|  | ||||
|  | ||||
		Reference in New Issue
	
	Block a user