1919 */
2020
2121#include "../lib/libcompat.h"
22+ #include "config.h"
2223
2324#include <stdlib.h>
2425#include <string.h>
3334#include "check_impl.h"
3435#include "check_pack.h"
3536
37+ #ifdef HAVE_WINDOWS_H
38+ #include <windows.h>
39+ #endif
40+
41+ #ifdef HAVE_SYNCHAPI_H
42+ #include <synchapi.h>
43+ #endif
44+
45+ #if defined(HAVE_INIT_ONCE_BEGIN_INITIALIZE ) && defined(HAVE_INIT_ONCE_COMPLETE )
46+ #define HAVE_WIN32_INIT_ONCE 1
47+ #endif
48+
3649#ifndef HAVE_PTHREAD
37- #define pthread_mutex_lock (arg )
38- #define pthread_mutex_unlock (arg )
3950#define pthread_cleanup_push (f , a ) {
4051#define pthread_cleanup_pop (e ) }
4152#endif
4253
54+ #ifndef HAVE_PTHREAD
55+ #if defined(HAVE_WIN32_INIT_ONCE )
56+ #define pthread_mutex_lock (arg ) EnterCriticalSection(arg)
57+ #define pthread_mutex_unlock (arg ) LeaveCriticalSection(arg)
58+ #else
59+ #define pthread_mutex_lock (arg )
60+ #define pthread_mutex_unlock (arg )
61+ #endif
62+ #endif
63+
4364/* Maximum size for one message in the message stream. */
4465static size_t ck_max_msg_size = 0 ;
4566#ifndef DEFAULT_MAX_MSG_SIZE
@@ -348,12 +369,15 @@ static void check_type(int type, const char *file, int line)
348369 eprintf ("Bad message type arg %d" , file , line , type );
349370}
350371
351- #ifdef HAVE_PTHREAD
372+ #if defined HAVE_PTHREAD
352373static pthread_mutex_t ck_mutex_lock = PTHREAD_MUTEX_INITIALIZER ;
353374static void ppack_cleanup (void * mutex )
354375{
355376 pthread_mutex_unlock ((pthread_mutex_t * )mutex );
356377}
378+ #elif defined HAVE_WIN32_INIT_ONCE
379+ static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT ;
380+ static CRITICAL_SECTION ck_mutex_lock ;
357381#endif
358382
359383void ppack (FILE * fdes , enum ck_msg_type type , CheckMsg * msg )
@@ -362,6 +386,20 @@ void ppack(FILE * fdes, enum ck_msg_type type, CheckMsg * msg)
362386 int n ;
363387 size_t r ;
364388
389+ #if !defined(HAVE_PTHREAD ) && defined(HAVE_WIN32_INIT_ONCE )
390+ BOOL pending ;
391+ if (!InitOnceBeginInitialize (& init_once , 0 , & pending , NULL ))
392+ {
393+ eprintf ("Cannot initialize Win32 synch object" , __FILE__ , __LINE__ - 5 );
394+ return ;
395+ }
396+
397+ if (pending )
398+ InitializeCriticalSection (& ck_mutex_lock );
399+
400+ InitOnceComplete (& init_once , 0 , NULL );
401+ #endif
402+
365403 n = pack (type , & buf , msg );
366404 if (n < 0 )
367405 eprintf ("pack failed" , __FILE__ , __LINE__ - 2 );
0 commit comments