Future.st
author Claus Gittinger <cg@exept.de>
Tue, 25 Jun 2019 09:03:59 +0200
changeset 5031 0cfe45b14c1a
parent 4814 62adbb76ba99
child 5034 bc09c9790f71
permissions -rw-r--r--
#REFACTORING by cg class: Future added: #signalSemaphoreAfterForked: #signalSemaphoreAfterForked:atPriority: comment/format in: #block: #block:value: #block:value:value: #block:value:value:value: #block:valueWithArguments: #priority:block: #priority:block:value: #priority:block:value:value: #priority:block:value:value:value: #priority:block:valueWithArguments: changed: #doesNotUnderstand: #perform:withArguments: #value class: Future class comment/format in: #examples
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4814
62adbb76ba99 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4574
diff changeset
     1
"{ Encoding: utf8 }"
62adbb76ba99 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4574
diff changeset
     2
1140
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     3
"
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     4
 This is a Manchester Goodie protected by copyright.
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     5
 These conditions are imposed on the whole Goodie, and on any significant
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     6
 part of it which is separately transmitted or stored:
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     7
	* You must ensure that every copy includes this notice, and that
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     8
	  source and author(s) of the material are acknowledged.
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     9
	* These conditions must be imposed on anyone who receives a copy.
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    10
	* The material shall not be used for commercial gain without the prior
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    11
	  written consent of the author(s).
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    12
 Further information on the copyright conditions may be obtained by
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    13
 sending electronic mail:
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    14
	To: goodies-lib@cs.man.ac.uk
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    15
	Subject: copyright
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    16
 or by writing to The Smalltalk Goodies Library Manager, Dept of
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    17
 Computer Science, The University, Manchester M13 9PL, UK
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    18
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    19
 (C) Copyright 1992 University of Manchester
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
    20
 For more information about the Manchester Goodies Library (from which
1140
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    21
 this file was distributed) send e-mail:
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    22
	To: goodies-lib@cs.man.ac.uk
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
    23
	Subject: help
1140
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    24
"
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
    25
"{ Package: 'stx:libbasic2' }"
457
c862c91716b6 packages
Claus Gittinger <cg@exept.de>
parents: 351
diff changeset
    26
3848
9a227b37e76b #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3455
diff changeset
    27
"{ NameSpace: Smalltalk }"
9a227b37e76b #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3455
diff changeset
    28
1327
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
    29
ProtoObject subclass:#Future
1140
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    30
	instanceVariableNames:'result semaphore'
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    31
	classVariableNames:''
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    32
	poolDictionaries:''
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    33
	category:'Kernel-Processes'
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    34
!
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    35
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    36
!Future class methodsFor:'documentation'!
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    37
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    38
copyright
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    39
"
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    40
 This is a Manchester Goodie protected by copyright.
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    41
 These conditions are imposed on the whole Goodie, and on any significant
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    42
 part of it which is separately transmitted or stored:
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    43
	* You must ensure that every copy includes this notice, and that
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    44
	  source and author(s) of the material are acknowledged.
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    45
	* These conditions must be imposed on anyone who receives a copy.
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    46
	* The material shall not be used for commercial gain without the prior
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    47
	  written consent of the author(s).
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    48
 Further information on the copyright conditions may be obtained by
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    49
 sending electronic mail:
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    50
	To: goodies-lib@cs.man.ac.uk
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    51
	Subject: copyright
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    52
 or by writing to The Smalltalk Goodies Library Manager, Dept of
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    53
 Computer Science, The University, Manchester M13 9PL, UK
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    54
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    55
 (C) Copyright 1992 University of Manchester
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
    56
 For more information about the Manchester Goodies Library (from which
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    57
 this file was distributed) send e-mail:
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    58
	To: goodies-lib@cs.man.ac.uk
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
    59
	Subject: help
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    60
"
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    61
!
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    62
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    63
documentation
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    64
"
3064
7fe7e32ae4dd class: Future
Claus Gittinger <cg@exept.de>
parents: 2829
diff changeset
    65
    I represent an execution in progress, which will be required some time
7fe7e32ae4dd class: Future
Claus Gittinger <cg@exept.de>
parents: 2829
diff changeset
    66
    in the future.
