Implement error checking for pthreads calls in thread-safe mode. They really

should always succeed, but in the likely event of a failure we would
previously fall through *without locking* - the new code will exit(1).

Printing the error message on stderr will not work for all applications, but
it's better than nothing at all - and our API doesn't provide a way to return
the error to the caller.
This commit is contained in:
Magnus Hagander 2008-05-16 18:30:53 +00:00
parent 0ff81a525e
commit 1d89026788
4 changed files with 56 additions and 17 deletions

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.357 2008/03/31 02:43:14 tgl Exp $ * $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.358 2008/05/16 18:30:53 mha Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -3835,14 +3835,23 @@ default_threadlock(int acquire)
while (InterlockedExchange(&mutex_initlock, 1) == 1) while (InterlockedExchange(&mutex_initlock, 1) == 1)
/* loop, another thread own the lock */ ; /* loop, another thread own the lock */ ;
if (singlethread_lock == NULL) if (singlethread_lock == NULL)
pthread_mutex_init(&singlethread_lock, NULL); {
if (pthread_mutex_init(&singlethread_lock, NULL))
PGTHREAD_ERROR("failed to initialize mutex");
}
InterlockedExchange(&mutex_initlock, 0); InterlockedExchange(&mutex_initlock, 0);
} }
#endif #endif
if (acquire) if (acquire)
pthread_mutex_lock(&singlethread_lock); {
if (pthread_mutex_lock(&singlethread_lock))
PGTHREAD_ERROR("failed to lock mutex");
}
else else
pthread_mutex_unlock(&singlethread_lock); {
if (pthread_mutex_unlock(&singlethread_lock))
PGTHREAD_ERROR("failed to unlock mutex");
}
#endif #endif
} }

View File

@ -11,7 +11,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.104 2008/03/31 02:43:14 tgl Exp $ * $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.105 2008/05/16 18:30:53 mha Exp $
* *
* NOTES * NOTES
* [ Most of these notes are wrong/obsolete, but perhaps not all ] * [ Most of these notes are wrong/obsolete, but perhaps not all ]
@ -796,12 +796,21 @@ static void
pq_lockingcallback(int mode, int n, const char *file, int line) pq_lockingcallback(int mode, int n, const char *file, int line)
{ {
if (mode & CRYPTO_LOCK) if (mode & CRYPTO_LOCK)
pthread_mutex_lock(&pq_lockarray[n]); {
if (pthread_mutex_lock(&pq_lockarray[n]))
PGTHREAD_ERROR("failed to lock mutex");
}
else else
pthread_mutex_unlock(&pq_lockarray[n]); {
if (pthread_mutex_unlock(&pq_lockarray[n]))
PGTHREAD_ERROR("failed to unlock mutex");
}
} }
#endif /* ENABLE_THREAD_SAFETY */ #endif /* ENABLE_THREAD_SAFETY */
/*
* Also see similar code in fe-connect.c, default_threadlock()
*/
static int static int
init_ssl_system(PGconn *conn) init_ssl_system(PGconn *conn)
{ {
@ -817,11 +826,15 @@ init_ssl_system(PGconn *conn)
while (InterlockedExchange(&mutex_initlock, 1) == 1) while (InterlockedExchange(&mutex_initlock, 1) == 1)
/* loop, another thread own the lock */ ; /* loop, another thread own the lock */ ;
if (init_mutex == NULL) if (init_mutex == NULL)
pthread_mutex_init(&init_mutex, NULL); {
if (pthread_mutex_init(&init_mutex, NULL))
return -1;
}
InterlockedExchange(&mutex_initlock, 0); InterlockedExchange(&mutex_initlock, 0);
} }
#endif #endif
pthread_mutex_lock(&init_mutex); if (pthread_mutex_lock(&init_mutex))
return -1;
if (pq_initssllib && pq_lockarray == NULL) if (pq_initssllib && pq_lockarray == NULL)
{ {
@ -836,7 +849,10 @@ init_ssl_system(PGconn *conn)
return -1; return -1;
} }
for (i = 0; i < CRYPTO_num_locks(); i++) for (i = 0; i < CRYPTO_num_locks(); i++)
pthread_mutex_init(&pq_lockarray[i], NULL); {
if (pthread_mutex_init(&pq_lockarray[i], NULL))
return -1;
}
CRYPTO_set_locking_callback(pq_lockingcallback); CRYPTO_set_locking_callback(pq_lockingcallback);
} }

View File

@ -12,7 +12,7 @@
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.129 2008/01/01 19:46:00 momjian Exp $ * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.130 2008/05/16 18:30:53 mha Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -439,6 +439,13 @@ extern bool pqGetHomeDirectory(char *buf, int bufsize);
#ifdef ENABLE_THREAD_SAFETY #ifdef ENABLE_THREAD_SAFETY
extern pgthreadlock_t pg_g_threadlock; extern pgthreadlock_t pg_g_threadlock;
#define PGTHREAD_ERROR(msg) \
do { \
fprintf(stderr, "%s\n", msg); \
exit(1); \
} while (0)
#define pglock_thread() pg_g_threadlock(true) #define pglock_thread() pg_g_threadlock(true)
#define pgunlock_thread() pg_g_threadlock(false) #define pgunlock_thread() pg_g_threadlock(false)
#else #else

View File

@ -5,7 +5,7 @@
* *
* Copyright (c) 2004-2008, PostgreSQL Global Development Group * Copyright (c) 2004-2008, PostgreSQL Global Development Group
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/interfaces/libpq/pthread-win32.c,v 1.15 2008/01/01 19:46:00 momjian Exp $ * $PostgreSQL: pgsql/src/interfaces/libpq/pthread-win32.c,v 1.16 2008/05/16 18:30:53 mha Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -32,20 +32,27 @@ pthread_getspecific(pthread_key_t key)
return NULL; return NULL;
} }
void int
pthread_mutex_init(pthread_mutex_t *mp, void *attr) pthread_mutex_init(pthread_mutex_t *mp, void *attr)
{ {
*mp = CreateMutex(0, 0, 0); *mp = CreateMutex(0, 0, 0);
if (*mp == NULL)
return 1;
return 0;
} }
void int
pthread_mutex_lock(pthread_mutex_t *mp) pthread_mutex_lock(pthread_mutex_t *mp)
{ {
WaitForSingleObject(*mp, INFINITE); if (WaitForSingleObject(*mp, INFINITE) != WAIT_OBJECT_0)
return 1;
return 0;
} }
void int
pthread_mutex_unlock(pthread_mutex_t *mp) pthread_mutex_unlock(pthread_mutex_t *mp)
{ {
ReleaseMutex(*mp); if (!ReleaseMutex(*mp))
return 1;
return 0;
} }