Commit dad4962a authored by DailyShana's avatar DailyShana Committed by GitHub

use standard library to implement Signal class (#2225)

parent a8db21d3
...@@ -837,7 +837,7 @@ void Game::MainLoop() { ...@@ -837,7 +837,7 @@ void Game::MainLoop() {
} }
} }
driver->endScene(); driver->endScene();
if(closeSignal.Wait(0)) if(closeSignal.Wait(1))
CloseDuelWindow(); CloseDuelWindow();
fps++; fps++;
cur_time = timer->getTime(); cur_time = timer->getTime();
......
#ifndef SIGNAL_H #ifndef SIGNAL_H
#define SIGNAL_H #define SIGNAL_H
#ifdef _WIN32 #include <mutex>
#include <condition_variable>
#include <windows.h>
class Signal {
public:
Signal() {
_event = CreateEvent(0, FALSE, FALSE, 0);
_nowait = false;
}
~Signal() {
CloseHandle(_event);
}
void Set() {
SetEvent(_event);
}
void Reset() {
ResetEvent(_event);
}
void Wait() {
if(_nowait)
return;
WaitForSingleObject(_event, INFINITE);
}
bool Wait(long milli) {
if(_nowait)
return false;
return WaitForSingleObject(_event, milli + 1) != WAIT_TIMEOUT;
}
void SetNoWait(bool nowait) {
_nowait = nowait;
}
private:
HANDLE _event;
bool _nowait;
};
#else // _WIN32
#include <sys/time.h>
#include <pthread.h>
class Signal { class Signal {
public: public:
Signal() { Signal() {
_state = false; _state = false;
_nowait = false; _nowait = false;
pthread_mutex_init(&_mutex, NULL);
pthread_cond_init(&_cond, NULL);
} }
~Signal() { ~Signal() {
pthread_cond_destroy(&_cond);
pthread_mutex_destroy(&_mutex);
} }
void Set() { void Set() {
if(pthread_mutex_lock(&_mutex)) std::unique_lock<std::mutex> lock(_mutex);
return;
_state = true; _state = true;
if(pthread_cond_broadcast(&_cond)) _cond.notify_all();
{
pthread_mutex_unlock(&_mutex);
// ERROR Broadcasting event status!
return;
}
pthread_mutex_unlock(&_mutex);
} }
void Reset() { void Reset() {
if(pthread_mutex_lock(&_mutex)) std::unique_lock<std::mutex> lock(_mutex);
return;
_state = false; _state = false;
pthread_mutex_unlock(&_mutex);
} }
void Wait() { void Wait() {
if(_nowait || pthread_mutex_lock(&_mutex)) if(_nowait)
return; return;
while(!_state) std::unique_lock<std::mutex> lock(_mutex);
{ _cond.wait(lock, [this]() { return _state; });
if(pthread_cond_wait(&_cond, &_mutex))
{
pthread_mutex_unlock(&_mutex);
// ERROR Waiting events;
return;
}
}
_state = false; _state = false;
pthread_mutex_unlock(&_mutex);
} }
bool Wait(long milliseconds) bool Wait(long milliseconds) {
{ if(_nowait)
if (_nowait || pthread_mutex_lock(&_mutex) != 0)
return false; return false;
std::unique_lock<std::mutex> lock(_mutex);
int rc = 0; bool res = _cond.wait_for(lock, std::chrono::milliseconds(milliseconds), [this]() { return _state; });
struct timespec abstime;
struct timeval tv;
gettimeofday(&tv, NULL);
abstime.tv_sec = tv.tv_sec + milliseconds / 1000;
abstime.tv_nsec = tv.tv_usec*1000 + (milliseconds % 1000)*1000000;
if (abstime.tv_nsec >= 1000000000)
{
abstime.tv_nsec -= 1000000000;
abstime.tv_sec++;
}
while (!_state)
{
if ((rc = pthread_cond_timedwait(&_cond, &_mutex, &abstime)))
{
if (rc == ETIMEDOUT) break;
pthread_mutex_unlock(&_mutex);
return false;
}
}
_state = false; _state = false;
pthread_mutex_unlock(&_mutex); return res;
return rc == 0;
} }
void SetNoWait(bool nowait) { void SetNoWait(bool nowait) {
_nowait = nowait; _nowait = nowait;
} }
private: private:
pthread_mutex_t _mutex; std::mutex _mutex;
pthread_cond_t _cond; std::condition_variable _cond;
bool _state; bool _state;
bool _nowait; bool _nowait;
}; };
#endif // _WIN32
#endif // SIGNAL_H #endif // SIGNAL_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment