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

Enable coap_async_trigger() to be called from a different thread #800

Open
franzhollerer opened this issue Dec 20, 2021 · 3 comments
Open

Comments

@franzhollerer
Copy link

Related: Issue #794, PR #795

It would be great if coap_async_trigger() could be called from a thread different to that one running the libcoap server code.

Use case: Separate response, where it takes some time to prepare the data for the response.

  1. Request handler:
  • registers an async operation with coap_register_async(), delay = 0
  • starts a worker thread to prepare the data required for the response
  • send an emtpy ACK
  1. Worker thread:
  • prepares the data for the response
  • calls coap_async_trigger() to trigger the coap response
  1. Request handler:
  • Sends the response
@mrdeep1
Copy link
Collaborator

mrdeep1 commented Dec 20, 2021

libcoap currently has no multi-thread support / protection, but is on a TODO List. No planned dates.

coap_async_trigger() does (multi-thread unsafe) update the coap_context_t structure and does not itself call the request_handler() but relies on (in the epoll enabled case) a timerfd triggering to set the read available on the coap_fd (determined by coap_context_get_coap_fd)).

When the data is updated by the worker thread, that too will need to be multi-thread access safe.

So currently, this design would not safely work and an alternative needs to be found.

@franzhollerer
Copy link
Author

Well, it's a nice to have feature request. I am totally aware that this is nothing for the near future. Here some personal notes on this topic. Maybe it gives some interesting input for future features. Or you consider it to completely miss the point - which is fine for me either.

I see no need for libcoap to be completely thread safe. There might be some performance considerations, but for me it is absolutely reasonable to have the coap context assigned to a single thread. It is a very good design choice.

Having said that, there are a few functions I would like to be able to invoke from a different thread. The coap_async_trigger() is one of them.

I could consider some kind of event handling, which allows me to queue in events. In the most generic case libcoap could provide an API which allows to queue in a function and its parameter, which is then executed in the context of the coap thread.

@mrdeep1
Copy link
Collaborator

mrdeep1 commented Nov 30, 2023

libcoap (if compiled appropriately) is now multi-thread safe in the develop branch

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

2 participants