30: Working With Resources
- Problem to be solved: some blocks of execution within an operation must be defined as a critical conditional region to work properly.
- A critical conditional region may have problems if executed by two or more threads simultaneously. (Not so if the threads are read-only, but perhaps if the threads write as they execute.)
- Critical conditional regions are often blocks that make use of external connections or that must assure that all calculations and variable setting within must be done by one thread alone.
- A resource is an object associated with a block of code. It keeps track of the block's state of execution, and restricts the block's execution as requested.
- A resource is typically used within a method that may be executed by many different agents and places, possibly simultaneously.
- A resource may carry with it a list of arbitrary conditions specified by the programmer. The resource is always set to one of those conditions. The resource's condition can be changed during execution of its associated block.
- A resource's association with a block determines the execution access provided by the resource.
- Exclusive/shared
- If the association grants exclusive access, only one process at a time may execute the resource's block.
- If the association grants shared access, more than one process at a time may execute the resource's block.
- Waiting time
- If a process is blocked from executing a resource block, the process waits indefinitely until the block is available unless the block specifies a waiting time.
- If the resource's association with the block specifies a waiting time, the process will wait only for that amount of time. If the process still hasn't gained access to the resource block, the process executes a "time out" block and no longer waits for the resource block.
- Conditions are right
- If a resource has conditions, its association with a block may specify that the resource be in any one of a set of those conditions before the block can be executed.
- Creating a resource
- The resource's "conditions" attribute contains a set of possible conditions, defined by the programmer.
- The resource's condition attribute contains one of the conditions from its conditions set. The contents of this attribute define the resource's current condition.
- On initialization, you may or may not define a set of conditions.
- Each condition is an identifier that can stand for whatever you care it to stand for.
- You may have as many conditions as you wish.
- If you choose not to have conditions for a resource, simply initialize using nil for conditions.
- On initialization you set the the resource's condition to one of the possible conditions listed in the conditions attribute.
- Associating a resource with a block
- Creating a use block
- The "use" keyword
- Putting the resource in place
- Specifying shared or exclusive access (the "shared" keyword)
- Specifying possible conditions for access (setting the gate)
- The gate is a set of condition identifiers, each of which is a condition in which the resource will allow block execution.
- The identifiers must be a subset of the conditions attribute of the resource (in other words, don't specify conditions that can't exist in the resource).
- Creating a timeout block (optional)
- The "after" keyword followed by an integer expression or value specifying seconds to wait.
- The timeout block, to be executed if the requesting process can't use the resource block within the specified amount of time.
- Resources are not necessary for features of built-in classes, which are always executed atomically--that is, by one process at a time.
- The following built-in features don't execute atomically:
- meet, wait, own, preserve, restrict, and use operations
TS Ref - 26 JUN 1996
Generated by the sweat of Mike Boom's brow.