tpm
YaBB Newbies
Offline
Posts: 1
Serial class problem?
Dec 12th , 2008, 3:09am
I've ported Processing 1.0.1 to opensolaris, and have been using it with XBee receivers. It was fairly easy to get things to build and work - I used the platform Java implementation, and scooped up JOGL and librxtx from the usual places. librxtx was actually the most work .. but that's another story. I'm impressed - this is a cool system you guys have built. One particularly tricky problem I had seems to be with the Serial class - I could see characters being delivered to the buffer, but every time my applet went to look for them, there was nothing there. After tearing my hair out a bit, I did the usual println thing, and eventually discovered that while the serialEvent method was being called repeatedly delivering data to the buffer, the available() method only ever returned zero to its caller - until, of course, I added a println in *that* method, and everything started working. So, given I'm trying all this on a four core AMD box, and both opensolaris and Sun's Java are good at exposing concurrency to applications, I began to wonder if there's a race condition problem? Specifically, the serialEvent method manipulates the buffer array and bufferLast and bufferIndex under 'synchronized(buffer)', while available() uses no synchronization whatsoever. Thus available() can observe the bufferLast and bufferIndex variables in an inconsistent state. Then I took *all* the println's away, and simply wrapped the body of the available() method in synchronized(buffer) and everything kept on working. Maybe this is using a sledgehammer to crack a nut, but my XBee experiments are working just fine now. If this is indeed the problem, I wonder if there's a better/more elegant/more efficient way to ensure that the buffer and its indices are kept in a consistent state? Comments?