feat(lab03): add epoll
This commit is contained in:
@@ -32,6 +32,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/timerfd.h>
|
#include <sys/timerfd.h>
|
||||||
|
#include <sys/epoll.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* status led - gpioa.10 --> gpio10
|
* status led - gpioa.10 --> gpio10
|
||||||
@@ -45,6 +46,7 @@
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int timer_fd;
|
int timer_fd;
|
||||||
|
int epoll_fd;
|
||||||
} ThreadData;
|
} ThreadData;
|
||||||
|
|
||||||
static int open_led() {
|
static int open_led() {
|
||||||
@@ -129,6 +131,17 @@ static void configure_timer(int* timer_fd, long period_ms) {
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void link_timer_to_epoll(int* timer_fd, int* epoll_fd) {
|
||||||
|
static struct epoll_event ev;
|
||||||
|
ev.events = EPOLLIN;
|
||||||
|
ev.data.fd = *timer_fd;
|
||||||
|
if (epoll_ctl(*epoll_fd, EPOLL_CTL_ADD, *timer_fd, &ev) == -1) {
|
||||||
|
perror("ERROR while add timerfd to epoll");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
ThreadData data;
|
ThreadData data;
|
||||||
pthread_t thread;
|
pthread_t thread;
|
||||||
@@ -142,6 +155,18 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
configure_timer(&data.timer_fd, DEFAULT_TIME_MS);
|
configure_timer(&data.timer_fd, DEFAULT_TIME_MS);
|
||||||
|
|
||||||
|
// Create epoll instance
|
||||||
|
data.epoll_fd = epoll_create1(0);
|
||||||
|
if (data.epoll_fd == -1) {
|
||||||
|
perror("ERROR while create epoll");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
link_timer_to_epoll(&data.timer_fd, &data.epoll_fd);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (pthread_create(&thread, NULL, timer_thread, &data) != 0) {
|
if (pthread_create(&thread, NULL, timer_thread, &data) != 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user