Skip to content

Commit 798986c

Browse files
committed
[PBCKP-248] Use native mingw pthread
Mingw pthread "implementation" works reasonably well. There's no need to use emulation. Mingw's gcc links with winpthread.dll automatically, no need to force flags.
1 parent 81560fc commit 798986c

File tree

7 files changed

+19
-121
lines changed

7 files changed

+19
-121
lines changed

src/archive.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ push_files(void *arg)
279279
int rc;
280280
archive_push_arg *args = (archive_push_arg *) arg;
281281

282-
my_thread_num = args->thread_num;
282+
set_my_thread_num(args->thread_num);
283283

284284
for (i = 0; i < parray_num(args->files); i++)
285285
{
@@ -1011,7 +1011,7 @@ get_files(void *arg)
10111011
char from_fullpath[MAXPGPATH];
10121012
archive_get_arg *args = (archive_get_arg *) arg;
10131013

1014-
my_thread_num = args->thread_num;
1014+
set_my_thread_num(args->thread_num);
10151015

10161016
for (i = 0; i < parray_num(args->files); i++)
10171017
{

src/checkdb.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ check_indexes(void *arg)
293293
int i;
294294
check_indexes_arg *arguments = (check_indexes_arg *) arg;
295295
int n_indexes = 0;
296-
my_thread_num = arguments->thread_num;
296+
297+
set_my_thread_num(arguments->thread_num);
297298

298299
if (arguments->index_list)
299300
n_indexes = parray_num(arguments->index_list);

src/pg_probackup.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ bool no_color = false;
7575
bool show_color = true;
7676
bool is_archive_cmd = false;
7777
pid_t my_pid = 0;
78-
__thread int my_thread_num = 1;
7978
bool progress = false;
8079
bool no_sync = false;
8180
time_t start_time = 0;

src/pg_probackup.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,6 @@
4343

4444
#include "pg_probackup_state.h"
4545

46-
47-
#ifdef WIN32
48-
#define __thread __declspec(thread)
49-
#else
50-
#include <pthread.h>
51-
#endif
52-
5346
/* Wrap the code that we're going to delete after refactoring in this define*/
5447
#define REFACTORE_ME
5548

@@ -759,7 +752,6 @@ typedef struct StopBackupCallbackParams
759752

760753
/* common options */
761754
extern pid_t my_pid;
762-
extern __thread int my_thread_num;
763755
extern int num_threads;
764756
extern bool stream_wal;
765757
extern bool show_color;

src/utils/logger.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ elog_internal(int elevel, bool file_only, const char *message)
344344
if (format_file == JSON || format_console == JSON)
345345
{
346346
snprintf(str_pid_json, sizeof(str_pid_json), "%d", my_pid);
347-
snprintf(str_thread_json, sizeof(str_thread_json), "[%d-1]", my_thread_num);
347+
snprintf(str_thread_json, sizeof(str_thread_json), "[%d-1]", my_thread_num());
348348

349349
initPQExpBuffer(&show_buf);
350350
json_add_min(buf_json, JT_BEGIN_OBJECT);
@@ -424,7 +424,7 @@ elog_internal(int elevel, bool file_only, const char *message)
424424
{
425425
char str_thread[64];
426426
/* [Issue #213] fix pgbadger parsing */
427-
snprintf(str_thread, sizeof(str_thread), "[%d-1]:", my_thread_num);
427+
snprintf(str_thread, sizeof(str_thread), "[%d-1]:", my_thread_num());
428428

429429
fprintf(stderr, "%s ", strfbuf);
430430
fprintf(stderr, "%s ", str_pid);
@@ -498,7 +498,7 @@ elog_stderr(int elevel, const char *fmt, ...)
498498
strftime(strfbuf, sizeof(strfbuf), "%Y-%m-%d %H:%M:%S %Z",
499499
localtime(&log_time));
500500
snprintf(str_pid, sizeof(str_pid), "%d", my_pid);
501-
snprintf(str_thread_json, sizeof(str_thread_json), "[%d-1]", my_thread_num);
501+
snprintf(str_thread_json, sizeof(str_thread_json), "[%d-1]", my_thread_num());
502502

503503
initPQExpBuffer(&show_buf);
504504
json_add_min(buf_json, JT_BEGIN_OBJECT);

src/utils/thread.c

Lines changed: 6 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -17,97 +17,17 @@
1717
*/
1818
bool thread_interrupted = false;
1919

20-
#ifdef WIN32
21-
DWORD main_tid = 0;
22-
#else
2320
pthread_t main_tid = 0;
24-
#endif
25-
#ifdef WIN32
26-
#include <errno.h>
27-
28-
typedef struct win32_pthread
29-
{
30-
HANDLE handle;
31-
void *(*routine) (void *);
32-
void *arg;
33-
void *result;
34-
} win32_pthread;
35-
36-
static long mutex_initlock = 0;
37-
38-
static unsigned __stdcall
39-
win32_pthread_run(void *arg)
40-
{
41-
win32_pthread *th = (win32_pthread *)arg;
42-
43-
th->result = th->routine(th->arg);
44-
45-
return 0;
46-
}
47-
48-
int
49-
pthread_create(pthread_t *thread,
50-
pthread_attr_t *attr,
51-
void *(*start_routine) (void *),
52-
void *arg)
53-
{
54-
int save_errno;
55-
win32_pthread *th;
56-
57-
th = (win32_pthread *)pg_malloc(sizeof(win32_pthread));
58-
th->routine = start_routine;
59-
th->arg = arg;
60-
th->result = NULL;
61-
62-
th->handle = (HANDLE)_beginthreadex(NULL, 0, win32_pthread_run, th, 0, NULL);
63-
if (th->handle == NULL)
64-
{
65-
save_errno = errno;
66-
free(th);
67-
return save_errno;
68-
}
69-
70-
*thread = th;
71-
return 0;
72-
}
21+
static __thread int my_thread_num_var = 1;
7322

7423
int
75-
pthread_join(pthread_t th, void **thread_return)
24+
my_thread_num(void)
7625
{
77-
if (th == NULL || th->handle == NULL)
78-
return errno = EINVAL;
79-
80-
if (WaitForSingleObject(th->handle, INFINITE) != WAIT_OBJECT_0)
81-
{
82-
_dosmaperr(GetLastError());
83-
return errno;
84-
}
85-
86-
if (thread_return)
87-
*thread_return = th->result;
88-
89-
CloseHandle(th->handle);
90-
free(th);
91-
return 0;
26+
return my_thread_num_var;
9227
}
9328

94-
#endif /* WIN32 */
95-
96-
int
97-
pthread_lock(pthread_mutex_t *mp)
29+
void
30+
set_my_thread_num(int th)
9831
{
99-
#ifdef WIN32
100-
if (*mp == NULL)
101-
{
102-
while (InterlockedExchange(&mutex_initlock, 1) == 1)
103-
/* loop, another thread own the lock */ ;
104-
if (*mp == NULL)
105-
{
106-
if (pthread_mutex_init(mp, NULL))
107-
return -1;
108-
}
109-
InterlockedExchange(&mutex_initlock, 0);
110-
}
111-
#endif
112-
return pthread_mutex_lock(mp);
32+
my_thread_num_var = th;
11333
}

src/utils/thread.h

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,18 @@
1010
#ifndef PROBACKUP_THREAD_H
1111
#define PROBACKUP_THREAD_H
1212

13-
#ifdef WIN32
14-
#include "postgres_fe.h"
15-
#include "port/pthread-win32.h"
16-
17-
/* Use native win32 threads on Windows */
18-
typedef struct win32_pthread *pthread_t;
19-
typedef int pthread_attr_t;
20-
21-
#define PTHREAD_MUTEX_INITIALIZER NULL //{ NULL, 0 }
22-
#define PTHREAD_ONCE_INIT false
13+
#if defined(WIN32) && !(defined(__MINGW64__) || defined(__MINGW32__) || defined(HAVE_PTHREAD))
14+
#error "Windows build supports only 'pthread' threading"
15+
#endif
2316

24-
extern int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
25-
extern int pthread_join(pthread_t th, void **thread_return);
26-
#else
2717
/* Use platform-dependent pthread capability */
2818
#include <pthread.h>
29-
#endif
30-
31-
#ifdef WIN32
32-
extern DWORD main_tid;
33-
#else
3419
extern pthread_t main_tid;
35-
#endif
20+
#define pthread_lock(mp) pthread_mutex_lock(mp)
3621

3722
extern bool thread_interrupted;
3823

39-
extern int pthread_lock(pthread_mutex_t *mp);
24+
int my_thread_num(void);
25+
void set_my_thread_num(int);
4026

4127
#endif /* PROBACKUP_THREAD_H */

0 commit comments

Comments
 (0)