JavaMonitor.st
changeset 3773 f2fdd527603f
parent 3431 82790b1e6d54
child 3726 b133650e5820
child 3777 971961e59d5e
equal deleted inserted replaced
3772:4820f3d55523 3773:f2fdd527603f
     1 "{ Encoding: utf8 }"
       
     2 
       
     3 "
     1 "
     4  COPYRIGHT (c) 1996-2015 by Claus Gittinger
     2  COPYRIGHT (c) 1996-2015 by Claus Gittinger
     5 
     3 
     6  New code and modifications done at SWING Research Group [1]:
     4  New code and modifications done at SWING Research Group [1]:
     7 
     5 
   117 
   115 
   118     "Created: / 20-11-2011 / 20:34:27 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   116     "Created: / 20-11-2011 / 20:34:27 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   119 !
   117 !
   120 
   118 
   121 decrement
   119 decrement
   122     "owning process released monitor, lets lower our counter so we know how many times
   120     "owning process released monitor, let's lower our counter so we know how many times
   123      do we have to release it"
   121      we have to release it"
   124     
   122     
       
   123     "/ the lock is probably not helping here to protect against problems with
       
   124     "/ increment/decrement:
       
   125     "/ the write-access (in reinitCOunter) will be either done before incrementing/decrementing or after.
       
   126     "/ if after, everything is ok;
       
   127     "/ if before, the decremented value will be zero, which is probably wrong.
       
   128 
       
   129     "/ without the critical region, we might also get possible inconsistencies:
       
   130     "/ if written before, the counter will drop to zero
       
   131     "/ if written in-between, the counter wil ahve the old value decremented;
       
   132     "/ if written afterwards, everything is ok.
       
   133 
       
   134     "/ so, the lock should be really elsewhere, making sure that noone will count
       
   135     "/ the monitor at-all and the monitor is reininialized without anyone depending on the
       
   136     "/ count at all.
       
   137     "/ but then, no critical section is needed in reinitCounter!!.
       
   138 
   125     instVarAccess critical: [ count := count - 1 ].
   139     instVarAccess critical: [ count := count - 1 ].
   126 
   140 
   127     "Created: / 22-11-2011 / 10:49:33 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   141     "Created: / 22-11-2011 / 10:49:33 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   128 !
   142 !
   129 
   143 
   130 disableWait
   144 disableWait
   131     JavaVM monitorTrace ifTrue:[
   145     JavaVM monitorTrace ifTrue:[
   132         Logger log: ('Waiting is disabled on monitor for %1' bindWith: ownerPrintString) severity:Logger severityDEBUG facility:#JVM.
   146         Logger log: ('Waiting is disabled on monitor for %1' bindWith: ownerPrintString) severity:Logger severityDEBUG facility:#JVM.
   133     ].
   147     ].
   134     instVarAccess critical: [ waitEnabled := false ].
   148     "/ critical region not needed here
       
   149     "/ instVarAccess critical: [ waitEnabled := false ].
       
   150     waitEnabled := false.
   135 
   151 
   136     "Created: / 30-11-2011 / 20:34:40 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   152     "Created: / 30-11-2011 / 20:34:40 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   137     "Modified: / 02-03-2015 / 14:06:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   153     "Modified: / 02-03-2015 / 14:06:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   138 !
   154 !
   139 
   155 
   140 enableWait
   156 enableWait
   141     instVarAccess critical: [waitEnabled := true].
   157     "/ critical region not needed here
   142 
   158     "/ instVarAccess critical: [waitEnabled := true].
       
   159     waitEnabled := true
       
   160     
   143     "Created: / 30-11-2011 / 20:34:31 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   161     "Created: / 30-11-2011 / 20:34:31 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   144     "Modified (format): / 11-10-2013 / 11:17:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   162     "Modified (format): / 11-10-2013 / 11:17:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   145 !
   163 !
   146 
   164 
   147 increment
   165 increment
   148     "owning process entered monitor again (recursion ...), lets raise our counter so we know how many times
   166     "owning process entered monitor again (recursion ...), lets raise our counter so we know how many times
   149      do we have to release it"
   167      do we have to release it"
   150     
   168     
       
   169     "/ the lock is probably not helping here to protect against problems with
       
   170     "/ increment/decrement:
       
   171     "/ the write-access (in reinitCOunter) will be either done before incrementing/decrementing or after.
       
   172     "/ if after, everything is ok;
       
   173     "/ if before, the decremented value will be zero, which is probably wrong.
       
   174 
       
   175     "/ without the critical region, we might also get possible inconsistencies:
       
   176     "/ if written before, the counter will drop to zero
       
   177     "/ if written in-between, the counter wil ahve the old value decremented;
       
   178     "/ if written afterwards, everything is ok.
       
   179 
       
   180     "/ so, the lock should be really elsewhere, making sure that noone will count
       
   181     "/ the monitor at-all and the monitor is reininialized without anyone depending on the
       
   182     "/ count at all.
       
   183     "/ but then, no critical section is needed in reinitCounter!!.
       
   184 
   151     instVarAccess critical: [ count := count + 1 ].
   185     instVarAccess critical: [ count := count + 1 ].
   152 
   186 
   153     "Created: / 22-11-2011 / 10:49:07 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   187     "Created: / 22-11-2011 / 10:49:07 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   154     "Modified: / 27-08-2012 / 10:42:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   188     "Modified: / 27-08-2012 / 10:42:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   155 !
   189 !
   181     "Created: / 22-11-2011 / 12:59:14 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   215     "Created: / 22-11-2011 / 12:59:14 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   182 !
   216 !
   183 
   217 
   184 reinitCounter
   218 reinitCounter
   185     "owning process is different from previous, lets start counting from beginning"
   219     "owning process is different from previous, lets start counting from beginning"
       
   220 
       
   221     "/ the lock is probably not helping here to protect against problems with
       
   222     "/ increment/decrement:
       
   223     "/ the write-access here will be either done before incrementing/decrementing or after.
       
   224     "/ if after, everything is ok;
       
   225     "/ if before, the decremented value will be zero, which is probably wrong.
       
   226 
       
   227     "/ without the critical region, we might also get possible inconsistencies:
       
   228     "/ if written before, the counter will drop to zero
       
   229     "/ if written in-between, the counter wil ahve the old value decremented;
       
   230     "/ if written afterwards, everything is ok.
       
   231     
       
   232     "/ so, the lock should be really elsewhere, making sure that noone will count
       
   233     "/ the monitor at-all and the monitor is reininialized without anyone depending on the
       
   234     "/ count at all.
       
   235     "/ but then, no critical section is needed!!.
   186     
   236     
   187     instVarAccess critical: [ count := 1 ].
   237     instVarAccess critical: [ count := 1 ].
   188 
   238 
   189     "Created: / 22-11-2011 / 10:51:09 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   239     "Created: / 22-11-2011 / 10:51:09 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   190 !
   240 !
   191 
   241 
   192 reinitCounter: newCount 
   242 reinitCounter: newCount 
       
   243     "/ the lock is probably not helping here to protect against problems with
       
   244     "/ increment/decrement:
       
   245     "/ the write-access here will be either done before incrementing/decrementing or after.
       
   246     "/ if after, everything is ok;
       
   247     "/ if before, the decremented value will be zero, which is probably wrong.
       
   248 
       
   249     "/ without the critical region, we might also get possible inconsistencies:
       
   250     "/ if written before, the counter will drop to zero
       
   251     "/ if written in-between, the counter wil ahve the old value decremented;
       
   252     "/ if written afterwards, everything is ok.
       
   253 
       
   254     "/ so, the lock should be really elsewhere, making sure that noone will count
       
   255     "/ the monitor at-all and the monitor is reininialized without anyone depending on the
       
   256     "/ count at all.
       
   257     "/ but then, no critical section is needed!!.
       
   258 
   193     instVarAccess critical: [ count := newCount ].
   259     instVarAccess critical: [ count := newCount ].
   194 
   260 
   195     "Created: / 22-11-2011 / 13:00:51 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   261     "Created: / 22-11-2011 / 13:00:51 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   196 !
   262 !
   197 
   263 
   204 
   270 
   205     "Created: / 20-11-2011 / 20:28:37 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   271     "Created: / 20-11-2011 / 20:28:37 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   206 !
   272 !
   207 
   273 
   208 waitEnabled
   274 waitEnabled
   209     instVarAccess critical: [ ^ waitEnabled].
   275     "/ critical region not needed here
       
   276     "/ instVarAccess critical: [ ^ waitEnabled].
       
   277     ^ waitEnabled.
   210 
   278 
   211     "Created: / 30-11-2011 / 20:34:56 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   279     "Created: / 30-11-2011 / 20:34:56 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   212 ! !
   280 ! !
   213 
   281 
   214 !JavaMonitor methodsFor:'initialization'!
   282 !JavaMonitor methodsFor:'initialization'!
   215 
   283 
   216 initialize
   284 initialize
       
   285     "Q: is this ever called?"
       
   286     
   217     owningProcess := nil.
   287     owningProcess := nil.
   218     processesEntered := OrderedCollection new.
   288     processesEntered := OrderedCollection new.
   219     monitorSema := Semaphore new: 1.
   289     monitorSema := Semaphore new: 1.
   220     processesWaiting := Dictionary new.
   290     processesWaiting := Dictionary new.
   221     waitingSema := Semaphore new: 0.
   291     waitingSema := Semaphore new: 0.
   222     waitEnabled.
   292     waitEnabled := true.
   223 
   293 
   224     "Created: / 20-11-2011 / 13:28:28 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   294     "Created: / 20-11-2011 / 13:28:28 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   225 !
   295 !
   226 
   296 
   227 initializeFor: owningObject
   297 initializeFor: owningObject
   433 ! !
   503 ! !
   434 
   504 
   435 !JavaMonitor class methodsFor:'documentation'!
   505 !JavaMonitor class methodsFor:'documentation'!
   436 
   506 
   437 version_CVS
   507 version_CVS
   438     ^ '$Header: /cvs/stx/stx/libjava/JavaMonitor.st,v 1.6 2015-03-20 12:08:00 vrany Exp $'
   508     ^ '$Header$'
   439 !
   509 !
   440 
   510 
   441 version_HG
   511 version_HG
   442 
   512 
   443     ^ '$Changeset: <not expanded> $'
   513     ^ '$Changeset: <not expanded> $'
   444 !
   514 !
   445 
   515 
   446 version_SVN
   516 version_SVN
   447     ^ 'Id'
   517     ^ '$Id$'
   448 ! !
   518 ! !
   449 
   519 
   450 
   520 
   451 JavaMonitor initialize!
   521 JavaMonitor initialize!