[gmime] Got rid of the need for a msgid mutex lock



commit 254c3c023f9f7167028b3277bccb54e8033f9825
Author: Jeffrey Stedfast <jestedfa microsoft com>
Date:   Tue Feb 7 15:55:34 2017 -0500

    Got rid of the need for a msgid mutex lock

 gmime/gmime-common.c    |   33 +++++++++++++++++++++++++++++++++
 gmime/gmime-common.h    |    6 ++++++
 gmime/gmime-multipart.c |   35 +----------------------------------
 gmime/gmime-utils.c     |   45 +++++++++++++++++++++++++++++----------------
 gmime/gmime.c           |   33 ---------------------------------
 5 files changed, 69 insertions(+), 83 deletions(-)
---
diff --git a/gmime/gmime-common.c b/gmime/gmime-common.c
index f4aa180..5de50e6 100644
--- a/gmime/gmime-common.c
+++ b/gmime/gmime-common.c
@@ -28,6 +28,39 @@
 #include "gmime-table-private.h"
 #include "gmime-common.h"
 
+void
+g_mime_read_random_pool (unsigned char *buffer, size_t bytes)
+{
+#ifdef __unix__
+       size_t nread = 0;
+       ssize_t n;
+       int fd;
+       
+       if ((fd = open ("/dev/urandom", O_RDONLY)) == -1) {
+               if ((fd = open ("/dev/random", O_RDONLY)) == -1)
+                       return;
+       }
+       
+       do {
+               do {
+                       n = read (fd, (char *) buffer + nread, bytes - nread);
+               } while (n == -1 && errno == EINTR);
+               
+               if (n == -1 || n == 0)
+                       break;
+               
+               nread += n;
+       } while (nread < bytes);
+       
+       close (fd);
+#else
+       size_t i;
+       
+       for (i = 0; i < bytes; i++)
+               buffer[i] = (unsigned char) (rand () % 256);
+#endif
+}
+
 #ifndef g_tolower
 #define g_tolower(x) (((x) >= 'A' && (x) <= 'Z') ? (x) - 'A' + 'a' : (x))
 #endif
diff --git a/gmime/gmime-common.h b/gmime/gmime-common.h
index 14a9d76..9b7b101 100644
--- a/gmime/gmime-common.h
+++ b/gmime/gmime-common.h
@@ -22,10 +22,16 @@
 #ifndef __GMIME_COMMON_H__
 #define __GMIME_COMMON_H__
 
+#include <sys/types.h>
+#include <string.h>
+#include <stdlib.h>
+
 #include <glib.h>
 
 G_BEGIN_DECLS
 
+G_GNUC_INTERNAL void g_mime_read_random_pool (unsigned char *buffer, size_t bytes);
+
 G_GNUC_INTERNAL int g_mime_strcase_equal (gconstpointer v, gconstpointer v2);
 
 G_GNUC_INTERNAL guint g_mime_strcase_hash (gconstpointer key);
diff --git a/gmime/gmime-multipart.c b/gmime/gmime-multipart.c
index 7e1c446..3ac2dc3 100644
--- a/gmime/gmime-multipart.c
+++ b/gmime/gmime-multipart.c
@@ -694,39 +694,6 @@ g_mime_multipart_get_count (GMimeMultipart *multipart)
 
 
 static void
-read_random_pool (unsigned char *buffer, size_t bytes)
-{
-#ifdef __unix__
-       size_t nread = 0;
-       ssize_t n;
-       int fd;
-       
-       if ((fd = open ("/dev/urandom", O_RDONLY)) == -1) {
-               if ((fd = open ("/dev/random", O_RDONLY)) == -1)
-                       return;
-       }
-       
-       do {
-               do {
-                       n = read (fd, (char *) buffer + nread, bytes - nread);
-               } while (n == -1 && errno == EINTR);
-               
-               if (n == -1 || n == 0)
-                       break;
-               
-               nread += n;
-       } while (nread < bytes);
-       
-       close (fd);
-#else
-       size_t i;
-       
-       for (i = 0; i < bytes; i++)
-               buffer[i] = (unsigned char) (rand () % 256);
-#endif
-}
-
-static void
 multipart_set_boundary (GMimeMultipart *multipart, const char *boundary)
 {
        char bbuf[35];
@@ -737,7 +704,7 @@ multipart_set_boundary (GMimeMultipart *multipart, const char *boundary)
                guint32 save = 0;
                int state = 0;
                
-               read_random_pool (digest, 16);
+               g_mime_read_random_pool (digest, 16);
                
                strcpy (bbuf, "=-");
                p = (unsigned char *) bbuf + 2;
diff --git a/gmime/gmime-utils.c b/gmime/gmime-utils.c
index b35d954..0e553c6 100644
--- a/gmime/gmime-utils.c
+++ b/gmime/gmime-utils.c
@@ -51,6 +51,7 @@
 #include <errno.h>
 
 #include "gmime-utils.h"
+#include "gmime-common.h"
 #include "gmime-table-private.h"
 #include "gmime-parse-utils.h"
 #include "gmime-part.h"
@@ -77,16 +78,6 @@
  * and encodings.
  **/
 
-#ifdef G_THREADS_ENABLED
-extern void _g_mime_msgid_unlock (void);
-extern void _g_mime_msgid_lock (void);
-#define MSGID_UNLOCK() _g_mime_msgid_unlock ()
-#define MSGID_LOCK()   _g_mime_msgid_lock ()
-#else
-#define MSGID_UNLOCK()
-#define MSGID_LOCK()
-#endif
-
 #define GMIME_FOLD_PREENCODED  (GMIME_FOLD_LEN / 2)
 
 /* date parser macros */
@@ -798,10 +789,14 @@ g_mime_utils_header_decode_date (const char *str, int *tz_offset)
 char *
 g_mime_utils_generate_message_id (const char *fqdn)
 {
+       const char base36[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        static unsigned long int count = 0;
        const char *hostname = NULL;
+       unsigned char block[8];
+       unsigned long value;
        char *name = NULL;
-       char *msgid;
+       GString *msgid;
+       int i;
        
        if (!fqdn) {
 #ifdef HAVE_UTSNAME_DOMAINNAME
@@ -865,14 +860,32 @@ g_mime_utils_generate_message_id (const char *fqdn)
                fqdn = name != NULL ? name : (hostname[0] ? hostname : "localhost.localdomain");
        }
        
-       MSGID_LOCK ();
-       msgid = g_strdup_printf ("%lu.%lu.%lu@%s", (unsigned long int) time (NULL),
-                                (unsigned long int) getpid (), count++, fqdn);
-       MSGID_UNLOCK ();
+       value = (unsigned long) time (NULL);
+       g_mime_read_random_pool (block, 8);
+       msgid = g_string_new ("");
+       
+       do {
+               g_string_append_c (msgid, base36[(int) (value % 36)]);
+               value /= 36;
+       } while (value != 0);
+       
+       g_string_append_c (msgid, '.');
+       
+       value = 0;
+       for (i = 0; i < 8; i++)
+               value = (value << 8) | block[i];
+       
+       do {
+               g_string_append_c (msgid, base36[(int) (value % 36)]);
+               value /= 36;
+       } while (value != 0);
+       
+       g_string_append_c (msgid, '@');
+       g_string_append (msgid, fqdn);
        
        g_free (name);
        
-       return msgid;
+       return g_string_free (msgid, FALSE);
 }
 
 static char *
diff --git a/gmime/gmime.c b/gmime/gmime.c
index 6028b1e..e33a9b5 100644
--- a/gmime/gmime.c
+++ b/gmime/gmime.c
@@ -51,9 +51,6 @@
 extern void g_mime_iconv_utils_shutdown (void);
 extern void g_mime_iconv_utils_init (void);
 
-extern void _g_mime_msgid_unlock (void);
-extern void _g_mime_msgid_lock (void);
-
 GQuark gmime_gpgme_error_quark;
 GQuark gmime_error_quark;
 
@@ -63,8 +60,6 @@ const guint gmime_micro_version = GMIME_MICRO_VERSION;
 const guint gmime_interface_age = GMIME_INTERFACE_AGE;
 const guint gmime_binary_age = GMIME_BINARY_AGE;
 
-G_LOCK_DEFINE_STATIC (msgid);
-
 static unsigned int initialized = 0;
 
 
@@ -120,10 +115,6 @@ g_mime_init (void)
        g_type_init ();
 #endif
        
-#ifdef G_THREADS_ENABLED
-       g_mutex_init (&G_LOCK_NAME (msgid));
-#endif
-       
        g_mime_parser_options_init ();
        g_mime_charset_map_init ();
        g_mime_iconv_utils_init ();
@@ -216,28 +207,4 @@ g_mime_shutdown (void)
        g_mime_charset_map_shutdown ();
        g_mime_iconv_utils_shutdown ();
        g_mime_iconv_shutdown ();
-       
-#ifdef G_THREADS_ENABLED
-       if (glib_check_version (2, 37, 4) == NULL) {
-               /* The implementation of g_mutex_clear() prior
-                * to glib 2.37.4 did not properly reset the
-                * internal mutex pointer to NULL, so re-initializing
-                * GMime would not properly re-initialize the mutexes.
-                **/
-               g_mutex_clear (&G_LOCK_NAME (msgid));
-       }
-#endif
-}
-
-
-void
-_g_mime_msgid_unlock (void)
-{
-       return G_UNLOCK (msgid);
-}
-
-void
-_g_mime_msgid_lock (void)
-{
-       return G_LOCK (msgid);
 }


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