Lazy.st
author Claus Gittinger <cg@exept.de>
Sat, 02 May 2020 21:40:13 +0200
changeset 5476 7355a4b11cb6
parent 5203 c0d8c3ff1c9c
permissions -rw-r--r--
#FEATURE by cg class: Socket class added: #newTCPclientToHost:port:domain:domainOrder:withTimeout: changed: #newTCPclientToHost:port:domain:withTimeout:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5033
256f0b1cdd22 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4573
diff changeset
     1
"{ Encoding: utf8 }"
256f0b1cdd22 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4573
diff changeset
     2
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     3
"
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     4
 This is a Manchester Goodie protected by copyright.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     5
 These conditions are imposed on the whole Goodie, and on any significant
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     6
 part of it which is separately transmitted or stored:
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     7
	* You must ensure that every copy includes this notice, and that
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     8
	  source and author(s) of the material are acknowledged.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     9
	* These conditions must be imposed on anyone who receives a copy.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    10
	* The material shall not be used for commercial gain without the prior
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    11
	  written consent of the author(s).
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    12
 Further information on the copyright conditions may be obtained by
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    13
 sending electronic mail:
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    14
	To: goodies-lib@cs.man.ac.uk
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    15
	Subject: copyright
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    16
 or by writing to The Smalltalk Goodies Library Manager, Dept of
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    17
 Computer Science, The University, Manchester M13 9PL, UK
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    18
b8e168f120ce comments
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: 1515
diff changeset
    20
 For more information about the Manchester Goodies Library (from which
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    21
 this file was distributed) send e-mail:
b8e168f120ce comments
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: 1515
diff changeset
    23
	Subject: help
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    24
"
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1515
diff changeset
    25
"{ Package: 'stx:libbasic2' }"
457
c862c91716b6 packages
Claus Gittinger <cg@exept.de>
parents: 351
diff changeset
    26
3849
62f298788374 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3063
diff changeset
    27
"{ NameSpace: Smalltalk }"
62f298788374 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3063
diff changeset
    28
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    29
ProtoObject subclass:#Lazy
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    30
	instanceVariableNames:'result startSemaphore endSemaphore'
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    31
	classVariableNames:''
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    32
	poolDictionaries:''
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    33
	category:'Kernel-Processes'
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    34
!
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    35
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    36
!Lazy class methodsFor:'documentation'!
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    37
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    38
copyright
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    39
"
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    40
 This is a Manchester Goodie protected by copyright.
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    41
 These conditions are imposed on the whole Goodie, and on any significant
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    42
 part of it which is separately transmitted or stored:
67
6d8a403eff0a *** empty log message ***
claus
parents: 62
diff changeset
    43
	* You must ensure that every copy includes this notice, and that
6d8a403eff0a *** empty log message ***
claus
parents: 62
diff changeset
    44
	  source and author(s) of the material are acknowledged.
6d8a403eff0a *** empty log message ***
claus
parents: 62
diff changeset
    45
	* These conditions must be imposed on anyone who receives a copy.
6d8a403eff0a *** empty log message ***
claus
parents: 62
diff changeset
    46
	* The material shall not be used for commercial gain without the prior
6d8a403eff0a *** empty log message ***
claus
parents: 62
diff changeset
    47
	  written consent of the author(s).
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    48
 Further information on the copyright conditions may be obtained by
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    49
 sending electronic mail:
67
6d8a403eff0a *** empty log message ***
claus
parents: 62
diff changeset
    50
	To: goodies-lib@cs.man.ac.uk
6d8a403eff0a *** empty log message ***
claus
parents: 62
diff changeset
    51
	Subject: copyright
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    52
 or by writing to The Smalltalk Goodies Library Manager, Dept of
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    53
 Computer Science, The University, Manchester M13 9PL, UK
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    54
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    55
 (C) Copyright 1992 University of Manchester
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1515
diff changeset
    56
 For more information about the Manchester Goodies Library (from which
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    57
 this file was distributed) send e-mail:
67
6d8a403eff0a *** empty log message ***
claus
parents: 62
diff changeset
    58
	To: goodies-lib@cs.man.ac.uk
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1515
diff changeset
    59
	Subject: help
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    60
"
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    61
!
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    62
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    63
documentation
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    64
"
3063
b4af2ee4ed93 class: Lazy
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
    65
    I represent an execution which may or may not be required.
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1515
diff changeset
    66
    I will not start execution until at least one message has been received.
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    67
    The messages sent to me are delayed until execution has completed.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    68
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1515
diff changeset
    69
    [author:]
3063
b4af2ee4ed93 class: Lazy
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
    70
        tph@cs.man.ac.uk
1515
006cd21bbc25 Inherit from ProtoObject.
Stefan Vogel <sv@exept.de>
parents: 1326
diff changeset
    71
006cd21bbc25 Inherit from ProtoObject.
Stefan Vogel <sv@exept.de>
parents: 1326
diff changeset
    72
    [see also:]
3063
b4af2ee4ed93 class: Lazy
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
    73
        Block LazyValue Future
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    74
"
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    75
!
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    76
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    77
examples
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    78
"
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    79
  Evaluates the factorial, starting only when the
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    80
  result is actually required (when printString is sent).
3977
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
    81
                                                [exBegin]
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    82
    | fac |
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    83
    fac := [100 factorial] lazyValue.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    84
    Transcript showCR: 'Doing nothing. '.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    85
    (Delay forSeconds: 2) wait.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    86
    Transcript showCR: fac printString.
3977
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
    87
                                                [exEnd]
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    88
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    89
3977
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
    90
  Starts evaluating both factorials only when required (by the value),
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    91
  and waits until both blocks have finished before continuing.
3977
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
    92
                                                [exBegin]
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    93
    | fac1 fac2 |
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    94
    fac1 := [Transcript showCR: 'Starting fac1.. '. 100 factorial] lazyValue.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    95
    fac2 := [Transcript showCR: 'Starting fac2.. '. 120 factorial] lazyValue.
3977
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
    96
    fac2 value.
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
    97
    fac1 value.
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    98
    Transcript showCR: 'both completed.'.
3977
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
    99
                                                [exEnd]
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   100
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   101
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   102
  Demonstrates how to pass arguments to a lazy evaluation block.
3977
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   103
                                                [exBegin]
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   104
    | temp |
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   105
    temp := [:x :y :z | x * y * z] lazyValueWithArguments: #(2 3 4).
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   106
    Transcript  showCR: temp printString.
3977
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   107
                                                [exEnd]
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   108
"
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   109
! !
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   110
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   111
!Lazy methodsFor:'evaluating'!
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   112
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   113
block: aBlock
3977
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   114
    "Execute aBlock in parallel, but ensure that any messages sent
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   115
     to me before execution of the block has terminated are
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   116
     suspended until it has terminated. Do not start the evaluation
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   117
     until at least one message has been sent to the receiver."
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   118
3977
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   119
    startSemaphore := Semaphore new.
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   120
    endSemaphore := Semaphore new.
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   121
    [
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   122
        startSemaphore wait.
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   123
        result := aBlock value.
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   124
        endSemaphore signal
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   125
    ] fork.
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   126
!
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   127
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   128
block: aBlock value: aValue
5203
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   129
    "Execute aBlock in parallel, but ensure that any messages sent
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   130
     to me before execution of the block has terminated are
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   131
     suspended until it has terminated. Do not start the evaluation
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   132
     until at least one message has been sent to the receiver."
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   133
5203
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   134
    startSemaphore := Semaphore new.
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   135
    endSemaphore := Semaphore new.
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   136
    [
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   137
        startSemaphore wait.
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   138
        result := aBlock value: aValue.
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   139
        endSemaphore signal
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   140
    ] fork
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   141
!
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   142
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   143
block: aBlock value: value1 value: value2
5203
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   144
    "Execute aBlock in parallel, but ensure that any messages sent
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   145
     to me before execution of the block has terminated are
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   146
     suspended until it has terminated. Do not start the evaluation
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   147
     until at least one message has been sent to the receiver."
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   148
5203
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   149
    startSemaphore := Semaphore new.
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   150
    endSemaphore := Semaphore new.
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   151
    [
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   152
        startSemaphore wait.
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   153
        result := aBlock value: value1 value: value2.
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   154
        endSemaphore signal
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   155
    ] fork
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   156
!
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   157
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   158
block: aBlock value: value1 value: value2 value: value3
5203
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   159
    "Execute aBlock in parallel, but ensure that any messages sent
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   160
     to me before execution of the block has terminated are
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   161
     suspended until it has terminated. Do not start the evaluation
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   162
     until at least one message has been sent to the receiver."
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   163
5203
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   164
    startSemaphore := Semaphore new.
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   165
    endSemaphore := Semaphore new.
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   166
    [
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   167
        startSemaphore wait.
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   168
        result := aBlock value: value1 value: value2 value: value3.
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   169
        endSemaphore signal
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   170
    ] fork
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   171
!
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   172
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   173
block: aBlock valueWithArguments: anArray
5203
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   174
    "Execute aBlock in parallel, but ensure that any messages sent
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   175
     to me before execution of the block has terminated are
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   176
     suspended until it has terminated. Do not start the evaluation
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   177
     until at least one message has been sent to the receiver."
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   178
5203
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   179
    startSemaphore := Semaphore new.
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   180
    endSemaphore := Semaphore new.
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   181
    [
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   182
        startSemaphore wait.
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   183
        result := aBlock valueWithArguments: anArray.
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   184
        endSemaphore signal
c0d8c3ff1c9c #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   185
    ] fork
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   186
! !
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   187
4007
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   188
!Lazy methodsFor:'printing'!
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   189
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   190
displayOn:aGCOrStream
5045
b7e343586357 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5033
diff changeset
   191
    "notice: displayString and displayOn: will not wait for the value (they are for developers and inspectors),
b7e343586357 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5033
diff changeset
   192
     whereas printString and printOn: will wait (they are for the program to print data)."
4007
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   193
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   194
    "/ what a kludge - Dolphin and Squeak mean: printOn: a stream;
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   195
    "/ ST/X (and some old ST80's) mean: draw-yourself on a GC.
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   196
    (aGCOrStream isStream) ifFalse:[
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   197
        ^ self _evaluate_ displayOn:aGCOrStream
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   198
    ].
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   199
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   200
    startSemaphore isNil ifTrue:[
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   201
        result displayOn:aGCOrStream.
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   202
        aGCOrStream nextPutAll:' (Lazy evaluated)'.
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   203
        ^ self.
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   204
    ].    
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   205
    aGCOrStream nextPutAll:'Lazy (unevaluated)'
5045
b7e343586357 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5033
diff changeset
   206
b7e343586357 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5033
diff changeset
   207
    "Modified (comment): / 25-06-2019 / 10:49:32 / Claus Gittinger"
4007
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   208
!
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   209
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   210
displayString
5045
b7e343586357 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5033
diff changeset
   211
    "notice: displayString and displayOn: will not wait for the value (they are for developers and inspectors),
b7e343586357 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5033
diff changeset
   212
     whereas printString and printOn: will wait (they are for the program to print data)."
b7e343586357 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5033
diff changeset
   213
5033
256f0b1cdd22 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4573
diff changeset
   214
    "defined here, because I inherit from nobody"
