Lazy.st
changeset 62 a759b5c72c98
child 67 6d8a403eff0a
equal deleted inserted replaced
61:13d2435bc955 62:a759b5c72c98
       
     1 "       NAME            Parallelism
       
     2         AUTHOR          tph@cs.man.ac.uk
       
     3         FUNCTION throttled Futures; lazy eval; explicit pa'l'l procs 
       
     4         ST-VERSIONS     stx
       
     5         PREREQUISITES    
       
     6         CONFLICTS       
       
     7         DISTRIBUTION    world
       
     8         VERSION         1.1
       
     9         DATE    22 Jan 1989
       
    10         SUMMARY 
       
    11 Parallelism contains a number of explicitly parallel constructs,
       
    12 including a new version of Future, Lazy evaluation, and explicit
       
    13 parallel processes.  Lots of code in here.  Contains an early
       
    14 version (read: doesn't work) of a ""throttled"" future mechanism.
       
    15 New version RSN.(2.2).TPH
       
    16 
       
    17 claus: I have separated the original Parallelism package into
       
    18        individual ones: Lazy, Future, ThrottledFuture and ParallelEvaluation
       
    19 "!
       
    20 
       
    21 'From Smalltalk-80, version 2, of April 1, 1983 on 29 March 1987 at 5:13:27 pm'!
       
    22 
       
    23 !Object methodsFor: 'parallel evaluation'!
       
    24 
       
    25 touch
       
    26         "Simply returns self.  If the receiver is an uncompleted
       
    27          Future or Lazy, this forces complete evaluation."
       
    28 
       
    29         ^self
       
    30 ! !
       
    31 
       
    32 'From Smalltalk-80, version 2, of April 1, 1983 on 29 March 1987 at 5:09:10 pm'!
       
    33 
       
    34 Object subclass: #Lazy
       
    35         instanceVariableNames: 'result startSemaphore endSemaphore '
       
    36         classVariableNames: ''
       
    37         poolDictionaries: ''
       
    38         category: 'Kernel-Processes'!
       
    39 Lazy comment:
       
    40 'I represent an execution which may not be required.  I will
       
    41 not start execution until at least one message has been
       
    42 received.  The messages sent to me are delayed until execution
       
    43 has completed.'!
       
    44 
       
    45 
       
    46 !Lazy methodsFor: 'synchronising'!
       
    47 
       
    48 doesNotUnderstand: aMessage
       
    49         "Any message to a Lazy will end up here."
       
    50 
       
    51         startSemaphore signal.          "Start the evaluation."
       
    52         endSemaphore wait.              "Wait until evaluation completed."
       
    53         endSemaphore signal.            "Wake up anything else."
       
    54         ^result perform: aMessage selector withArguments: aMessage arguments
       
    55                 "Perform the message, having re-synchronised."! !
       
    56 
       
    57 !Lazy methodsFor: 'evaluating'!
       
    58 
       
    59 block: aBlock
       
    60         "Execute aBlock in parallel, but ensure that any messages sent
       
    61          to me before execution of the block has terminated are
       
    62          suspended until it has terminated. Do not start the evaluation
       
    63          until at least one message has been sent to the receiver."
       
    64 
       
    65         startSemaphore _ Semaphore new.
       
    66         endSemaphore _ Semaphore new.
       
    67         [startSemaphore wait.
       
    68          result _ aBlock value.
       
    69          endSemaphore signal] fork!
       
    70 
       
    71 block: aBlock value: aValue
       
    72         "Execute aBlock in parallel, but ensure that any messages sent
       
    73          to me before execution of the block has terminated are
       
    74          suspended until it has terminated. Do not start the evaluation
       
    75          until at least one message has been sent to the receiver."
       
    76 
       
    77         startSemaphore _ Semaphore new.
       
    78         endSemaphore _ Semaphore new.
       
    79         [startSemaphore wait.
       
    80          result _ aBlock value: aValue.
       
    81          endSemaphore signal] fork!
       
    82 
       
    83 block: aBlock value: value1 value: value2
       
    84         "Execute aBlock in parallel, but ensure that any messages sent
       
    85          to me before execution of the block has terminated are
       
    86          suspended until it has terminated. Do not start the evaluation
       
    87          until at least one message has been sent to the receiver."
       
    88 
       
    89         startSemaphore _ Semaphore new.
       
    90         endSemaphore _ Semaphore new.
       
    91         [startSemaphore wait.
       
    92          result _ aBlock value: value1 value: value2.
       
    93          endSemaphore signal] fork!
       
    94 
       
    95 block: aBlock value: value1 value: value2 value: value3
       
    96         "Execute aBlock in parallel, but ensure that any messages sent
       
    97          to me before execution of the block has terminated are
       
    98          suspended until it has terminated. Do not start the evaluation
       
    99          until at least one message has been sent to the receiver."
       
   100 
       
   101         startSemaphore _ Semaphore new.
       
   102         endSemaphore _ Semaphore new.
       
   103         [startSemaphore wait.
       
   104          result _ aBlock value: value1 value: value2 value: value3.
       
   105          endSemaphore signal] fork!
       
   106 
       
   107 block: aBlock valueWithArguments: anArray
       
   108         "Execute aBlock in parallel, but ensure that any messages sent
       
   109          to me before execution of the block has terminated are
       
   110          suspended until it has terminated. Do not start the evaluation
       
   111          until at least one message has been sent to the receiver."
       
   112 
       
   113         startSemaphore _ Semaphore new.
       
   114         endSemaphore _ Semaphore new.
       
   115         [startSemaphore wait.
       
   116          result _ aBlock valueWithArguments: anArray.
       
   117          endSemaphore signal] fork! !
       
   118 
       
   119 !Lazy class methodsFor: 'class initialization'!
       
   120 
       
   121 initialize
       
   122         "must avoid the checks"
       
   123 
       
   124         superclass _ nil
       
   125 
       
   126         "Lazy initialize."! !
       
   127 
       
   128 !Lazy class methodsFor: 'examples'!
       
   129 
       
   130 example1
       
   131         "Evaluates the factorial, starting only when the
       
   132          result is actually required (when printString is sent)."
       
   133 
       
   134         | fac |
       
   135         fac _ [100 factorial] futureValue.
       
   136         Transcript showCr: 'Doing nothing. '.
       
   137         (Delay forSeconds: 2) wait.
       
   138         Transcript showCr: fac printString
       
   139 
       
   140         "Lazy example1"!
       
   141 
       
   142 example2
       
   143         "Starts evaluating both factorials only when required (by the touch),
       
   144          and waits until both blocks have finished before continuing."
       
   145 
       
   146         | fac1 fac2 |
       
   147         fac1 _ [Transcript showCr: 'Starting fac1.. '. 100 factorial] lazyValue.
       
   148         fac2 _ [Transcript showCr: 'Starting fac2.. '. 120 factorial] lazyValue.
       
   149         fac2 touch.
       
   150         fac1 touch.
       
   151         Transcript showCr: 'both completed.'.
       
   152 
       
   153         "Lazy example2"!
       
   154 
       
   155 example3
       
   156         "Demonstrates how to pass arguments to a lazy evaluation block."
       
   157 
       
   158         | temp |
       
   159         temp _ [:x :y :z | x * y * z] lazyValueWithArguments: #(2 3 4).
       
   160         Transcript  showCr: temp printString.
       
   161 
       
   162         "Lazy example3"! !
       
   163 
       
   164 Lazy initialize!
       
   165 
       
   166 !Block methodsFor: 'parallel evaluation'!
       
   167 
       
   168 lazyValue
       
   169         "Fork a synchronised evaluation of myself. Only starts
       
   170          the evaluation when the result is requested."
       
   171 
       
   172         ^Lazy new block: self!
       
   173 
       
   174 lazyValue: aValue
       
   175         "Fork a synchronised evaluation of myself. Only starts
       
   176          the evaluation when the result is requested."
       
   177 
       
   178         ^Lazy new block: self value: aValue!
       
   179 
       
   180 lazyValue: aValue value: anotherValue
       
   181         "Fork a synchronised evaluation of myself. Only starts
       
   182          the evaluation when the result is requested."
       
   183 
       
   184         ^Lazy new block: self value: aValue value: anotherValue!
       
   185 
       
   186 lazyValue: aValue value: anotherValue value: bValue
       
   187         "Fork a synchronised evaluation of myself. Only starts
       
   188          the evaluation when the result is requested."
       
   189 
       
   190         ^Lazy new block: self value: aValue value: anotherValue value: bValue!
       
   191 
       
   192 lazyValueWithArguments: anArray
       
   193         "Fork a synchronised evaluation of myself. Only starts
       
   194          the evaluation when the result is requested."
       
   195 
       
   196         ^Lazy new block: self valueWithArguments: anArray
       
   197 ! !
       
   198 
       
   199 "COPYRIGHT.
       
   200  The above file is a Manchester Goodie protected by copyright.
       
   201  These conditions are imposed on the whole Goodie, and on any significant
       
   202  part of it which is separately transmitted or stored:
       
   203         * You must ensure that every copy includes this notice, and that
       
   204           source and author(s) of the material are acknowledged.
       
   205         * These conditions must be imposed on anyone who receives a copy.
       
   206         * The material shall not be used for commercial gain without the prior
       
   207           written consent of the author(s).
       
   208  Further information on the copyright conditions may be obtained by
       
   209  sending electronic mail:
       
   210         To: goodies-lib@cs.man.ac.uk
       
   211         Subject: copyright
       
   212  or by writing to The Smalltalk Goodies Library Manager, Dept of
       
   213  Computer Science, The University, Manchester M13 9PL, UK
       
   214 
       
   215  (C) Copyright 1992 University of Manchester
       
   216  For more information about the Manchester Goodies Library (from which 
       
   217  this file was distributed) send e-mail:
       
   218         To: goodies-lib@cs.man.ac.uk
       
   219         Subject: help 
       
   220 "!