[PATCH] Cygwin 1.2.25+ support via gix in gwlib/conn.c (openssl thread callbacks)

Stipe Tolj <[email protected]>
Newsgroups gmane.comp.mobile.kannel.devel
Organization tolj.org system architecture
Message-ID <[email protected]>
Hi list,

as many of you may now, we support the Cygwin 1.2.x (POSIX.1 layer on Win32) 
natively, but claimed always to be "unstable" on this platform due to lacks in 
the pthreads implementation of Cygwin.

I tried to debug the "manged to lock the mutex twice" bug we face on higher load 
of the daemons. It seems that our callback scheme has an issue, presumably the 
gwthread_self() way we provide to the openssl core to identify the associated 
thread. Our mutex struct is based on pthreads itself. Now, what I did is to 
"exchange" our wrapped way by native pthread calls in the gwlib/conn.c layer for 
the openssl thread callback scheme, and voila it works without crashing.

Asking anyone interested in running on Win32 via Cygwin to test and vote.

Thanks,
Stipe

-------------------------------------------------------------------
Kölner Landstrasse 419
40589 DÃŒsseldorf, NRW, Germany

tolj.org system architecture      Kannel Software Foundation (KSF)
http://www.tolj.org/              http://www.kannel.org/

mailto:st_{at}_tolj.org           mailto:stolj_{at}_kannel.org
-------------------------------------------------------------------
cygwin-openssl-thread-callback.diff (text/plain, 2.3 KB)
### Eclipse Workspace Patch 1.0
#P gateway-cvs-head
Index: gwlib/conn.c
===================================================================
RCS file: /home/cvs/gateway/gwlib/conn.c,v
retrieving revision 1.86
diff -u -r1.86 conn.c
--- gwlib/conn.c	9 Jan 2008 20:06:55 -0000	1.86
+++ gwlib/conn.c	18 Jul 2008 13:28:03 -0000
@@ -1237,43 +1237,57 @@
 }
 */
 
-static Mutex **ssl_static_locks = NULL;
+static pthread_mutex_t *ssl_static_locks = NULL;
+static int ssl_lock_num_locks = 0;
 
 /* the call-back function for the openssl crypto thread locking */
 static void openssl_locking_function(int mode, int n, const char *file, int line)
 {
-    if (mode & CRYPTO_LOCK)
-        mutex_lock(ssl_static_locks[n-1]);
-    else
-        mutex_unlock(ssl_static_locks[n-1]);
+    if (n < ssl_lock_num_locks) {
+        if (mode & CRYPTO_LOCK) {
+            pthread_mutex_lock(&(ssl_static_locks[n]));
+        } else {
+            pthread_mutex_unlock(&(ssl_static_locks[n]));
+        }
+    }
+}
+
+static unsigned long openssl_thread_id(void)
+{
+    return (unsigned long) pthread_self();
 }
 
 void openssl_init_locks(void)
 {
-    int c, maxlocks = CRYPTO_num_locks();
+    int c;
+    
+    ssl_lock_num_locks = CRYPTO_num_locks();
 
     gw_assert(ssl_static_locks == NULL);
 
-    ssl_static_locks = gw_malloc(sizeof(Mutex *) * maxlocks);
-    for (c = 0; c < maxlocks; c++)
-        ssl_static_locks[c] = mutex_create();
+    ssl_static_locks = gw_malloc(sizeof(pthread_mutex_t) * ssl_lock_num_locks);
+    for (c = 0; c < ssl_lock_num_locks; c++) {
+        pthread_mutex_init(&(ssl_static_locks[c]), NULL);
+    }
 
     /* after the mutexes have been created, apply the call-back to it */
+    CRYPTO_set_id_callback(openssl_thread_id);
     CRYPTO_set_locking_callback(openssl_locking_function);
-    CRYPTO_set_id_callback((CRYPTO_CALLBACK_PTR)gwthread_self);
 }
 
 void openssl_shutdown_locks(void)
 {
-    int c, maxlocks = CRYPTO_num_locks();
+    int c;
 
     gw_assert(ssl_static_locks != NULL);
 
     /* remove call-back from the locks */
     CRYPTO_set_locking_callback(NULL);
+    CRYPTO_set_id_callback(NULL);
 
-    for (c = 0; c < maxlocks; c++) 
-        mutex_destroy(ssl_static_locks[c]);
+    for (c = 0; c < ssl_lock_num_locks; c++) {
+        pthread_mutex_destroy(&(ssl_static_locks[c]));
+    }
 
     gw_free(ssl_static_locks);
     ssl_static_locks = NULL;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.