summaryrefslogtreecommitdiff
path: root/trunk/users/hoeken/gcode-host-old/src/org/reprap/comms/snap/CommsLock.java
blob: f8ea91d26ca936377a823dfb4ae8624a9bc1aca5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package org.reprap.comms.snap;

/**
 * A temporary class to allow locking of comms while waiting
 * for the asynchronous context delivery mechanism to be implemented
 */
public class CommsLock {

	/**
	 * indicates if the comms is locked or not
	 */
	private boolean locked = false;
	
	/**
	 * Lock the comms
	 */
	synchronized public void lock() {
		while(locked) {
			//System.out.println("Lock: " + Thread.currentThread().getName() + " waiting");
			try {
				wait();
			} catch (InterruptedException e) {
				// Ignore interrupts and continue
			}
		}
		locked = true;
		//System.out.println("Lock: " + Thread.currentThread().getName() + " acquired");
	}
	
	/**
	 * Unlock the comms
	 */
	synchronized public void unlock() {
		if (!locked)
			System.err.println("Warning: Calling unlock from an already unlocked state!");
		locked = false;
		notify();
		//System.out.println("Unlock: " + Thread.currentThread().getName() + " released");
	}
	
}