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