2022-11-13 23:37:41 +08:00
|
|
|
|
#pragma once
|
2023-01-11 22:29:45 +08:00
|
|
|
|
#include <vector>
|
2022-11-13 23:37:41 +08:00
|
|
|
|
#include "fiber_cpp_define.hpp"
|
2023-01-11 22:29:45 +08:00
|
|
|
|
#include "fiber_mutex_stat.hpp"
|
2022-11-13 23:37:41 +08:00
|
|
|
|
|
|
|
|
|
struct ACL_FIBER_MUTEX;
|
|
|
|
|
|
|
|
|
|
namespace acl {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <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><EFBFBD>
|
|
|
|
|
* <EFBFBD>߳<EFBFBD>֮<EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>֮<EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><EFBFBD><EFBFBD>.
|
|
|
|
|
*/
|
|
|
|
|
class FIBER_CPP_API fiber_mutex
|
|
|
|
|
{
|
|
|
|
|
public:
|
2023-01-11 22:29:45 +08:00
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>캯<EFBFBD><EFBFBD>
|
|
|
|
|
* @param mutex {ACL_FIBER_MUTEX*} <EFBFBD>ǿ<EFBFBD>ʱ,<EFBFBD><EFBFBD><EFBFBD><EFBFBD> C <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> C++ <EFBFBD><EFBFBD>
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> C <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>
|
|
|
|
|
* C <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>.
|
|
|
|
|
*/
|
|
|
|
|
fiber_mutex(ACL_FIBER_MUTEX* mutex = NULL);
|
2022-11-13 23:37:41 +08:00
|
|
|
|
~fiber_mutex(void);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <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><EFBFBD><EFBFBD>ʾ<EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
bool lock(void);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <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><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڱ<EFBFBD>ռ<EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
bool trylock(void);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <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><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
bool unlock(void);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> C <EFBFBD>汾<EFBFBD>Ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {ACL_FIBER_MUTEX*}
|
|
|
|
|
*/
|
|
|
|
|
ACL_FIBER_MUTEX* get_mutex(void) const
|
|
|
|
|
{
|
|
|
|
|
return mutex_;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 22:29:45 +08:00
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param out {fiber_mutex_stats&} <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>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> out <EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
static bool deadlock(fiber_mutex_stats& out);
|
|
|
|
|
|
2023-02-05 10:25:54 +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>
|
|
|
|
|
*/
|
|
|
|
|
static void deadlock_show(void);
|
|
|
|
|
|
2022-11-13 23:37:41 +08:00
|
|
|
|
private:
|
|
|
|
|
ACL_FIBER_MUTEX* mutex_;
|
2023-01-11 22:29:45 +08:00
|
|
|
|
ACL_FIBER_MUTEX* mutex_internal_;
|
2022-11-13 23:37:41 +08:00
|
|
|
|
|
|
|
|
|
fiber_mutex(const fiber_mutex&);
|
|
|
|
|
void operator=(const fiber_mutex&);
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-17 17:39:57 +08:00
|
|
|
|
class FIBER_CPP_API fiber_mutex_guard
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
fiber_mutex_guard(fiber_mutex& mutex) : mutex_(mutex) {
|
|
|
|
|
mutex_.lock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~fiber_mutex_guard(void) {
|
|
|
|
|
mutex_.unlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
fiber_mutex& mutex_;
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2022-11-13 23:37:41 +08:00
|
|
|
|
} // namespace acl
|