Thread safety

13 views
Skip to first unread message

rol...@formreturn.com

unread,
Jan 28, 2015, 9:18:14 PM1/28/15
to jfreesan...@googlegroups.com
Hi James,

Is JFreeSane thread safe as far as different sessions go? Is there anything we need to be aware of if we are using multiple sessions at the same time?

I understand that it is not thread safe for the same session.

Thanks!

James Ring

unread,
Jan 28, 2015, 11:26:58 PM1/28/15
to rol...@formreturn.com, jfreesane-discuss
Hey Roland,

You should be able to use multiple devices in the one session, e.g.

SaneDevice a = session.getDevice("a");
SaneDevice b = session.getDevice("b");

The thing to realize is that each of these devices shares a pointer to
a single underlying session stream. You should take care to ensure
that calls to the various SaneDevice methods are serialized across all
devices in a given SaneSession.

Something I hadn't really thought about was acquiring images from two
scanners simultaneously. In theory, it's possible but JFreeSane makes
it a bit tricky: you need to call acquireImage on both devices, but
you can't call it on the second device until you're sure that
JFreeSane has sent the SANE_NET_START RPC for the first call. You can
use the ScanListener interface to know when this happens, but it's
tricky. At this point it's probably easier to just use two separate
SaneSessions. :)

If you do use more than one SaneSession, be aware that you may receive
a SANE_STATUS_DEVICE_BUSY error (see
http://www.sane-project.org/html/doc011.html#s4.2.7) if you try to do
multiple operations on a single scanner. I've never tested JFreeSane
(or SANE) like this, so I'd be happy to hear any feedback!

Hope this helps!

Regards,
James
> --
> You received this message because you are subscribed to the Google Groups
> "jfreesane-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to jfreesane-disc...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

rol...@formreturn.com

unread,
Jan 29, 2015, 12:40:07 AM1/29/15
to jfreesan...@googlegroups.com, rol...@formreturn.com
Got it... serializing calls on the same session to different devices sounds really unsafe. Will definitely be using different sessions, even if the devices are on the same server.

So device busy.. that was one thing I was wondering about. I assume that sane will only let one device be accessed at one time, especially in the case when sane is launched by inetd. I also hope it would allow you to access two different devices on the same sane server.

Main thing is there are no singletons or anything odd in jfreesane to cause issues with threading. :)

James Ring

unread,
Jan 29, 2015, 12:57:36 AM1/29/15
to rol...@formreturn.com, jfreesane-discuss
On Wed, Jan 28, 2015 at 9:40 PM, <rol...@formreturn.com> wrote:
> Got it... serializing calls on the same session to different devices sounds
> really unsafe. Will definitely be using different sessions, even if the
> devices are on the same server.

I don't immediately see any drawbacks to this approach. :)

> So device busy.. that was one thing I was wondering about. I assume that
> sane will only let one device be accessed at one time, especially in the
> case when sane is launched by inetd. I also hope it would allow you to
> access two different devices on the same sane server.

I'm not sure what the rules are. I would assume that it's up to the
individual backend drivers to implement the rules for when the BUSY
error is returned.

> Main thing is there are no singletons or anything odd in jfreesane to cause
> issues with threading. :)

Nothing like that, you should be fine having two separate SaneSessions going.

Regards,
James
Reply all
Reply to author
Forward
0 new messages