cross-platform define update
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
#include<hgl/thread/Semaphore.h>
|
||||
#include<hgl/LogInfo.h>
|
||||
#include<pthread.h>
|
||||
#include<dispatch/dispatch.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
|
@@ -10,13 +10,13 @@ namespace hgl
|
||||
{
|
||||
cond_var=new pthread_cond_t;
|
||||
|
||||
pthread_cond_init((pthread_cond_t *)cond_var,nullptr);
|
||||
pthread_cond_init(cond_var,nullptr);
|
||||
}
|
||||
|
||||
CondVar::~CondVar()
|
||||
{
|
||||
pthread_cond_destroy((pthread_cond_t *)cond_var);
|
||||
delete (pthread_cond_t *)cond_var;
|
||||
pthread_cond_destroy(cond_var);
|
||||
delete cond_var;
|
||||
}
|
||||
|
||||
bool CondVar::Wait(ThreadMutex *tm,double t)
|
||||
@@ -27,21 +27,21 @@ namespace hgl
|
||||
|
||||
GetWaitTime(abstime,t);
|
||||
|
||||
return(!pthread_cond_timedwait((pthread_cond_t *)cond_var,tm->GetThreadMutex(),&abstime));
|
||||
return(!pthread_cond_timedwait(cond_var,tm->GetThreadMutex(),&abstime));
|
||||
}
|
||||
else
|
||||
{
|
||||
return(!pthread_cond_wait((pthread_cond_t *)cond_var,tm->GetThreadMutex()));
|
||||
return(!pthread_cond_wait(cond_var,tm->GetThreadMutex()));
|
||||
}
|
||||
}
|
||||
|
||||
void CondVar::Signal()
|
||||
{
|
||||
pthread_cond_signal((pthread_cond_t *)cond_var);
|
||||
pthread_cond_signal(cond_var);
|
||||
}
|
||||
|
||||
void CondVar::Broadcast()
|
||||
{
|
||||
pthread_cond_broadcast((pthread_cond_t *)cond_var);
|
||||
pthread_cond_broadcast(cond_var);
|
||||
}
|
||||
}//namespace hgl
|
||||
|
@@ -9,15 +9,15 @@ namespace hgl
|
||||
{
|
||||
lock=new pthread_rwlock_t;
|
||||
|
||||
pthread_rwlock_init((pthread_rwlock_t *)lock,nullptr);
|
||||
pthread_rwlock_init(lock,nullptr);
|
||||
}
|
||||
|
||||
RWLock::~RWLock()
|
||||
{
|
||||
pthread_rwlock_destroy((pthread_rwlock_t *)lock);
|
||||
pthread_rwlock_destroy(lock);
|
||||
}
|
||||
|
||||
bool RWLock::TryReadLock() {return !pthread_rwlock_tryrdlock((pthread_rwlock_t *)lock);}
|
||||
bool RWLock::TryReadLock() {return !pthread_rwlock_tryrdlock(lock);}
|
||||
|
||||
#if HGL_OS != HGL_OS_macOS
|
||||
bool RWLock::WaitReadLock(double t)
|
||||
@@ -26,14 +26,14 @@ namespace hgl
|
||||
|
||||
GetWaitTime(abstime,t);
|
||||
|
||||
return !pthread_rwlock_timedrdlock((pthread_rwlock_t *)lock,&abstime);
|
||||
return !pthread_rwlock_timedrdlock(lock,&abstime);
|
||||
}
|
||||
#endif//HGL_OS != HGL_OS_macOS
|
||||
|
||||
bool RWLock::ReadLock() {return !pthread_rwlock_rdlock((pthread_rwlock_t *)lock);}
|
||||
bool RWLock::ReadUnlock() {return !pthread_rwlock_unlock((pthread_rwlock_t *)lock);}
|
||||
bool RWLock::ReadLock() {return !pthread_rwlock_rdlock(lock);}
|
||||
bool RWLock::ReadUnlock() {return !pthread_rwlock_unlock(lock);}
|
||||
|
||||
bool RWLock::TryWriteLock() {return !pthread_rwlock_trywrlock((pthread_rwlock_t *)lock);}
|
||||
bool RWLock::TryWriteLock() {return !pthread_rwlock_trywrlock(lock);}
|
||||
|
||||
#if HGL_OS != HGL_OS_macOS
|
||||
bool RWLock::WaitWriteLock(double t)
|
||||
@@ -42,10 +42,10 @@ namespace hgl
|
||||
|
||||
GetWaitTime(abstime,t);
|
||||
|
||||
return !pthread_rwlock_timedwrlock((pthread_rwlock_t *)lock,&abstime);
|
||||
return !pthread_rwlock_timedwrlock(lock,&abstime);
|
||||
}
|
||||
#endif//HGL_OS != HGL_OS_macOS
|
||||
|
||||
bool RWLock::WriteLock() {return !pthread_rwlock_wrlock((pthread_rwlock_t *)lock);}
|
||||
bool RWLock::WriteUnlock() {return !pthread_rwlock_unlock((pthread_rwlock_t *)lock);}
|
||||
bool RWLock::WriteLock() {return !pthread_rwlock_wrlock(lock);}
|
||||
bool RWLock::WriteUnlock() {return !pthread_rwlock_unlock(lock);}
|
||||
}//namespace hgl
|
||||
|
@@ -14,10 +14,10 @@ namespace hgl
|
||||
{
|
||||
ptr=new sem_t;
|
||||
|
||||
if(sem_init((sem_t *)ptr,PTHREAD_PROCESS_PRIVATE,0))
|
||||
if(sem_init(ptr,PTHREAD_PROCESS_PRIVATE,0))
|
||||
{
|
||||
LOG_ERROR(OS_TEXT("sem_init error,max_count=")+OSString(max_count));
|
||||
delete (sem_t *)ptr;
|
||||
delete ptr;
|
||||
ptr=nullptr;
|
||||
}
|
||||
}
|
||||
@@ -26,8 +26,8 @@ namespace hgl
|
||||
{
|
||||
if(!ptr)return;
|
||||
|
||||
sem_destroy((sem_t *)ptr);
|
||||
delete (sem_t *)ptr;
|
||||
sem_destroy(ptr);
|
||||
delete ptr;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -43,14 +43,14 @@ namespace hgl
|
||||
int result=0;
|
||||
|
||||
for(int i=0;i<n;i++)
|
||||
result+=sem_post((sem_t *)ptr);
|
||||
result+=sem_post(ptr);
|
||||
|
||||
return !result;
|
||||
|
||||
//if(n==1)
|
||||
// return !sem_post((sem_t *)ptr);
|
||||
// return !sem_post(ptr);
|
||||
//else
|
||||
// return !sem_post_multiple((sem_t *)ptr,n); //注:这个函数不是所有os都支持
|
||||
// return !sem_post_multiple(ptr,n); //注:这个函数不是所有os都支持
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,7 +61,7 @@ namespace hgl
|
||||
{
|
||||
if(!ptr)return(false);
|
||||
|
||||
return !sem_trywait((sem_t *)ptr);
|
||||
return !sem_trywait(ptr);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,14 +74,14 @@ namespace hgl
|
||||
if(!ptr)return(false);
|
||||
|
||||
if(t<=0)
|
||||
return !sem_wait((sem_t *)ptr);
|
||||
return !sem_wait(ptr);
|
||||
else
|
||||
{
|
||||
struct timespec abstime;
|
||||
|
||||
GetWaitTime(abstime,t);
|
||||
|
||||
return !sem_timedwait((sem_t *)ptr,&abstime);
|
||||
return !sem_timedwait(ptr,&abstime);
|
||||
}
|
||||
}
|
||||
}//namespace hgl
|
||||
|
@@ -8,31 +8,31 @@ namespace hgl
|
||||
CondVar::CondVar()
|
||||
{
|
||||
cond_var = new CONDITION_VARIABLE;
|
||||
InitializeConditionVariable((CONDITION_VARIABLE *)cond_var);
|
||||
InitializeConditionVariable(cond_var);
|
||||
}
|
||||
|
||||
CondVar::~CondVar()
|
||||
{
|
||||
delete (CONDITION_VARIABLE *)cond_var;
|
||||
delete cond_var;
|
||||
}
|
||||
|
||||
bool CondVar::Wait(ThreadMutex *tm, double time)
|
||||
{
|
||||
return SleepConditionVariableCS((CONDITION_VARIABLE *)cond_var,(CRITICAL_SECTION *)(tm->GetThreadMutex()),(DWORD)(time>0?time*1000:INFINITE));
|
||||
return SleepConditionVariableCS(cond_var,tm->GetThreadMutex(),(DWORD)(time>0?time*1000:INFINITE));
|
||||
}
|
||||
|
||||
bool CondVar::Wait(RWLock *lock, double time, bool read)
|
||||
{
|
||||
return SleepConditionVariableSRW((CONDITION_VARIABLE *)cond_var,(SRWLOCK *)(lock->GetRWLock()),(DWORD)(time>0?time*1000:INFINITE),read?CONDITION_VARIABLE_LOCKMODE_SHARED:0);
|
||||
return SleepConditionVariableSRW(cond_var,lock->GetRWLock(),(DWORD)(time>0?time*1000:INFINITE),read?CONDITION_VARIABLE_LOCKMODE_SHARED:0);
|
||||
}
|
||||
|
||||
void CondVar::Signal()
|
||||
{
|
||||
WakeConditionVariable((CONDITION_VARIABLE *)cond_var);
|
||||
WakeConditionVariable(cond_var);
|
||||
}
|
||||
|
||||
void CondVar::Broadcast()
|
||||
{
|
||||
WakeAllConditionVariable((CONDITION_VARIABLE *)cond_var);
|
||||
WakeAllConditionVariable(cond_var);
|
||||
}
|
||||
}//namespace hgl
|
||||
|
@@ -1,4 +1,4 @@
|
||||
#include<hgl/thread/RWLock.h>
|
||||
#include<hgl/thread/RWLock.h>
|
||||
|
||||
#pragma warning(disable:4800) // BOOL -> bool 性能损失警告
|
||||
namespace hgl
|
||||
@@ -7,19 +7,19 @@ namespace hgl
|
||||
{
|
||||
lock = new SRWLOCK;
|
||||
|
||||
InitializeSRWLock((SRWLOCK *)lock);
|
||||
InitializeSRWLock(lock);
|
||||
}
|
||||
|
||||
RWLock::~RWLock()
|
||||
{
|
||||
delete (SRWLOCK *)lock;
|
||||
delete lock;
|
||||
}
|
||||
|
||||
bool RWLock::TryReadLock() { return TryAcquireSRWLockShared((SRWLOCK *)lock); }
|
||||
bool RWLock::ReadLock() { AcquireSRWLockShared((SRWLOCK *)lock); return(true); }
|
||||
bool RWLock::ReadUnlock() { ReleaseSRWLockShared((SRWLOCK *)lock); return(true); }
|
||||
bool RWLock::TryReadLock(){ return TryAcquireSRWLockShared(lock); }
|
||||
bool RWLock::ReadLock (){ AcquireSRWLockShared(lock); return(true); }
|
||||
bool RWLock::ReadUnlock (){ ReleaseSRWLockShared(lock); return(true); }
|
||||
|
||||
bool RWLock::TryWriteLock() { return TryAcquireSRWLockExclusive((SRWLOCK *)lock); }
|
||||
bool RWLock::WriteLock() { AcquireSRWLockExclusive((SRWLOCK *)lock); return(true); }
|
||||
bool RWLock::WriteUnlock() { ReleaseSRWLockExclusive((SRWLOCK *)lock); return(true); }
|
||||
bool RWLock::TryWriteLock (){ return TryAcquireSRWLockExclusive(lock); }
|
||||
bool RWLock::WriteLock (){ AcquireSRWLockExclusive(lock); return(true); }
|
||||
bool RWLock::WriteUnlock (){ ReleaseSRWLockExclusive(lock); return(true); }
|
||||
}//namespace hgl
|
||||
|
@@ -11,7 +11,7 @@ namespace hgl
|
||||
*/
|
||||
Semaphore::Semaphore(int max_count)
|
||||
{
|
||||
ptr=CreateSemaphore(nullptr,0,max_count,nullptr);
|
||||
ptr=CreateSemaphoreW(nullptr,0,max_count,nullptr);
|
||||
|
||||
if(!ptr)
|
||||
LOG_ERROR(OS_TEXT("CreateSemaphore error,max_count=")+OSString(max_count));
|
||||
|
Reference in New Issue
Block a user