4007
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   215
5033
256f0b1cdd22 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4573
diff changeset
   216
    ^ (Object compiledMethodAt:#displayString)
256f0b1cdd22 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4573
diff changeset
   217
        valueWithReceiver:self
256f0b1cdd22 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4573
diff changeset
   218
        arguments:nil
256f0b1cdd22 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4573
diff changeset
   219
        selector:#displayString
256f0b1cdd22 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4573
diff changeset
   220
5045
b7e343586357 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5033
diff changeset
   221
    "Modified (comment): / 25-06-2019 / 10:49:36 / Claus Gittinger"
4007
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   222
! !
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   223
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   224
!Lazy methodsFor:'synchronising'!
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   225
3977
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   226
_evaluate_
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   227
    "answer the computed value"
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   228
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   229
    |startSema endSema|
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   230
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   231
    startSema := startSemaphore.
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   232
    startSema notNil ifTrue:[
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   233
        startSema signal.          "Start the evaluation."
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   234
        endSema := endSemaphore.
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   235
        endSema notNil ifTrue:[
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   236
            endSema waitUncounted.     "Wait until evaluation completed."
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   237
        ].
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   238
        startSemaphore := endSemaphore := nil.
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   239
    ].
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   240
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   241
    ^ result
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   242
!
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   243
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   244
doesNotUnderstand: aMessage
3849
62f298788374 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3063
diff changeset
   245
    "Any message to a Lazy will end up here."
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   246
4007
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   247
    startSemaphore notNil ifTrue:[
4573
7033574e421a #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4226
diff changeset
   248
        Processor activeProcess isDebuggerProcess ifTrue:[
4007
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   249
            "enable debugging / inspecting without evaluating"
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   250
            ^ aMessage sendTo:self usingClass:Object.
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   251
        ].
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   252
        self _evaluate_.
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   253
    ].
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   254
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   255
    ^ aMessage sendTo:result.
4573
7033574e421a #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4226
diff changeset
   256
7033574e421a #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4226
diff changeset
   257
    "Modified: / 01-02-2018 / 10:17:27 / stefan"
4226
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   258
!
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   259
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   260
perform:aSelector withArguments:argArray
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   261
    "send the message aSelector with all args taken from argArray
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   262
     to the receiver."
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   263
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   264
    startSemaphore notNil ifTrue:[
4573
7033574e421a #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4226
diff changeset
   265
        Processor activeProcess isDebuggerProcess ifTrue:[
4226
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   266
            "enable debugging / inspecting"
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   267
            ^ super perform:aSelector withArguments:argArray.
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   268
        ].
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   269
        self _evaluate_.
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   270
    ].
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   271
    ^ result perform:aSelector withArguments:argArray.
4573
7033574e421a #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4226
diff changeset
   272
7033574e421a #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4226
diff changeset
   273
    "Modified: / 01-02-2018 / 10:17:08 / stefan"
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   274
! !
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   275
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   276
!Lazy methodsFor:'testing'!
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   277
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   278
isLazyValue
5048
63f42cfbaf59 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5045
diff changeset
   279
    "true if I have no value yet 
63f42cfbaf59 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5045
diff changeset
   280
     (i.e. would block when sending me a message)"
63f42cfbaf59 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5045
diff changeset
   281
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1515
diff changeset
   282
    ^ endSemaphore wouldBlock
5048
63f42cfbaf59 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5045
diff changeset
   283
63f42cfbaf59 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5045
diff changeset
   284
    "Modified (comment): / 25-06-2019 / 11:08:45 / Claus Gittinger"
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   285
! !
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   286
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   287
!Lazy class methodsFor:'documentation'!
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   288
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   289
version
3849
62f298788374 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3063
diff changeset
   290
    ^ '$Header$'
5045
b7e343586357 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5033
diff changeset
   291
!
b7e343586357 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5033
diff changeset
   292
b7e343586357 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5033
diff changeset
   293
version_CVS
b7e343586357 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 5033
diff changeset
   294
    ^ '$Header$'
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   295
! !
3063
b4af2ee4ed93 class: Lazy
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   296