]> rtime.felk.cvut.cz Git - frescor/forb.git/commitdiff
forb: Set cancelation points in receiver thread
authorMichal Sojka <sojkam1@fel.cvut.cz>
Fri, 25 Feb 2011 07:17:34 +0000 (08:17 +0100)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Fri, 25 Feb 2011 07:17:34 +0000 (08:17 +0100)
This is to ensure that the thread is canceled only in a defined place
and no resources (like references to peer structures) remain in the system.

src/iop.c

index dc1aa23a4155925700c6991d0dab38316f7cc979..5a937aa344137a9afcb3f71e7c39a4325692e814 100644 (file)
--- a/src/iop.c
+++ b/src/iop.c
@@ -597,12 +597,15 @@ void *forb_iop_receiver_thread(void *arg)
        size_t len;
        forb_iop_message_header mh;
        bool header_received = false;
+       int oldstate;
 
        while (!port->finish) {
                if (c->rptr == c->wptr) {
                        /* The buffer is empty now - start writing from begining*/
                        FORB_CDR_buffer_reset(c, 0);
                }
+
+               pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
                /* TODO: If there is not enough space for reception,
                 * we should shift the already received data to the
                 * beginning of the buffer. */
@@ -616,10 +619,12 @@ void *forb_iop_receiver_thread(void *arg)
                c->wptr += rcvd;
                 c->wptr_last = c->wptr;
 
+               pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
+
                /* While there are some data in the buffer, process them. */
                while (FORB_CDR_data_size(c) > 0) {
                        len = FORB_CDR_data_size(c);
-                       /* Wait for and then process message header */
+                       /* Find and process message header (if there is any) */
                        if (!header_received) {
                                if (len >= forb_iop_MESSAGE_HEADER_SIZE) {
                                        if (c->rptr % 8 != 0) {
@@ -636,7 +641,7 @@ void *forb_iop_receiver_thread(void *arg)
                                }
                        }
                        
-                       /* Wait for and then process the message body */
+                       /* If the whole message is received, then process it */
                        if (header_received) {
                                if (len >= mh.message_size) {
                                        process_message(port, &mh, c);