summaryrefslogtreecommitdiff
path: root/tags/host/0.8.1/src/org/reprap/comms/snap/CommsLock.java
blob: 5ab1037733ba577fd52b9793181bc8564b8230a6 (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.out.println("Warning: Calling unlock from an already unlocked state!");
		locked = false;
		notify();
		//System.out.println("Unlock: " + Thread.currentThread().getName() + " released");
	}
	
}