Semaphore.st
author Claus Gittinger <cg@exept.de>
Wed, 25 Aug 1999 18:21:24 +0200
changeset 4633 b6908af8f900
parent 4437 212260fce312
child 4902 caf73c9821c5
permissions -rw-r--r--
comment
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     1
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1993 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
     3
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     4
a27a279701f8 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
375
claus
parents: 362
diff changeset
    13
Object subclass:#Semaphore
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    14
	instanceVariableNames:'count waitingProcesses lastOwnerID name'
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
    15
	classVariableNames:''
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
    16
	poolDictionaries:''
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
    17
	category:'Kernel-Processes'
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    18
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    19
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    20
!Semaphore class methodsFor:'documentation'!
69
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    21
88
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    22
copyright
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    23
"
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    24
 COPYRIGHT (c) 1993 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    25
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    26
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    27
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    28
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    29
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    30
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    31
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    32
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    33
"
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    34
!
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    35
69
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    36
documentation
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    37
"
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    38
    Semaphores are used to synchronize processes providing a nonBusy wait
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    39
    mechanism. A process can wait for the availability of some resource by
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
    40
    performing a Semaphore>>wait, which will suspend the process until the
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
    41
    resource becomes available. Signalling is done by (another process performing) 
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
    42
    Semaphore>>signal.
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    43
    If the resource has been already available before the wait, no suspending is
69
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    44
    done, but the resource immediately allocated.
2234
21fe6c17fe8d commentary
Claus Gittinger <cg@exept.de>
parents: 2142
diff changeset
    45
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
    46
    There are also semaphores for mutual access to a critical region
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
    47
    (Semaphore>>forMutualExclusion and Semaphore>>critical:).
88
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    48
2234
21fe6c17fe8d commentary
Claus Gittinger <cg@exept.de>
parents: 2142
diff changeset
    49
    Additional protocol is provided for oneShot semaphores, 
