Semaphore.st
author Stefan Vogel <sv@exept.de>
Tue, 21 Sep 2004 20:06:25 +0200
changeset 8569 657853ae04cd
parent 7718 657b832f54a3
child 8617 5fb5a717deff
permissions -rw-r--r--
Change waitXXX category change
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
6616
70d54ffebc91 valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents: 6615
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
5556
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5256
diff changeset
    13
"{ Package: 'stx:libbasic' }"
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5256
diff changeset
    14
375
claus
parents: 362
diff changeset
    15
Object subclass:#Semaphore
6616
70d54ffebc91 valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents: 6615
diff changeset
    16
	instanceVariableNames:'count waitingProcesses lastOwnerID name'
70d54ffebc91 valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents: 6615
diff changeset
    17
	classVariableNames:''
70d54ffebc91 valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents: 6615
diff changeset
    18
	poolDictionaries:''
70d54ffebc91 valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents: 6615
diff changeset
    19
	category:'Kernel-Processes'
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
    20
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    21
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    22
!Semaphore class methodsFor:'documentation'!
69
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    23
88
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    24
copyright
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    25
"
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    26
 COPYRIGHT (c) 1993 by Claus Gittinger
6616
70d54ffebc91 valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents: 6615
diff changeset
    27
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    28
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    29
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    30
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    31
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    32
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    33
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    34
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    35
"
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    36
!
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    37
69
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    38
documentation
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    39
"
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    40
    Semaphores are used to synchronize processes providing a nonBusy wait
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    41
    mechanism. A process can wait for the availability of some resource by
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
    42
    performing a Semaphore>>wait, which will suspend the process until the
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
    43
    resource becomes available. Signalling is done by (another process performing) 
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
    44
    Semaphore>>signal.
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    45
    If the resource has been already available before the wait, no suspending is
69
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
    46
    done, but the resource immediately allocated.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    47
    The resource internally keeps a count, the number of times the resource can be
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    48
    allocated. If the semaphore is created with a count greater than 1, the sema
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    49
    can be waited-upon that many times without blocking.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    50
    On the other hand, if initialized with a negative count, the semaphore
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    51
    must be signalled that many times more often in order for a wait to not block.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    52
    In other words: whenever the semaphore has a count greater than zero, 
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    53
    the wait operation will proceed. Otherwise, it will block until the count has
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    54
    been incremented by signal operations to be greater than zero.
2234
21fe6c17fe8d commentary
Claus Gittinger <cg@exept.de>
parents: 2142
diff changeset
    55
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
    56
    There are also semaphores for mutual access to a critical region
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
    57
    (Semaphore>>forMutualExclusion and Semaphore>>critical:).
88
81dacba7a63a *** empty log message ***
claus
parents: 79
diff changeset
    58
2234
21fe6c17fe8d commentary
Claus Gittinger <cg@exept.de>
parents: 2142
diff changeset
    59
    Additional protocol is provided for oneShot semaphores, 
21fe6c17fe8d commentary
Claus Gittinger <cg@exept.de>
parents: 2142
diff changeset
    60
    (#signalOnce) and for conditional signalling (#signalIf).
21fe6c17fe8d commentary
Claus Gittinger <cg@exept.de>
parents: 2142
diff changeset
    61
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    62
    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
    63
    timer events). 
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
    64
    This is done by telling the Processor to signal the semaphore 
183
7899f380e77d signalIf
claus
parents: 159
diff changeset
    65
    under some condition.
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    66
    See 'Processor>>signal:afterSeconds:', 'Processor>>signal:onInput:' etc.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    67
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
    68
    See examples in doc/coding (found in the CodingExamples-nameSpace).
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1185
diff changeset
    69
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    70
    Warning/Note/Hint:
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    71
        a Semaphore-forMutualExclusion does NEVER allow for the critical
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    72
        region to be entered twice - NOT EVEN by the same process.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    73
        That means, that a recursive attempt to enter that section leads
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    74
        to a deadlock.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    75
        Use a RecursionLock instead, to avoid this.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    76
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    77
    Hint:
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    78
        now (Jul2002), Semaphores now allow for a negative count; this allows for
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    79
        a sync-point to be implemented easily (i.e. to wait for multiple other processes
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    80
        to arrive at a sync-point).
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
    81
        See examples.
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    82
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    83
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    84
    [instance variables:]
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    85
        count                   <SmallInteger>          the number of waits, that will go through
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    86
                                                        without blocking.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    87
                                                        Incremented on #signal; decremented on #wait.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    88
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    89
        waitingProcesses        <OrderedCollection>     waiting processes - will be served first
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    90
                                                        come first served when signalled.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    91
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    92
        lastOwnerID             <SmallInteger>          a debugging aid: set when count drops
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    93
                                                        to zero to the current processes id.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    94
                                                        Helps in finding deadlocks.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    95
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    96
        name                    <String>                a debugging aid: an optional userFriendly
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
    97
                                                        name; helps to identify a semaphore easier.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
    98
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1185
diff changeset
    99
    [see also:]
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   100
        SemaphoreSet RecursionLock Monitor
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   101
        SharedQueue Delay 
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   102
        Process ProcessorScheduler
1294
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
   103
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
   104
    [author:]
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   105
        Claus Gittinger
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   106
"
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   107
!
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   108
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   109
examples
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   110
"
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   111
    two processes synchronizing on a sema:
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   112
                                                        [exBegin]
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   113
        |sema thread1 thread2|
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
        sema := Semaphore new.
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   116
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   117
        thread1 := [
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   118
                        Transcript showCR:'here is thread 1; now waiting ...'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   119
                        sema wait.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   120
                        Transcript showCR:'here is thread 1 again.'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   121
                   ] newProcess.
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   122
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   123
        thread2 := [
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   124
                        Transcript showCR:'here is thread 2; delaying a bit ...'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   125
                        Delay waitForSeconds:5.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   126
                        Transcript showCR:'here is thread 2 again; now signalling the sema'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   127
                        sema signal.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   128
                        Transcript showCR:'here is thread 2 after the signalling.'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   129
                  ] newProcess.
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   130
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   131
        thread1 priority:7.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   132
        thread2 priority:6.
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   133
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   134
        thread1 resume.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   135
        thread2 resume.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   136
                                                        [exEnd]
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   137
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   138
    semaphore for critical regions:
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   139
                                                        [exBegin]
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   140
        |accessLock|
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   141
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   142
        accessLock := Semaphore forMutualExclusion.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   143
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   144
        [
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   145
            5 timesRepeat:[
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   146
                Delay waitForSeconds:2.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   147
                accessLock critical:[
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   148
                    Transcript showCR:'thread1 in critical region'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   149
                    Delay waitForSeconds:1.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   150
                    Transcript showCR:'thread1 leaving critical region'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   151
                ].
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   152
            ]
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   153
        ] forkAt:5.
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
        [
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   156
            5 timesRepeat:[
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   157
                Delay waitForSeconds:1.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   158
                accessLock critical:[
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   159
                    Transcript showCR:'thread2 in critical region'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   160
                    Delay waitForSeconds:2.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   161
                    Transcript showCR:'thread2 leaving critical region'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   162
                ].
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   163
            ]
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   164
        ] forkAt:4.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   165
                                                        [exEnd]
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   166
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   167
    a deadlock due to recursive enter of a critical region:
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   168
                                                        [exBegin]
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   169
        |accessLock block|
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   170
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   171
        accessLock := Semaphore forMutualExclusion.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   172
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   173
        block := [:arg |
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   174
                    Transcript showCR:'about to enter'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   175
                    accessLock critical:[
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   176
                        Transcript showCR:'entered - doing action'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   177
                        arg value
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   178
                    ].
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   179
                    Transcript showCR:'left region'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   180
                 ].
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   181
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   182
        block value:[].                 'this works'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   183
        block value:[block value:[] ].  'this deadlocks'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   184
                                                        [exEnd]
3392
84332bb01a86 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3272
diff changeset
   185
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   186
    Avoid the deadlock by using a RecursionLock instead:
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   187
                                                        [exBegin]
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   188
        |accessLock block|
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   189
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   190
        accessLock := RecursionLock new.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   191
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   192
        block := [:arg |
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   193
                    Transcript showCR:'about to enter'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   194
                    accessLock critical:[
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   195
                        Transcript showCR:'entered - doing action'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   196
                        arg value
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   197
                    ].
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   198
                    Transcript showCR:'left region'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   199
                 ].
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   200
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   201
        block value:[].                 'this works'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   202
        block value:[block value:[] ].  'this deadlocks'.
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   203
                                                        [exEnd]
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   204
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   205
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   206
   Wait for multiple processes to arrive at a sync-point:
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   207
                                                        [exBegin]
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   208
        |syncSema proceedSema thread1 thread2 thread3|
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   209
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   210
        syncSema := Semaphore new.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   211
        syncSema setCount:(1-3).
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   212
        proceedSema := Semaphore new.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   213
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   214
        thread1 := [
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   215
                        Transcript showCR:'here is thread 1; now busy ...'.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   216
                        Delay waitForSeconds:(2 + (Random nextIntegerBetween:2 and:4)).
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   217
                        Transcript showCR:'here is thread 1 again - now syncing.'.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   218
                        syncSema signal.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   219
                        Transcript showCR:'thread 1 is waiting for all others...'.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   220
                        proceedSema wait.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   221
                        Transcript showCR:'thread 1 done.'.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   222
                   ] newProcess.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   223
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   224
        thread2 := [
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   225
                        Transcript showCR:'here is thread 2; now busy ...'.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   226
                        Delay waitForSeconds:(3 + (Random nextIntegerBetween:2 and:4)).
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   227
                        Transcript showCR:'here is thread 2 again - now syncing.'.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   228
                        syncSema signal.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   229
                        Transcript showCR:'thread 2 is waiting for all others...'.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   230
                        proceedSema wait.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   231
                        Transcript showCR:'thread 2 done.'.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   232
                  ] newProcess.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   233
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   234
        thread3 := [
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   235
                        Transcript showCR:'here is thread 3; now busy ...'.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   236
                        Delay waitForSeconds:(4 + (Random nextIntegerBetween:2 and:4)).
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   237
                        Transcript showCR:'here is thread 3 again - now syncing.'.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   238
                        syncSema signal.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   239
                        Transcript showCR:'thread 3 is waiting for all others...'.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   240
                        proceedSema wait.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   241
                        Transcript showCR:'thread 3 done.'.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   242
                  ] newProcess.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   243
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   244
        thread1 priority:7.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   245
        thread2 priority:6.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   246
        thread3 priority:9.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   247
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   248
        thread1 resume.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   249
        thread2 resume.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   250
        thread3 resume.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   251
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   252
        Transcript showCR:'main thread: now waiting for other threads...'.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   253
        syncSema wait.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   254
        Transcript showCR:'main thread: all other threads at syncPoint.'.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   255
        Delay waitForSeconds:2.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   256
        Transcript showCR:'main thread: now let them proceed...'.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   257
        proceedSema signalForAll.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   258
        Transcript showCR:'main thread: done.'.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   259
                                                        [exEnd]
6999
b0f293f2780c comment
ca
parents: 6986
diff changeset
   260
b0f293f2780c comment
ca
parents: 6986
diff changeset
   261
   waitWithTimeout:0 can also be used to conditionally aquire the semaphore
b0f293f2780c comment
ca
parents: 6986
diff changeset
   262
   i.e. only aquire it if its available.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   263
     |s|
b0f293f2780c comment
ca
parents: 6986
diff changeset
   264
b0f293f2780c comment
ca
parents: 6986
diff changeset
   265
     s := Semaphore new.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   266
     [
b0f293f2780c comment
ca
parents: 6986
diff changeset
   267
          (s waitWithTimeout:0) notNil ifTrue:[
b0f293f2780c comment
ca
parents: 6986
diff changeset
   268
                Transcript showCR:'process1 got the sema'.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   269
                Delay waitForSeconds:1.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   270
                Transcript showCR:'process1 signals sema'.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   271
                s signal.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   272
          ] ifFalse:[
b0f293f2780c comment
ca
parents: 6986
diff changeset
   273
                Transcript showCR:'process1 has NOT got the sema'.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   274
          ].
b0f293f2780c comment
ca
parents: 6986
diff changeset
   275
     ] fork.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   276
     [
b0f293f2780c comment
ca
parents: 6986
diff changeset
   277
          (s waitWithTimeout:0) notNil ifTrue:[
b0f293f2780c comment
ca
parents: 6986
diff changeset
   278
                Transcript showCR:'process2 got the sema'.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   279
                Delay waitForSeconds:1.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   280
                Transcript showCR:'process2 signals sema'.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   281
                s signal.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   282
          ] ifFalse:[
b0f293f2780c comment
ca
parents: 6986
diff changeset
   283
                Transcript showCR:'process2 has NOT got the sema'.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   284
          ]
b0f293f2780c comment
ca
parents: 6986
diff changeset
   285
     ] fork.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   286
     s signal.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   287
     Delay waitForSeconds:0.5.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   288
     Transcript showCR:'master waits for sema'.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   289
     s wait.
b0f293f2780c comment
ca
parents: 6986
diff changeset
   290
     Transcript showCR:'master got the sema'.
69
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
   291
"
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
   292
! !
4564b6328136 *** empty log message ***
claus
parents: 25
diff changeset
   293
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   294
!Semaphore class methodsFor:'instance creation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   295
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   296
forMutualExclusion
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   297
    "create & return a new semaphore which allows exactly one process to
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   298
     wait on it without blocking. This type of semaphore is used
4437
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   299
     for mutual exclusion from critical regions (see #critical:).
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   300
     Also see RecursionLock, to avoid deadlock in case of recursive entered
Claus Gittinger <cg@exept.de>
parents: 3836
diff changeset
   301
     critical regions."
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   302
3600
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   303
    ^ super new setCount:1; name:'criticalRegionSema'
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   304
3600
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   305
    "Modified: / 17.6.1998 / 16:23:09 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   306
!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   307
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   308
new
a27a279701f8 Initial revision
claus
parents:
diff changeset
   309
    "create & return a new semaphore which blocks until a signal is sent"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   310
a27a279701f8 Initial revision
claus
parents:
diff changeset
   311
    ^ super new setCount:0
a27a279701f8 Initial revision
claus
parents:
diff changeset
   312
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   313
a27a279701f8 Initial revision
claus
parents:
diff changeset
   314
new:n
a27a279701f8 Initial revision
claus
parents:
diff changeset
   315
    "create & return a new semaphore which allows n waits before
a27a279701f8 Initial revision
claus
parents:
diff changeset
   316
     blocking"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   317
a27a279701f8 Initial revision
claus
parents:
diff changeset
   318
    ^ super new setCount:n
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   319
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   320
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   321
!Semaphore methodsFor:'printing & storing'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   322
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   323
displayString
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   324
    "return a string to display the receiver - include the
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   325
     count for your convenience"
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   326
2723
0d4c775c14d1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2690
diff changeset
   327
    ^ self class name , '(' , count printString , ' name: ' , (name ? 'unnamed') , ')'
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   328
2723
0d4c775c14d1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2690
diff changeset
   329
    "Modified: 28.6.1997 / 16:21:09 / cg"
2265
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
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   332
name
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   333
    "return the semaphores userFriendly name"
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   334
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   335
    ^ name
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   336
!
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   337
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   338
name:aString
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   339
    "set the semaphores userFriendly name"
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   340
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   341
    name := aString
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   342
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   343
6986
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   344
!Semaphore methodsFor:'private'!
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   345
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   346
addWaitingProcess:aProcess
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   347
    "add aProcess to the list of waiting processes.
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   348
     all processes are ordered first-come-first-serve.
6986
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   349
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   350
     NOTE: must be called with blocked interrupts"
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   351
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   352
    "for now"
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   353
    waitingProcesses isNil ifTrue:[
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   354
        waitingProcesses := Array with:aProcess
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   355
    ] ifFalse:[
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   356
        waitingProcesses class == Array ifTrue:[
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   357
            waitingProcesses := OrderedCollection withAll:waitingProcesses.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   358
        ].
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   359
        waitingProcesses add:aProcess.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   360
    ].
6986
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   361
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   362
"/    "Sort, so that higher priority process are resumed first.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   363
"/    Processes having the same priority are ordered first-come-first-serve."
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   364
"/
6986
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   365
"/    |priority insertIndex|
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   366
"/
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   367
"/    (waitingProcesses size == 0 
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   368
"/     or:[(priority := aProcess priority) <= waitingProcesses last priority]) ifTrue:[
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   369
"/        waitingProcesses add:aProcess.
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   370
"/        ^ self.
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   371
"/    ].
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   372
"/
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   373
"/    insertIndex := waitingProcesses findFirst:[:process| process priority < priority].
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   374
"/    waitingProcesses add:aProcess beforeIndex:insertIndex.
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   375
!
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   376
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   377
removeFirstWaitingProcess
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   378
    "remove the first waiting process from the list of waiting processes
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   379
     and return it; Nil if there is none.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   380
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   381
     NOTE: must be called with blocked interrupts"
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   382
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   383
    |nWaiting p|
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   384
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   385
    nWaiting := waitingProcesses size.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   386
    nWaiting == 0 ifTrue:[^ nil].
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   387
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   388
    nWaiting == 1 ifTrue:[
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   389
        p := waitingProcesses at:1.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   390
        waitingProcesses := nil.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   391
        ^ p.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   392
    ].
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   393
    ^ waitingProcesses removeFirst.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   394
!
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   395
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   396
removeWaitingProcess:aProcess
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   397
    "remove aProcess from the list of waiting processes
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   398
     NO action if it is not in the list.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   399
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   400
     NOTE: must be called with blocked interrupts"
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   401
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   402
    |nWaiting|
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   403
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   404
    nWaiting := waitingProcesses size.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   405
    nWaiting == 0 ifTrue:[^ self].
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   406
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   407
    nWaiting == 1 ifTrue:[
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   408
        (waitingProcesses at:1) == aProcess ifTrue:[
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   409
            waitingProcesses := nil.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   410
        ].
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   411
        ^ self.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   412
    ].
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   413
   waitingProcesses removeIdentical:aProcess ifAbsent:[].
6986
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   414
! !
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   415
7258
9ccdbee7d1ad method category rename
Claus Gittinger <cg@exept.de>
parents: 6999
diff changeset
   416
!Semaphore methodsFor:'private-accessing'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   417
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   418
clear
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   419
    "clear the semaphores count"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   420
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   421
    count := 0
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   422
!
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   423
2407
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   424
initSignals
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   425
    "set the count of the semaphore to zero.
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   426
     provided for ST-80 compatibility."
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   427
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   428
    count := 0
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   429
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   430
    "Created: 17.2.1997 / 11:31:19 / cg"
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   431
!
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   432
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   433
setCount:n
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   434
    "set the count of the semaphore;
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   435
     thats the number of possible waits, without blocking"
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   436
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   437
    waitingProcesses := nil.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   438
    count := n
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   439
2407
6b0e21391915 added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents: 2352
diff changeset
   440
    "Modified: 17.2.1997 / 11:36:40 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   441
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   442
5256
1209e167d63e category rename
Claus Gittinger <cg@exept.de>
parents: 4902
diff changeset
   443
!Semaphore methodsFor:'queries'!
217
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   444
2235
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   445
count
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   446
    "return the number of 'already-counted' trigger events.
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   447
     Thats the number of waits which will succeed without blocking"
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   448
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   449
    ^ count
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   450
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   451
    "Created: 23.1.1997 / 02:55:58 / cg"
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   452
!
c6a15bd9a33c added access to count
Claus Gittinger <cg@exept.de>
parents: 2234
diff changeset
   453
2265
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   454
lastOwnerId
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   455
    "return the processId of the last owning process
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   456
     (the one which counted to zero).
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   457
     May be very useful in debugging deadLock situations"
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   458
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   459
    ^ lastOwnerID
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   460
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   461
    "Created: 24.1.1997 / 23:09:33 / cg"
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   462
!
775feb718a9d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   463
1329
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   464
numberOfWaitingProcesses
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   465
    "return the number of processes waiting on the receiver"
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   466
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   467
    ^ waitingProcesses size
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   468
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   469
    "Created: 3.5.1996 / 18:06:27 / cg"
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   470
!
0a025ca40653 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   471
1577
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   472
waitingProcesses
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   473
    "return the processes waiting on the receiver"
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   474
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   475
    ^ waitingProcesses ? #()
1577
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   476
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   477
    "Created: 18.7.1996 / 20:53:33 / cg"
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   478
! !
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   479
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   480
!Semaphore methodsFor:'semaphoreSet interface'!
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   481
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   482
checkAndAddWaitingProcess:process
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   483
    "interface for SemaphoreSet.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   484
     If the semaphore is available, decrement it and return true.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   485
     Otherwise register our process to be wakened up once the semaphore is available
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   486
     and return false.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   487
     ATTENTION: this must be invoked with OperatingSystem-interrupts-blocked.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   488
    "
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   489
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   490
    count > 0 ifTrue:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   491
        count := count - 1.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   492
        count == 0 ifTrue:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   493
            lastOwnerID := Processor activeProcessId.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   494
        ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   495
        ^ true
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   496
    ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   497
    (waitingProcesses notNil and:[(waitingProcesses includesIdentical:process)]) ifFalse:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   498
        self addWaitingProcess:process.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   499
    ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   500
    ^ false
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   501
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   502
    "Modified: 14.12.1995 / 10:32:17 / stefan"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   503
    "Modified: 10.1.1997 / 21:42:18 / cg"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   504
! !
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   505
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   506
!Semaphore methodsFor:'signaling'!
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   507
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   508
signal
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   509
    "waking up (the first) waiter.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   510
     Q: should this be the highest prio waiter ?"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   511
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   512
    |wasBlocked|
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   513
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   514
    wasBlocked := OperatingSystem blockInterrupts.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   515
    [
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   516
        |p|
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   517
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   518
        count := count + 1.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   519
        waitingProcesses notNil ifTrue:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   520
            p := self removeFirstWaitingProcess.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   521
            p notNil ifTrue:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   522
                Processor resume:p.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   523
            ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   524
        ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   525
    ] ensure:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   526
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   527
    ]
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   528
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   529
    "Modified: / 4.2.1998 / 21:01:07 / cg"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   530
