clausecker / nfc

Go bindings for the libnfc
https://pkg.go.dev/github.com/clausecker/nfc/v2
GNU Lesser General Public License v3.0
101 stars 20 forks source link

thread safety? #13

Open draeron opened 3 months ago

draeron commented 3 months ago

This is more of support question than an issue, but how would you manage multithreading with libnfc? Even with mutexes it seems the Target struct can't be reused in a different thread (ex: InitiatorTargetIsPresent return err Invalid Argument).

clausecker commented 3 months ago

Quite honestly: I don't. This library is likely to explode horribly in your face if you use it concurrently.

draeron commented 3 months ago

i'm trying to create a network layer over the lib and it seems the Target/handle become invalid when changing context...

clausecker commented 3 months ago

Yes, that's very likely. The library is designed to only have a single global context for the application. I made this simplification as there seemed no point in having more than one context.

If you want to extend the code base to support multiple contexts, feel free to submit patches for review.

draeron commented 3 months ago

honestly i woudn't know where to start... but I did manage to find a way to make polling for card presence workable by recalling InitiatorSelectPassiveTarget to get a new target instance and using https://github.com/golang-design/mainthread as a way to keep problem from appearing. So far so good...