SerialPort IO Locking - Comments

Steve McFarlin <[email protected]> Tue, 01 Jun 2004 20:44:23 -0700
Newsgroups gmane.comp.hardware.microcontrollers.tini
Message-ID <[email protected]>
This message is just to see what you would prefer between the two 
methods suggested. (please don't bash on me too hard)

Problem:
Multiple threads accessing a single serial port. We assume all threads 
have a reference to the serial port.

Requirement:
A locking mechanism to prevent 2 or more threads from performing IO at 
the same time.


Method 1:
Write a wrapper over the javax.comm.SerialPort class to add two methods


public synchronized Object getLock() {

	if(cls_Lock.bool_Locked) {
	   try {
		wait() ;
	   }
	   catch(Exception e) {
   		return null ;
	   }
	}

	cls_Lock.bool_Locked = true ;
	return cls_Lock ;
}

public synchronized void releaseLock(Object lock) {
	if(lock != cls_Lock)
		return ;
	
	cls_Lock.bool_Locked = false ;
	notifyAll() ;
}


// example code

Object obj = SerialPortWrapper.getLock() ;
... Do some IO
SerialPortWrapper.releaseLock(obj) ;



Method 2:
Synchronize on the InputStream and OutputStream of the 
javax.comm.SerialPort class.


OutputStream out = SerialPortRef.getOutputStream() ;
InputStream in = SerialPortRef.getInputStream() ;

...

synchronize(out) {
    synchronize(in) {
	
	...Do some IO
    }
}


Thanks,
Steve

_______________________________________________
TINI mailing list
TINI-6tN4nzCoH/[email protected]
To UNSUBSCRIBE, edit your profile, or see list archives:
http://lists.dalsemi.com/mailman/listinfo/tini