Future.st
author Claus Gittinger <cg@exept.de>
Tue, 04 Oct 2011 14:55:22 +0200
changeset 2667 e65a29b55fe1
parent 2666 fb363ea60a10
child 2668 67b51e1ee66d
permissions -rw-r--r--
added: #priority:block:valueWithArguments: changed: #block:valueWithArguments:
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
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
    18
 For more information about the Manchester Goodies Library (from which
1140
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
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
    21
	Subject: help
1140
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    22
"
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
    23
"{ Package: 'stx:libbasic2' }"
457
c862c91716b6 packages
Claus Gittinger <cg@exept.de>
parents: 351
diff changeset
    24
1327
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
    25
ProtoObject subclass:#Future
1140
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    26
	instanceVariableNames:'result semaphore'
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    27
	classVariableNames:''
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    28
	poolDictionaries:''
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    29
	category:'Kernel-Processes'
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    30
!
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    31
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    32
!Future class methodsFor:'documentation'!
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    33
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    34
copyright
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    35
"
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    36
 This is a Manchester Goodie protected by copyright.
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    37
 These conditions are imposed on the whole Goodie, and on any significant
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    38
 part of it which is separately transmitted or stored:
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    39
	* You must ensure that every copy includes this notice, and that
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    40
	  source and author(s) of the material are acknowledged.
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    41
	* These conditions must be imposed on anyone who receives a copy.
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    42
	* The material shall not be used for commercial gain without the prior
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    43
	  written consent of the author(s).
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    44
 Further information on the copyright conditions may be obtained by
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    45
 sending electronic mail:
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    46
	To: goodies-lib@cs.man.ac.uk
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    47
	Subject: copyright
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    48
 or by writing to The Smalltalk Goodies Library Manager, Dept of
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    49
 Computer Science, The University, Manchester M13 9PL, UK
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    50
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    51
 (C) Copyright 1992 University of Manchester
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
    52
 For more information about the Manchester Goodies Library (from which
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    53
 this file was distributed) send e-mail:
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    54
	To: goodies-lib@cs.man.ac.uk
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
    55
	Subject: help
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    56
"
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
documentation
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    60
"
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
    61
    I represent an execution in progress.
1327
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
    62
    I will immediately start execution in a separate process.
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    63
    Any messages sent to me are delayed until execution has completed.'
1327
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
    64
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
    65
    [author:]
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
    66
	tph@cs.man.ac.uk
1516
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
    67
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
    68
    [see also:]
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
    69
	Block Lazy LazyValue
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    70
"
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
examples
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    74
"
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    75
  Starts evaluating the factorial immediately, but waits until
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    76
  the result is available before printing the answer
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
    77
								    [exBegin]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    78
    | fac |
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    79
1516
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
    80
    fac := [5000 factorial] futureValue.
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    81
    Transcript showCR: 'evaluating factorial...'.
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    82
    Transcript showCR: fac printString
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
    83
								    [exEnd]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    84
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    85
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    86
  An example illustrating the use of multiple futures and
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    87
  explicit resynchronisation.
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    88
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    89
  Starts evaluating both factorials immediately, but waits until
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    90
  both blocks have finished before continuing.
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
    91
								    [exBegin]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    92
    | fac1 fac2 |
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    93
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    94
    fac1 := [Transcript showCR: 'Starting fac1.. '. 1000 factorial] futureValue.
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    95
    fac2 := [Transcript showCR: 'Starting fac2.. '. 2000 factorial] futureValue.
1516
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
    96
    fac2 isString.
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
    97
    fac1 isString.
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    98
    Transcript showCR: 'both completed.'.
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
    99
								    [exEnd]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   100
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   101
  Example showing how arguments may be passed to futures.
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   102
								    [exBegin]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   103
    | temp |
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   104
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   105
    temp := [:x :y | 10 * x * y] futureValue: 3 value: 4.
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   106
    Transcript  showCR: temp printString.
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   107
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   108
								    [exEnd]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   109
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   110
  Claus:
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   111
    The above examples do not really show the power of Futures;
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   112
    they can be useful, whenever some long-time computation is
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   113
    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
   114
    for example:
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   115
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   116
    Here, the input is read before - the readTime and view creation
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   117
    times sum up:
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   118
								    [exBegin]
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   119
	|p text v|
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   120
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   121
	p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'.
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   122
	text := p contents.
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   123
	p close.
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   124
	v := TextView new openAndWait.
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   125
	v contents:text
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   126
								    [exEnd]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   127
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   128
    The same here:
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   129
								    [exBegin]
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   130
	|p text v|
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   131
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   132
	v := TextView new openAndWait.
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   133
	p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'.
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   134
	text := p contents.
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   135
	p close.
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   136
	v contents:text
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   137
								    [exEnd]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   138
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   139
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   140
    Here, the view creation and reading are done in parallel:
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   141
    (if the user is slow when opening the view, the contents may
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   142
     be already available)
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   143
								    [exBegin]
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   144
	|p text v|
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   145
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   146
	text := [   |p t|
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   147
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   148
		    p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'.
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   149
		    t := p contents.
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   150
		    p close.
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   151
		    t
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   152
		] futureValue.
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   153
	v := TextView new openAndWait.
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   154
	v contents:text
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   155
								    [exEnd]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   156
"
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   157
! !
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   158
1140
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   159
!Future methodsFor:'evaluating'!
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   160
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   161
block:aBlock
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   162
    "Execute aBlock in parallel with whatever called me, but
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   163
     ensure that any messages sent to me before execution
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   164
     of the block has terminated are suspended until it has terminated."
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   165
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   166
    semaphore := Semaphore new name:'Future'.
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   167
    [result := aBlock ensure:[semaphore signal]] fork
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   168
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   169
    "Modified (format): / 04-10-2011 / 14:54:11 / cg"
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   170
!
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   171
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   172
block:aBlock value:aValue
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   173
    "Execute aBlock in parallel with whatever called me, but
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   174
     ensure that any messages sent to me before execution
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   175
     of the block has terminated are suspended until it has terminated."
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   176
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   177
    semaphore := Semaphore new name:'Future'.
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   178
    [
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   179
        result := [
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   180
            aBlock value:aValue
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   181
        ] ensure:[semaphore signal]
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   182
    ] fork
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   183
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   184
    "Modified (format): / 04-10-2011 / 14:54:18 / cg"
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   185
!
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   186
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   187
block:aBlock value:value1 value:value2
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   188
    "Execute aBlock in parallel with whatever called me, but
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   189
     ensure that any messages sent to me before execution
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   190
     of the block has terminated are suspended until it has terminated."
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   191
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   192
    semaphore := Semaphore new name:'Future'.
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   193
    [
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   194
        result := [
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   195
            aBlock value:value1 value:value2
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   196
        ] ensure:[semaphore signal]
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   197
    ] fork
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   198
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   199
    "Modified (format): / 04-10-2011 / 14:54:28 / cg"
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   200
!
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   201
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   202
block:aBlock value:value1 value:value2 value:value3
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   203
    "Execute aBlock in parallel with whatever called me, but
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   204
     ensure that any messages sent to me before execution
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   205
     of the block has terminated are suspended until it has terminated."
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   206
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   207
    semaphore := Semaphore new name:'Future'.
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   208
    [
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   209
        result := [
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   210
            aBlock value:value1 value:value2 value:value3
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   211
        ] ensure:[semaphore signal]
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   212
    ] fork
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   213
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   214
    "Modified (format): / 04-10-2011 / 14:54:45 / cg"
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   215
!
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   216
2667
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   217
block:aBlock valueWithArguments:anArray
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   218
    "Execute aBlock in parallel with whatever called me, but
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   219
     ensure that any messages sent to me before execution
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   220
     of the block has terminated are suspended until it has terminated."
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   221
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   222
    semaphore := Semaphore new name:'Future'.
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   223
    [
2667
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   224
        result := [
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   225
            aBlock valueWithArguments:anArray
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   226
        ] ensure:[semaphore signal]
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   227
    ] fork
2667
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   228
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   229
    "Modified (format): / 04-10-2011 / 14:55:09 / cg"
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   230
!
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   231
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   232
priority:prio block: aBlock
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   233
    "Execute aBlock in parallel with whatever called me, but
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   234
     ensure that any messages sent to me before execution
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   235
     of the block has terminated are suspended until it has terminated."
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   236
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   237
    semaphore := Semaphore new name:'Future'.
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   238
    [
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   239
        result := aBlock ensure:[semaphore signal]
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   240
    ] forkAt:prio
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   241
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   242
    "Created: / 04-10-2011 / 14:53:21 / cg"
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   243
!
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   244
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   245
priority:prio block: aBlock value: aValue
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   246
    "Execute aBlock in parallel with whatever called me, but
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   247
     ensure that any messages sent to me before execution
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   248
     of the block has terminated are suspended until it has terminated."
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   249
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   250
    semaphore := Semaphore new name:'Future'.
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   251
    [
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   252
        result := [
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   253
            aBlock value:aValue
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   254
        ] ensure:[semaphore signal]
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   255
    ] forkAt:prio
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   256
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   257
    "Created: / 04-10-2011 / 14:53:35 / cg"
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   258
!
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   259
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   260
priority:prio block:aBlock value:value1 value:value2
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   261
    "Execute aBlock in parallel with whatever called me, but
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   262
     ensure that any messages sent to me before execution
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   263
     of the block has terminated are suspended until it has terminated."
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   264
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   265
    semaphore := Semaphore new name:'Future'.
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   266
    [
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   267
        result := [
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   268
            aBlock value:value1 value:value2
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   269
        ] ensure:[semaphore signal]
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   270
    ] forkAt:prio
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   271
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   272
    "Created: / 04-10-2011 / 14:54:03 / cg"
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   273
!
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   274
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   275
priority:prio block:aBlock value:value1 value:value2 value:value3
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   276
    "Execute aBlock in parallel with whatever called me, but
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   277
     ensure that any messages sent to me before execution
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   278
     of the block has terminated are suspended until it has terminated."
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   279
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   280
    semaphore := Semaphore new name:'Future'.
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   281
    [
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   282
        result := [
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   283
            aBlock value:value1 value:value2 value:value3
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   284
        ] ensure:[semaphore signal]
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   285
    ] forkAt:prio
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   286
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   287
    "Created: / 04-10-2011 / 14:54:51 / cg"
2667
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   288
!
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   289
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   290
priority:prio block:aBlock valueWithArguments:anArray
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   291
    "Execute aBlock in parallel with whatever called me, but
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   292
     ensure that any messages sent to me before execution
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   293
     of the block has terminated are suspended until it has terminated."
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   294
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   295
    semaphore := Semaphore new name:'Future'.
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   296
    [
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   297
        result := [
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   298
            aBlock valueWithArguments:anArray
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   299
        ] ensure:[semaphore signal]
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   300
    ] forkAt:prio
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   301
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   302
    "Created: / 04-10-2011 / 14:55:14 / cg"
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   303
! !
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   304
1140
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   305
!Future methodsFor:'synchronising'!
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   306
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   307
doesNotUnderstand:aMessage
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   308
    "Any message to a Future will end up here."
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   309
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   310
    semaphore waitUncounted. "Wait for evaluation to complete"
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   311
			     "(if not already completed)"
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   312
    ^ result perform:aMessage selector withArguments:aMessage arguments
1140
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   313
! !
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   314
1327
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
   315
!Future methodsFor:'testing'!
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
   316
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   317
hasValue
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   318
    ^ semaphore wouldBlock not
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   319
!
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   320
1327
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
   321
isLazyValue
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
   322
    ^ semaphore wouldBlock
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
   323
! !
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
   324
1140
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   325
!Future class methodsFor:'documentation'!
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   326
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   327
version
2667
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   328
    ^ '$Header: /cvs/stx/stx/libbasic2/Future.st,v 1.13 2011-10-04 12:55:22 cg Exp $'
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   329
!
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   330
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   331
version_CVS
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   332
    ^ '$Header: /cvs/stx/stx/libbasic2/Future.st,v 1.13 2011-10-04 12:55:22 cg Exp $'
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   333
! !