forked from commaai/msgq
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dd dd dd dd dd dd dd
- Loading branch information
Showing
4 changed files
with
68 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include "msgq/futex.h" | ||
|
||
#include <cassert> | ||
#include <syscall.h> | ||
#include <fcntl.h> | ||
// #include <stdlib.h> | ||
#include <unistd.h> | ||
#include <linux/futex.h> | ||
#include <stdio.h> | ||
#include <limits.h> | ||
#include <sys/mman.h> | ||
|
||
Futex::Futex(const std::string &path) { | ||
auto fd = open(path.c_str(), O_RDWR | O_CREAT, 0664); | ||
assert(fd >= 0); | ||
int rc = ftruncate(fd, sizeof(int)); | ||
assert(rc >= 0); | ||
int *mem = (int*)mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | ||
close(fd); | ||
futex = (std::atomic<int>*)(mem); | ||
} | ||
|
||
Futex::~Futex() { | ||
munmap(futex, sizeof(int)); | ||
} | ||
|
||
void Futex::broadcast() { | ||
*futex += 1; | ||
syscall(SYS_futex, futex, FUTEX_WAKE, INT_MAX, NULL, NULL, 0); | ||
} | ||
|
||
int Futex::wait(int expected, struct timespec *timeout) { | ||
if ((*futex) == expected) { | ||
int ret = syscall(SYS_futex, futex, FUTEX_WAIT, expected, timeout, NULL, 0); | ||
if (ret == -1 && errno == ETIMEDOUT) { | ||
return -1; // Timeout occurred | ||
} | ||
} | ||
return 0; | ||
} |
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,16 @@ | ||
#pragma once | ||
|
||
#include <atomic> | ||
#include <string> | ||
|
||
class Futex { | ||
public: | ||
Futex(const std::string &path); | ||
~Futex(); | ||
void broadcast(); | ||
int wait(int expected, struct timespec *timeout); | ||
inline int value() const { return futex->load(); } | ||
|
||
private: | ||
std::atomic<int> *futex = nullptr; | ||
}; |
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