2 * Copyright 2016 IoT.bzh
3 * Author: José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <sys/epoll.h>
25 #include "utils-upoll.h"
31 void (*process)(void *closure, int fd, uint32_t events);
36 static int pollfd = 0;
37 static struct upoll *head = NULL;
38 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
40 int upoll_is_valid(struct upoll *upoll)
42 struct upoll *it = head;
51 struct upoll *upoll_open(int fd, uint32_t events, void (*process)(void *closure, int fd, uint32_t events), void *closure)
57 /* opens the epoll stream */
59 pollfd = epoll_create1(EPOLL_CLOEXEC);
71 result = malloc(sizeof *result);
77 result->process = process;
78 result->closure = closure;
79 pthread_mutex_lock(&mutex);
82 pthread_mutex_unlock(&mutex);
87 rc = epoll_ctl(pollfd, EPOLL_CTL_ADD, fd, &e);
98 int upoll_update(struct upoll *upoll, uint32_t events)
100 struct epoll_event e;
103 assert(upoll_is_valid(upoll));
107 return epoll_ctl(pollfd, EPOLL_CTL_MOD, upoll->fd, &e);
110 void upoll_close(struct upoll *upoll)
115 assert(upoll_is_valid(upoll));
117 epoll_ctl(pollfd, EPOLL_CTL_DEL, upoll->fd, NULL);
118 pthread_mutex_lock(&mutex);
123 pthread_mutex_unlock(&mutex);
127 void upoll_wait(int timeout)
130 struct epoll_event e;
136 rc = epoll_wait(pollfd, &e, 1, timeout);
139 upoll->process(upoll->closure, upoll->fd, e.events);