Bistro Threads and Synchronization

Bistro supports the common Smalltalk block fork idiom for spawning threads, but implements these threads using primitive Java™ threads. In Bistro, block fork expressions return instances of smalltalk.behavior.ZeroArgumentBlock.

aBlock (ZeroArgumentBlock) := [ "...block expressions..." ] fork.

Once a new thread has been created, you can access the primitive thread by sending the message primitiveThread to the block instance.

primitiveThread (Thread) := aBlock primitiveThread.

Blocks that have not been forked return primitive null in response to primitiveThread.

Java™ supports thread synchronization on methods and within methods. Bistro supports the declaration of synchronized methods and also supports object synchronization within methods. The base class, smalltalk.behavior.Object, provides the following instance method that acquires an instance monitor. Thus, any Bistro method may synchronize threads on an object by using a statement similar to the following one.

anObject acquireMonitorDuring: [ "...critical section..." ].

Bistro methods can also wait on an object monitor using the following idioms.

"Wait until notified or interrupted."
anObject waitForChangeIfInterrupted: [ "..." ].
"Wait until notified, interrupted, or a millisecondDuration expires."
anObject waitForChange: millisecondDuration ifInterrupted: [ "..." ].

After a thread has been suspended using one these wait idioms, another thread can awaken the sleeping thread using one of the following idioms.

"Awaken a single waiting thread."
anObject awakenWaitingThread.
"Awaken all waiting threads."
anObject awakenAllWaitingThreads.

Java™ is a trademark of Sun Microsystems, Inc.

Permission is granted to copy this document provided this copyright statement is retained in all copies.
Copyright 1999-2001 Nikolas S. Boyd.