KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > utility > SemaphoreImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.jdo.spi.persistence.utility;
25
26 import java.util.ResourceBundle JavaDoc;
27 import com.sun.jdo.spi.persistence.utility.logging.Logger;
28
29
30 /** Implements a simple semaphore.
31  *
32  * @author Dave Bristor
33  * @author Marina Vatkina
34  */

35 // db13166: I would rather we use Doug Lea's stuff, but don't want to
36
// introduce that magnitude of change at this point in time.
37
public class SemaphoreImpl implements Semaphore {
38     /** Where to log messages about locking operations
39      */

40     private static final Logger _logger = LogHelperUtility.getLogger();
41
42     /** For logging, indicates on whose behalf locking is done.
43      */

44     private final String JavaDoc _owner;
45
46     /** Synchronizes the lock.
47      */

48     private final Object JavaDoc _lock = new Object JavaDoc();
49
50     /** Thread which holds the lock.
51      */

52     private Thread JavaDoc _holder = null;
53
54     /** Semaphore counter.
55      */

56     private int _counter = 0;
57     
58     /**
59      * I18N message handler
60      */

61     private final static ResourceBundle JavaDoc messages =
62         I18NHelper.loadBundle(SemaphoreImpl.class);
63
64
65     public SemaphoreImpl(String JavaDoc owner) {
66         _owner = owner;
67     }
68
69     /** Acquire a lock.
70      */

71     public void acquire() {
72         boolean debug = _logger.isLoggable(Logger.FINEST);
73
74         if (debug) {
75             Object JavaDoc[] items = new Object JavaDoc[] {_owner, Thread.currentThread(),new Integer JavaDoc(_counter)};
76             _logger.finest("utility.semaphoreimpl.acquire",items); // NOI18N
77
}
78
79         synchronized (_lock) {
80             //
81
// If the current thread already holds this lock, we simply
82
// update the count and return.
83
//
84
if (Thread.currentThread() == _holder) {
85                 _counter++;
86                 
87             } else {
88                 while (_counter > 0) {
89                     try {
90                         // wait for the lock to be released
91
_lock.wait();
92                     } catch (InterruptedException JavaDoc e) {
93                     }
94                 }
95                 _holder = Thread.currentThread();
96                 _counter++;
97                 
98                 if (debug) {
99                     Object JavaDoc[] items = new Object JavaDoc[] {_owner, Thread.currentThread(),new Integer JavaDoc(_counter)};
100                     _logger.finest("utility.semaphoreimpl.gotlock",items); // NOI18N
101
}
102             }
103         }
104     }
105
106     /** Release a lock.
107      */

108     public void release() {
109         boolean debug = _logger.isLoggable(Logger.FINEST);
110         
111         if (debug) {
112             Object JavaDoc[] items = new Object JavaDoc[] {_owner, Thread.currentThread(),new Integer JavaDoc(_counter)};
113             _logger.finest("utility.semaphoreimpl.release",items); // NOI18N
114
}
115         
116         synchronized (_lock) {
117             //
118
// If the current thread already holds this lock, we simply
119
// update the count and return.
120
//
121
if (Thread.currentThread() == _holder) {
122                 if (--_counter == 0) {
123                     _holder = null;
124                     _lock.notify();
125                 }
126             } else {
127                 throw new IllegalMonitorStateException JavaDoc(
128                   I18NHelper.getMessage(messages,
129                                         "utility.semaphoreimpl.wrongthread", // NOI18N
130
new Object JavaDoc[] {_owner, Thread.currentThread()}));
131             }
132         }
133     }
134 }
135
Popular Tags