Skip to content

Commit c4a6c2f

Browse files
author
Michael Meskes
committed
Hopefully fixed some stuff that causes Windows builds to fail.
1 parent 6d673a6 commit c4a6c2f

File tree

9 files changed

+69
-60
lines changed

9 files changed

+69
-60
lines changed

src/interfaces/ecpg/ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,5 +2252,9 @@ Tue, 02 Oct 2007 11:32:25 +0200
22522252

22532253
- ITAGAKI Takahiro <itagaki.takahiro@oss.ntt.co.jp> added thread-safe
22542254
descriptor handling
2255+
2256+
Wed, 03 Oct 2007 10:48:39 +0200
2257+
2258+
- Hopefully fixed some stuff that causes Windows builds to fail.
22552259
- Set ecpg library version to 6.0.
22562260
- Set ecpg version to 4.4.

src/interfaces/ecpg/ecpglib/connect.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.45 2007/10/02 09:49:59 meskes Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.46 2007/10/03 08:55:22 meskes Exp $ */
22

33
#define POSTGRES_ECPG_INTERNAL
44
#include "postgres_fe.h"
@@ -11,17 +11,15 @@
1111
#include "sqlca.h"
1212

1313
#ifdef ENABLE_THREAD_SAFETY
14-
NON_EXEC_STATIC pthread_mutex_t connections_mutex = PTHREAD_MUTEX_INITIALIZER;
14+
static pthread_mutex_t connections_mutex = PTHREAD_MUTEX_INITIALIZER;
1515
static pthread_key_t actual_connection_key;
16-
#ifndef WIN32
1716
static pthread_once_t actual_connection_key_once = PTHREAD_ONCE_INIT;
1817
#endif
19-
#endif
2018
static struct connection *actual_connection = NULL;
2119
static struct connection *all_connections = NULL;
2220

