[evolution-data-server/gnome-3-22] Bug 778265 - Serialize access to underlying NNTP stream



commit 9bef14681db101ad42d6c2c72abd9a32d6d85ab3
Author: Milan Crha <mcrha redhat com>
Date:   Mon Feb 13 10:15:42 2017 +0100

    Bug 778265 - Serialize access to underlying NNTP stream

 camel/providers/nntp/camel-nntp-stream.c |   69 +++++++++++++++++++++++++----
 camel/providers/nntp/camel-nntp-stream.h |    2 +
 2 files changed, 61 insertions(+), 10 deletions(-)
---
diff --git a/camel/providers/nntp/camel-nntp-stream.c b/camel/providers/nntp/camel-nntp-stream.c
index eb39870..f265717 100644
--- a/camel/providers/nntp/camel-nntp-stream.c
+++ b/camel/providers/nntp/camel-nntp-stream.c
@@ -60,6 +60,8 @@ nntp_stream_finalize (GObject *object)
        g_free (stream->buf);
        g_free (stream->linebuf);
 
+       g_rec_mutex_clear (&stream->lock);
+
        /* Chain up to parent's finalize () method. */
        G_OBJECT_CLASS (camel_nntp_stream_parent_class)->finalize (object);
 }
@@ -111,8 +113,12 @@ nntp_stream_read (CamelStream *stream,
        guchar *p, *e, c;
        gint state;
 
-       if (is->mode != CAMEL_NNTP_STREAM_DATA || n == 0)
+       g_rec_mutex_lock (&is->lock);
+
+       if (is->mode != CAMEL_NNTP_STREAM_DATA || n == 0) {
+               g_rec_mutex_unlock (&is->lock);
                return 0;
+       }
 
        o = buffer;
        oe = buffer + n;
@@ -127,8 +133,10 @@ nntp_stream_read (CamelStream *stream,
        case 0:         /* start of line, always read at least 3 chars */
                while (e - p < 3) {
                        is->ptr = p;
-                       if (nntp_stream_fill (is, cancellable, error) == -1)
+                       if (nntp_stream_fill (is, cancellable, error) == -1) {
+                               g_rec_mutex_unlock (&is->lock);
                                return -1;
+                       }
                        p = is->ptr;
                        e = is->end;
                }
@@ -137,6 +145,7 @@ nntp_stream_read (CamelStream *stream,
                                is->ptr = p + 3;
                                is->mode = CAMEL_NNTP_STREAM_EOD;
                                is->state = 0;
+                               g_rec_mutex_unlock (&is->lock);
                                return o - buffer;
                        }
                        p++;
@@ -150,8 +159,10 @@ nntp_stream_read (CamelStream *stream,
                                /* end of input sentinal check */
                                if (p > e) {
                                        is->ptr = e;
-                                       if (nntp_stream_fill (is, cancellable, error) == -1)
+                                       if (nntp_stream_fill (is, cancellable, error) == -1) {
+                                               g_rec_mutex_unlock (&is->lock);
                                                return -1;
+                                       }
                                        p = is->ptr;
                                        e = is->end;
                                } else {
@@ -169,6 +180,8 @@ nntp_stream_read (CamelStream *stream,
        is->ptr = p;
        is->state = state;
 
+       g_rec_mutex_unlock (&is->lock);
+
        return o - buffer;
 }
 
@@ -180,8 +193,13 @@ nntp_stream_write (CamelStream *stream,
                    GError **error)
 {
        CamelNNTPStream *is = (CamelNNTPStream *) stream;
+       gssize written;
+
+       g_rec_mutex_lock (&is->lock);
+       written = camel_stream_write (is->source, buffer, n, cancellable, error);
+       g_rec_mutex_unlock (&is->lock);
 
-       return camel_stream_write (is->source, buffer, n, cancellable, error);
+       return written;
 }
 
 static gint
@@ -241,6 +259,8 @@ camel_nntp_stream_init (CamelNNTPStream *is)
 
        is->state = 0;
        is->mode = CAMEL_NNTP_STREAM_LINE;
+
+       g_rec_mutex_init (&is->lock);
 }
 
 CamelNNTPStream *
@@ -270,7 +290,10 @@ camel_nntp_stream_line (CamelNNTPStream *is,
        g_return_val_if_fail (data != NULL, -1);
        g_return_val_if_fail (len != NULL, -1);
 
+       g_rec_mutex_lock (&is->lock);
+
        if (is->mode == CAMEL_NNTP_STREAM_EOD) {
+               g_rec_mutex_unlock (&is->lock);
                *data = is->linebuf;
                *len = 0;
                return 0;
@@ -287,8 +310,10 @@ camel_nntp_stream_line (CamelNNTPStream *is,
                /* need at least 3 chars in buffer */
                while (e - p < 3) {
                        is->ptr = p;
-                       if (nntp_stream_fill (is, cancellable, error) == -1)
+                       if (nntp_stream_fill (is, cancellable, error) == -1) {
+                               g_rec_mutex_unlock (&is->lock);
                                return -1;
+                       }
                        p = is->ptr;
                        e = is->end;
                }
@@ -304,6 +329,8 @@ camel_nntp_stream_line (CamelNNTPStream *is,
 
                                dd (printf ("NNTP_STREAM_LINE (END)\n"));
 
+                               g_rec_mutex_unlock (&is->lock);
+
                                return 0;
                        }
                        p++;
@@ -317,8 +344,10 @@ camel_nntp_stream_line (CamelNNTPStream *is,
                                /* sentinal? */
                                if (p> e) {
                                        is->ptr = e;
-                                       if (nntp_stream_fill (is, cancellable, error) == -1)
+                                       if (nntp_stream_fill (is, cancellable, error) == -1) {
+                                               g_rec_mutex_unlock (&is->lock);
                                                return -1;
+                                       }
                                        p = is->ptr;
                                        e = is->end;
                                } else {
@@ -327,6 +356,8 @@ camel_nntp_stream_line (CamelNNTPStream *is,
                                        *len = o - is->linebuf;
                                        *o = 0;
 
+                                       g_rec_mutex_unlock (&is->lock);
+
                                        dd (printf ("NNTP_STREAM_LINE (%d): '%s'\n", *len, *data));
 
                                        return 1;
@@ -344,6 +375,8 @@ camel_nntp_stream_line (CamelNNTPStream *is,
                oe = is->lineend - 1;
                o = is->linebuf + oldlen;
        }
+
+       g_rec_mutex_unlock (&is->lock);
 }
 
 /* returns -1 on error, 0 if last lot of data, >0 if more remaining */
@@ -363,11 +396,15 @@ camel_nntp_stream_gets (CamelNNTPStream *is,
 
        *len = 0;
 
+       g_rec_mutex_lock (&is->lock);
+
        max = is->end - is->ptr;
        if (max == 0) {
                max = nntp_stream_fill (is, cancellable, error);
-               if (max <= 0)
+               if (max <= 0) {
+                       g_rec_mutex_unlock (&is->lock);
                        return max;
+               }
        }
 
        *start = is->ptr;
@@ -378,6 +415,8 @@ camel_nntp_stream_gets (CamelNNTPStream *is,
        *len = max;
        is->ptr += max;
 
+       g_rec_mutex_unlock (&is->lock);
+
        return end == NULL ? 1 : 0;
 }
 
@@ -407,10 +446,15 @@ camel_nntp_stream_getd (CamelNNTPStream *is,
 
        *len = 0;
 
-       if (is->mode == CAMEL_NNTP_STREAM_EOD)
+       g_rec_mutex_lock (&is->lock);
+
+       if (is->mode == CAMEL_NNTP_STREAM_EOD) {
+               g_rec_mutex_unlock (&is->lock);
                return 0;
+       }
 
        if (is->mode == CAMEL_NNTP_STREAM_LINE) {
+               g_rec_mutex_unlock (&is->lock);
                g_warning ("nntp_stream reading data in line mode\n");
                return 0;
        }
@@ -421,8 +465,10 @@ camel_nntp_stream_getd (CamelNNTPStream *is,
 
        while (e - p < 3) {
                is->ptr = p;
-               if (nntp_stream_fill (is, cancellable, error) == -1)
+               if (nntp_stream_fill (is, cancellable, error) == -1) {
+                       g_rec_mutex_unlock (&is->lock);
                        return -1;
+               }
                p = is->ptr;
                e = is->end;
        }
@@ -440,6 +486,7 @@ camel_nntp_stream_getd (CamelNNTPStream *is,
                                        *start = s;
                                        is->mode = CAMEL_NNTP_STREAM_EOD;
                                        is->state = 0;
+                                       g_rec_mutex_unlock (&is->lock);
 
                                        return 0;
                                }
@@ -454,6 +501,7 @@ camel_nntp_stream_getd (CamelNNTPStream *is,
                                        *len = p-s;
                                        *start = s;
                                        is->state = 1;
+                                       g_rec_mutex_unlock (&is->lock);
 
                                        return 1;
                                }
@@ -479,6 +527,7 @@ camel_nntp_stream_getd (CamelNNTPStream *is,
        *len = p-s;
        *start = s;
 
+       g_rec_mutex_unlock (&is->lock);
+
        return 1;
 }
-
diff --git a/camel/providers/nntp/camel-nntp-stream.h b/camel/providers/nntp/camel-nntp-stream.h
index a54ca16..b1296dd 100644
--- a/camel/providers/nntp/camel-nntp-stream.h
+++ b/camel/providers/nntp/camel-nntp-stream.h
@@ -61,6 +61,8 @@ struct _CamelNNTPStream {
 
        guchar *buf, *ptr, *end;
        guchar *linebuf, *lineptr, *lineend;
+
+       GRecMutex lock;
 };
 
 struct _CamelNNTPStreamClass {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]