[gnutls-devel] Waiting for input data
Jouko Orava
jouko.orava at helsinki.fi
Tue Feb 5 17:49:27 CET 2013
Hello!
On Tue, 5 Feb 2013, Jaak Ristioja wrote:
> I want my thread to sleep (most of the time) until either (1) data
> becomes available or (2) another thread signals the thread to stop
> waiting for any data.
Have you simply set the socket descriptor to nonblocking?
fcntl(sd, F_SETFL, O_NONBLOCK);
At a quick look, GnuTLS should return GNUTLS_E_AGAIN if there is no data
pending on a nonblocking socket [so underlying recv() returns EAGAIN].
You can use select()/epoll()/etc. on the socket
descriptor (sd) to check if there might be data available
(gnutls_record_recv() may still return GNUTLS_E_AGAIN).
Another option is to keep the socket blocking, but use an empty-body
signal handler to interrupt the gnutls_record_recv(), i.e. via
pthread_kill(thread, signum);
or
pthread_sigqueue(thread, signum, value);
The delivery of the signal to the target thread will interrupt blocking
I/O calls; the body of the signal handler can be empty.
Note, however, that gnutls_record_recv() may return with data even if the
signal was fired, if sufficient data arrives in a big enough clump.
That means you must not rely on always noticing the signal (by receiving
an error from gnutls_record_recv()); you must have a secondary flag
(semaphore, or atomic flag) to indicate that there is extra processing
needed to be done.
I haven't done this with GnuTLS, but I'm very familiar with the underlying
low-level I/O in Linux.
Regards,
Jouko Orava
More information about the Gnutls-devel
mailing list