7fe7e32ae4dd class: Future
Claus Gittinger <cg@exept.de>
parents: 2829
diff changeset
    67
    I will immediately start execution in a separate process,
7fe7e32ae4dd class: Future
Claus Gittinger <cg@exept.de>
parents: 2829
diff changeset
    68
    but delay any messages sent to me, until the execution has completed.
7fe7e32ae4dd class: Future
Claus Gittinger <cg@exept.de>
parents: 2829
diff changeset
    69
    This is useful for time consuming operations (print jobs, compile jobs etc.),
7fe7e32ae4dd class: Future
Claus Gittinger <cg@exept.de>
parents: 2829
diff changeset
    70
    which can be done in the background and the user can do something else
7fe7e32ae4dd class: Future
Claus Gittinger <cg@exept.de>
parents: 2829
diff changeset
    71
    in the meantime. If the computation is finished before the user needs its
7fe7e32ae4dd class: Future
Claus Gittinger <cg@exept.de>
parents: 2829
diff changeset
    72
    value, he is not forced to wait.
7fe7e32ae4dd class: Future
Claus Gittinger <cg@exept.de>
parents: 2829
diff changeset
    73
    If the computation is unfinished, he has to wait for the remaining time only.
1327
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
    74
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
    75
    [author:]
3064
7fe7e32ae4dd class: Future
Claus Gittinger <cg@exept.de>
parents: 2829
diff changeset
    76
        tph@cs.man.ac.uk
1516
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
    77
08829dfb29fe comment
Stefan Vogel <sv@exept.de>
parents: 1514
diff changeset
    78
    [see also:]
3064
7fe7e32ae4dd class: Future
Claus Gittinger <cg@exept.de>
parents: 2829
diff changeset
    79
        Block Lazy LazyValue
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    80
"
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    81
!
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    82
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    83
examples
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    84
"
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    85
  Starts evaluating the factorial immediately, but waits until
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    86
  the result is available before printing the answer
2829
e84f89563562 changed:
Stefan Vogel <sv@exept.de>
parents: 2828
diff changeset
    87
                                                                    [exBegin]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    88
    | fac |
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    89
4006
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
    90
    fac := [50000 factorial] futureValue.
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    91
    Transcript showCR: 'evaluating factorial...'.
3064
7fe7e32ae4dd class: Future
Claus Gittinger <cg@exept.de>
parents: 2829
diff changeset
    92
    Dialog information:'You can do something useful now...'.
2829
e84f89563562 changed:
Stefan Vogel <sv@exept.de>
parents: 2828
diff changeset
    93
    Transcript showCR: fac
e84f89563562 changed:
Stefan Vogel <sv@exept.de>
parents: 2828
diff changeset
    94
                                                                    [exEnd]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    95
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    96
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    97
  An example illustrating the use of multiple futures and
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    98
  explicit resynchronisation.
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
    99
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   100
  Starts evaluating both factorials immediately, but waits until
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   101
  both blocks have finished before continuing.
2829
e84f89563562 changed:
Stefan Vogel <sv@exept.de>
parents: 2828
diff changeset
   102
                                                                    [exBegin]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   103
    | fac1 fac2 |
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   104
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   105
    fac1 := [Transcript showCR: 'Starting fac1.. '. 90000 factorial. Transcript showCR: 'Finished fac1'] futureValue.
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   106
    fac2 := [Transcript showCR: 'Starting fac2.. '. 50000 factorial. Transcript showCR: 'Finished fac2'] futureValue.
4006
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   107
    fac2 value.
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   108
    fac1 value.
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   109
    Transcript showCR: 'both completed.'.
2829
e84f89563562 changed:
Stefan Vogel <sv@exept.de>
parents: 2828
diff changeset
   110
                                                                    [exEnd]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   111
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   112
  Example showing how arguments may be passed to futures.
2829
e84f89563562 changed:
Stefan Vogel <sv@exept.de>
parents: 2828
diff changeset
   113
                                                                    [exBegin]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   114
    | temp |
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   115
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   116
    temp := [:x :y | 10 * x * y] futureValue: 3 value: 4.
2829
e84f89563562 changed:
Stefan Vogel <sv@exept.de>
parents: 2828
diff changeset
   117
    Transcript  showCR: temp.
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   118
2829
e84f89563562 changed:
Stefan Vogel <sv@exept.de>
parents: 2828
diff changeset
   119
                                                                    [exEnd]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   120
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   121
  Claus:
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   122
    The above examples do not really show the power of Futures;
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   123
    they can be useful, whenever some long-time computation is
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   124
    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
   125
    for example:
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   126
3064
7fe7e32ae4dd class: Future
Claus Gittinger <cg@exept.de>
parents: 2829
diff changeset
   127
    Without futures, the inputfile is read before opening the view;
7fe7e32ae4dd class: Future
Claus Gittinger <cg@exept.de>
parents: 2829
diff changeset
   128
    the readTime and view creation times sum up:
2829
e84f89563562 changed:
Stefan Vogel <sv@exept.de>
parents: 2828
diff changeset
   129
                                                                    [exBegin]
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   130
        |p text v t1 t2 tAll|
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   131
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   132
        tAll := TimeDuration toRun:[
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   133
            t1 := TimeDuration toRun:[
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   134
                p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib /etc'.
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   135
                text := p contents.
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   136
                p close.
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   137
            ].
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   138
            t2 := TimeDuration toRun:[
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   139
                v := TextView new openAndWait.
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   140
            ].    
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   141
            v contents:text
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   142
        ].
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   143
        Transcript showCR:'Time to read: %1' with:t1.
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   144
        Transcript showCR:'Time to open: %1' with:t2.
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   145
        Transcript showCR:'Time overall: %1' with:tAll.
2829
e84f89563562 changed:
Stefan Vogel <sv@exept.de>
parents: 2828
diff changeset
   146
                                                                    [exEnd]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   147
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   148
    The same here:
2829
e84f89563562 changed:
Stefan Vogel <sv@exept.de>
parents: 2828
diff changeset
   149
                                                                    [exBegin]
e84f89563562 changed:
Stefan Vogel <sv@exept.de>
parents: 2828
diff changeset
   150
        |p text v|
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   151
2829
e84f89563562 changed:
Stefan Vogel <sv@exept.de>
parents: 2828
diff changeset
   152
        v := TextView new openAndWait.
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   153
        p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib /etc'.
2829
e84f89563562 changed:
Stefan Vogel <sv@exept.de>
parents: 2828
diff changeset
   154
        text := p contents.
e84f89563562 changed:
Stefan Vogel <sv@exept.de>
parents: 2828
diff changeset
   155
        p close.
e84f89563562 changed:
Stefan Vogel <sv@exept.de>
parents: 2828
diff changeset
   156
        v contents:text
e84f89563562 changed:
Stefan Vogel <sv@exept.de>
parents: 2828
diff changeset
   157
                                                                    [exEnd]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   158
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   159
3064
7fe7e32ae4dd class: Future
Claus Gittinger <cg@exept.de>
parents: 2829
diff changeset
   160
    With futures, the view creation and reading are done in parallel:
7fe7e32ae4dd class: Future
Claus Gittinger <cg@exept.de>
parents: 2829
diff changeset
   161
    (if the windowing system is slow when opening the view, the contents may
7fe7e32ae4dd class: Future
Claus Gittinger <cg@exept.de>
parents: 2829
diff changeset
   162
     be already available - especially on X window systems, where the user
7fe7e32ae4dd class: Future
Claus Gittinger <cg@exept.de>
parents: 2829
diff changeset
   163
     has to provide the window position with the mouse)
2829
e84f89563562 changed:
Stefan Vogel <sv@exept.de>
parents: 2828
diff changeset
   164
                                                                    [exBegin]
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   165
        |p text v t1 t2 tAll|
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   166
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   167
        tAll := TimeDuration toRun:[
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   168
            text := [   
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   169
                        |p t|
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   170
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   171
                        t1 := TimeDuration toRun:[
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   172
                            p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib /etc'.
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   173
                            t := p contents.
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   174
                            p close.
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   175
                        ].     
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   176
                        t
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   177
                    ] futureValue.
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   178
            t2 := TimeDuration toRun:[
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   179
                v := TextView new openAndWait.
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   180
            ].    
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   181
            v contents:text
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   182
        ].
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   183
        Transcript showCR:'Time to read: %1' with:t1.
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   184
        Transcript showCR:'Time to open: %1' with:t2.
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   185
        Transcript showCR:'Time overall: %1' with:tAll.
2829
e84f89563562 changed:
Stefan Vogel <sv@exept.de>
parents: 2828
diff changeset
   186
                                                                    [exEnd]
484
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   187
"
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   188
! !
d50c08ae6e3d documentation
Claus Gittinger <cg@exept.de>
parents: 457
diff changeset
   189
1140
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   190
!Future methodsFor:'evaluating'!
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   191
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   192
block:aBlock
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   193
    "Execute aBlock in parallel with whoever called me, 
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   194
     but ensure that any messages sent to me before execution
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   195
     of the block has terminated are suspended until it has terminated."
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   196
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   197
    self signalSemaphoreAfterForked:aBlock
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   198
4501
d8d48f028be7 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4225
diff changeset
   199
    "Modified: / 09-08-2017 / 11:54:19 / cg"
4814
62adbb76ba99 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4574
diff changeset
   200
    "Modified: / 12-02-2019 / 20:26:39 / Stefan Vogel"
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   201
    "Modified (comment): / 25-06-2019 / 07:50:12 / Claus Gittinger"
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   202
!
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   203
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   204
block:aBlock value:aValue
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   205
    "Execute aBlock in parallel with whoever called me, 
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   206
     but ensure that any messages sent to me before execution
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   207
     of the block has terminated are suspended until it has terminated."
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   208
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   209
    self signalSemaphoreAfterForked:[aBlock value:aValue]
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   210
4501
d8d48f028be7 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4225
diff changeset
   211
    "Modified: / 09-08-2017 / 11:54:23 / cg"
4814
62adbb76ba99 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4574
diff changeset
   212
    "Modified: / 12-02-2019 / 20:27:03 / Stefan Vogel"
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   213
    "Modified (comment): / 25-06-2019 / 07:50:09 / Claus Gittinger"
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   214
!
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   215
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   216
block:aBlock value:value1 value:value2
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   217
    "Execute aBlock in parallel with whoever called me, 
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   218
     but ensure that any messages sent to me before execution
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   219
     of the block has terminated are suspended until it has terminated."
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   220
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   221
    self signalSemaphoreAfterForked:[aBlock value:value1 value:value2]
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   222
4501
d8d48f028be7 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4225
diff changeset
   223
    "Modified: / 09-08-2017 / 11:54:27 / cg"
4814
62adbb76ba99 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4574
diff changeset
   224
    "Modified: / 12-02-2019 / 20:27:20 / Stefan Vogel"
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   225
    "Modified (comment): / 25-06-2019 / 07:50:06 / Claus Gittinger"
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   226
!
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   227
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   228
block:aBlock value:value1 value:value2 value:value3
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   229
    "Execute aBlock in parallel with whoever called me, 
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   230
     but ensure that any messages sent to me before execution
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   231
     of the block has terminated are suspended until it has terminated."
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   232
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   233
    self signalSemaphoreAfterForked:[aBlock value:value1 value:value2 value:value3]
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   234
4501
d8d48f028be7 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4225
diff changeset
   235
    "Modified: / 09-08-2017 / 11:54:31 / cg"
4814
62adbb76ba99 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4574
diff changeset
   236
    "Modified: / 12-02-2019 / 20:27:31 / Stefan Vogel"
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   237
    "Modified (comment): / 25-06-2019 / 07:50:03 / Claus Gittinger"
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   238
!
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   239
2667
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   240
block:aBlock valueWithArguments:anArray
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   241
    "Execute aBlock in parallel with whoever called me, 
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   242
     but ensure that any messages sent to me before execution
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   243
     of the block has terminated are suspended until it has terminated."
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   244
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   245
    self signalSemaphoreAfterForked:[aBlock valueWithArguments:anArray]
2667
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   246
4501
d8d48f028be7 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4225
diff changeset
   247
    "Modified: / 09-08-2017 / 11:54:34 / cg"
4814
62adbb76ba99 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4574
diff changeset
   248
    "Modified: / 12-02-2019 / 20:27:40 / Stefan Vogel"
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   249
    "Modified (comment): / 25-06-2019 / 07:49:52 / Claus Gittinger"
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   250
!
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   251
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   252
priority:prio block:aBlock
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   253
    "Execute aBlock in parallel with whoever called me, 
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   254
     but ensure that any messages sent to me before execution
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   255
     of the block has terminated are suspended until it has terminated."
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   256
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   257
    self signalSemaphoreAfterForked:[aBlock value] atPriority:prio
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   258
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   259
    "Created: / 04-10-2011 / 14:53:21 / cg"
4501
d8d48f028be7 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4225
diff changeset
   260
    "Modified: / 09-08-2017 / 11:54:38 / cg"
4814
62adbb76ba99 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4574
diff changeset
   261
    "Modified: / 12-02-2019 / 20:27:52 / Stefan Vogel"
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   262
    "Modified (comment): / 25-06-2019 / 07:50:20 / Claus Gittinger"
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   263
!
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   264
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   265
priority:prio block: aBlock value: aValue
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   266
    "Execute aBlock in parallel with whoever called me, 
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   267
     but ensure that any messages sent to me before execution
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   268
     of the block has terminated are suspended until it has terminated."
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   269
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   270
    self signalSemaphoreAfterForked:[aBlock value:aValue] atPriority:prio
2666
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:53:35 / cg"
4501
d8d48f028be7 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4225
diff changeset
   273
    "Modified: / 09-08-2017 / 11:54:41 / cg"
4814
62adbb76ba99 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4574
diff changeset
   274
    "Modified: / 12-02-2019 / 20:28:02 / Stefan Vogel"
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   275
    "Modified (comment): / 25-06-2019 / 07:50:23 / Claus Gittinger"
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   276
!
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   277
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   278
priority:prio block:aBlock value:value1 value:value2
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   279
    "Execute aBlock in parallel with whoever called me, 
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   280
     but ensure that any messages sent to me before execution
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   281
     of the block has terminated are suspended until it has terminated."
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   282
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   283
    self signalSemaphoreAfterForked:[aBlock value:value1 value:value2] atPriority:prio
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   284
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   285
    "Created: / 04-10-2011 / 14:54:03 / cg"
4501
d8d48f028be7 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4225
diff changeset
   286
    "Modified: / 09-08-2017 / 11:54:44 / cg"
4814
62adbb76ba99 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4574
diff changeset
   287
    "Modified: / 12-02-2019 / 20:28:10 / Stefan Vogel"
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   288
    "Modified (comment): / 25-06-2019 / 07:50:26 / Claus Gittinger"
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   289
!
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   290
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   291
priority:prio block:aBlock value:value1 value:value2 value:value3
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   292
    "Execute aBlock in parallel with whoever called me, 
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   293
     but ensure that any messages sent to me before execution
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   294
     of the block has terminated are suspended until it has terminated."
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   295
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   296
    self signalSemaphoreAfterForked:[aBlock value:value1 value:value2 value:value3] atPriority:prio
2666
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   297
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   298
    "Created: / 04-10-2011 / 14:54:51 / cg"
4501
d8d48f028be7 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4225
diff changeset
   299
    "Modified: / 09-08-2017 / 11:54:47 / cg"
4814
62adbb76ba99 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4574
diff changeset
   300
    "Modified: / 12-02-2019 / 20:28:21 / Stefan Vogel"
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   301
    "Modified (comment): / 25-06-2019 / 07:50:29 / Claus Gittinger"
2667
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   302
!
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   303
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   304
priority:prio block:aBlock valueWithArguments:anArray
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   305
    "Execute aBlock in parallel with whoever called me, 
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   306
     but ensure that any messages sent to me before execution
2667
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   307
     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
   308
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   309
    self signalSemaphoreAfterForked:[aBlock valueWithArguments:anArray] atPriority:prio
2667
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   310
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   311
    "Created: / 04-10-2011 / 14:55:14 / cg"
4501
d8d48f028be7 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4225
diff changeset
   312
    "Modified: / 09-08-2017 / 11:54:50 / cg"
4814
62adbb76ba99 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4574
diff changeset
   313
    "Modified: / 12-02-2019 / 20:28:30 / Stefan Vogel"
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   314
    "Modified (comment): / 25-06-2019 / 07:50:32 / Claus Gittinger"
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   315
! !
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   316
4006
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   317
!Future methodsFor:'printing'!
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   318
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   319
displayOn:aGCOrStream
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   320
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   321
    "/ what a kludge - Dolphin and Squeak mean: printOn: a stream;
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   322
    "/ ST/X (and some old ST80's) mean: draw-yourself on a GC.
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   323
    (aGCOrStream isStream) ifFalse:[
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   324
        ^ self value displayOn:aGCOrStream
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   325
    ].
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   326
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   327
    semaphore isNil ifTrue:[
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   328
        result displayOn:aGCOrStream.
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   329
        aGCOrStream nextPutAll:' (Future evaluated)'.
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   330
        ^ self.
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   331
    ].    
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   332
    aGCOrStream nextPutAll:'Future (unevaluated)'
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   333
!
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   334
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   335
displayString
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   336
    |s|
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   337
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   338
    "/ attention: TextStream is not present in ultra-mini standalone apps
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   339
    s := TextStream isNil
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   340
            ifTrue:['' writeStream]
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   341
            ifFalse:[TextStream on:(String new:32)].
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   342
    self displayOn:s.
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   343
    ^ s contents
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   344
! !
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   345
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   346
!Future methodsFor:'private'!
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   347
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   348
signalSemaphoreAfterForked:aBlock
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   349
    "common code for all block:* methods.
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   350
     Execute aBlock in parallel with whatever called me, 
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   351
     and ensure that my private semaphore is signalled at the end."
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   352
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   353
    semaphore := Semaphore name:'Future'.
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   354
    [
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   355
        result := aBlock ensure:[semaphore signal. semaphore := nil.]
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   356
    ] fork
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   357
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   358
    "Created: / 25-06-2019 / 07:32:28 / Claus Gittinger"
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   359
!
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   360
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   361
signalSemaphoreAfterForked:aBlock atPriority:prio
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   362
    "common code for all block:* methods.
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   363
     Execute aBlock in parallel with whatever called me, 
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   364
     and ensure that my private semaphore is signalled at the end."
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   365
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   366
    semaphore := Semaphore name:'Future'.
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   367
    [
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   368
        result := aBlock ensure:[semaphore signal. semaphore := nil.]
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   369
    ] forkAt:prio
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   370
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   371
    "Created: / 25-06-2019 / 07:33:55 / Claus Gittinger"
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   372
! !
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   373
1140
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   374
!Future methodsFor:'synchronising'!
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   375
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1516
diff changeset
   376
doesNotUnderstand:aMessage
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   377
    "Any message to a Future will end up here."
2669
a01e7c23bf61 added: #value
Claus Gittinger <cg@exept.de>
parents: 2668
diff changeset
   378
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   379
    |sema|
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   380
    
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   381
    (sema := semaphore) notNil ifTrue:[
4574
8e32161e5056 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4501
diff changeset
   382
        Processor activeProcess isDebuggerProcess ifTrue:[
4006
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   383
            "enable debugging / inspecting"
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   384
            ^ aMessage sendTo:self usingClass:Object.
2828
8894852ab900 Use IsDebuggingQuery
Stefan Vogel <sv@exept.de>
parents: 2669
diff changeset
   385
        ].
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   386
        sema waitUncounted. "Wait for evaluation to complete"
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   387
                            "(if not already completed)"
2669
a01e7c23bf61 added: #value
Claus Gittinger <cg@exept.de>
parents: 2668
diff changeset
   388
    ].
3848
9a227b37e76b #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3455
diff changeset
   389
    ^ aMessage sendTo:result
2668
67b51e1ee66d changed:
Claus Gittinger <cg@exept.de>
parents: 2667
diff changeset
   390
2669
a01e7c23bf61 added: #value
Claus Gittinger <cg@exept.de>
parents: 2668
diff changeset
   391
    "Modified: / 04-10-2011 / 17:37:18 / cg"
4574
8e32161e5056 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4501
diff changeset
   392
    "Modified: / 01-02-2018 / 10:17:48 / stefan"
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   393
    "Modified: / 25-06-2019 / 07:52:42 / Claus Gittinger"
2669
a01e7c23bf61 added: #value
Claus Gittinger <cg@exept.de>
parents: 2668
diff changeset
   394
!
a01e7c23bf61 added: #value
Claus Gittinger <cg@exept.de>
parents: 2668
diff changeset
   395
