From 132c9c994e603f29082c94d75c556efb379d33bc Mon Sep 17 00:00:00 2001 From: Leo Yuriev Date: Tue, 23 May 2017 21:54:06 +0300 Subject: [PATCH] mdbx: add mdbx_fastmutex_t. --- src/osal.c | 38 ++++++++++++++++++++++++++++++++++++++ src/osal.h | 7 +++++++ 2 files changed, 45 insertions(+) diff --git a/src/osal.c b/src/osal.c index 5a0f4009..b0e1d6b1 100644 --- a/src/osal.c +++ b/src/osal.c @@ -267,6 +267,44 @@ int mdbx_condmutex_wait(mdbx_condmutex_t *condmutex) { /*----------------------------------------------------------------------------*/ +int mdbx_fastmutex_init(mdbx_fastmutex_t *fastmutex) { +#if defined(_WIN32) || defined(_WIN64) + InitializeCriticalSection(fastmutex); + return MDB_SUCCESS; +#else + return pthread_mutex_init(fastmutex, NULL); +#endif +} + +int mdbx_fastmutex_destroy(mdbx_fastmutex_t *fastmutex) { +#if defined(_WIN32) || defined(_WIN64) + DeleteCriticalSection(fastmutex); + return MDB_SUCCESS; +#else + return pthread_mutex_destroy(fastmutex); +#endif +} + +int mdbx_fastmutex_acquire(mdbx_fastmutex_t *fastmutex) { +#if defined(_WIN32) || defined(_WIN64) + EnterCriticalSection(fastmutex); + return MDB_SUCCESS; +#else + return pthread_mutex_lock(fastmutex); +#endif +} + +int mdbx_fastmutex_release(mdbx_fastmutex_t *fastmutex) { +#if defined(_WIN32) || defined(_WIN64) + LeaveCriticalSection(fastmutex); + return MDB_SUCCESS; +#else + return pthread_mutex_unlock(fastmutex); +#endif +} + +/*----------------------------------------------------------------------------*/ + int mdbx_openfile(const char *pathname, int flags, mode_t mode, mdbx_filehandle_t *fd) { *fd = INVALID_HANDLE_VALUE; diff --git a/src/osal.h b/src/osal.h index 661337ca..8698fea9 100644 --- a/src/osal.h +++ b/src/osal.h @@ -68,6 +68,7 @@ typedef struct { HANDLE mutex; HANDLE event; } mdbx_condmutex_t; +typedef CRITICAL_SECTION mdbx_fastmutex_t; #else #include #include @@ -85,6 +86,7 @@ typedef struct { pthread_mutex_t mutex; pthread_cond_t cond; } mdbx_condmutex_t; +typedef pthread_mutex_t mdbx_fastmutex_t; #endif /* Platform */ #ifndef SSIZE_MAX @@ -395,6 +397,11 @@ int mdbx_condmutex_signal(mdbx_condmutex_t *condmutex); int mdbx_condmutex_wait(mdbx_condmutex_t *condmutex); int mdbx_condmutex_destroy(mdbx_condmutex_t *condmutex); +int mdbx_fastmutex_init(mdbx_fastmutex_t *fastmutex); +int mdbx_fastmutex_acquire(mdbx_fastmutex_t *fastmutex); +int mdbx_fastmutex_release(mdbx_fastmutex_t *fastmutex); +int mdbx_fastmutex_destroy(mdbx_fastmutex_t *fastmutex); + int mdbx_pwritev(mdbx_filehandle_t fd, struct iovec *iov, int iovcnt, off_t offset, size_t expected_written); int mdbx_pread(mdbx_filehandle_t fd, void *buf, size_t count, off_t offset);