Future.st
author Claus Gittinger <cg@exept.de>
Tue, 26 Feb 2008 11:29:14 +0100
changeset 1930 935b2870be2e
parent 1516 08829dfb29fe
child 2341 36e455705ee6
permissions -rw-r--r--
arrow points reusable (class protocol)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1140
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     1
"
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     2
 This is a Manchester Goodie protected by copyright.
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     3
 These conditions are imposed on the whole Goodie, and on any significant
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     4
 part of it which is separately transmitted or stored:
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     5
	* You must ensure that every copy includes this notice, and that
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     6
	  source and author(s) of the material are acknowledged.
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     7
	* These conditions must be imposed on anyone who receives a copy.
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     8
	* The material shall not be used for commercial gain without the prior
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     9
	  written consent of the author(s).
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    10
 Further information on the copyright conditions may be obtained by
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    11
 sending electronic mail:
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    12
	To: goodies-lib@cs.man.ac.uk
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    13
	Subject: copyright
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    14
 or by writing to The Smalltalk Goodies Library Manager, Dept of
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    15
 Computer Science, The University, Manchester M13 9PL, UK
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    16
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    17
 (C) Copyright 1992 University of Manchester
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    18
 For more information about the Manchester Goodies Library (from which 
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    19
 this file was distributed) send e-mail:
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    20
	To: goodies-lib@cs.man.ac.uk
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    21
	Subject: help 
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    22
"
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    23
885
c31412b26306 package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents: 484
diff changeset
    24
"{ Package: 'stx:goodies' }"
457
c862c91716b6 packages
Claus Gittinger <cg@exept.de>
parents: 351
diff changeset
    25
1327
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
    26
ProtoObject subclass:#Future
1140
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    27
	instanceVariableNames:'result semaphore'
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    28
	classVariableNames:''
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    29
	poolDictionaries:''
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    30
	category:'Kernel-Processes'
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    31
!
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    32
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    33
!Future class methodsFor:'documentation'!
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    34
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    35
copyright
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    36
"
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    37
 This is a Manchester Goodie protected by copyright.
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    38
 These conditions are imposed on the whole Goodie, and on any significant
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    39
 part of it which is separately transmitted or stored:
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    40
	* You must ensure that every copy includes this notice, and that
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    41
	  source and author(s) of the material are acknowledged.
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    42
	* These conditions must be imposed on anyone who receives a copy.
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    43
	* The material shall not be used for commercial gain without the prior
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    44
	  written consent of the author(s).
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    45
 Further information on the copyright conditions may be obtained by
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    46
 sending electronic mail:
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    47
	To: goodies-lib@cs.man.ac.uk
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    48
	Subject: copyright
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    49
 or by writing to The Smalltalk Goodies Library Manager, Dept of
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    50
 Computer Science, The University, Manchester M13 9PL, UK
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    51
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    52
 (C) Copyright 1992 University of Manchester
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    53
 For more information about the Manchester Goodies Library (from which 
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    54
 this file was distributed) send e-mail:
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    55
	To: goodies-lib@cs.man.ac.uk
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    56
	Subject: help 
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    57
"
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    58
!
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    59
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    60
documentation
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    61
"
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    62
    I represent an execution in progress.  
1327
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
    63
    I will immediately start execution in a separate process.
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    64
    Any messages sent to me are delayed until execution has completed.'
1327
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
    65
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
    66
    [author:]        
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
    67
        tph@cs.man.ac.uk
1516
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
    68
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
    69
    [see also:]
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
    70
        Block Lazy LazyValue
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    71
"
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    72
!
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    73
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    74
examples
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    75
"
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    76
  Starts evaluating the factorial immediately, but waits until
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    77
  the result is available before printing the answer
1516
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
    78
                                                                    [exBegin]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    79
    | fac |
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    80
1516
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
    81
    fac := [5000 factorial] futureValue.
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    82
    Transcript showCR: 'evaluating factorial...'.
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    83
    Transcript showCR: fac printString
1516
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
    84
                                                                    [exEnd]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    85
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    86
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    87
  An example illustrating the use of multiple futures and
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    88
  explicit resynchronisation.
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    89
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    90
  Starts evaluating both factorials immediately, but waits until
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    91
  both blocks have finished before continuing.
1516
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
    92
                                                                    [exBegin]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    93
    | fac1 fac2 |
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    94
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    95
    fac1 := [Transcript showCR: 'Starting fac1.. '. 1000 factorial] futureValue.
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    96
    fac2 := [Transcript showCR: 'Starting fac2.. '. 2000 factorial] futureValue.
1516
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
    97
    fac2 isString.
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
    98
    fac1 isString.
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    99
    Transcript showCR: 'both completed.'.
1516
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   100
                                                                    [exEnd]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   101
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   102
  Example showing how arguments may be passed to futures.
