1 /*
2  * Copyright (c) 2002-2008 LWJGL Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of 'LWJGL' nor the names of
17  *   its contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package org.lwjgl.opengl;
33 
34 import java.nio.ByteBuffer;
35 
36 import org.lwjgl.LWJGLException;
37 import org.lwjgl.LWJGLUtil;
38 
39 /**
40  *
41  * @author elias_naur <elias_naur@users.sourceforge.net>
42  * @version $Revision$
43  * $Id$
44  */
45 abstract class PeerInfo {
46 	private final ByteBuffer handle;
47 	private Thread locking_thread; // Thread that has locked this PeerInfo
48 	private int lock_count;
49 
PeerInfo(ByteBuffer handle)50 	protected PeerInfo(ByteBuffer handle) {
51 		this.handle = handle;
52 	}
53 
lockAndInitHandle()54 	private void lockAndInitHandle() throws LWJGLException {
55 		doLockAndInitHandle();
56 	}
57 
unlock()58 	public final synchronized void unlock() throws LWJGLException {
59 		if (lock_count <= 0)
60 			throw new IllegalStateException("PeerInfo not locked!");
61 		if (Thread.currentThread() != locking_thread)
62 			throw new IllegalStateException("PeerInfo already locked by " + locking_thread);
63 		lock_count--;
64 		if (lock_count == 0) {
65 			doUnlock();
66 			locking_thread = null;
67 			notify();
68 		}
69 	}
70 
doLockAndInitHandle()71 	protected abstract void doLockAndInitHandle() throws LWJGLException;
doUnlock()72 	protected abstract void doUnlock() throws LWJGLException;
73 
lockAndGetHandle()74 	public final synchronized ByteBuffer lockAndGetHandle() throws LWJGLException {
75 		Thread this_thread = Thread.currentThread();
76 		while (locking_thread != null && locking_thread != this_thread) {
77 			try {
78 				wait();
79 			} catch (InterruptedException e) {
80 				LWJGLUtil.log("Interrupted while waiting for PeerInfo lock: " + e);
81 			}
82 		}
83 		if (lock_count == 0) {
84 			locking_thread = this_thread;
85 			doLockAndInitHandle();
86 		}
87 		lock_count++;
88 		return getHandle();
89 	}
90 
getHandle()91 	protected final ByteBuffer getHandle() {
92 		return handle;
93 	}
94 
destroy()95 	public void destroy() {
96 	}
97 }
98