@@ -111,22 +111,36 @@ typedef struct socket_set
111
111
#endif /* POLL_USING_SELECT */
112
112
113
113
/*
114
- * Multi-platform pthread implementations
114
+ * Multi-platform thread implementations
115
115
*/
116
116
117
117
#ifdef WIN32
118
- /* Use native win32 threads on Windows */
119
- typedef struct win32_pthread * pthread_t ;
120
- typedef int pthread_attr_t ;
121
-
122
- static int pthread_create (pthread_t * thread , pthread_attr_t * attr , void * (* start_routine ) (void * ), void * arg );
123
- static int pthread_join (pthread_t th , void * * thread_return );
118
+ /* Use Windows threads */
119
+ #include <windows.h>
120
+ #define GETERRNO () (_dosmaperr(GetLastError()), errno)
121
+ #define THREAD_T HANDLE
122
+ #define THREAD_FUNC_RETURN_TYPE unsigned
123
+ #define THREAD_FUNC_RETURN return 0
124
+ #define THREAD_CREATE (handle , function , arg ) \
125
+ ((*(handle) = (HANDLE) _beginthreadex(NULL, 0, (function), (arg), 0, NULL)) == 0 ? errno : 0)
126
+ #define THREAD_JOIN (handle ) \
127
+ (WaitForSingleObject(handle, INFINITE) != WAIT_OBJECT_0 ? \
128
+ GETERRNO() : CloseHandle(handle) ? 0 : GETERRNO())
124
129
#elif defined(ENABLE_THREAD_SAFETY )
125
- /* Use platform-dependent pthread capability */
130
+ /* Use POSIX threads */
126
131
#include <pthread.h>
132
+ #define THREAD_T pthread_t
133
+ #define THREAD_FUNC_RETURN_TYPE void *
134
+ #define THREAD_FUNC_RETURN return NULL
135
+ #define THREAD_CREATE (handle , function , arg ) \
136
+ pthread_create((handle), NULL, (function), (arg))
137
+ #define THREAD_JOIN (handle ) \
138
+ pthread_join((handle), NULL)
127
139
#else
128
140
/* No threads implementation, use none (-j 1) */
129
- #define pthread_t void *
141
+ #define THREAD_T void *
142
+ #define THREAD_FUNC_RETURN_TYPE void *
143
+ #define THREAD_FUNC_RETURN return NULL
130
144
#endif
131
145
132
146
@@ -436,7 +450,7 @@ typedef struct
436
450
typedef struct
437
451
{
438
452
int tid ; /* thread id */
439
- pthread_t thread ; /* thread handle */
453
+ THREAD_T thread ; /* thread handle */
440
454
CState * state ; /* array of CState */
441
455
int nstate ; /* length of state[] */
442
456
@@ -459,8 +473,6 @@ typedef struct
459
473
int64 latency_late ; /* executed but late transactions */
460
474
} TState ;
461
475
462
- #define INVALID_THREAD ((pthread_t) 0)
463
-
464
476
/*
465
477
* queries read from files
466
478
*/
@@ -604,7 +616,7 @@ static void doLog(TState *thread, CState *st,
604
616
static void processXactStats (TState * thread , CState * st , instr_time * now ,
605
617
bool skipped , StatsData * agg );
606
618
static void addScript (ParsedScript script );
607
- static void * threadRun (void * arg );
619
+ static THREAD_FUNC_RETURN_TYPE threadRun (void * arg );
608
620
static void finishCon (CState * st );
609
621
static void setalarm (int seconds );
610
622
static socket_set * alloc_socket_set (int count );
@@ -6142,26 +6154,21 @@ main(int argc, char **argv)
6142
6154
/* the first thread (i = 0) is executed by main thread */
6143
6155
if (i > 0 )
6144
6156
{
6145
- int err = pthread_create (& thread -> thread , NULL , threadRun , thread );
6157
+ errno = THREAD_CREATE (& thread -> thread , threadRun , thread );
6146
6158
6147
- if (err != 0 || thread -> thread == INVALID_THREAD )
6159
+ if (errno != 0 )
6148
6160
{
6149
6161
pg_log_fatal ("could not create thread: %m" );
6150
6162
exit (1 );
6151
6163
}
6152
6164
}
6153
- else
6154
- {
6155
- thread -> thread = INVALID_THREAD ;
6156
- }
6157
6165
}
6158
6166
#else
6159
6167
INSTR_TIME_SET_CURRENT (threads [0 ].start_time );
6160
6168
/* compute when to stop */
6161
6169
if (duration > 0 )
6162
6170
end_time = INSTR_TIME_GET_MICROSEC (threads [0 ].start_time ) +
6163
6171
(int64 ) 1000000 * duration ;
6164
- threads [0 ].thread = INVALID_THREAD ;
6165
6172
#endif /* ENABLE_THREAD_SAFETY */
6166
6173
6167
6174
/* wait for threads and accumulate results */
@@ -6172,12 +6179,12 @@ main(int argc, char **argv)
6172
6179
TState * thread = & threads [i ];
6173
6180
6174
6181
#ifdef ENABLE_THREAD_SAFETY
6175
- if (threads [ i ]. thread == INVALID_THREAD )
6182
+ if (i == 0 )
6176
6183
/* actually run this thread directly in the main thread */
6177
6184
(void ) threadRun (thread );
6178
6185
else
6179
6186
/* wait of other threads. should check that 0 is returned? */
6180
- pthread_join (thread -> thread , NULL );
6187
+ THREAD_JOIN (thread -> thread );
6181
6188
#else
6182
6189
(void ) threadRun (thread );
6183
6190
#endif /* ENABLE_THREAD_SAFETY */
@@ -6216,7 +6223,7 @@ main(int argc, char **argv)
6216
6223
return exit_code ;
6217
6224
}
6218
6225
6219
- static void *
6226
+ static THREAD_FUNC_RETURN_TYPE
6220
6227
threadRun (void * arg )
6221
6228
{
6222
6229
TState * thread = (TState * ) arg ;
@@ -6501,7 +6508,7 @@ threadRun(void *arg)
6501
6508
thread -> logfile = NULL ;
6502
6509
}
6503
6510
free_socket_set (sockets );
6504
- return NULL ;
6511
+ THREAD_FUNC_RETURN ;
6505
6512
}
6506
6513
6507
6514
static void
@@ -6732,74 +6739,3 @@ socket_has_input(socket_set *sa, int fd, int idx)
6732
6739
}
6733
6740
6734
6741
#endif /* POLL_USING_SELECT */
6735
-
6736
-
6737
- /* partial pthread implementation for Windows */
6738
-
6739
- #ifdef WIN32
6740
-
6741
- typedef struct win32_pthread
6742
- {
6743
- HANDLE handle ;
6744
- void * (* routine ) (void * );
6745
- void * arg ;
6746
- void * result ;
6747
- } win32_pthread ;
6748
-
6749
- static unsigned __stdcall
6750
- win32_pthread_run (void * arg )
6751
- {
6752
- win32_pthread * th = (win32_pthread * ) arg ;
6753
-
6754
- th -> result = th -> routine (th -> arg );
6755
-
6756
- return 0 ;
6757
- }
6758
-
6759
- static int
6760
- pthread_create (pthread_t * thread ,
6761
- pthread_attr_t * attr ,
6762
- void * (* start_routine ) (void * ),
6763
- void * arg )
6764
- {
6765
- int save_errno ;
6766
- win32_pthread * th ;
6767
-
6768
- th = (win32_pthread * ) pg_malloc (sizeof (win32_pthread ));
6769
- th -> routine = start_routine ;
6770
- th -> arg = arg ;
6771
- th -> result = NULL ;
6772
-
6773
- th -> handle = (HANDLE ) _beginthreadex (NULL , 0 , win32_pthread_run , th , 0 , NULL );
6774
- if (th -> handle == NULL )
6775
- {
6776
- save_errno = errno ;
6777
- free (th );
6778
- return save_errno ;
6779
- }
6780
-
6781
- * thread = th ;
6782
- return 0 ;
6783
- }
6784
-
6785
- static int
6786
- pthread_join (pthread_t th , void * * thread_return )
6787
- {
6788
- if (th == NULL || th -> handle == NULL )
6789
- return errno = EINVAL ;
6790
-
6791
- if (WaitForSingleObject (th -> handle , INFINITE ) != WAIT_OBJECT_0 )
6792
- {
6793
- _dosmaperr (GetLastError ());
6794
- return errno ;
6795
- }
6796
-
6797
- if (thread_return )
6798
- * thread_return = th -> result ;
6799
-
6800
- CloseHandle (th -> handle );
6801
- free (th );
6802
- return 0 ;
6803
- }
6804
-
6805
- #endif /* WIN32 */
0 commit comments