Semaphore.st
changeset 20263 9f3ddb907b77
parent 20116 c29d96c1db82
child 20342 219a5a47e8b1
child 20426 2ec010b1a559
equal deleted inserted replaced
20262:ffae5acd468c 20263:9f3ddb907b77
   910     "Modified: 1.2.1997 / 12:11:41 / cg"
   910     "Modified: 1.2.1997 / 12:11:41 / cg"
   911 !
   911 !
   912 
   912 
   913 waitUncountedWithTimeoutMs:milliSeconds
   913 waitUncountedWithTimeoutMs:milliSeconds
   914     "wait for the semaphore; do not consume the resource
   914     "wait for the semaphore; do not consume the resource
   915      (i.e. do not count down). 
   915      (i.e. do not count down).
   916      Abort the wait after some time.
   916      Abort the wait after some time.
   917      return the receiver if the semaphore triggered normal, nil if we return
   917      return the receiver if the semaphore triggered normal, nil if we return
   918      due to a timeout.
   918      due to a timeout.
   919      With zero timeout, this can be used to poll a semaphore (returning
   919      With zero timeout, this can be used to poll a semaphore (returning
   920      the receiver if the semaphore is available, nil if not).
   920      the receiver if the semaphore is available, nil if not).
   921      However, polling is not the intended use of semaphores, though.
   921      However, polling is not the intended use of semaphores, though.
   922      If milliSeconds is nil, wait without timeout."
   922      If milliSeconds is nil, wait without timeout."
   923 
   923 
   924     |activeProcess timeoutOccured wasBlocked timeoutBlock now endTime|
   924     |activeProcess timeoutOccurred wasBlocked timeoutBlock now endTime|
   925 
   925 
   926     count > 0 ifTrue:[
   926     count > 0 ifTrue:[
   927         ^ self
   927         ^ self
   928     ].
   928     ].
   929 
   929 
   935             wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   935             wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   936             ^ nil
   936             ^ nil
   937         ].
   937         ].
   938 
   938 
   939         activeProcess := Processor activeProcess.
   939         activeProcess := Processor activeProcess.
   940         timeoutOccured := false.
   940         timeoutOccurred := false.
   941 
   941 
   942         milliSeconds notNil ifTrue:[
   942         milliSeconds notNil ifTrue:[
   943             "Wait with timeout: calculate the end-time"
   943             "Wait with timeout: calculate the end-time"
   944             now := OperatingSystem getMillisecondTime.
   944             now := OperatingSystem getMillisecondTime.
   945             endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds.
   945             endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds.
   946 
   946 
   947             timeoutBlock := [
   947             timeoutBlock := [
   948                     timeoutOccured := true.
   948                     timeoutOccurred := true.
   949                     timeoutBlock:= nil.
   949                     timeoutBlock:= nil.
   950                     Processor resume:activeProcess.
   950                     Processor resume:activeProcess.
   951                 ].
   951                 ].
   952             Processor addTimedBlock:timeoutBlock for:activeProcess atMilliseconds:endTime.
   952             Processor addTimedBlock:timeoutBlock for:activeProcess atMilliseconds:endTime.
   953         ].
   953         ].
   978                 self removeWaitingProcess:activeProcess.
   978                 self removeWaitingProcess:activeProcess.
   979                 wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   979                 wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   980             ].
   980             ].
   981 
   981 
   982             self removeWaitingProcess:activeProcess.
   982             self removeWaitingProcess:activeProcess.
   983             timeoutOccured ifTrue:[
   983             timeoutOccurred ifTrue:[
   984                 wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   984                 wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   985                 ^ nil
   985                 ^ nil
   986             ].
   986             ].
   987 
   987 
   988             count <= 0
   988             count <= 0
  1026      With zero timeout, this can be used to poll a semaphore (returning
  1026      With zero timeout, this can be used to poll a semaphore (returning
  1027      the receiver if the semaphore is available, nil if not).
  1027      the receiver if the semaphore is available, nil if not).
  1028      However, polling is not the intended use of semaphores, though.
  1028      However, polling is not the intended use of semaphores, though.
  1029      If milliSeconds is nil, wait without timeout."
  1029      If milliSeconds is nil, wait without timeout."
  1030 
  1030 
  1031     |activeProcess timeoutOccured wasBlocked timeoutBlock now endTime|
  1031     |activeProcess timeoutOccurred wasBlocked timeoutBlock now endTime|
  1032 
  1032 
  1033     wasBlocked := OperatingSystem blockInterrupts.
  1033     wasBlocked := OperatingSystem blockInterrupts.
  1034 
  1034 
  1035     count <= 0 ifTrue:[
  1035     count <= 0 ifTrue:[
  1036         "with zero-timeout, this is a poll"
  1036         "with zero-timeout, this is a poll"
  1038             wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
  1038             wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
  1039             ^ nil
  1039             ^ nil
  1040         ].
  1040         ].
  1041 
  1041 
  1042         activeProcess := Processor activeProcess.
  1042         activeProcess := Processor activeProcess.
  1043         timeoutOccured := false.
  1043         timeoutOccurred := false.
  1044 
  1044 
  1045         milliSeconds notNil ifTrue:[
  1045         milliSeconds notNil ifTrue:[
  1046             "Wait with timeout: calculate the end-time"
  1046             "Wait with timeout: calculate the end-time"
  1047             now := OperatingSystem getMillisecondTime.
  1047             now := OperatingSystem getMillisecondTime.
  1048             endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds.
  1048             endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds.
  1049 
  1049 
  1050             timeoutBlock := [
  1050             timeoutBlock := [
  1051                     timeoutOccured := true.
  1051                     timeoutOccurred := true.
  1052                     timeoutBlock:= nil.
  1052                     timeoutBlock:= nil.
  1053                     Processor resume:activeProcess.
  1053                     Processor resume:activeProcess.
  1054                 ].
  1054                 ].
  1055             Processor addTimedBlock:timeoutBlock for:activeProcess atMilliseconds:endTime.
  1055             Processor addTimedBlock:timeoutBlock for:activeProcess atMilliseconds:endTime.
  1056         ].
  1056         ].
  1081                 self removeWaitingProcess:activeProcess.
  1081                 self removeWaitingProcess:activeProcess.
  1082                 wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
  1082                 wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
  1083             ].
  1083             ].
  1084 
  1084 
  1085             self removeWaitingProcess:activeProcess.
  1085             self removeWaitingProcess:activeProcess.
  1086             timeoutOccured ifTrue:[
  1086             timeoutOccurred ifTrue:[
  1087                 wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
  1087                 wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
  1088                 ^ nil
  1088                 ^ nil
  1089             ].
  1089             ].
  1090 
  1090 
  1091             count <= 0
  1091             count <= 0
  1118      However, polling is not the intended use of semaphores, though.
  1118      However, polling is not the intended use of semaphores, though.
  1119      If milliSeconds is nil, wait without timeout.
  1119      If milliSeconds is nil, wait without timeout.
  1120 
  1120 
  1121      waitStateSymbol is the state the process is set to while waiting - normally #wait."
  1121      waitStateSymbol is the state the process is set to while waiting - normally #wait."
  1122 
  1122 
  1123     |activeProcess timeoutOccured wasBlocked timeoutBlock now endTime|
  1123     |activeProcess timeoutOccurred wasBlocked timeoutBlock now endTime|
  1124 
  1124 
  1125     wasBlocked := OperatingSystem blockInterrupts.
  1125     wasBlocked := OperatingSystem blockInterrupts.
  1126 
  1126 
  1127     count <= 0 ifTrue:[
  1127     count <= 0 ifTrue:[
  1128         "with zero-timeout, this is a poll"
  1128         "with zero-timeout, this is a poll"
  1130             wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
  1130             wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
  1131             ^ nil
  1131             ^ nil
  1132         ].
  1132         ].
  1133 
  1133 
  1134         activeProcess := Processor activeProcess.
  1134         activeProcess := Processor activeProcess.
  1135         timeoutOccured := false.
  1135         timeoutOccurred := false.
  1136 
  1136 
  1137         milliSeconds notNil ifTrue:[
  1137         milliSeconds notNil ifTrue:[
  1138             "Wait with timeout: calculate the end-time"
  1138             "Wait with timeout: calculate the end-time"
  1139             now := OperatingSystem getMillisecondTime.
  1139             now := OperatingSystem getMillisecondTime.
  1140             endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds.
  1140             endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds.
  1141 
  1141 
  1142             timeoutBlock := [
  1142             timeoutBlock := [
  1143                     timeoutOccured := true.
  1143                     timeoutOccurred := true.
  1144                     timeoutBlock:= nil.
  1144                     timeoutBlock:= nil.
  1145                     Processor resume:activeProcess.
  1145                     Processor resume:activeProcess.
  1146                 ].
  1146                 ].
  1147             Processor addTimedBlock:timeoutBlock for:activeProcess atMilliseconds:endTime.
  1147             Processor addTimedBlock:timeoutBlock for:activeProcess atMilliseconds:endTime.
  1148         ].
  1148         ].
  1173                 self removeWaitingProcess:activeProcess.
  1173                 self removeWaitingProcess:activeProcess.
  1174                 wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
  1174                 wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
  1175             ].
  1175             ].
  1176 
  1176 
  1177             self removeWaitingProcess:activeProcess.
  1177             self removeWaitingProcess:activeProcess.
  1178             timeoutOccured ifTrue:[
  1178             timeoutOccurred ifTrue:[
  1179                 wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
  1179                 wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
  1180                 ^ nil
  1180                 ^ nil
  1181             ].
  1181             ].
  1182 
  1182 
  1183             count <= 0
  1183             count <= 0