2019-07-28 10:31:56 +08:00
|
|
|
|
#pragma once
|
2018-08-19 15:31:26 +08:00
|
|
|
|
#include "../acl_cpp_define.hpp"
|
2019-05-09 13:57:51 +08:00
|
|
|
|
#include "noncopyable.hpp"
|
2018-08-19 15:31:26 +08:00
|
|
|
|
|
|
|
|
|
#if !defined(_WIN32) && !defined(_WIN64)
|
|
|
|
|
# include <pthread.h>
|
|
|
|
|
# ifndef acl_pthread_mutex_t
|
|
|
|
|
# define acl_pthread_mutex_t pthread_mutex_t
|
|
|
|
|
# endif
|
|
|
|
|
#else
|
|
|
|
|
struct acl_pthread_mutex_t;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
namespace acl {
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD>̻߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-08-19 15:31:26 +08:00
|
|
|
|
*/
|
2019-05-09 13:57:51 +08:00
|
|
|
|
class ACL_CPP_API thread_mutex : public noncopyable
|
2018-08-19 15:31:26 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>췽<EFBFBD><EFBFBD>
|
|
|
|
|
* @param recursive {bool} <EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>õݹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ
|
2018-08-19 15:31:26 +08:00
|
|
|
|
*/
|
|
|
|
|
thread_mutex(bool recursive = true);
|
|
|
|
|
~thread_mutex(void);
|
|
|
|
|
|
|
|
|
|
/**
|
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><EFBFBD><EFBFBD><EFBFBD>)
|
|
|
|
|
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> false ˵<EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-08-19 15:31:26 +08:00
|
|
|
|
*/
|
|
|
|
|
bool lock(void);
|
|
|
|
|
|
|
|
|
|
/**
|
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>
|
|
|
|
|
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> true <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> false <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>
|
2018-08-19 15:31:26 +08:00
|
|
|
|
*/
|
|
|
|
|
bool try_lock(void);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> false <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD><EFBFBD>п<EFBFBD><EFBFBD><EFBFBD>֮ǰ<EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-08-19 15:31:26 +08:00
|
|
|
|
*/
|
|
|
|
|
bool unlock(void);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> acl <EFBFBD><EFBFBD> C <EFBFBD>汾<EFBFBD><EFBFBD>ϵͳ<EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD>
|
2018-08-19 15:31:26 +08:00
|
|
|
|
* @return {acl_pthread_mutex_t*}
|
|
|
|
|
*/
|
|
|
|
|
acl_pthread_mutex_t* get_mutex(void) const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
acl_pthread_mutex_t* mutex_;
|
|
|
|
|
#if !defined(_WIN32) && !defined(_WIN64)
|
|
|
|
|
pthread_mutexattr_t mutex_attr_;
|
|
|
|
|
#endif
|
|
|
|
|
};
|
|
|
|
|
|
2019-05-09 13:57:51 +08:00
|
|
|
|
class ACL_CPP_API thread_mutex_guard : public noncopyable
|
2018-08-19 15:31:26 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
thread_mutex_guard(thread_mutex& mutex);
|
|
|
|
|
~thread_mutex_guard(void);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
thread_mutex& mutex_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace acl
|