Semaphore.st
changeset 375 e5019c22f40e
parent 362 4131e87e79ec
child 379 5b5a130ccd09
equal deleted inserted replaced
374:7eb5bedfaa1c 375:e5019c22f40e
     8  be provided or otherwise made available to, or used by, any
     8  be provided or otherwise made available to, or used by, any
     9  other person.  No title to or ownership of the software is
     9  other person.  No title to or ownership of the software is
    10  hereby transferred.
    10  hereby transferred.
    11 "
    11 "
    12 
    12 
    13 LinkedList subclass:#Semaphore
    13 Object subclass:#Semaphore
    14 	 instanceVariableNames:'count waitingProcesses'
    14 	 instanceVariableNames:'count waitingProcesses'
    15 "/         instanceVariableNames:'count'
       
    16 	 classVariableNames:''
    15 	 classVariableNames:''
    17 	 poolDictionaries:''
    16 	 poolDictionaries:''
    18 	 category:'Kernel-Processes'!
    17 	 category:'Kernel-Processes'!
    19 
    18 
    20 Semaphore comment:'
    19 Semaphore comment:'
    21 COPYRIGHT (c) 1993 by Claus Gittinger
    20 COPYRIGHT (c) 1993 by Claus Gittinger
    22 	      All Rights Reserved
    21 	      All Rights Reserved
    23 
    22 
    24 $Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.18 1995-07-22 19:24:21 claus Exp $
    23 $Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.19 1995-08-08 00:48:42 claus Exp $
    25 '!
    24 '!
    26 
    25 
    27 !Semaphore class methodsFor:'documentation'!
    26 !Semaphore class methodsFor:'documentation'!
    28 
    27 
    29 copyright
    28 copyright
    40 "
    39 "
    41 !
    40 !
    42 
    41 
    43 version
    42 version
    44 "
    43 "
    45 $Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.18 1995-07-22 19:24:21 claus Exp $
    44 $Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.19 1995-08-08 00:48:42 claus Exp $
    46 "
    45 "
    47 !
    46 !
    48 
    47 
    49 documentation
    48 documentation
    50 "
    49 "
    90 ! !
    89 ! !
    91 
    90 
    92 !Semaphore methodsFor:'private accessing'!
    91 !Semaphore methodsFor:'private accessing'!
    93 
    92 
    94 setCount:n
    93 setCount:n
       
    94     waitingProcesses := OrderedCollection new:3.
    95     count := n
    95     count := n
    96 ! !
    96 ! !
    97 
    97 
    98 !Semaphore methodsFor:'queries '!
    98 !Semaphore methodsFor:'queries '!
    99 
    99 
   136      wait for it and another one may also wake up.
   136      wait for it and another one may also wake up.
   137      Thus, the count is not always non-zero after returning from
   137      Thus, the count is not always non-zero after returning from
   138      suspend.
   138      suspend.
   139     "
   139     "
   140     [count == 0] whileTrue:[
   140     [count == 0] whileTrue:[
   141 	waitingProcesses isNil ifTrue:[
   141 	waitingProcesses add:current.
   142 	    waitingProcesses := OrderedCollection with:current
       
   143 	] ifFalse:[
       
   144 	    waitingProcesses add:current
       
   145 	].
       
   146 "/        self add:current.
       
   147 	"
   142 	"
   148 	 for some more descriptive info in processMonitor ...
   143 	 for some more descriptive info in processMonitor ...
   149 	 (notice that state could already be #ioWait, #timeWait or anything else)
   144 	 (notice that state could already be #ioWait, #timeWait or anything else)
   150 	"
   145 	"
   151 	current setStateTo:#wait if:#active.
   146 	current setStateTo:#wait if:#active.
   179      wait for it and another one may also wake up.
   174      wait for it and another one may also wake up.
   180      Thus, the count is not always non-zero after returning from
   175      Thus, the count is not always non-zero after returning from
   181      suspend.
   176      suspend.
   182     "
   177     "
   183     [count == 0] whileTrue:[
   178     [count == 0] whileTrue:[
   184 	waitingProcesses isNil ifTrue:[
   179 	waitingProcesses add:current.
   185 	    waitingProcesses := OrderedCollection with:current
       
   186 	] ifFalse:[
       
   187 	    waitingProcesses add:current
       
   188 	].
       
   189 "/        self add:current.
       
   190 	"
   180 	"
   191 	 for some more descriptive info in processMonitor ...
   181 	 for some more descriptive info in processMonitor ...
   192 	 (notice that state could already be #ioWait, #timeWait or anything else)
   182 	 (notice that state could already be #ioWait, #timeWait or anything else)
   193 	"
   183 	"
   194 	current setStateTo:#wait if:#active.
   184 	current setStateTo:#wait if:#active.
   241      wait for it and another one may also wake up.
   231      wait for it and another one may also wake up.
   242      Thus, the count is not always non-zero after returning from
   232      Thus, the count is not always non-zero after returning from
   243      suspend.
   233      suspend.
   244     "
   234     "
   245     [count == 0] whileTrue:[
   235     [count == 0] whileTrue:[
   246 	waitingProcesses isNil ifTrue:[
   236 	waitingProcesses add:current.
   247 	    waitingProcesses := OrderedCollection with:current
       
   248 	] ifFalse:[
       
   249 	    waitingProcesses add:current
       
   250 	].
       
   251 "/        self add:current.
       
   252 
   237 
   253 	"
   238 	"
   254 	 for some more descriptive info in processMonitor ...
   239 	 for some more descriptive info in processMonitor ...
   255 	 (notice that state could already be #ioWait or #timeWait)
   240 	 (notice that state could already be #ioWait or #timeWait)
   256 	"
   241 	"
   292 
   277 
   293     |p wasBlocked|
   278     |p wasBlocked|
   294 
   279 
   295     wasBlocked := OperatingSystem blockInterrupts.
   280     wasBlocked := OperatingSystem blockInterrupts.
   296     count := count + 1.
   281     count := count + 1.
   297     (waitingProcesses notNil and:[waitingProcesses notEmpty]) ifTrue:[
   282     waitingProcesses notEmpty ifTrue:[
   298 	p := waitingProcesses removeFirst.
   283 	p := waitingProcesses removeFirst.
   299 "/    self isEmpty ifFalse:[
       
   300 "/        p := self removeFirst.
       
   301 
   284 
   302 	wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   285 	wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   303 	p resume.
   286 	p resume.
   304 	^ self
   287 	^ self
   305     ].
   288     ].
   311      This can be used for one-shot semaphores (i.e. not remembering
   294      This can be used for one-shot semaphores (i.e. not remembering
   312      previous signals)"
   295      previous signals)"
   313 
   296 
   314     |wasBlocked|
   297     |wasBlocked|
   315 
   298 
   316     (waitingProcesses notNil and:[waitingProcesses notEmpty]) ifTrue:[
   299     waitingProcesses notEmpty ifTrue:[
   317 	wasBlocked := OperatingSystem blockInterrupts.
   300 	wasBlocked := OperatingSystem blockInterrupts.
   318 	(waitingProcesses notNil and:[waitingProcesses notEmpty]) ifTrue:[
   301 	waitingProcesses notEmpty ifTrue:[
   319 	    self signal
   302 	    self signal
   320 	].
   303 	].
   321 	wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   304 	wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   322     ]
   305     ]
   323 !
   306 !
   327      This can be used for process synchronization, if multiple processes are
   310      This can be used for process synchronization, if multiple processes are
   328      waiting for a common event."
   311      waiting for a common event."
   329 
   312 
   330     |wasBlocked|
   313     |wasBlocked|
   331 
   314 
   332     [waitingProcesses notNil and:[waitingProcesses notEmpty]] whileTrue:[
   315     [waitingProcesses notEmpty] whileTrue:[
   333 	wasBlocked := OperatingSystem blockInterrupts.
   316 	wasBlocked := OperatingSystem blockInterrupts.
   334 	(waitingProcesses notNil and:[waitingProcesses notEmpty]) ifTrue:[
   317 	waitingProcesses notEmpty ifTrue:[
   335 	    self signal
   318 	    self signal
   336 	].
   319 	].
   337 	wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   320 	wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   338     ]
   321     ]
   339 !
   322 !