30: Working With Resources

  1. Problem to be solved: some blocks of execution within an operation must be defined as a critical conditional region to work properly.
    1. 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.)
    2. 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.
  2. 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.
    1. A resource is typically used within a method that may be executed by many different agents and places, possibly simultaneously.
    2. 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.
    3. A resource's association with a block determines the execution access provided by the resource.
      1. Exclusive/shared
        1. If the association grants exclusive access, only one process at a time may execute the resource's block.
        2. If the association grants shared access, more than one process at a time may execute the resource's block.
      2. Waiting time
        1. 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.
        2. 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.
      3. Conditions are right
        1. 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.
  3. Creating a resource
    1. The resource's "conditions" attribute contains a set of possible conditions, defined by the programmer.
    2. 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.
    3. On initialization, you may or may not define a set of conditions.
      1. Each condition is an identifier that can stand for whatever you care it to stand for.
      2. You may have as many conditions as you wish.
      3. If you choose not to have conditions for a resource, simply initialize using nil for conditions.
    4. On initialization you set the the resource's condition to one of the possible conditions listed in the conditions attribute.
  4. Associating a resource with a block
    1. Creating a use block
      1. The "use" keyword
      2. Putting the resource in place
      3. Specifying shared or exclusive access (the "shared" keyword)
      4. Specifying possible conditions for access (setting the gate)
        1. The gate is a set of condition identifiers, each of which is a condition in which the resource will allow block execution.
        2. 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).
    2. Creating a timeout block (optional)
      1. The "after" keyword followed by an integer expression or value specifying seconds to wait.
      2. The timeout block, to be executed if the requesting process can't use the resource block within the specified amount of time.
  5. Resources are not necessary for features of built-in classes, which are always executed atomically--that is, by one process at a time.
    1. The following built-in features don't execute atomically:
      1. meet, wait, own, preserve, restrict, and use operations

TS Ref - 26 JUN 1996

Generated by the sweat of Mike Boom's brow.