2321
#ifdef ENABLE_THREAD_SAFETY
24-
NON_EXEC_STATIC void
22+
static void
2523
ecpg_actual_connection_init(void)
2624
{
2725
pthread_key_create(&actual_connection_key, NULL);
@@ -447,7 +445,6 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
447445

448446
this->cache_head = NULL;
449447
this->prep_stmts = NULL;
450-
this->descriptors = NULL;
451448

452449
if (all_connections == NULL)
453450
this->next = NULL;

src/interfaces/ecpg/ecpglib/descriptor.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* dynamic SQL support routines
22
*
3-
* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/descriptor.c,v 1.24 2007/10/02 09:49:59 meskes Exp $
3+
* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/descriptor.c,v 1.25 2007/10/03 08:55:22 meskes Exp $
44
*/
55

66
#define POSTGRES_ECPG_INTERNAL
@@ -21,17 +21,15 @@ static void descriptor_deallocate_all(struct descriptor *list);
2121
/* We manage descriptors separately for each thread. */
2222
#ifdef ENABLE_THREAD_SAFETY
2323
static pthread_key_t descriptor_key;
24-
#ifndef WIN32
2524
static pthread_once_t descriptor_once = PTHREAD_ONCE_INIT;
26-
#endif
2725

2826
static void
2927
descriptor_destructor(void *arg)
3028
{
3129
descriptor_deallocate_all(arg);
3230
}
3331

34-
NON_EXEC_STATIC void
32+
static void
3533
descriptor_key_init(void)
3634
{
3735
pthread_key_create(&descriptor_key, descriptor_destructor);

src/interfaces/ecpg/ecpglib/extern.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/extern.h,v 1.29 2007/10/02 09:49:59 meskes Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/extern.h,v 1.30 2007/10/03 08:55:22 meskes Exp $ */
22

33
#ifndef _ECPG_LIB_EXTERN_H
44
#define _ECPG_LIB_EXTERN_H
@@ -36,8 +36,6 @@ bool ECPGget_data(const PGresult *, int, int, int, enum ECPGttype type,
3636

3737
#ifdef ENABLE_THREAD_SAFETY
3838
void ecpg_pthreads_init(void);
39-
#else
40-
#define ecpg_pthreads_init() ((void)0)
4139
#endif
4240
struct connection *ECPGget_connection(const char *);
4341
char *ECPGalloc(long, int);
@@ -94,7 +92,6 @@ struct connection
9492
int autocommit;
9593
struct ECPGtype_information_cache *cache_head;
9694
struct prepared_statement *prep_stmts;
97-
struct descriptor *descriptors;
9895
struct connection *next;
9996
};
10097

src/interfaces/ecpg/ecpglib/memory.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/memory.c,v 1.9 2007/09/30 11:38:48 meskes Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/memory.c,v 1.10 2007/10/03 08:55:22 meskes Exp $ */
22

33
#define POSTGRES_ECPG_INTERNAL
44
#include "postgres_fe.h"
@@ -70,17 +70,15 @@ struct auto_mem
7070

7171
#ifdef ENABLE_THREAD_SAFETY
7272
static pthread_key_t auto_mem_key;
73-
#ifndef WIN32
7473
static pthread_once_t auto_mem_once = PTHREAD_ONCE_INIT;
75-
#endif
7674

7775
static void
7876
auto_mem_destructor(void *arg)
7977
{
8078
ECPGfree_auto_mem();
8179
}
8280

83-
NON_EXEC_STATIC void
81+
static void
8482
auto_mem_key_init(void)
8583
{
8684
pthread_key_create(&auto_mem_key, auto_mem_destructor);

src/interfaces/ecpg/ecpglib/misc.c

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/misc.c,v 1.38 2007/10/02 09:49:59 meskes Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/misc.c,v 1.39 2007/10/03 08:55:22 meskes Exp $ */
22

33
#define POSTGRES_ECPG_INTERNAL
44
#include "postgres_fe.h"
@@ -57,9 +57,7 @@ static struct sqlca_t sqlca_init =
5757

5858
#ifdef ENABLE_THREAD_SAFETY
5959
static pthread_key_t sqlca_key;
60-
#ifndef WIN32
6160
static pthread_once_t sqlca_key_once = PTHREAD_ONCE_INIT;
62-
#endif
6361
#else
6462
static struct sqlca_t sqlca =
6563
{
@@ -90,8 +88,8 @@ static struct sqlca_t sqlca =
9088
#endif
9189

9290
#ifdef ENABLE_THREAD_SAFETY
93-
NON_EXEC_STATIC pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
94-
NON_EXEC_STATIC pthread_mutex_t debug_init_mutex = PTHREAD_MUTEX_INITIALIZER;
91+
static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
92+
static pthread_mutex_t debug_init_mutex = PTHREAD_MUTEX_INITIALIZER;
9593
#endif
9694
static int simple_debug = 0;
9795
static FILE *debugstream = NULL;
@@ -125,7 +123,7 @@ ecpg_sqlca_key_destructor(void *arg)
125123
free(arg); /* sqlca structure allocated in ECPGget_sqlca */
126124
}
127125

128-
NON_EXEC_STATIC void
126+
static void
129127
ecpg_sqlca_key_init(void)
130128
{
131129
pthread_key_create(&sqlca_key, ecpg_sqlca_key_destructor);
@@ -415,24 +413,37 @@ ECPGis_noind_null(enum ECPGttype type, void *ptr)
415413
}
416414

417415
#ifdef WIN32
416+
#ifdef ENABLE_THREAD_SAFETY
417+
418+
void
419+
win32_pthread_mutex(volatile pthread_mutex_t *mutex)
420+
{
421+
if (mutex->handle == NULL)
422+
{
423+
while (InterlockedExchange((LONG *)&mutex->initlock, 1) == 1)
424+
Sleep(0);
425+
if (mutex->handle == NULL)
426+
mutex->handle = CreateMutex(NULL, FALSE, NULL);
427+
InterlockedExchange((LONG *)&mutex->initlock, 0);
428+
}
429+
}
418430

419-
/*
420-
* Initialize mutexes and call init-once functions on loading.
421-
*/
431+
static pthread_mutex_t win32_pthread_once_lock = PTHREAD_MUTEX_INITIALIZER;
422432

423-
BOOL WINAPI
424-
DllMain(HANDLE module, DWORD reason, LPVOID reserved)
433+
void
434+
win32_pthread_once(volatile pthread_once_t *once, void (*fn)(void))
425435
{
426-
if (reason == DLL_PROCESS_ATTACH)
436+
if (!*once)
427437
{
428-
connections_mutex = CreateMutex(NULL, FALSE, NULL);
429-
debug_mutex = CreateMutex(NULL, FALSE, NULL);
430-
debug_init_mutex = CreateMutex(NULL, FALSE, NULL);
431-
auto_mem_key_init();
432-
ecpg_actual_connection_init();
433-
ecpg_sqlca_key_init();
434-
descriptor_key_init();
438+
pthread_mutex_lock(&win32_pthread_once_lock);
439+
if (!*once)
440+
{
441+
*once = true;
442+
fn();
443+
}
444+
pthread_mutex_unlock(&win32_pthread_once_lock);
435445
}
436-
return TRUE;
437446
}
438-
#endif
447+
448+
#endif /* ENABLE_THREAD_SAFETY */
449+
#endif /* WIN32 */

src/interfaces/ecpg/include/ecpg-pthread-win32.h

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/include/ecpg-pthread-win32.h,v 1.3 2007/10/02 09:49:59 meskes Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/include/ecpg-pthread-win32.h,v 1.4 2007/10/03 08:55:23 meskes Exp $ */
22
/*
33
* pthread mapping macros for win32 native thread implementation
44
*/
@@ -10,22 +10,33 @@
1010
#ifndef WIN32
1111

1212
#include <pthread.h>
13-
#define NON_EXEC_STATIC static
1413

1514
#else
1615

17-
#define NON_EXEC_STATIC
16+
typedef struct pthread_mutex_t
17+
{
18+
HANDLE handle;
19+
LONG initlock;
20+
} pthread_mutex_t;
1821

19-
typedef HANDLE pthread_mutex_t;
2022
typedef DWORD pthread_key_t;
23+
typedef bool pthread_once_t;
2124

22-
#define PTHREAD_MUTEX_INITIALIZER INVALID_HANDLE_VALUE
25+
#define PTHREAD_MUTEX_INITIALIZER { NULL, 0 }
26+
#define PTHREAD_ONCE_INIT false
27+
28+
void win32_pthread_mutex(volatile pthread_mutex_t *mutex);
29+
void win32_pthread_once(volatile pthread_once_t *once, void (*fn)(void));
2330

2431
#define pthread_mutex_lock(mutex) \
25-
WaitForSingleObject(*(mutex), INFINITE);
32+
do { \
33+
if ((mutex)->handle == NULL) \
34+
win32_pthread_mutex((mutex)); \
35+
WaitForSingleObject((mutex)->handle, INFINITE); \
36+
} while(0)
2637

2738
#define pthread_mutex_unlock(mutex) \
28-
ReleaseMutex(*(mutex))
39+
ReleaseMutex((mutex)->handle)
2940

3041
#define pthread_getspecific(key) \
3142
TlsGetValue((key))
@@ -37,18 +48,11 @@ typedef DWORD pthread_key_t;
3748
#define pthread_key_create(key, destructor) \
3849
do { *(key) = TlsAlloc(); ((void)(destructor)); } while(0)
3950

40-
/* init-once functions are always called when libecpg is loaded */
41-
#define pthread_once(key, fn) \
42-
((void)0)
43-
44-
extern pthread_mutex_t connections_mutex;
45-
extern pthread_mutex_t debug_mutex;
46-
extern pthread_mutex_t debug_init_mutex;
47-
extern void auto_mem_key_init(void);
48-
extern void ecpg_actual_connection_init(void);
49-
extern void ecpg_sqlca_key_init(void);
50-
extern void descriptor_key_init(void);
51-
extern BOOL WINAPI DllMain(HANDLE module, DWORD reason, LPVOID reserved);
51+
#define pthread_once(once, fn) \
52+
do { \
53+
if (!*(once)) \
54+
win32_pthread_once((once), (fn)); \
55+
} while(0)
5256

5357
#endif /* WIN32 */
5458

src/interfaces/ecpg/test/expected/thread-descriptor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ struct sqlca_t *ECPGget_sqlca(void);
100100
#line 17 "descriptor.pgc"
101101

102102

103-
#ifdef WIN32
103+
#if defined(ENABLE_THREAD_SAFETY) && defined(WIN32)
104104
static unsigned STDCALL fn(void* arg)
105105
#else
106106
static void* fn(void* arg)

src/interfaces/ecpg/test/thread/descriptor.pgc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ EXEC SQL include sqlca;
1616
EXEC SQL whenever sqlerror sqlprint;
1717
EXEC SQL whenever not found sqlprint;
1818

19-
#ifdef WIN32
19+
#if defined(ENABLE_THREAD_SAFETY) && defined(WIN32)
2020
static unsigned STDCALL fn(void* arg)
2121
#else
2222
static void* fn(void* arg)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy