Previous Table of Contents Next


11.10.1. The Text Interface

This section defines the Text interface that defines text operations.

   INTERFACE Text;
   TYPE
     T = TEXT;

A non-nil TEXT represents a zero-based sequence of characters. NIL does not represent any sequence of characters, it will not be returned from any procedure in the interface, and it is a checked runtime error to pass it to any procedure in the interface.

   PROCEDURE Cat(t, u: T): T;

The concatenation of t and u.

   PROCEDURE Equal(t, u: T): BOOLEAN;

TRUE if t and u have the same length and (case-sensitive) contents.

   PROCEDURE GetChar(t: T; i: CARDINAL): CHAR;

Character i of t. A checked runtime error if i >= Length(t).

   PROCEDURE Length(t: T): CARDINAL;

The number of characters in t.

   PROCEDURE Empty(t: T): BOOLEAN;

TRUE if Length(t) = 0.

   PROCEDURE Sub(t: T; start, length: CARDINAL): T;

Return a subsequence of t: empty if start >= Length(t) or length = 0; otherwise, the subsequence ranging from start to the minimum of start+length-1 and Length(t)-1.

   PROCEDURE SetChars(VAR a: ARRAY OF CHAR; t: T);

For each i from 0 to MIN(LAST(a), Length(t)-1), set a[i] to GetChar(t, i).

   PROCEDURE FromChar(ch: CHAR): T;

A text containing the single character ch.

   PROCEDURE FromChars(READONLY a: ARRAY OF CHAR): T;

A text containing the characters of a.

   PROCEDURE Hash(t: T): INTEGER;

Return a hash function of the contents of t.

   END Text.

11.10.2. The Thread Interface

If a shared variable is written concurrently by two threads, or written by one and read concurrently by another, the effect is to set the variable to an implementation-dependent value of its type. For example, if one thread writes a[0] while another concurrently writes a[1], one of the writes might be lost. Thus, portable programs must use the Thread interface to provide mutual exclusion for shared variables.

   INTERFACE Thread;
   TYPE
     T <: REFANY;
     Mutex = MUTEX;
     Condition <: ROOT;
     Closure = OBJECT METHODS apply(): REFANY END;

A Thread.T is a handle on a thread. A Mutex is locked by some thread or unlocked. A Condition is a set of waiting threads. A newly allocated Mutex is unlocked; a newly allocated Condition is empty. It is a checked runtime error to pass the NIL Mutex, Condition or T to any procedure in this interface.

   PROCEDURE Fork(cl: Closure): T;

A handle on a newly created thread executing cl.apply().

   PROCEDURE Join(t: T): REFANY;

Wait until t has terminated and return its result. It is a checked error to call this more than once for any t.

   PROCEDURE Wait(m: Mutex; c: Condition);

The calling thread must have m locked. Atomically unlocks m and waits on c. Then relocks m and returns.

   PROCEDURE Acquire(m: Mutex);

Wait until m is unlocked and then lock it.

   PROCEDURE Release(m: Mutex);

The calling thread must have m locked. Unlocks m.

   PROCEDURE Broadcast(c: Condition);

All threads waiting on c become eligible to run.

   PROCEDURE Signal(c: Condition);

One or more threads waiting on c become eligible to run.

   PROCEDURE Self(): T;

Return the handle of the calling thread.

   EXCEPTION Alerted;

Used to approximate asynchronous interrupts.

   PROCEDURE Alert(t: T);

Mark t as an alerted thread.

   PROCEDURE TestAlert(): BOOLEAN;

TRUE if the calling thread has been marked alerted.

   PROCEDURE AlertWait(m: Mutex; c: Condition) RAISES {Alerted};

Like Wait, but if the thread is marked alerted at the time of call or sometime during the wait, lock m and raise Alerted.

   PROCEDURE AlertJoin(t: T): REFANY RAISES {Alerted};

Like Join, but if the calling thread is marked alerted at the time of call or sometime during the wait, raise Alerted.

   CONST
     AtomicSize = ...;

An implementation-dependent integer constant: the number of bits in a memory-coherent block. If two components of a record or array fall in different blocks, they can be accessed concurrently by different threads without locking.

   END Thread.


Previous Table of Contents Next