4225
4cb5eeb736bc #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4006
diff changeset
   396
perform:aSelector withArguments:argArray
4cb5eeb736bc #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4006
diff changeset
   397
    "send the message aSelector with all args taken from argArray
4cb5eeb736bc #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4006
diff changeset
   398
     to the receiver."
4cb5eeb736bc #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4006
diff changeset
   399
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   400
    |sema|
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   401
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   402
    (sema := semaphore) notNil ifTrue:[
4574
8e32161e5056 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4501
diff changeset
   403
        Processor activeProcess isDebuggerProcess ifTrue:[
4225
4cb5eeb736bc #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4006
diff changeset
   404
            "enable debugging / inspecting"
4cb5eeb736bc #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4006
diff changeset
   405
            ^ super perform:aSelector withArguments:argArray.
4cb5eeb736bc #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4006
diff changeset
   406
        ].
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   407
        sema waitUncounted.
4225
4cb5eeb736bc #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4006
diff changeset
   408
    ].
4cb5eeb736bc #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4006
diff changeset
   409
    ^ result perform:aSelector withArguments:argArray.
4574
8e32161e5056 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4501
diff changeset
   410
8e32161e5056 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4501
diff changeset
   411
    "Modified (format): / 01-02-2018 / 10:17:44 / stefan"
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   412
    "Modified: / 25-06-2019 / 07:53:01 / Claus Gittinger"
4225
4cb5eeb736bc #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4006
diff changeset
   413
!
4cb5eeb736bc #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4006
diff changeset
   414
2669
a01e7c23bf61 added: #value
Claus Gittinger <cg@exept.de>
parents: 2668
diff changeset
   415
value
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   416
    "retrieve the value, possibly waiting for the result to arrive"
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   417
    
4006
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   418
    |sema|
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   419
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   420
    (sema := semaphore) notNil ifTrue:[
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   421
        sema waitUncounted. "Wait for evaluation to complete"
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   422
                            "(if not already completed)"
2669
a01e7c23bf61 added: #value
Claus Gittinger <cg@exept.de>
parents: 2668
diff changeset
   423
    ].
a01e7c23bf61 added: #value
Claus Gittinger <cg@exept.de>
parents: 2668
diff changeset
   424
    ^ result
a01e7c23bf61 added: #value
Claus Gittinger <cg@exept.de>
parents: 2668
diff changeset
   425
a01e7c23bf61 added: #value
Claus Gittinger <cg@exept.de>
parents: 2668
diff changeset
   426
    "Created: / 04-10-2011 / 17:36:06 / cg"
5031
0cfe45b14c1a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4814
diff changeset
   427
    "Modified (comment): / 25-06-2019 / 07:53:28 / Claus Gittinger"
1140
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   428
! !
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   429
1327
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
   430
!Future methodsFor:'testing'!
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
   431
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   432
hasValue
4006
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   433
    ^ semaphore isNil or:[semaphore wouldBlock not].
2668
67b51e1ee66d changed:
Claus Gittinger <cg@exept.de>
parents: 2667
diff changeset
   434
67b51e1ee66d changed:
Claus Gittinger <cg@exept.de>
parents: 2667
diff changeset
   435
    "Modified: / 04-10-2011 / 17:29:36 / cg"
1514
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   436
!
83af7e61d415 Inherit from protoObject.
Stefan Vogel <sv@exept.de>
parents: 1327
diff changeset
   437
1327
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
   438
isLazyValue
4006
1bd5da475215 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3848
diff changeset
   439
    ^ semaphore notNil and:[semaphore wouldBlock]
1327
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
   440
! !
cf928a994f81 comments
Claus Gittinger <cg@exept.de>
parents: 1140
diff changeset
   441
1140
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   442
!Future class methodsFor:'documentation'!
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   443
cbb20fd710fa added #hasValue
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   444
version
3848
9a227b37e76b #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3455
diff changeset
   445
    ^ '$Header$'
2667
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   446
!
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   447
e65a29b55fe1 added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents: 2666
diff changeset
   448
version_CVS
3848
9a227b37e76b #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3455
diff changeset
   449
    ^ '$Header$'
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
   450
! !
3064
7fe7e32ae4dd class: Future
Claus Gittinger <cg@exept.de>
parents: 2829
diff changeset
   451