Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

thread safety? #13

Open
draeron opened this issue Jun 7, 2024 · 8 comments
Open

thread safety? #13

draeron opened this issue Jun 7, 2024 · 8 comments

Comments

@draeron
Copy link

draeron commented Jun 7, 2024

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
Copy link
Owner

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

@draeron
Copy link
Author

draeron commented Jun 7, 2024

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

@clausecker
Copy link
Owner

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
Copy link
Author

draeron commented Jun 7, 2024

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...

@Limezy
Copy link

Limezy commented Nov 30, 2024

@draeron may I know what is your use case ? Do you use more than one nfc reader handled by the same app ?

@draeron
Copy link
Author

draeron commented Dec 2, 2024

no, I just wanted to create a webservice over a card reader, seems to me the libNFC has strange behavior when being accessed from a different thread / go routine.

in the end I dropped the idea and bundle all logic inside the same process.

I wish there was some pure go layer for NFC in something like periph.io but what I could find wasn't very reliable.

@Limezy
Copy link

Limezy commented Dec 2, 2024

@draeron understood. I have also struggled a lot to have a NFC reader working well with pure Go, and had to fallback using libNFC through this library. Many thanks @clausecker by the way !

For what it's worth (not a lot), I'm also creating a webservice over a card reader.
I managed (with some generative AI help) to get something that looks quite reliable here https://github.com/Cartophone/cartophone-server/tree/main. You can have a look at my dirty code there.

@clausecker
Copy link
Owner

I can look into making a v3 of the library that exposes the context structure of libnfc, allowing you to use it from more than one thread at once. But alas, I don't really do much with NFC these days, so it may take some time for me to get around to doing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants