Skip to content

Instantly share code, notes, and snippets.

@smarr
Last active August 9, 2016 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smarr/dc2804d97451836c15fc41f40b4947f5 to your computer and use it in GitHub Desktop.
Save smarr/dc2804d97451836c15fc41f40b4947f5 to your computer and use it in GitHub Desktop.
class StackApp usingPlatform: platform = Value (
| private Vector = platform kernel Vector.
private actors = platform actors.
|)(
private class Stack = (
| private waitingPush = Vector new.
private waitingPop = Vector new.
private vector = Vector new: 2.
|)(
public push: x = (
vector capacity = vector size
ifTrue: [
'push' println.
vector append: x.
notifyPop ]
ifFalse: [
'waiting for pop...because stack is full' println.
p := waitPush.
p whenResolved: [: value | vector append: x ] ]
)
public waitPush = (
| pair |
pair := actors createPromisePair.
r := pair resolver.
p := pair promise.
waitingPush append: r.
^ p
)
public notifyPush = (
waitingPush isEmpty
ifFalse: [
'notify push' println.
r := waitingPush at: 1 resolve: ok.
waitingPush removeFirst.
]
)
public pop = (
vector isEmpty
ifTrue: [
'waiting for a push, stack is empty' println.
p := waitPop.
^ p whenResolved: [: value | pop ] ]
ifFalse: [
val := vector pop.
notifyPush.
^ val ]
)
public waitPop = (
| pair |
pair := actors createPromisePair.
r := pair resolver.
p := pair promise.
waitingPop append: r.
^ p
)
public notifyPop = (
waitingPop isEmpty ifFalse: [
'notify pop' println.
waitingPop at: 1 resolve: ok.
waitingPop removeFirst ]
)
)
public main: args = (
| p |
'Hello Stack!' println.
p := testAsyncStack.
'testAsyncStack Done' println.
^ p
)
public testAsyncStack = (
| stack pair r p promise1 promise2 promise3 |
pair := actors createPromisePair.
r := pair resolver.
p := pair promise.
stack := Stack new.
promise1 := stack <-: push: 1.
promise2 := stack <-: push: 2.
promise3 := stack <-: push: 3.
^ promise3 whenResolved: [: value |
stack <-: pop.
'pop value' + value println.
r resolve: ok ]
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment