I agree abotu the wiki (in fact, the information there is somwhere between misleading and wrong), but what's vague about my email?
"Both the netconn and the socket API are thread-safe as long as you don't share
one netconn/socket between multiple threads (i.e. don't access it from more
than one thread at a time)."
What you want to do is sharing a connection between threads: one thread for reading, one thread for writing. That's not supported, simple as that.
Ok, I suppose your email makes sense when you don't read the wiki. It's kind of silly that the wiki uses the example of another thread closing a socket, whilest thread 1 is still using it for send/recv, which causes a problem - as yes, that would be a problem in any socket environment.
> By the looks of it, netconn_send goes
> through a message (either using a lock or a message queue), so I think it
> should be safe?
That's what the wiki talks about, I guess. However, while this *might* work for UDP, it's not something it's supposed to do. If it works, it does so "by accident".
So the send there would be happening on the TCP thread (as a result of the message), is that safe to happen while you are reading on your read/write thread anyway? It must be, as if your read/write thread does a send() followed immedietly by a recv(), it's going to be the same thing. But I understand what you're saying: it may work, but it's not designed to, so just don't do it.
> Also, how am I supposed to use the callback provided
> to netconn_new_with_proto_and_callback? Should I/Can I be calling
> netconn_recv in the thread context of the callback? If not, how can I
> abort
> a netconn_recv if, for example, I need to shut down the connection?
The callback is used for socket select(), for example. It gets called from tcpip_thread, so if you implement your own callback, it has to be thread-safe.
Using a select-like mechanism is usually the way to go with lwIP (instead of having different threads for TX/RX).
Ok. A little while ago I had an lwIP app that was written using the BSD socket layer, and this is exactly what I did. I had to modify the select() call to be able to pass in another object (representing a semaphore, really) that could be used to interrupt the select() call. I couldn't see any way of creating a socketpair() like object to interrupt the select() call. There is no built in way to do this with the BSD socket layer in lwIP, correct?
But for the netconn layer, I guess I can do my own thing. Is this (below)the correct way to do it (sorry, very vague pseudo-code, I don't have the source with me at the moment)? Once I understand this fully I'll write something up for the wiki page to clarify the whole situation.
Thanks
David
enum
{
READ_READY,
WRITE_READY
};
static mbox_type sMySelectMbox;
void netconn_callback(....) // always called in tcp thread context
{
if (RECV_PLUS) // or whatever it's called
{
mbox_put(sMySelectMbox, READ_READY);
}
}
void some_button_pressed() // could happen in any thread context, or interrupt context...
{
mbox_put(sMySelectMbox, WRITE_READ); // also pass the data somehow.
}
void my_send_recv_thread() // created with sys_thread_new
{
while(1)
{
msg = mbox_get(sMySelectMbox);
switch (msg)
{
case READ_READY:
pbuf = netconn_recv(....);
// do something with pbuf and release it
break;
case WRITE_READY:
// figure out what to send, store it in pbuf
netconn_send(..., pbuf);
break;
}
}
}
Simon
--
GMX DSL Doppel-Flat ab 19,99 Euro/mtl.! Jetzt mit
gratis Handy-Flat! http://portal.gmx.net/de/go/dsl