ecpg threading vs win32
| От | Magnus Hagander |
|---|---|
| Тема | ecpg threading vs win32 |
| Дата | |
| Msg-id | 45FC6919.1090702@hagander.net обсуждение исходный текст |
| Ответы |
Re: ecpg threading vs win32
|
| Список | pgsql-patches |
This patch replaces the pthreads code in ecpg with native win32 threads,
in order to make it threadsafe. The idea is not to have to download the
non-standard pthreads library on windows.
Does it seem like it should be doing the right thing? Does somebody have
a good test-case where ecpg breaks when not built thread-safe? (which
would then also break when built thread-safe with a broken implementation)
//Magnus
Index: connect.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/interfaces/ecpg/ecpglib/connect.c,v
retrieving revision 1.39
diff -u -r1.39 connect.c
--- connect.c 12 Jan 2007 10:00:12 -0000 1.39
+++ connect.c 14 Mar 2007 12:47:48 -0000
@@ -4,7 +4,11 @@
#include "postgres_fe.h"
#ifdef ENABLE_THREAD_SAFETY
+#ifndef WIN32
#include <pthread.h>
+#else
+#include "ecpg-pthread-win32.h"
+#endif
#endif
#include "ecpgtype.h"
#include "ecpglib.h"
@@ -13,9 +17,14 @@
#include "sqlca.h"
#ifdef ENABLE_THREAD_SAFETY
+#ifndef WIN32
static pthread_mutex_t connections_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_key_t actual_connection_key;
static pthread_once_t actual_connection_key_once = PTHREAD_ONCE_INIT;
+#else
+static HANDLE connections_mutex = INVALID_HANDLE_VALUE;
+static DWORD actual_connection_key;
+#endif /* WIN32 */
#endif
static struct connection *actual_connection = NULL;
static struct connection *all_connections = NULL;
@@ -30,7 +39,13 @@
void
ecpg_pthreads_init(void)
{
+#ifndef WIN32
pthread_once(&actual_connection_key_once, ecpg_actual_connection_init);
+#else
+ static long has_run = 0;
+ if (InterlockedCompareExchange(&has_run, 1, 0) == 0)
+ ecpg_actual_connection_init();
+#endif
}
#endif
Index: misc.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/interfaces/ecpg/ecpglib/misc.c,v
retrieving revision 1.34
diff -u -r1.34 misc.c
--- misc.c 12 Jan 2007 10:00:13 -0000 1.34
+++ misc.c 14 Mar 2007 12:48:03 -0000
@@ -6,7 +6,11 @@
#include <limits.h>
#include <unistd.h>
#ifdef ENABLE_THREAD_SAFETY
+#ifndef WIN32
#include <pthread.h>
+#else
+#include "ecpg-pthread-win32.h"
+#endif
#endif
#include "ecpgtype.h"
#include "ecpglib.h"
@@ -58,9 +62,13 @@
};
#ifdef ENABLE_THREAD_SAFETY
+#ifndef WIN32
static pthread_key_t sqlca_key;
static pthread_once_t sqlca_key_once = PTHREAD_ONCE_INIT;
#else
+static DWORD sqlca_key;
+#endif
+#else
static struct sqlca_t sqlca =
{
{
@@ -90,8 +98,13 @@
#endif
#ifdef ENABLE_THREAD_SAFETY
+#ifndef WIN32
static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t debug_init_mutex = PTHREAD_MUTEX_INITIALIZER;
+#else
+static HANDLE debug_mutex = INVALID_HANDLE_VALUE;
+static HANDLE debug_init_mutex = INVALID_HANDLE_VALUE;
+#endif /* WIN32 */
#endif
static int simple_debug = 0;
static FILE *debugstream = NULL;
@@ -138,8 +151,13 @@
{
#ifdef ENABLE_THREAD_SAFETY
struct sqlca_t *sqlca;
-
+#ifdef WIN32
+ static long has_run = 0;
+ if (InterlockedCompareExchange(&has_run, 1, 0) == 0)
+ ecpg_sqlca_key_init();
+#else
pthread_once(&sqlca_key_once, ecpg_sqlca_key_init);
+#endif
sqlca = pthread_getspecific(sqlca_key);
if (sqlca == NULL)
/* $PostgreSQL$ */
/*
* pthread mapping macros for win32 native thread implementation
*/
#ifndef _ECPG_PTHREAD_WIN32_H
#define _ECPG_PTHREAD_WIN32_H
#define pthread_mutex_lock(x) do { \
if (*x == INVALID_HANDLE_VALUE) \
*x = CreateMutex(NULL, FALSE, NULL); \
WaitForSingleObject(*x, INFINITE); \
} while (0);
#define pthread_mutex_unlock(x) ReleaseMutex(*x)
#define pthread_getspecific(x) TlsGetValue(x)
#define pthread_setspecific(x,y) TlsSetValue(x,y)
#define pthread_key_create(x,y) *x = TlsAlloc();
#endif
В списке pgsql-patches по дате отправления: