1 23 24 package com.sun.jdo.spi.persistence.utility; 25 26 import java.util.ResourceBundle ; 27 import com.sun.jdo.spi.persistence.utility.logging.Logger; 28 29 30 35 public class SemaphoreImpl implements Semaphore { 38 40 private static final Logger _logger = LogHelperUtility.getLogger(); 41 42 44 private final String _owner; 45 46 48 private final Object _lock = new Object (); 49 50 52 private Thread _holder = null; 53 54 56 private int _counter = 0; 57 58 61 private final static ResourceBundle messages = 62 I18NHelper.loadBundle(SemaphoreImpl.class); 63 64 65 public SemaphoreImpl(String owner) { 66 _owner = owner; 67 } 68 69 71 public void acquire() { 72 boolean debug = _logger.isLoggable(Logger.FINEST); 73 74 if (debug) { 75 Object [] items = new Object [] {_owner, Thread.currentThread(),new Integer (_counter)}; 76 _logger.finest("utility.semaphoreimpl.acquire",items); } 78 79 synchronized (_lock) { 80 if (Thread.currentThread() == _holder) { 85 _counter++; 86 87 } else { 88 while (_counter > 0) { 89 try { 90 _lock.wait(); 92 } catch (InterruptedException e) { 93 } 94 } 95 _holder = Thread.currentThread(); 96 _counter++; 97 98 if (debug) { 99 Object [] items = new Object [] {_owner, Thread.currentThread(),new Integer (_counter)}; 100 _logger.finest("utility.semaphoreimpl.gotlock",items); } 102 } 103 } 104 } 105 106 108 public void release() { 109 boolean debug = _logger.isLoggable(Logger.FINEST); 110 111 if (debug) { 112 Object [] items = new Object [] {_owner, Thread.currentThread(),new Integer (_counter)}; 113 _logger.finest("utility.semaphoreimpl.release",items); } 115 116 synchronized (_lock) { 117 if (Thread.currentThread() == _holder) { 122 if (--_counter == 0) { 123 _holder = null; 124 _lock.notify(); 125 } 126 } else { 127 throw new IllegalMonitorStateException ( 128 I18NHelper.getMessage(messages, 129 "utility.semaphoreimpl.wrongthread", new Object [] {_owner, Thread.currentThread()})); 131 } 132 } 133 } 134 } 135 | Popular Tags |