CMPlatform/src/UNIX/ThreadMutex.cpp

68 lines
1.3 KiB
C++
Raw Normal View History

2019-08-23 10:54:57 +08:00
#include<hgl/thread/ThreadMutex.h>
#include<hgl/TypeFunc.h>
#include<pthread.h>
#include<sys/time.h>
namespace hgl
{
void GetWaitTime(struct timespec &,double);
ThreadMutex::ThreadMutex()
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutex_init(&ptr,&attr);
pthread_mutexattr_destroy(&attr);
}
ThreadMutex::~ThreadMutex()
{
pthread_mutex_destroy(&ptr);
}
/**
*
*
*/
void ThreadMutex::Lock()
{
pthread_mutex_lock(&ptr);
}
/**
*
* @return
*/
bool ThreadMutex::TryLock()
{
return(!pthread_mutex_trylock(&ptr));
}
#if !defined(__APPLE__)&&!defined(__ANDROID__)
/**
*
* @param time ,0
* @return
*/
bool ThreadMutex::WaitLock(double t)
{
struct timespec abstime;
GetWaitTime(abstime,t);
return !pthread_mutex_timedlock(&ptr, &abstime);
}
#endif//__APPLE__
/**
*
*/
void ThreadMutex::Unlock()
{
pthread_mutex_unlock(&ptr);
}
}//namespace hgl