2019-07-28 10:31:56 +08:00
|
|
|
|
#pragma once
|
2017-06-02 14:47:24 +08:00
|
|
|
|
#include "../acl_cpp_define.hpp"
|
2019-05-09 13:57:51 +08:00
|
|
|
|
#include "noncopyable.hpp"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
#include <stdlib.h>
|
2015-06-29 17:33:11 +08:00
|
|
|
|
#if !defined(_WIN32) && !defined(_WIN64)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-06-29 17:33:11 +08:00
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
struct acl_pthread_mutex_t;
|
|
|
|
|
#else
|
|
|
|
|
# ifndef acl_pthread_mutex_t
|
|
|
|
|
# define acl_pthread_mutex_t pthread_mutex_t
|
|
|
|
|
# endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
namespace acl {
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2019-05-09 13:57:51 +08:00
|
|
|
|
class ACL_CPP_API locker : public noncopyable
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>캯<EFBFBD><EFBFBD>
|
|
|
|
|
* @param use_mutex {bool} <EFBFBD>Ƿ<EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param use_spinlock {bool} <EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2015-06-03 11:19:13 +08:00
|
|
|
|
locker(bool use_mutex = true, bool use_spinlock = false);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
virtual ~locker();
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>·<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param file_path {const char*} <EFBFBD>ļ<EFBFBD>·<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǿ<EFBFBD>
|
|
|
|
|
* @return {bool} <EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD>
|
|
|
|
|
* ע<EFBFBD><EFBFBD><EFBFBD>˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> open <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
|
|
|
|
bool open(const char* file_path);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param fh {int} <EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {bool} <EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2015-06-29 17:33:11 +08:00
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
bool open(void* fh);
|
|
|
|
|
#else
|
|
|
|
|
bool open(int fh);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>)<EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
|
|
|
|
bool lock();
|
|
|
|
|
|
2014-12-14 18:26:52 +08:00
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>)<EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD>
|
2014-12-14 18:26:52 +08:00
|
|
|
|
*/
|
|
|
|
|
bool try_lock();
|
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>)<EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
|
|
|
|
bool unlock();
|
2014-12-14 18:26:52 +08:00
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
private:
|
2015-06-03 11:19:13 +08:00
|
|
|
|
acl_pthread_mutex_t* mutex_;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
char* pFile_;
|
2015-06-29 17:33:11 +08:00
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
void* fHandle_;
|
|
|
|
|
#else
|
|
|
|
|
int fHandle_;
|
2015-06-03 11:19:13 +08:00
|
|
|
|
pthread_mutexattr_t mutex_attr_;
|
2017-03-12 14:49:48 +08:00
|
|
|
|
# if !defined(MINGW) && !defined(__APPLE__) && !defined(ANDROID)
|
2015-06-03 11:19:13 +08:00
|
|
|
|
pthread_spinlock_t* spinlock_;
|
|
|
|
|
# endif
|
2014-11-19 00:25:21 +08:00
|
|
|
|
#endif
|
|
|
|
|
bool myFHandle_;
|
2015-06-03 11:19:13 +08:00
|
|
|
|
|
|
|
|
|
void init_mutex(bool use_spinlock);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
};
|
|
|
|
|
|
2019-05-09 13:57:51 +08:00
|
|
|
|
class ACL_CPP_API lock_guard : public noncopyable
|
2015-09-08 17:21:04 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
lock_guard(locker& lk);
|
|
|
|
|
~lock_guard();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
locker& lk_;
|
|
|
|
|
};
|
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
} // namespace acl
|