21fe6c17fe8d commentary
Claus Gittinger <cg@exept.de>
parents: 2142
diff changeset
    50
    (#signalOnce) and for conditional signalling (#signalIf).
21fe6c17fe8d commentary
Claus Gittinger <cg@exept.de>
parents: 2142
diff changeset
    51
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    52
    You can also attach semaphores to external events (such as I/O arrival or
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
    53
    timer events). 
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
    54
    This is done by telling the Processor to signal the semaphore 
183
7899f380e77d signalIf
claus
parents: 159
diff changeset
    55
    under some condition.
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    56
    See 'Processor>>signal:afterSeconds:', 'Processor>>signal:onInput:' etc.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    57
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
    58
    See examples in doc/coding (found in the CodingExamples-nameSpace).
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1185
diff changeset
    59
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    60
    Warning/Note/Hint:
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    61
        a Semaphore-forMutualExclusion does NEVER allow for the critical
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    62
        region to be entered twice - NOT EVEN by the same process.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    63
        That means, that a recursive attempt to enter that section leads
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    64
        to a deadlock.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    65
        Use a RecursionLock instead of a semaphore to avoid this.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    66
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    67
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    68
    [instance variables:]
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    69
        count                   <SmallInteger>          the number of waits, that will go through
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    70
                                                        without blocking.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    71
                                                        Incremented on #signal; decremented on #wait.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    72
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    73
        waitingProcesses        <OrderedCollection>     waiting processes - will be served first
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    74
                                                        come first served when signalled.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    75
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    76
        lastOwnerID             <SmallInteger>          a debugging aid: set when count drops
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    77
                                                        to zero to the current processes id.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    78
                                                        Helps in finding deadlocks.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    79
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    80
        name                    <String>                a debugging aid: an optional userFriendly
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    81
                                                        name; helps to identify a semaphore easier.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    82
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1185
diff changeset
    83
    [see also:]
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    84
        SemaphoreSet RecursionLock Monitor
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    85
        SharedQueue Delay 
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    86
        Process ProcessorScheduler
1294
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    87
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    88
    [author:]
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    89
        Claus Gittinger
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
    90
"
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
    91
!
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
    92
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
    93
examples
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
    94
"
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
    95
    two processes synchronizing on a sema:
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    96
                                                        [exBegin]
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    97
        |sema thread1 thread2|
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
    98
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    99
        sema := Semaphore new.
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   100
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   101
        thread1 := [
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   102
                        Transcript showCR:'here is thread 1; now waiting ...'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   103
                        sema wait.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   104
                        Transcript showCR:'here is thread 1 again.'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   105
                   ] newProcess.
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   106
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   107
        thread2 := [
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   108
                        Transcript showCR:'here is thread 2; delaying a bit ...'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   109
                        Delay waitForSeconds:5.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   110
                        Transcript showCR:'here is thread 2 again; now signalling the sema'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   111
                        sema signal.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   112
                        Transcript showCR:'here is thread 2 after the signalling.'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   113
                  ] newProcess.
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   114
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   115
        thread1 priority:7.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   116
        thread2 priority:6.
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   117
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   118
        thread1 resume.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   119
        thread2 resume.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   120
                                                        [exEnd]
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   121
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   122
    semaphore for critical regions:
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   123
                                                        [exBegin]
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   124
        |accessLock|
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   125
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   126
        accessLock := Semaphore forMutualExclusion.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   127
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   128
        [
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   129
            5 timesRepeat:[
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   130
                Delay waitForSeconds:2.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   131
                accessLock critical:[
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   132
                    Transcript showCR:'thread1 in critical region'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   133
                    Delay waitForSeconds:1.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   134
                    Transcript showCR:'thread1 leaving critical region'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   135
                ].
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   136
            ]
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   137
        ] forkAt:5.
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   138
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   139
        [
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   140
            5 timesRepeat:[
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   141
                Delay waitForSeconds:1.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   142
                accessLock critical:[
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   143
                    Transcript showCR:'thread2 in critical region'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   144
                    Delay waitForSeconds:2.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   145
                    Transcript showCR:'thread2 leaving critical region'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   146
                ].
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   147
            ]
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   148
        ] forkAt:4.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   149
                                                        [exEnd]
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   150
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   151
    a deadlock due to recursive enter of a critical region:
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   152
                                                        [exBegin]
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   153
        |accessLock block|
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   154
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   155
        accessLock := Semaphore forMutualExclusion.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   156
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   157
        block := [:arg |
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   158
                    Transcript showCR:'about to enter'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   159
                    accessLock critical:[
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   160
                        Transcript showCR:'entered - doing action'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   161
                        arg value
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   162
                    ].
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   163
                    Transcript showCR:'left region'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   164
                 ].
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   165
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   166
        block value:[].                 'this works'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   167
        block value:[block value:[] ].  'this deadlocks'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   168
                                                        [exEnd]
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   169
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   170
    Avoid the deadlock by using a RecursionLock instead:
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   171
                                                        [exBegin]
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   172
        |accessLock block|
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   173
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   174
        accessLock := RecursionLock new.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   175
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   176
        block := [:arg |
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   177
                    Transcript showCR:'about to enter'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   178
                    accessLock critical:[
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   179
                        Transcript showCR:'entered - doing action'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   180
                        arg value
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   181
                    ].
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   182
                    Transcript showCR:'left region'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   183
                 ].
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   184
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   185
        block value:[].                 'this works'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   186
        block value:[block value:[] ].  'this deadlocks'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   187
                                                        [exEnd]
69
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
   188
"
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
   189
! !
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
   190
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   191
!Semaphore class methodsFor:'instance creation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   192
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   193
forMutualExclusion
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   194
    "create & return a new semaphore which allows exactly one process to
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   195
     wait on it without blocking. This type of semaphore is used
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   196
     for mutual exclusion from critical regions (see #critical:).
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   197
     Also see RecursionLock, to avoid deadlock in case of recursive entered
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   198
     critical regions."
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   199
3600
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   200
    ^ super new setCount:1; name:'criticalRegionSema'
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   201
3600
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   202
    "Modified: / 17.6.1998 / 16:23:09 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   203
!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   204
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   205
new
a27a279701f8 Initial revision
claus
parents:
diff changeset
   206
    "create & return a new semaphore which blocks until a signal is sent"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   207
a27a279701f8 Initial revision
claus
parents:
diff changeset
   208
    ^ super new setCount:0
a27a279701f8 Initial revision
claus
parents:
diff changeset
   209
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   210
a27a279701f8 Initial revision
claus
parents:
diff changeset
   211
new:n
a27a279701f8 Initial revision
claus
parents:
diff changeset
   212
    "create & return a new semaphore which allows n waits before
a27a279701f8 Initial revision
claus
parents:
diff changeset
   213
     blocking"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   214
a27a279701f8 Initial revision
claus
parents:
diff changeset
   215
    ^ super new setCount:n
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   216
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   217
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   218
!Semaphore methodsFor:'friend-class interface'!
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   219
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   220
checkAndRegisterProcess:process
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   221
    "interface for SemaphoreSet.
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   222
     If the semaphore is available, decrement it and return true.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   223
     Otherwise register our process to be wakened up once the semaphore is available
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   224
     and return false..
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   225
    "
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   226
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   227
    "
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   228
     bad ST/X trick (needs change, when multiProcessor support is added):
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   229
     this works only since interrupts are only serviced at 
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   230
     message send and method-return time ....
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   231
     If you add a message send into the ifTrue:-block, things will
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   232
     go mad ... (especially be careful when adding a debugPrint-here)
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   233
    "
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   234
    count ~~ 0 ifTrue:[
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   235
	count := count - 1.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   236
	count == 0 ifTrue:[
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   237
	    lastOwnerID := Processor activeProcessId.
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   238
	].
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   239
	^ true
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   240
    ].
1122
251ea6ec6c61 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1031
diff changeset
   241
    (waitingProcesses identityIndexOf:process) == 0 ifTrue:[
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   242
	waitingProcesses add:process.
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   243
    ].
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   244
    ^ false
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   245
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   246
    "Modified: 14.12.1995 / 10:32:17 / stefan"
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   247
    "Modified: 10.1.1997 / 21:42:18 / cg"
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   248
!
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   249
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   250
unregisterProcess:process
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   251
    "interface for SemaphoreSet.
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   252
     Unregister our process from the Semaphore"
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   253
2352
9588e545f4a5 use #removeIdentical
Claus Gittinger <cg@exept.de>
parents: 2265
diff changeset
   254
    waitingProcesses removeIdentical:process ifAbsent:[].
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   255
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   256
    "Created: 14.12.1995 / 10:31:50 / stefan"
2352
9588e545f4a5 use #removeIdentical
Claus Gittinger <cg@exept.de>
parents: 2265
diff changeset
   257
    "Modified: 1.2.1997 / 12:11:22 / cg"
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   258
! !
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 752
diff changeset
   259
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   260
!Semaphore methodsFor:'printing & storing'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   261
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   262
displayString
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   263
    "return a string to display the receiver - include the
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   264
     count for your convenience"
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   265
2723
0d4c775c14d1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2690
diff changeset
   266
    ^ self class name , '(' , count printString , ' name: ' , (name ? 'unnamed') , ')'
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   267
2723
0d4c775c14d1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2690
diff changeset
   268
    "Modified: 28.6.1997 / 16:21:09 / cg"
2265
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   269
!
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   270
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   271
name
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   272
    "return the semaphores userFriendly name"
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   273
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   274
    ^ name
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   275
!
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   276
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   277
name:aString
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   278
    "set the semaphores userFriendly name"
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   279
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   280
    name := aString
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   281
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   282
a27a279701f8 Initial revision
claus
parents:
diff changeset
   283
!Semaphore methodsFor:'private accessing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   284
2407
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   285
initSignals
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   286
    "set the count of the semaphore to zero.
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   287
     provided for ST-80 compatibility."
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   288
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   289
    count := 0
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   290
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   291
    "Created: 17.2.1997 / 11:31:19 / cg"
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   292
!
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   293
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   294
setCount:n
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   295
    "set the count of the semaphore;
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   296
     thats the number of possible waits, without blocking"
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   297
2407
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   298
    waitingProcesses := OrderedCollection new:2.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   299
    count := n
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   300
2407
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   301
    "Modified: 17.2.1997 / 11:36:40 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   302
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   303
217
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   304
!Semaphore methodsFor:'queries '!
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   305
2235
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   306
count
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   307
    "return the number of 'already-counted' trigger events.
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   308
     Thats the number of waits which will succeed without blocking"
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   309
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   310
    ^ count
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   311
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   312
    "Created: 23.1.1997 / 02:55:58 / cg"
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   313
!
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   314
3609
f2ccf6a43330 added #isEmpty for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 3600
diff changeset
   315
isEmpty
f2ccf6a43330 added #isEmpty for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 3600
diff changeset
   316
    "ST80 compatibility - return true if there are no waiters"
f2ccf6a43330 added #isEmpty for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 3600
diff changeset
   317
f2ccf6a43330 added #isEmpty for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 3600
diff changeset
   318
    ^ waitingProcesses isEmpty
f2ccf6a43330 added #isEmpty for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 3600
diff changeset
   319
f2ccf6a43330 added #isEmpty for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 3600
diff changeset
   320
    "Created: / 3.5.1996 / 18:06:27 / cg"
f2ccf6a43330 added #isEmpty for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 3600
diff changeset
   321
    "Modified: / 18.6.1998 / 16:07:38 / cg"
f2ccf6a43330 added #isEmpty for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 3600
diff changeset
   322
!
f2ccf6a43330 added #isEmpty for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 3600
diff changeset
   323
2265
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   324
lastOwnerId
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   325
    "return the processId of the last owning process
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   326
     (the one which counted to zero).
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   327
     May be very useful in debugging deadLock situations"
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   328
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   329
    ^ lastOwnerID
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   330
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   331
    "Created: 24.1.1997 / 23:09:33 / cg"
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   332
!
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   333
1329
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   334
numberOfWaitingProcesses
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   335
    "return the number of processes waiting on the receiver"
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   336
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   337
    ^ waitingProcesses size
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   338
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   339
    "Created: 3.5.1996 / 18:06:27 / cg"
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   340
!
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   341
1577
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   342
waitingProcesses
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   343
    "return the processes waiting on the receiver"
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   344
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   345
    ^ waitingProcesses
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   346
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   347
    "Created: 18.7.1996 / 20:53:33 / cg"
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   348
!
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   349
217
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   350
wouldBlock
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   351
    "return true, if the receiver would block the activeProcess
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   352
     if a wait was performed. False otherwise."
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   353
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   354
    ^ count == 0
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   355
! !
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   356
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   357
!Semaphore methodsFor:'wait & signal'!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   358
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   359
critical:aBlock
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   360
    "evaluate aBlock as a critical region; the receiver must be
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   361
     created using Semaphore>>forMutualExclusion"
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   362
1185
2b24258b4f24 Fix in critical: don't signal a semaphore that we didn't get when unwinding.
Stefan Vogel <sv@exept.de>
parents: 1122
diff changeset
   363
    |retVal gotSema|
926
101239898989 unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   364
101239898989 unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   365
    [
3669
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   366
        gotSema := self wait.
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   367
        retVal := aBlock value.
1185
2b24258b4f24 Fix in critical: don't signal a semaphore that we didn't get when unwinding.
Stefan Vogel <sv@exept.de>
parents: 1122
diff changeset
   368
    ] valueOnUnwindDo:[
3669
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   369
        "/ be careful - the unwind may occur both while waiting
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   370
        "/ AND while evaluating the block.
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   371
        gotSema notNil ifTrue:[self signal].
1185
2b24258b4f24 Fix in critical: don't signal a semaphore that we didn't get when unwinding.
Stefan Vogel <sv@exept.de>
parents: 1122
diff changeset
   372
    ].
2b24258b4f24 Fix in critical: don't signal a semaphore that we didn't get when unwinding.
Stefan Vogel <sv@exept.de>
parents: 1122
diff changeset
   373
    self signal.
926
101239898989 unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   374
    ^ retVal
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   375
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   376
    "
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   377
      the example below is stupid (it should use a SharedQueue,
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   378
      or at least a Queue with critical regions).
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   379
      Anyhow, it demonstrates how two processes lock each other
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   380
      from accessing coll at the same time
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   381
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   382
     |sema coll|
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   383
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   384
     sema := Semaphore forMutualExclusion.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   385
     coll := OrderedCollection new:10.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   386
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   387
     [
3669
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   388
        1 to:1000 do:[:i |
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   389
            sema critical:[
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   390
                coll addLast:i.
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   391
                (Delay forSeconds:0.1) wait.
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   392
            ]
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   393
        ]
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   394
     ] forkAt:4.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   395
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   396
     [
3669
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   397
        1 to:1000 do:[:i |
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   398
            sema critical:[
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   399
                coll removeFirst.
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   400
                (Delay forSeconds:0.1) wait.
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   401
            ]
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   402
        ]
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   403
     ] forkAt:4.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   404
    "
1185
2b24258b4f24 Fix in critical: don't signal a semaphore that we didn't get when unwinding.
Stefan Vogel <sv@exept.de>
parents: 1122
diff changeset
   405
3669
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   406
    "Modified: / 16.4.1996 / 10:00:46 / stefan"
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   407
    "Modified: / 21.7.1998 / 17:45:26 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   408
!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   409
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   410
signal
4633
b6908af8f900 comment
Claus Gittinger <cg@exept.de>
parents: 4437
diff changeset
   411
    "waking up (the first) waiter.
b6908af8f900 comment
Claus Gittinger <cg@exept.de>
parents: 4437
diff changeset
   412
     Q: should this be the highest prio waiter ?"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   413
3272
3e35f761be17 when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents: 2723
diff changeset
   414
    |wasBlocked|
362
claus
parents: 329
diff changeset
   415
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   416
    wasBlocked := OperatingSystem blockInterrupts.
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   417
    [
3836
318f7d6f242b avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents: 3669
diff changeset
   418
        |p|
3272
3e35f761be17 when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents: 2723
diff changeset
   419
3836
318f7d6f242b avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents: 3669
diff changeset
   420
        count := count + 1.
318f7d6f242b avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents: 3669
diff changeset
   421
        p := waitingProcesses removeFirstIfAbsent:nil.
318f7d6f242b avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents: 3669
diff changeset
   422
        p notNil ifTrue:[
318f7d6f242b avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents: 3669
diff changeset
   423
            Processor resume:p.
318f7d6f242b avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents: 3669
diff changeset
   424
        ].
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   425
    ] valueNowOrOnUnwindDo:[
3836
318f7d6f242b avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents: 3669
diff changeset
   426
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   427
    ]
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   428
3272
3e35f761be17 when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents: 2723
diff changeset
   429
    "Modified: / 4.2.1998 / 21:01:07 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   430
!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   431
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   432
signalForAll
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   433
    "signal the semaphore for all waiters.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   434
     This can be used for process synchronization, if multiple processes are
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   435
     waiting for a common event."
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   436
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   437
    |wasBlocked|
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   438
3272
3e35f761be17 when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents: 2723
diff changeset
   439
    waitingProcesses notEmpty ifTrue:[
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   440
	wasBlocked := OperatingSystem blockInterrupts.
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   441
	[
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   442
	    |p|
3272
3e35f761be17 when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents: 2723
diff changeset
   443
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   444
	    "/ first, make them all runnable, but do not schedule
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   445
	    "/ (in case one has higher prio and goes into a wait
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   446
	    "/  immediately again.)
3272
3e35f761be17 when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents: 2723
diff changeset
   447
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   448
	    [waitingProcesses notEmpty] whileTrue:[
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   449
		p := waitingProcesses removeFirst.
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   450
		count := count + 1.
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   451
		Processor makeRunnable:p.
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   452
	    ].
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   453
	] valueNowOrOnUnwindDo:[
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   454
	    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   455
	].
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   456
	"/
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   457
	"/ now, reschedule
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   458
	"/
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   459
	Processor reschedule
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   460
    ]
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   461
3272
3e35f761be17 when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents: 2723
diff changeset
   462
    "Modified: / 5.2.1998 / 10:40:26 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   463
!
362
claus
parents: 329
diff changeset
   464
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   465
signalIf
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   466
    "signal the semaphore, but only if being waited upon.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   467
     This can be used for one-shot semaphores (i.e. not remembering
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   468
     previous signals)"
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   469
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   470
    |wasBlocked|
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   471
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   472
    waitingProcesses notEmpty ifTrue:[
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   473
	wasBlocked := OperatingSystem blockInterrupts.
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   474
	[
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   475
	    waitingProcesses notEmpty ifTrue:[
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   476
		self signal
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   477
	    ].
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   478
	] valueNowOrOnUnwindDo:[
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   479
	    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   480
	]
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   481
    ]
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   482
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   483
    "Modified: 28.2.1996 / 21:23:57 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   484
!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   485
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   486
signalOnce
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   487
    "wakeup waiters - but only once.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   488
     I.e. if the semaphore has already been signalled, this
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   489
     is ignored."
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   490
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   491
    |wasBlocked|
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   492
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   493
    count == 0 ifTrue:[
3836
318f7d6f242b avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents: 3669
diff changeset
   494
        wasBlocked := OperatingSystem blockInterrupts.
318f7d6f242b avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents: 3669
diff changeset
   495
        [
318f7d6f242b avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents: 3669
diff changeset
   496
            |p|
318f7d6f242b avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents: 3669
diff changeset
   497
318f7d6f242b avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents: 3669
diff changeset
   498
            count == 0 ifTrue:[
318f7d6f242b avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents: 3669
diff changeset
   499
                count := 1.
318f7d6f242b avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents: 3669
diff changeset
   500
                p := waitingProcesses removeFirstIfAbsent:nil.
318f7d6f242b avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents: 3669
diff changeset
   501
                p notNil ifTrue:[
318f7d6f242b avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents: 3669
diff changeset
   502
                    Processor resume:p.
318f7d6f242b avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents: 3669
diff changeset
   503
                ].
318f7d6f242b avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents: 3669
diff changeset
   504
            ].
318f7d6f242b avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents: 3669
diff changeset
   505
        ] valueNowOrOnUnwindDo:[
318f7d6f242b avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents: 3669
diff changeset
   506
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
318f7d6f242b avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents: 3669
diff changeset
   507
        ]
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   508
    ]
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   509
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 954
diff changeset
   510
    "Modified: 28.2.1996 / 21:24:08 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   511
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   512
a27a279701f8 Initial revision
claus
parents:
diff changeset
   513
wait
a27a279701f8 Initial revision
claus
parents:
diff changeset
   514
    "wait for the semaphore"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   515
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   516
    |activeProcess wasBlocked|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   517
a27a279701f8 Initial revision
claus
parents:
diff changeset
   518
    "
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   519
     bad ST/X trick (needs change, when multiProcessor support is added):
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   520
     this works only since interrupts are only serviced at 
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   521
     message send and method-return time ....
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   522
     If you add a message send between the compare and the decrement,
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   523
     things will go mad ... (especially be careful when adding a debugPrint-here)
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   524
    "
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   525
    count ~~ 0 ifTrue:[
3600
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   526
        count := count - 1.
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   527
        count == 0 ifTrue:[
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   528
            lastOwnerID := Processor activeProcessId.
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   529
        ].
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   530
        ^ self
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   531
    ].
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   532
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   533
    activeProcess := Processor activeProcess.
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   534
302
1f76060d58a4 *** empty log message ***
claus
parents: 269
diff changeset
   535
    wasBlocked := OperatingSystem blockInterrupts.
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   536
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   537
     need a while-loop here, since more than one process may
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   538
     wait for it and another one may also wake up.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   539
     Thus, the count is not always non-zero after returning from
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   540
     suspend.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   541
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   542
    [count == 0] whileTrue:[
3600
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   543
        waitingProcesses add:activeProcess.
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   544
        "
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   545
         for some more descriptive info in processMonitor ...
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   546
         ... set the state to #wait (instead of #suspend)
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   547
        "
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   548
        [
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   549
            activeProcess suspendWithState:#wait
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   550
        ] valueOnUnwindDo:[
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   551
            waitingProcesses removeIdentical:activeProcess ifAbsent:[].
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   552
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   553
        ].
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   554
        
3600
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   555
        count == 0 ifTrue:[
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   556
            "/ care for someone manually resuming me ...
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   557
            "/ being multiple times on waitingProcesses
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   558
            waitingProcesses removeIdentical:activeProcess ifAbsent:[].
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   559
        ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   560
    ].
10
claus
parents: 3
diff changeset
   561
    count := count - 1.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   562
    count == 0 ifTrue:[
3600
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   563
        lastOwnerID := Processor activeProcessId.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   564
    ].
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   565
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
752
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   566
3600
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   567
    "Modified: / 13.12.1995 / 13:26:33 / stefan"
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   568
    "Modified: / 17.6.1998 / 15:26:27 / cg"
10
claus
parents: 3
diff changeset
   569
!
claus
parents: 3
diff changeset
   570
329
claus
parents: 302
diff changeset
   571
waitUncounted
claus
parents: 302
diff changeset
   572
    "wait for the semaphore; do not consume the resource
claus
parents: 302
diff changeset
   573
     (i.e. do not count down)"
claus
parents: 302
diff changeset
   574
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   575
    |activeProcess wasBlocked|
329
claus
parents: 302
diff changeset
   576
claus
parents: 302
diff changeset
   577
    "
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   578
     bad ST/X trick (needs change, when multiProcessor support is added):
329
claus
parents: 302
diff changeset
   579
     this works only since interrupts are only serviced at 
claus
parents: 302
diff changeset
   580
     message send and method-return time ....
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   581
     If you add a message send between the compare and the decrement,
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   582
     things will go mad ... (especially be careful when adding a debugPrint-here)
329
claus
parents: 302
diff changeset
   583
    "
claus
parents: 302
diff changeset
   584
    count ~~ 0 ifTrue:[
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   585
	^ self
329
claus
parents: 302
diff changeset
   586
    ].
claus
parents: 302
diff changeset
   587
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   588
    activeProcess := Processor activeProcess.
329
claus
parents: 302
diff changeset
   589
claus
parents: 302
diff changeset
   590
    wasBlocked := OperatingSystem blockInterrupts.
claus
parents: 302
diff changeset
   591
    "
claus
parents: 302
diff changeset
   592
     need a while-loop here, since more than one process may
claus
parents: 302
diff changeset
   593
     wait for it and another one may also wake up.
claus
parents: 302
diff changeset
   594
     Thus, the count is not always non-zero after returning from
claus
parents: 302
diff changeset
   595
     suspend.
claus
parents: 302
diff changeset
   596
    "
claus
parents: 302
diff changeset
   597
    [count == 0] whileTrue:[
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   598
	waitingProcesses add:activeProcess.
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   599
	"
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   600
	 for some more descriptive info in processMonitor ...
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   601
	 ... set the state to #wait (instead of #suspend)
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   602
	"
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   603
	[
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   604
	    activeProcess suspendWithState:#wait
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   605
	] valueOnUnwindDo:[
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   606
	    waitingProcesses removeIdentical:activeProcess ifAbsent:[].
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   607
	    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   608
	].
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   609
	count == 0 ifTrue:[
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   610
	    "/ care for someone manually resuming me ...
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   611
	    "/ being multiple times on waitingProcesses
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   612
	    waitingProcesses removeIdentical:activeProcess ifAbsent:[].
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   613
	]
329
claus
parents: 302
diff changeset
   614
    ].
claus
parents: 302
diff changeset
   615
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
752
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   616
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   617
    "Modified: 13.12.1995 / 13:26:49 / stefan"
2352
9588e545f4a5 use #removeIdentical
Claus Gittinger <cg@exept.de>
parents: 2265
diff changeset
   618
    "Modified: 1.2.1997 / 12:11:41 / cg"
329
claus
parents: 302
diff changeset
   619
!
claus
parents: 302
diff changeset
   620
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   621
waitWithTimeout:seconds
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   622
    "wait for the semaphore, but abort the wait after some time.
2690
f79d685e06af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2407
diff changeset
   623
     return the receiver if the semaphore triggered normal, nil if we return
926
101239898989 unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   624
     due to a timeout. 
101239898989 unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   625
     The seconds-argument may be a float (i.e. use 0.1 for a 100ms timeout).
2690
f79d685e06af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2407
diff changeset
   626
     With zero timeout, this can be used to poll a semaphore (returning
f79d685e06af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2407
diff changeset
   627
     the receiver if the semaphore is available, nil if not).
f79d685e06af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2407
diff changeset
   628
     However, polling is not the intended use of semaphores, though."
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   629
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   630
    |activeProcess timeoutOccured wasBlocked unblock now endTime|
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   631
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   632
    "
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   633
     bad ST/X trick (needs change, when multiProcessor support is added):
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   634
     this works only since interrupts are only serviced at 
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   635
     message send and method-return time ....
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   636
     If you add a message send between the compare and the decrement,
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   637
     things will go mad ... (especially be careful when adding a debugPrint-here)
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   638
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   639
    count ~~ 0 ifTrue:[
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   640
	count := count - 1.
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   641
	count == 0 ifTrue:[
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   642
	    lastOwnerID := Processor activeProcessId.
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   643
	].
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   644
	^ self
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   645
    ].
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   646
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   647
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   648
     with zero-timeout, this is a poll
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   649
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   650
    seconds = 0 ifTrue:[
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   651
	^ nil
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   652
    ].
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   653
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   654
    activeProcess := Processor activeProcess.
302
1f76060d58a4 *** empty log message ***
claus
parents: 269
diff changeset
   655
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   656
    wasBlocked := OperatingSystem blockInterrupts.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   657
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   658
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   659
     calculate the end-time
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   660
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   661
    now := OperatingSystem getMillisecondTime.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   662
    endTime := OperatingSystem millisecondTimeAdd:now and:(seconds * 1000).
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   663
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   664
    unblock := [timeoutOccured := true. Processor resume:activeProcess].
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   665
    Processor addTimedBlock:unblock for:activeProcess atMilliseconds:endTime.
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   666
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   667
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   668
     need a while-loop here, since more than one process may
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   669
     wait for it and another one may also wake up.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   670
     Thus, the count is not always non-zero after returning from
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   671
     suspend.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   672
    "
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   673
    [count == 0] whileTrue:[
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   674
	waitingProcesses add:activeProcess.
302
1f76060d58a4 *** empty log message ***
claus
parents: 269
diff changeset
   675
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   676
	timeoutOccured := false.
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   677
	"
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   678
	 for some more descriptive info in processMonitor ...
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   679
	 ... set the state to #wait (instead of #suspend)
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   680
	"
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   681
	[
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   682
	    activeProcess suspendWithState:#wait.
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   683
	] valueOnUnwindDo:[
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   684
	    unblock := nil.
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   685
	    waitingProcesses removeIdentical:activeProcess ifAbsent:[].
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   686
	    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   687
	].
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   688
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   689
	waitingProcesses removeIdentical:activeProcess ifAbsent:[].
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   690
	timeoutOccured ifTrue:[
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   691
	    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   692
	    unblock := nil.
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   693
	    ^ nil
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   694
	].
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   695
    ].
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   696
    Processor removeTimedBlock:unblock.
2690
f79d685e06af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2407
diff changeset
   697
    unblock := nil.
f79d685e06af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2407
diff changeset
   698
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   699
    count := count - 1.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   700
    count == 0 ifTrue:[
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   701
	lastOwnerID := Processor activeProcessId.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   702
    ].
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   703
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
770
402958905760 protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents: 769
diff changeset
   704
    ^ self
752
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   705
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   706
    "Modified: 13.12.1995 / 13:27:24 / stefan"
2690
f79d685e06af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2407
diff changeset
   707
    "Modified: 16.6.1997 / 21:54:38 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   708
! !
77
6c38ca59927f *** empty log message ***
claus
parents: 69
diff changeset
   709
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   710
!Semaphore class methodsFor:'documentation'!
183
7899f380e77d signalIf
claus
parents: 159
diff changeset
   711
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   712
version
4633
b6908af8f900 comment
Claus Gittinger <cg@exept.de>
parents: 4437
diff changeset
   713
    ^ '$Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.55 1999-08-25 16:21:24 cg Exp $'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   714
! !