Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

Commit

Permalink
using #uniqueInstance instead #new
Browse files Browse the repository at this point in the history
  • Loading branch information
estebanlm committed Nov 4, 2019
1 parent c70a16d commit d474218
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/ThreadedFFI-Tests/TFTestCase.class.st
Expand Up @@ -11,7 +11,7 @@ Class {
TFTestCase class >> testParameters [

^ ParametrizedTestMatrix new
forSelector: #runner addOptions: { [ TFWorker named: 'fortest' ]. [ TFSameThreadRunner new ] }
forSelector: #runner addOptions: { [ TFWorker named: 'fortest' ]. [ TFSameThreadRunner uniqueInstance ] }
]

{ #category : #accessing }
Expand Down
Expand Up @@ -19,7 +19,7 @@ TFTestLibraryUsingSameThreadRunner >> macLibraryName [
{ #category : #accessing }
TFTestLibraryUsingSameThreadRunner >> runner [

^ TFSameThreadRunner new
^ TFSameThreadRunner uniqueInstance
]

{ #category : #'accessing platform' }
Expand Down
9 changes: 9 additions & 0 deletions src/ThreadedFFI/TFMainThreadRunner.class.st
Expand Up @@ -4,9 +4,18 @@ Class {
#classVars : [
'MainThreadHandle'
],
#classInstVars : [
'uniqueInstance'
],
#category : #'ThreadedFFI-Worker'
}

{ #category : #accessing }
TFMainThreadRunner class >> uniqueInstance [

^ uniqueInstance ifNil: [ uniqueInstance := self basicNew initialize ]
]

{ #category : #private }
TFMainThreadRunner >> doInitialize [

Expand Down
34 changes: 32 additions & 2 deletions src/ThreadedFFI/TFRunner.class.st
Expand Up @@ -4,7 +4,8 @@ Class {
#instVars : [
'callbackInvocationStack',
'stackProtect',
'exceptionHandler'
'exceptionHandler',
'callbackReturnSemaphore'
],
#category : #'ThreadedFFI-Worker'
}
Expand Down Expand Up @@ -112,7 +113,7 @@ TFRunner >> release [
]

{ #category : #executing }
TFRunner >> returnCallback: aCallbackInvocation [
TFRunner >> returnCallback2: aCallbackInvocation [
"Entry point to return a callback invocation.
Check the callbackInvocation stack to see if the given invocation is the last one.
If so, it can return safely.
Expand All @@ -127,3 +128,32 @@ TFRunner >> returnCallback: aCallbackInvocation [
aCallbackInvocation returnExecution.
callbackInvocationStack pop ]
]

{ #category : #executing }
TFRunner >> returnCallback: aCallbackInvocation [
"Entry point to return a callback invocation.
Check the callbackInvocation stack to see if the given invocation is the last one.
If so, it can return safely.
Otherwise, throw an exception as returning means a bug in your application.
The user must guarantee callbacks return in the correct order"

callbackReturnSemaphore ifNil: [
callbackReturnSemaphore := Semaphore new ].

stackProtect critical: [
callbackInvocationStack ifNil: [
^ self cannotReturnCallbackFromOldSession: aCallbackInvocation ].
(callbackInvocationStack top == aCallbackInvocation)
ifTrue: [
aCallbackInvocation returnExecution.
callbackInvocationStack pop.
[
[ callbackReturnSemaphore isEmpty ]
whileFalse: [ callbackReturnSemaphore signal ]
] valueUnpreemptively.
^ self ] ].

"failed"
callbackReturnSemaphore wait.
self returnCallback: aCallbackInvocation
]
9 changes: 9 additions & 0 deletions src/ThreadedFFI/TFSameThreadRunner.class.st
@@ -1,9 +1,18 @@
Class {
#name : #TFSameThreadRunner,
#superclass : #TFRunner,
#classInstVars : [
'uniqueInstance'
],
#category : #'ThreadedFFI-Worker'
}

{ #category : #accessing }
TFSameThreadRunner class >> uniqueInstance [

^ uniqueInstance ifNil: [ uniqueInstance := self basicNew initialize ]
]

{ #category : #executing }
TFSameThreadRunner >> doInitialize [

Expand Down

0 comments on commit d474218

Please sign in to comment.