-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
219 additions
and
314 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include "event.h" | ||
|
||
#include <errno.h> | ||
#include <syscall.h> | ||
#include <time.h> | ||
#include <unistd.h> | ||
#include <linux/futex.h> | ||
|
||
|
||
#define futex_wait(futex, count, timeout) \ | ||
(syscall(SYS_futex, futex, FUTEX_WAIT, count, timeout, nullptr, 0) == 0) | ||
|
||
#define futex_post(futex, count) \ | ||
(syscall(SYS_futex, futex, FUTEX_WAKE, count, nullptr, nullptr, 0) == 0) | ||
|
||
|
||
Event::Event() : | ||
count_(0) | ||
{ | ||
} | ||
|
||
|
||
Event::~Event() | ||
{ | ||
// FIXME The current implementation is not correct as it wakes only the one waiter. | ||
futex_post(&count_, 1); | ||
} | ||
|
||
|
||
bool Event::wait(int msecs) | ||
{ | ||
timespec* timeout = nullptr; | ||
timespec tm; | ||
|
||
if(msecs >= 0) { | ||
int seconds = msecs / 1000; | ||
msecs %= 1000; | ||
|
||
tm.tv_sec = seconds; | ||
tm.tv_nsec = msecs * 1000000; | ||
|
||
timeout = &tm; | ||
} | ||
|
||
while(count_ == 0) { | ||
if(!futex_wait(&count_, 0, timeout) && errno != EWOULDBLOCK) | ||
return false; | ||
} | ||
|
||
count_--; | ||
return true; | ||
} | ||
|
||
|
||
void Event::post() | ||
{ | ||
count_++; | ||
futex_post(&count_, 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef COMMON_EVENT_H | ||
#define COMMON_EVENT_H | ||
|
||
#include <atomic> | ||
|
||
#ifdef bool | ||
#undef bool | ||
#endif | ||
|
||
|
||
class Event { | ||
public: | ||
static const int kInfinite = -1; | ||
|
||
Event(); | ||
~Event(); | ||
|
||
bool wait(int msecs = kInfinite); | ||
void post(); | ||
|
||
private: | ||
std::atomic<int> count_; | ||
}; | ||
|
||
|
||
#endif // COMMON_EVENT_H |
Oops, something went wrong.