Skip to content

Commit

Permalink
add CompletionEvent for thread synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
hxxft committed Jun 2, 2018
1 parent 149b895 commit 5c0e078
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
35 changes: 35 additions & 0 deletions Core/base/threading/completion_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef LYNX_BASE_THREAD_COMPLETION_EVENT_H_
#define LYNX_BASE_THREAD_COMPLETION_EVENT_H_

#include "base/threading/condition.h"
#include "base/threading/lock.h"

namespace base {
class CompletionEvent {
public:
CompletionEvent() : condition_(), lock_(), signaled_(false) {
pthread_cond_init(&condition_, NULL);
}
~CompletionEvent() { pthread_cond_destroy(&condition_); }

void Wait() {
AutoLock lock(lock_);
if (!signaled_) {
pthread_cond_wait(&condition_, &lock_.mutex_);
}
}

void Signal() {
AutoLock lock(lock_);
signaled_ = true;
pthread_cond_signal(&condition_);
}

private:
pthread_cond_t condition_;
Lock lock_;
bool signaled_;
};
} // namespace base

#endif
1 change: 1 addition & 0 deletions Core/base/threading/lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Lock {
void Release() { pthread_mutex_unlock(&mutex_); }

friend class Condition;
friend class CompletionEvent;
private:
pthread_mutex_t mutex_;
};
Expand Down
1 change: 1 addition & 0 deletions Core/base/threading/message_loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace base {
class MessageLoop : public MessagePump::Delegate {
public:
enum MESSAGE_LOOP_TYPE {
MESSAGE_LOOP_NONE,
MESSAGE_LOOP_UI,
MESSAGE_LOOP_POSIX,
MESSAGE_LOOP_IO,
Expand Down
13 changes: 9 additions & 4 deletions Core/base/threading/message_pump_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace base {

MessagePumpPosix::MessagePumpPosix() : condition_(), loop_running_(true) {}
MessagePumpPosix::MessagePumpPosix() : condition_(), keep_running_(true) {}

MessagePumpPosix::~MessagePumpPosix() {}

Expand All @@ -25,13 +25,18 @@ void MessagePumpPosix::ScheduleIntervalWork(Closure* closure, int delayed_time)
}

void MessagePumpPosix::Run(Delegate *delegate) {
while (loop_running_) {
while (keep_running_) {
timer_.Loop();
loop_running_ = delegate->DoWork();
if(loop_running_) {
keep_running_ &= delegate->DoWork();
if(keep_running_) {
condition_.Wait(timer_.NextTimeout() - CurrentTimeMillis());
}
}
delegate->DoQuit();
}

//Can only be called from the thread that owns the MessageLoop.
void MessagePumpPosix::Stop (){
keep_running_ = false;
}
} // namespace base
5 changes: 3 additions & 2 deletions Core/base/threading/message_pump_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ class MessagePumpPosix : public MessagePump {
virtual void ScheduleDelayedWork(Closure* closure, int delayed_time);

virtual void ScheduleIntervalWork(Closure* closure, int delayed_time);


virtual void Stop();
private:
Condition condition_;

Timer timer_;

bool loop_running_;
bool keep_running_;
};
} // namespace base

Expand Down

0 comments on commit 5c0e078

Please sign in to comment.