| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* pthread-win32.c |
| 4 |
* partial pthread implementation for win32 |
| 5 |
* |
| 6 |
* Copyright (c) 2004-2025, PostgreSQL Global Development Group |
| 7 |
* IDENTIFICATION |
| 8 |
* src/interfaces/libpq/pthread-win32.c |
| 9 |
* |
| 10 |
*------------------------------------------------------------------------- |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "postgres_fe.h" |
| 14 |
|
| 15 |
#include "pthread-win32.h" |
| 16 |
|
| 17 |
DWORD |
| 18 |
pthread_self(void) |
| 19 |
{ |
| 20 |
return GetCurrentThreadId(); |
| 21 |
} |
| 22 |
|
| 23 |
void |
| 24 |
pthread_setspecific(pthread_key_t key, void *val) |
| 25 |
{ |
| 26 |
} |
| 27 |
|
| 28 |
void * |
| 29 |
pthread_getspecific(pthread_key_t key) |
| 30 |
{ |
| 31 |
return NULL; |
| 32 |
} |
| 33 |
|
| 34 |
int |
| 35 |
pthread_mutex_init(pthread_mutex_t *mp, void *attr) |
| 36 |
{ |
| 37 |
mp->initstate = 0; |
| 38 |
return 0; |
| 39 |
} |
| 40 |
|
| 41 |
int |
| 42 |
pthread_mutex_lock(pthread_mutex_t *mp) |
| 43 |
{ |
| 44 |
/* Initialize the csection if not already done */ |
| 45 |
if (mp->initstate != 1) |
| 46 |
{ |
| 47 |
LONG istate; |
| 48 |
|
| 49 |
while ((istate = InterlockedExchange(&mp->initstate, 2)) == 2) |
| 50 |
Sleep(0); /* wait, another thread is doing this */ |
| 51 |
if (istate != 1) |
| 52 |
InitializeCriticalSection(&mp->csection); |
| 53 |
InterlockedExchange(&mp->initstate, 1); |
| 54 |
} |
| 55 |
EnterCriticalSection(&mp->csection); |
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
int |
| 60 |
pthread_mutex_unlock(pthread_mutex_t *mp) |
| 61 |
{ |
| 62 |
if (mp->initstate != 1) |
| 63 |
return EINVAL; |
| 64 |
LeaveCriticalSection(&mp->csection); |
| 65 |
return 0; |
| 66 |
} |