Process

Inherits from: Object

A lightweight concurrent process backed by a Go goroutine.

Processes are created by sending fork to a block, which evaluates the block in a new goroutine and returns a Process handle. Use wait to block until the process completes and retrieve its result.

Example
[42] fork wait          "=> 42"
[3 + 4] fork wait      "=> 7"

| p <Process> |
p := [100] fork.
p wait.
p isDone               "=> true"
Example
"Fan-out with channels"
| ch <Channel> |
ch := Channel new: 10.
1 to: 5 do: [:i |
    [ch send: i * 10] fork
].
5 timesRepeat: [ch receive]

Class Methods

primitives

class current

Return the current process.

class fork:

Fork block as a new process.

class forkWithout:do:

Fork block with restricted global access.

class primNamed:
class primReceive
class primReceive:
class primTryReceive
class sleep:

Suspend the current process for ms milliseconds.

class yield

Yield the current process's time slice.

uncategorized

class named:

Look up a process by its registered name. Returns the Process or nil if not found.

Example
| p <Process> |
p := [Process receive] fork.
Process sleep: 10.
p registerAs: 'lookup-test'.
Process named: 'lookup-test'
class receive

Block the current process until a message arrives in its mailbox. Returns a MailboxMessage.

Example
| p <Process> |
p := [Process receive payload] fork.
Process sleep: 10.
p send: 42.
p wait
class receive:

Block the current process for up to ms milliseconds waiting for a message. Returns a MailboxMessage or nil on timeout.

Example
| p <Process> |
p := [Process receive: 50] fork.
p wait
class tryReceive

Non-blocking receive. Returns a MailboxMessage if one is available, or nil if the mailbox is empty.

Test
Process tryReceive >>> nil
Test
Process current send: 42.
Process tryReceive class name >>> #MailboxMessage

Instance Methods

primitives

isDone

Return true if the process has finished.

primDemonitor:
primExitReason
primIsAlive
primLink:
primMonitor:
primPriority
primPriority:
primRegisterAs:
primResult
primSend:
primSend:with:
primTerminate
primTrapExit
primTrapExit:
primUnlink:
primUnregister
primWait
primYield

uncategorized

demonitor:

Cancel a monitor by its reference ID.

Example
| worker <Process> ref <Integer> |
worker := [Process sleep: 5000] fork.
ref := Process current monitor: worker.
Process current demonitor: ref
exitReason

Return the exit reason of this process (a Symbol like #normal, #linked, #error), or nil if still running.

Test
Process current exitReason >>> nil
isAlive

Return true if the process is still running.

Example
| p <Process> |
p := [1] fork.
p wait.
p isAlive  "=> false"
isTerminated

Return true if the process has finished executing. Opposite of isAlive.

Example
| p <Process> |
p := [1] fork.
p wait.
p isTerminated  "=> true"
link:

Create a bidirectional link with another process. If either linked process terminates abnormally, the other is also killed (unless it is trapping exits).

Example
| child <Process> |
child := [Process sleep: 1000] fork.
Process current link: child
monitor:

Monitor another process. Returns a reference ID (SmallInt). When the monitored process dies, a processDown: message is delivered to this process's mailbox with payload #(refID processValue reasonSymbol resultValue).

Works with both local and remote processes. For remote processes, a MonitorProcess RPC is sent to the remote node, and a NodeHealthMonitor heartbeat loop detects node failures.

Example
| worker <Process> ref <Integer> msg <MailboxMessage> |
worker := [42] fork.
ref := Process current monitor: worker.
worker wait.
Process sleep: 10.
msg := Process tryReceive.
msg selector    "=> #processDown:"
printString

Return a string representation of the process.

priority

Return the scheduling priority of this process.

Example
| p <Process> |
p := [1] fork.
p priority
priority:

Set the scheduling priority of this process.

Example
| p <Process> |
p := [Process sleep: 100] fork.
p priority: 2
registerAs:

Register this process under a name for lookup by other processes. Returns true if successful, false if the name is already taken by a live process.

Example
| p <Process> |
p := [Process receive] fork.
Process sleep: 10.
p registerAs: 'worker'
result

Return the result of the process, or nil if it has not yet completed.

Example
| p <Process> |
p := ['hello'] fork.
p wait.
p result  "=> 'hello'"
send:

Send a value to this process's mailbox (fire-and-forget). Returns true if the message was delivered, false if the process is dead or its mailbox is full.

Example
| p <Process> |
p := [Process receive] fork.
Process sleep: 10.
p send: 42
send:with:

Send a selector-based message to this process's mailbox. The receiver gets a MailboxMessage with the given selector and payload.

Example
| p <Process> |
p := [Process receive] fork.
Process sleep: 10.
p send: #doWork with: 42
terminate

Terminate the process. The process will stop executing and its result will be nil.

Example
| p <Process> |
p := [Process sleep: 10000] fork.
p terminate
trapExit

Return whether this process is trapping exits.

Test
Process current trapExit: true.
Process current trapExit >>> true
trapExit:

Set whether this process traps exit signals from linked processes. When true, exit signals are delivered as mailbox messages with selector #exit instead of killing the process.

Test
Process current trapExit: true >>> true
unlink:

Remove a bidirectional link.

unregister

Remove this process's name registration.

wait

Block the caller until this process finishes, then return the process's result value.

Example
[42] fork wait  "=> 42"
yield

Yield the current process's time slice to allow other goroutines to run. Returns the receiver.

Example
100 timesRepeat: [Process yield]