!
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   531
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   532
signalForAll
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   533
    "signal the semaphore for all waiters.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   534
     This can be used for process synchronization, if multiple processes are
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   535
     waiting for a common event."
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   536
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   537
    |wasBlocked|
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   538
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   539
    waitingProcesses size ~~ 0 ifTrue:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   540
        wasBlocked := OperatingSystem blockInterrupts.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   541
        [
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   542
            |processes|
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   543
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   544
            "first, make them all runnable, but do not schedule
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   545
             (in case one has higher prio and goes into a wait immediately again.)"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   546
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   547
            processes := waitingProcesses.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   548
            waitingProcesses := nil.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   549
            processes do:[:eachWaitingProcess|
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   550
                count := count + 1.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   551
                Processor makeRunnable:eachWaitingProcess.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   552
            ].    
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   553
        ] ensure:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   554
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   555
        ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   556
        "now, schedule the highest priority process"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   557
        Processor reschedule
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   558
    ]
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   559
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   560
    "Modified: / 5.2.1998 / 10:40:26 / cg"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   561
!
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   562
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   563
signalIf
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   564
    "signal the semaphore, but only if being waited upon.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   565
     This can be used for one-shot semaphores (i.e. not remembering
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   566
     previous signals)"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   567
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   568
    |wasBlocked|
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   570
    waitingProcesses notNil ifTrue:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   571
        wasBlocked := OperatingSystem blockInterrupts.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   572
        [
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   573
            waitingProcesses size ~~ 0 ifTrue:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   574
                self signal
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   575
            ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   576
        ] ensure:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   577
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   578
        ]
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   579
    ]
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   580
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   581
    "Modified: 28.2.1996 / 21:23:57 / cg"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   582
!
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   583
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   584
signalOnce
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   585
    "wakeup waiters - but only once.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   586
     I.e. if the semaphore has already been signalled, this is ignored."
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   587
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   588
    |wasBlocked|
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   589
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   590
    count <= 0 ifTrue:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   591
        wasBlocked := OperatingSystem blockInterrupts.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   592
        "/ check again - now interrupts are blocked.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   593
        [
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   594
            |p|
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   595
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   596
            count <= 0 ifTrue:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   597
                count := count + 1.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   598
                count == 1 ifTrue:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   599
                    waitingProcesses notNil ifTrue:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   600
                        p := self removeFirstWaitingProcess.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   601
                        p notNil ifTrue:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   602
                            Processor resume:p.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   603
                        ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   604
                    ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   605
                ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   606
            ].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   607
        ] ensure:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   608
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   609
        ]
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   610
    ]
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   611
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   612
    "Modified: 28.2.1996 / 21:24:08 / cg"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   613
! !
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   614
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   615
!Semaphore methodsFor:'testing'!
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   616
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   617
isEmpty
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   618
    "ST80 compatibility - return true if there are no waiters"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   619
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   620
    ^ waitingProcesses size == 0
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   621
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   622
    "Created: / 3.5.1996 / 18:06:27 / cg"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   623
    "Modified: / 18.6.1998 / 16:07:38 / cg"
1577
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   624
!
720f14f90e2c access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents: 1329
diff changeset
   625
217
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   626
wouldBlock
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   627
    "return true, if the receiver would block the activeProcess
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   628
     if a wait was performed. False otherwise.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   629
     Attention: if asked without some global lock (blockedInterrupts),
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   630
     the returned value may be wrong right away."
217
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   631
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   632
    ^ count <= 0
217
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   633
! !
a0400fdbc933 *** empty log message ***
claus
parents: 183
diff changeset
   634
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   635
!Semaphore methodsFor:'waiting'!
5713
993805688c7f added #clear
Claus Gittinger <cg@exept.de>
parents: 5556
diff changeset
   636
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   637
critical:aBlock
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   638
    "evaluate aBlock as a critical region; the receiver must be
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   639
     created using Semaphore>>forMutualExclusion"
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   640
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   641
    |retVal gotSema wasBlocked|
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   642
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   643
    "/ inlined common case - ugly kludge but helpful
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   644
    wasBlocked := OperatingSystem blockInterrupts.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   645
    count > 0 ifTrue:[
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   646
        count := count - 1.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   647
        count == 0 ifTrue:[
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   648
            lastOwnerID := Processor activeProcessId.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   649
        ].
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   650
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   651
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   652
        retVal := aBlock ifCurtailed:[ self signal ].
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   653
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   654
        OperatingSystem blockInterrupts.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   655
        [
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   656
            |p|
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   657
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   658
            count := count + 1.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   659
            waitingProcesses notNil ifTrue:[
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   660
                p := self removeFirstWaitingProcess.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   661
                p notNil ifTrue:[
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   662
                    Processor resume:p.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   663
                ].
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   664
            ].
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   665
        ] ensure:[
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   666
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   667
        ].
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   668
        ^ retVal.
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   669
    ].
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   670
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   671
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   672
    [
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   673
        gotSema := self wait.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   674
        retVal := aBlock value.
6616
70d54ffebc91 valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents: 6615
diff changeset
   675
    ] ifCurtailed:[
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   676
        "/ be careful - the unwind may occur both while waiting
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   677
        "/ AND while evaluating the block.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   678
        gotSema notNil ifTrue:[self signal].
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   679
    ].
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   680
    self signal.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   681
    ^ retVal
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   682
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   683
    "
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   684
      the example below is stupid (it should use a SharedQueue,
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   685
      or at least a Queue with critical regions).
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   686
      Anyhow, it demonstrates how two processes lock each other
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   687
      from accessing coll at the same time
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   688
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   689
     |sema coll|
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   690
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   691
     sema := Semaphore forMutualExclusion.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   692
     coll := OrderedCollection new:10.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   693
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   694
     [
3669
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   695
        1 to:1000 do:[:i |
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   696
            sema critical:[
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   697
                coll addLast:i.
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   698
                (Delay forSeconds:0.1) wait.
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   699
            ]
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   700
        ]
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   701
     ] forkAt:4.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   702
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   703
     [
3669
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   704
        1 to:1000 do:[:i |
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   705
            sema critical:[
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   706
                coll removeFirst.
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   707
                (Delay forSeconds:0.1) wait.
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   708
            ]
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   709
        ]
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   710
     ] forkAt:4.
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   711
    "
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
   712
3669
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   713
    "Modified: / 16.4.1996 / 10:00:46 / stefan"
59baacaab0a8 comment
Claus Gittinger <cg@exept.de>
parents: 3609
diff changeset
   714
    "Modified: / 21.7.1998 / 17:45:26 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   715
!
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   716
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   717
wait
a27a279701f8 Initial revision
claus
parents:
diff changeset
   718
    "wait for the semaphore"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   719
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   720
    |activeProcess wasBlocked|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   721
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   722
    wasBlocked := OperatingSystem blockInterrupts.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   723
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   724
    count <= 0 ifTrue:[
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   725
        activeProcess := Processor activeProcess.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   726
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   727
        "
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   728
         need a while-loop here, since more than one process may
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   729
         wait for it and another one may also wake up.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   730
         Thus, the count is not always non-zero after returning from
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   731
         suspend.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   732
        "
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   733
        [
6986
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   734
            self addWaitingProcess:activeProcess.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   735
            "
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   736
             for some more descriptive info in processMonitor ...
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   737
             ... set the state to #wait (instead of #suspend)
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   738
            "
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   739
            [
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   740
                activeProcess suspendWithState:#wait
6616
70d54ffebc91 valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents: 6615
diff changeset
   741
            ] ifCurtailed:[
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   742
                self removeWaitingProcess:activeProcess.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   743
                wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   744
            ].
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   745
            self removeWaitingProcess:activeProcess.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   746
        ] doUntil:[count > 0].
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   747
    ].
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   748
10
claus
parents: 3
diff changeset
   749
    count := count - 1.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   750
    count == 0 ifTrue:[
3600
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   751
        lastOwnerID := Processor activeProcessId.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   752
    ].
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   753
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
752
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   754
3600
6c5629eea51d give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   755
    "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
   756
    "Modified: / 17.6.1998 / 15:26:27 / cg"
10
claus
parents: 3
diff changeset
   757
!
claus
parents: 3
diff changeset
   758
329
claus
parents: 302
diff changeset
   759
waitUncounted
claus
parents: 302
diff changeset
   760
    "wait for the semaphore; do not consume the resource
claus
parents: 302
diff changeset
   761
     (i.e. do not count down)"
claus
parents: 302
diff changeset
   762
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   763
    |activeProcess wasBlocked|
329
claus
parents: 302
diff changeset
   764
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   765
    count > 0 ifTrue:[
6601
6a946851d1a7 Simplify #critical:
Stefan Vogel <sv@exept.de>
parents: 6421
diff changeset
   766
        ^ self
329
claus
parents: 302
diff changeset
   767
    ].
claus
parents: 302
diff changeset
   768
2142
d4a21668c699 commentary
Claus Gittinger <cg@exept.de>
parents: 1812
diff changeset
   769
    activeProcess := Processor activeProcess.
329
claus
parents: 302
diff changeset
   770
claus
parents: 302
diff changeset
   771
    wasBlocked := OperatingSystem blockInterrupts.
claus
parents: 302
diff changeset
   772
    "
claus
parents: 302
diff changeset
   773
     need a while-loop here, since more than one process may
claus
parents: 302
diff changeset
   774
     wait for it and another one may also wake up.
claus
parents: 302
diff changeset
   775
     Thus, the count is not always non-zero after returning from
claus
parents: 302
diff changeset
   776
     suspend.
claus
parents: 302
diff changeset
   777
    "
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   778
    [count <= 0] whileTrue:[
6986
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   779
        self addWaitingProcess:activeProcess.
6601
6a946851d1a7 Simplify #critical:
Stefan Vogel <sv@exept.de>
parents: 6421
diff changeset
   780
        "
6a946851d1a7 Simplify #critical:
Stefan Vogel <sv@exept.de>
parents: 6421
diff changeset
   781
         for some more descriptive info in processMonitor ...
6a946851d1a7 Simplify #critical:
Stefan Vogel <sv@exept.de>
parents: 6421
diff changeset
   782
         ... set the state to #wait (instead of #suspend)
6a946851d1a7 Simplify #critical:
Stefan Vogel <sv@exept.de>
parents: 6421
diff changeset
   783
        "
6a946851d1a7 Simplify #critical:
Stefan Vogel <sv@exept.de>
parents: 6421
diff changeset
   784
        [
6a946851d1a7 Simplify #critical:
Stefan Vogel <sv@exept.de>
parents: 6421
diff changeset
   785
            activeProcess suspendWithState:#wait
6616
70d54ffebc91 valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents: 6615
diff changeset
   786
        ] ifCurtailed:[
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   787
            self removeWaitingProcess:activeProcess.
6601
6a946851d1a7 Simplify #critical:
Stefan Vogel <sv@exept.de>
parents: 6421
diff changeset
   788
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
6a946851d1a7 Simplify #critical:
Stefan Vogel <sv@exept.de>
parents: 6421
diff changeset
   789
        ].
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   790
        self removeWaitingProcess:activeProcess.
329
claus
parents: 302
diff changeset
   791
    ].
claus
parents: 302
diff changeset
   792
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
752
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   793
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   794
    "Modified: 13.12.1995 / 13:26:49 / stefan"
2352
9588e545f4a5 use #removeIdentical
Claus Gittinger <cg@exept.de>
parents: 2265
diff changeset
   795
    "Modified: 1.2.1997 / 12:11:41 / cg"
329
claus
parents: 302
diff changeset
   796
!
claus
parents: 302
diff changeset
   797
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   798
waitWithTimeout:seconds
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   799
    "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
   800
     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
   801
     due to a timeout. 
101239898989 unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   802
     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
   803
     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
   804
     the receiver if the semaphore is available, nil if not).
4902
caf73c9821c5 Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents: 4633
diff changeset
   805
     However, polling is not the intended use of semaphores, though.