1516
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   103
                                                                    [exBegin]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   104
    | temp |
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   105
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   106
    temp := [:x :y | 10 * x * y] futureValue: 3 value: 4.
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   107
    Transcript  showCR: temp printString.
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   108
1516
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   109
                                                                    [exEnd]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   110
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   111
  Claus:
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   112
    The above examples do not really show the power of Futures;
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   113
    they can be useful, whenever some long-time computation is
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   114
    to be done, and some other useful work can be done in the meanwhile.
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   115
    for example:
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   116
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   117
    Here, the input is read before - the readTime and view creation
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   118
    times sum up:
1516
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   119
                                                                    [exBegin]
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   120
        |p text v|
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   121
1516
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   122
        p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'.
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   123
        text := p contents.
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   124
        p close.
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   125
        v := TextView new openAndWait.
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   126
        v contents:text
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   127
                                                                    [exEnd]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   128
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   129
    The same here:
1516
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   130
                                                                    [exBegin]
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   131
        |p text v|
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   132
1516
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   133
        v := TextView new openAndWait.
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   134
        p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'.
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   135
        text := p contents.
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   136
        p close.
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   137
        v contents:text
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   138
                                                                    [exEnd]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   139
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   140
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   141
    Here, the view creation and reading are done in parallel:
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   142
    (if the user is slow when opening the view, the contents may
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   143
     be already available)
1516
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   144
                                                                    [exBegin]
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   145
        |p text v|
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   146
1516
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   147
        text := [   |p t|
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   148
1516
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   149
                    p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'.
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   150
                    t := p contents.
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   151
                    p close.
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   152
                    t
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   153
                ] futureValue.
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   154
        v := TextView new openAndWait.
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   155
        v contents:text
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   156
                                                                    [exEnd]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   157
"
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   158
! !
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   159
1140
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   160
!Future methodsFor:'evaluating'!
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   161
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   162
block: aBlock
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   163
    "Execute aBlock in parallel with whatever called me, but
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   164
     ensure that any messages sent to me before execution
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   165
     of the block has terminated are suspended until it has terminated."
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   166
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   167
    semaphore := Semaphore new name:'Future'.
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   168
    [result := aBlock ensure:[semaphore signal]] fork
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   169
!
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   170
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   171
block: aBlock value: aValue
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   172
    "Execute aBlock in parallel with whatever called me, but
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   173
     ensure that any messages sent to me before execution
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   174
     of the block has terminated are suspended until it has terminated."
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   175
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   176
    semaphore := Semaphore new name:'Future'.
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   177
    [
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   178
        result := [
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   179
            aBlock value:aValue
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   180
        ] ensure:[semaphore signal]
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   181
    ] fork
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   182
!
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   183
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   184
block: aBlock value: value1 value: value2
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   185
    "Execute aBlock in parallel with whatever called me, but
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   186
     ensure that any messages sent to me before execution
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   187
     of the block has terminated are suspended until it has terminated."
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   188
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   189
    semaphore := Semaphore new name:'Future'.
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   190
    [
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   191
        result := [
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   192
            aBlock value: value1 value: value2
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   193
        ] ensure:[semaphore signal]
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   194
    ] fork
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   195
!
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   196
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   197
block: aBlock value: value1 value: value2 value: value3
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   198
    "Execute aBlock in parallel with whatever called me, but
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   199
     ensure that any messages sent to me before execution
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   200
     of the block has terminated are suspended until it has terminated."
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   201
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   202
    semaphore := Semaphore new name:'Future'.
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   203
    [
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   204
        result := [
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   205
            aBlock value: value1 value: value2 value: value3
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   206
        ] ensure:[semaphore signal]
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   207
    ] fork
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   208
!
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   209
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   210
block: aBlock valueWithArguments: anArray
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   211
    "Execute aBlock in parallel with whatever called me, but
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   212
     ensure that any messages sent to me before execution
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   213
     of the block has terminated are suspended until it has terminated."
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   214
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   215
    semaphore := Semaphore new name:'Future'.
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   216
    [
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   217
        result := [
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   218
            aBlock valueWithArguments: anArray
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   219
        ] ensure:[semaphore signal]
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   220
    ] fork
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   221
! !
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   222
1140
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   223
!Future methodsFor:'synchronising'!
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   224
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   225
doesNotUnderstand:aMessage 
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   226
    "Any message to a Future will end up here."
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   227
    
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   228
    semaphore waitUncounted. "Wait for evaluation to complete"
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   229
                             "(if not already completed)" 
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   230
    ^ result perform:aMessage selector withArguments:aMessage arguments
1140
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   231
! !
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   232
1327
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
   233
!Future methodsFor:'testing'!
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
   234
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   235
hasValue
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   236
    ^ semaphore wouldBlock not
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   237
!
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   238
1327
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
   239
isLazyValue
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
   240
    ^ semaphore wouldBlock
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
   241
! !
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
   242
1140
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   243
!Future class methodsFor:'documentation'!
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   244
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   245
version
1516
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
   246
    ^ '$Header: /cvs/stx/stx/libbasic2/Future.st,v 1.10 2005-01-26 13:55:27 stefan Exp $'
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   247
! !