diff --git a/src/arch.h b/src/arch.h index 0a1bf3de..ca819170 100644 --- a/src/arch.h +++ b/src/arch.h @@ -435,6 +435,53 @@ (addr = (void*)_aligned_malloc((size), (align))) #define free_align(addr) _aligned_free(addr) +#ifdef __MINGW32__ + #ifndef spin_t + // spinlock + #include + #define spin_t pthread_spinlock_t + #define spin_init(arg) pthread_spin_init(arg, PTHREAD_PROCESS_SHARED) + #define spin_lock(arg) pthread_spin_lock(arg) + #define spin_trylock(arg) \ + (pthread_spin_trylock(arg) == 0) + #define spin_unlock(arg) pthread_spin_unlock(arg) + #define spin_destroy(arg) pthread_spin_destroy(arg) + #define SPIN_INITIALIZER (spin_t)(1) + #endif + #ifndef mutex_t + // mutex + #include + #define mutex_t pthread_mutex_t + #define mutex_init(arg) pthread_mutex_init(arg, NULL) + #define mutex_lock(arg) pthread_mutex_lock(arg) + #define mutex_trylock(arg) \ + (pthread_mutex_trylock(arg) == 0) + #define mutex_unlock(arg) pthread_mutex_unlock(arg) + #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER + #define mutex_destroy(arg) pthread_mutex_destroy(arg) + #endif + #ifndef thread_t + // thread + #include + #define thread_t pthread_t + #define thread_cond_t pthread_cond_t + #define thread_create(tid, func, args) \ + pthread_create((tid), NULL, (func), (args)) + #define thread_join(tid, ret) pthread_join(tid, ret) + #define thread_cancel(tid) pthread_cancel(tid) + #define thread_exit(code) pthread_exit(code) + #define thread_cond_init(cond) pthread_cond_init(cond, NULL) + #define thread_cond_destroy(cond) pthread_cond_destroy(cond) + #define thread_cond_wait(cond, mutex) pthread_cond_wait(cond, mutex) + #define thread_cond_timedwait(cond, mutex, ms) \ + { \ + struct timespec ts = convert_reltime_to_abstime(ms); \ + pthread_cond_timedwait(cond, mutex, &ts); \ + } + #define thread_cond_signal(cond) pthread_cond_signal(cond) + #define thread_cond_broadcast(cond) pthread_cond_broadcast(cond) + #endif +#else #ifndef spin_t // spinlock #define spin_t CRITICAL_SECTION @@ -474,6 +521,7 @@ #define thread_cond_signal(cond) WakeConditionVariable(cond) #define thread_cond_broadcast(cond) WakeAllConditionVariable(cond) #endif +#endif #elif __CYGWIN__ // cygwin compatiable diff --git a/src/atomic.h b/src/atomic.h index 07b05374..c5c3d2b5 100644 --- a/src/atomic.h +++ b/src/atomic.h @@ -165,7 +165,7 @@ extern "C" { #endif // RW Lock(s) -#if !defined(WIN32) && !defined(_WIN32) +#if (!defined(WIN32) && !defined(_WIN32)) || defined(__MINGW32__) #include typedef pthread_rwlock_t fdb_rw_lock; #else // WINDOWS @@ -176,7 +176,7 @@ typedef SRWLOCK fdb_rw_lock; // ---> RW Lock INLINE int init_rw_lock(fdb_rw_lock *lock) { -#if !defined(WIN32) && !defined(_WIN32) +#if (!defined(WIN32) && !defined(_WIN32)) || defined(__MINGW32__) int rv = pthread_rwlock_init(lock, NULL); return rv; #else @@ -197,7 +197,7 @@ INLINE int destroy_rw_lock(fdb_rw_lock *lock) { } INLINE int reader_lock(fdb_rw_lock *lock) { -#if !defined(WIN32) && !defined(_WIN32) +#if (!defined(WIN32) && !defined(_WIN32)) || defined(__MINGW32__) int result = pthread_rwlock_rdlock(lock); if (result != 0) { fprintf(stderr, "pthread_rwlock_rdlock returned %d (%s)\n", @@ -211,7 +211,7 @@ INLINE int reader_lock(fdb_rw_lock *lock) { } INLINE int reader_unlock(fdb_rw_lock *lock) { -#if !defined(WIN32) && !defined(_WIN32) +#if (!defined(WIN32) && !defined(_WIN32)) || defined(__MINGW32__) int result = pthread_rwlock_unlock(lock); if (result != 0) { fprintf(stderr, "pthread_rwlock_unlock returned %d (%s)\n", @@ -225,7 +225,7 @@ INLINE int reader_unlock(fdb_rw_lock *lock) { } INLINE int writer_lock(fdb_rw_lock *lock) { -#if !defined(WIN32) && !defined(_WIN32) +#if (!defined(WIN32) && !defined(_WIN32)) || defined(__MINGW32__) int result = pthread_rwlock_wrlock(lock); if (result != 0) { fprintf(stderr, "pthread_rwlock_wrlock returned %d (%s)\n", @@ -239,7 +239,7 @@ INLINE int writer_lock(fdb_rw_lock *lock) { } INLINE int writer_unlock(fdb_rw_lock *lock) { -#if !defined(WIN32) && !defined(_WIN32) +#if (!defined(WIN32) && !defined(_WIN32)) || defined(__MINGW32__) int result = pthread_rwlock_unlock(lock); if (result != 0) { fprintf(stderr, "pthread_rwlock_unlock returned %d (%s)\n", diff --git a/src/filemgr.h b/src/filemgr.h index ef648e1a..8fddfd80 100644 --- a/src/filemgr.h +++ b/src/filemgr.h @@ -18,7 +18,9 @@ #pragma once #include +#if !defined(__MINGW32__) #include +#endif #include #include diff --git a/src/kvs_handle.h b/src/kvs_handle.h index 4acdb993..c6932ff7 100644 --- a/src/kvs_handle.h +++ b/src/kvs_handle.h @@ -33,7 +33,7 @@ class BtreeV2; // _Atomic_address::store(void *) which breaks const qualifier // So to work around this, declare func_name_t as char * on windows only // compatible with __FUNCTION__ type. -#if defined(WIN32) || defined(_WIN32) +#if (defined(WIN32) || defined(_WIN32)) && !defined(__MINGW32__) #define BEGIN_HANDLE_BUSY(H) ((H)->beginBusy(__FUNCTION__)) #define END_HANDLE_BUSY(H) ((H)->endBusy(__FUNCTION__)) typedef char * func_name_t; diff --git a/tests/include/test.h b/tests/include/test.h index 26703974..1c9f0ae5 100644 --- a/tests/include/test.h +++ b/tests/include/test.h @@ -21,6 +21,10 @@ #include #if !defined(WIN32) && !defined(_WIN32) #include +#else +#if defined (__MINGW32__) +#include +#endif #endif #include "time_utils.h" diff --git a/utils/gettimeofday_vs.h b/utils/gettimeofday_vs.h index 69db7a9e..f6df825d 100644 --- a/utils/gettimeofday_vs.h +++ b/utils/gettimeofday_vs.h @@ -4,13 +4,15 @@ #include #undef NOMINMAX #include -#include +#include +#ifndef __MINGW32__ struct timezone { int tz_minuteswest; /* minutes W of Greenwich */ int tz_dsttime; /* type of dst correction */ }; +#endif int gettimeofday_vs(struct timeval *tv, void *tz); diff --git a/utils/time_utils.cc b/utils/time_utils.cc index 9affd43d..3940787e 100644 --- a/utils/time_utils.cc +++ b/utils/time_utils.cc @@ -41,7 +41,7 @@ extern "C" hrtime_t gethrtime_period(void) } #endif // _PLATFORM_LIB_AVAILABLE -#if defined(WIN32) || defined(_WIN32) +#if (defined(WIN32) || defined(_WIN32)) && !defined(__MINGW32__) #ifndef _PLATFORM_LIB_AVAILABLE void usleep(unsigned int usec) @@ -118,10 +118,18 @@ void decaying_usleep(unsigned int *sleep_time, unsigned int max_sleep_time) { */ ts_nsec get_monotonic_ts() { ts_nsec ts = 0; -#if defined(WIN32) +#if defined(WIN32) || defined (_WIN32) +#ifdef __MINGW32__ + struct timespec tm; + if (clock_gettime(CLOCK_MONOTONIC, &tm) == -1) { + abort(); + } + ts = tm.tv_nsec; +#else LARGE_INTEGER _ts; QueryPerformanceCounter(&_ts); ts = _ts.QuadPart; +#endif #elif defined(__APPLE__) long time = mach_absolute_time(); @@ -153,10 +161,12 @@ ts_nsec ts_diff(ts_nsec start, ts_nsec end) } else { diff = end-start; } -#if defined(WIN32) +#if defined(WIN32) || defined (_WIN32) +#ifndef __MINGW32__ LARGE_INTEGER Pf; QueryPerformanceFrequency(&Pf); return diff / (Pf.QuadPart/1000000); +#endif #else return diff/1000; #endif // defined(WIN32) diff --git a/utils/time_utils.h b/utils/time_utils.h index c89b599e..4436133d 100644 --- a/utils/time_utils.h +++ b/utils/time_utils.h @@ -17,7 +17,7 @@ #pragma once -#if defined(WIN32) || defined(_WIN32) +#if (defined(WIN32) || defined(_WIN32)) && !defined(__MINGW32__) #include #include #else @@ -50,13 +50,15 @@ uint64_t timeval_to_us(struct timeval tv); #else // If platform library has not been included, usleep // needs to be explicitly defined for windows. +#if !defined(__MINGW32__) void usleep(unsigned int useconds); +#endif #endif // _PLATFORM_LIB_AVAILABLE #endif // defined(WIN32) || defined(_WIN32) void decaying_usleep(unsigned int *sleep_time, unsigned int max_sleep_time); -#if !defined(WIN32) && !defined(_WIN32) +#if (!defined(WIN32) && !defined(_WIN32)) || defined(__MINGW32__) struct timespec convert_reltime_to_abstime(unsigned int ms); #endif