caf73c9821c5 Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents: 4633
diff changeset
   806
     If seconds is nil, wait without timeout."
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   807
6061
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
   808
    |millis|
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
   809
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
   810
    seconds notNil ifTrue:[
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
   811
        millis := seconds * 1000 
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
   812
    ].
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
   813
    ^ self waitWithTimeoutMs:millis.
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
   814
!
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
   815
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
   816
waitWithTimeoutMs:milliSeconds
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
   817
    "wait for the semaphore, but abort the wait after some time.
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
   818
     return the receiver if the semaphore triggered normal, nil if we return
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
   819
     due to a timeout. 
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
   820
     With zero timeout, this can be used to poll a semaphore (returning
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
   821
     the receiver if the semaphore is available, nil if not).
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
   822
     However, polling is not the intended use of semaphores, though.
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
   823
     If milliSeconds is nil, wait without timeout."
9e266783d5c2 care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents: 5713
diff changeset
   824
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   825
    |activeProcess timeoutOccured wasBlocked timeoutBlock now endTime|
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   826
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   827
    wasBlocked := OperatingSystem blockInterrupts.
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   828
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   829
    count <= 0 ifTrue:[
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   830
        "with zero-timeout, this is a poll"
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   831
        milliSeconds == 0 ifTrue:[
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   832
            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   833
            ^ nil
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   834
        ].
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   835
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   836
        activeProcess := Processor activeProcess.
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   837
        timeoutOccured := false.
302
1f76060d58a4 *** empty log message ***
claus
parents: 269
diff changeset
   838
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   839
        milliSeconds notNil ifTrue:[
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   840
            "Wait with timeout: calculate the end-time"
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   841
            now := OperatingSystem getMillisecondTime.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   842
            endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   843
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   844
            timeoutBlock := [timeoutOccured := true. timeoutBlock:= nil. Processor resume:activeProcess].
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   845
            Processor addTimedBlock:timeoutBlock for:activeProcess atMilliseconds:endTime.
4902
caf73c9821c5 Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents: 4633
diff changeset
   846
        ].
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   847
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   848
        "
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   849
         need a while-loop here, since more than one process may
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   850
         wait for it and another one may also wake up.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   851
         Thus, the count is not always non-zero after returning from
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   852
         suspend.
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   853
        "
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   854
        [
6986
d2ef4d62861e Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents: 6984
diff changeset
   855
            self addWaitingProcess:activeProcess.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   856
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   857
            "
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   858
             for some more descriptive info in processMonitor ...
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   859
             ... set the state to #wait (instead of #suspend)
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   860
            "
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   861
            [
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   862
                "sleep until resumed..."
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   863
                activeProcess suspendWithState:#wait.
6616
70d54ffebc91 valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents: 6615
diff changeset
   864
            ] ifCurtailed:[
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   865
                timeoutBlock notNil ifTrue:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   866
                    Processor removeTimedBlock:timeoutBlock.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   867
                    timeoutBlock := nil.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   868
                ].
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   869
                self removeWaitingProcess:activeProcess.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   870
                wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   871
            ].
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   872
7717
ac9f01ef7157 tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents: 7258
diff changeset
   873
            self removeWaitingProcess:activeProcess.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   874
            timeoutOccured ifTrue:[
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   875
                wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   876
                ^ nil
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   877
            ].
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   878
        ] doUntil:[count > 0].
4902
caf73c9821c5 Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents: 4633
diff changeset
   879
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   880
        timeoutBlock notNil ifTrue:[
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   881
            Processor removeTimedBlock:timeoutBlock.
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   882
            timeoutBlock := nil.
6615
2b56a43d0e89 allow for negative counts.
Claus Gittinger <cg@exept.de>
parents: 6601
diff changeset
   883
        ].
4902
caf73c9821c5 Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents: 4633
diff changeset
   884
    ].
2690
f79d685e06af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2407
diff changeset
   885
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   886
    "if we come here, we have accquired the semaphore"
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   887
    count := count - 1.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   888
    count == 0 ifTrue:[
4902
caf73c9821c5 Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents: 4633
diff changeset
   889
        lastOwnerID := Processor activeProcessId.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2235
diff changeset
   890
    ].
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   891
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
770
402958905760 protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents: 769
diff changeset
   892
    ^ self
752
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   893
0259dd855289 new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents: 699
diff changeset
   894
    "Modified: 13.12.1995 / 13:27:24 / stefan"
2690
f79d685e06af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2407
diff changeset
   895
    "Modified: 16.6.1997 / 21:54:38 / cg"
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   896
! !
77
6c38ca59927f *** empty log message ***
claus
parents: 69
diff changeset
   897
1812
a048a98aa299 kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   898
!Semaphore class methodsFor:'documentation'!
183
7899f380e77d signalIf
claus
parents: 159
diff changeset
   899
699
12f456343eea checkin from browser
Claus Gittinger <cg@exept.de>
parents: 560
diff changeset
   900
version
8569
657853ae04cd Change waitXXX
Stefan Vogel <sv@exept.de>
parents: 7718
diff changeset
   901
    ^ '$Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.72 2004-09-21 18:06:07 stefan Exp $'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   902
! !