2019-08-10 21:15:25 +02:00
|
|
|
#include <lorie-compositor.hpp>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2019-08-26 02:30:14 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/eventfd.h>
|
2019-08-10 21:15:25 +02:00
|
|
|
|
2021-08-21 04:46:50 +02:00
|
|
|
LorieMessageQueue::LorieMessageQueue() {
|
2019-08-10 21:15:25 +02:00
|
|
|
pthread_mutex_init(&write_mutex, nullptr);
|
|
|
|
pthread_mutex_init(&read_mutex, nullptr);
|
2021-08-21 04:46:50 +02:00
|
|
|
|
2019-08-26 02:30:14 +02:00
|
|
|
fd = eventfd(0, EFD_CLOEXEC);
|
|
|
|
if (fd == -1) {
|
|
|
|
LOGE("Failed to create socketpair for message queue: %s", strerror(errno));
|
2019-08-10 21:15:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LorieMessageQueue::write(std::function<void()> func) {
|
2019-08-26 02:30:14 +02:00
|
|
|
static uint64_t i = 1;
|
2019-08-10 21:15:25 +02:00
|
|
|
pthread_mutex_lock(&write_mutex);
|
|
|
|
queue.push(func);
|
|
|
|
pthread_mutex_unlock(&write_mutex);
|
2019-08-26 02:30:14 +02:00
|
|
|
::write(fd, &i, sizeof(uint64_t));
|
2019-08-10 21:15:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LorieMessageQueue::run() {
|
2019-08-26 02:30:14 +02:00
|
|
|
static uint64_t i = 0;
|
|
|
|
::read(fd, &i, sizeof(uint64_t));
|
2019-08-10 21:15:25 +02:00
|
|
|
while(!queue.empty()){
|
|
|
|
queue.front()();
|
2019-08-26 02:30:14 +02:00
|
|
|
|
|
|
|
pthread_mutex_lock(&read_mutex);
|
2019-08-10 21:15:25 +02:00
|
|
|
queue.pop();
|
2019-08-26 02:30:14 +02:00
|
|
|
pthread_mutex_unlock(&read_mutex);
|
2019-08-10 21:15:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int LorieMessageQueue::get_fd() {
|
2019-08-26 02:30:14 +02:00
|
|
|
return fd;
|
2019-08-10 21:15:25 +02:00
|
|
|
}
|