01: /*
02: *
03: *
04: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
05: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License version
09: * 2 only, as published by the Free Software Foundation.
10: *
11: * This program is distributed in the hope that it will be useful, but
12: * WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * General Public License version 2 for more details (a copy is
15: * included at /legal/license.txt).
16: *
17: * You should have received a copy of the GNU General Public License
18: * version 2 along with this work; if not, write to the Free Software
19: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA
21: *
22: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
23: * Clara, CA 95054 or visit www.sun.com if you need additional
24: * information or have any questions.
25: */
26:
27: package com.sun.cldc.util;
28:
29: public class Semaphore {
30: private SemaphoreLock lock;
31:
32: /**
33: * Creates a Semaphore with the given number of permits.
34: */
35: public Semaphore(int permits) {
36: lock = new SemaphoreLock(permits);
37: }
38:
39: /**
40: * Acquires a permit from this semaphore, blocking until one is
41: * available.
42: */
43: public void acquire() {
44: lock.acquire();
45: }
46:
47: /**
48: * Releases a permit, returning it to the semaphore. If any
49: * threads are blocking trying to acquire a permit, then one is
50: * selected and given the permit that was just released. That
51: * thread is re-enabled for thread scheduling purposes.
52: */
53: public void release() {
54: lock.release();
55: }
56: }
57:
58: /**
59: * This class implements the behavior of a Semaphore. Note that the
60: * synchronized methods are placed in this class, instead of in the
61: * public Semaphore class. This makes sure that an application cannot
62: * affect the behavior of a Semaphore by synchronizing on the
63: * Semaphore object itself.
64: */
65: class SemaphoreLock {
66: private int permits;
67:
68: SemaphoreLock(int permits) {
69: this .permits = permits;
70: }
71:
72: synchronized native void acquire();
73:
74: native void release();
75: }
|