1 /*
2  * Copyright (c) 2011-s2018 Intel Corporation.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *
32  */
33 
34 #ifndef _OFI_EPOLL_H_
35 #define _OFI_EPOLL_H_
36 
37 #include <unistd.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <poll.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 
44 #include <ofi_list.h>
45 #include <ofi_signal.h>
46 
47 enum ofi_pollfds_ctl {
48 	POLLFDS_CTL_ADD,
49 	POLLFDS_CTL_DEL,
50 	POLLFDS_CTL_MOD,
51 };
52 
53 struct ofi_pollfds_work_item {
54 	int		fd;
55 	uint32_t	events;
56 	void		*context;
57 	enum ofi_pollfds_ctl type;
58 	struct slist_entry entry;
59 };
60 
61 struct ofi_pollfds {
62 	int		size;
63 	int		nfds;
64 	struct pollfd	*fds;
65 	void		**context;
66 	int		index;
67 	struct fd_signal signal;
68 	struct slist	work_item_list;
69 	fastlock_t	lock;
70 };
71 
72 int ofi_pollfds_create(struct ofi_pollfds **pfds);
73 int ofi_pollfds_add(struct ofi_pollfds *pfds, int fd, uint32_t events,
74 		    void *context);
75 int ofi_pollfds_mod(struct ofi_pollfds *pfds, int fd, uint32_t events,
76 		    void *context);
77 int ofi_pollfds_del(struct ofi_pollfds *pfds, int fd);
78 int ofi_pollfds_wait(struct ofi_pollfds *pfds, void **contexts,
79 		     int max_contexts, int timeout);
80 void ofi_pollfds_close(struct ofi_pollfds *pfds);
81 
82 
83 #ifdef HAVE_EPOLL
84 #include <sys/epoll.h>
85 
86 #define OFI_EPOLL_IN  EPOLLIN
87 #define OFI_EPOLL_OUT EPOLLOUT
88 
89 typedef int ofi_epoll_t;
90 #define OFI_EPOLL_INVALID -1
91 
ofi_epoll_create(int * ep)92 static inline int ofi_epoll_create(int *ep)
93 {
94 	*ep = epoll_create(4);
95 	return *ep < 0 ? -ofi_syserr() : 0;
96 }
97 
ofi_epoll_add(int ep,int fd,uint32_t events,void * context)98 static inline int ofi_epoll_add(int ep, int fd, uint32_t events, void *context)
99 {
100 	struct epoll_event event;
101 	int ret;
102 
103 	event.data.ptr = context;
104 	event.events = events;
105 	ret = epoll_ctl(ep, EPOLL_CTL_ADD, fd, &event);
106 	if ((ret == -1) && (ofi_syserr() != EEXIST))
107 		return -ofi_syserr();
108 	return 0;
109 }
110 
ofi_epoll_mod(int ep,int fd,uint32_t events,void * context)111 static inline int ofi_epoll_mod(int ep, int fd, uint32_t events, void *context)
112 {
113 	struct epoll_event event;
114 
115 	event.data.ptr = context;
116 	event.events = events;
117 	return epoll_ctl(ep, EPOLL_CTL_MOD, fd, &event) ? -ofi_syserr() : 0;
118 }
119 
ofi_epoll_del(int ep,int fd)120 static inline int ofi_epoll_del(int ep, int fd)
121 {
122 	return epoll_ctl(ep, EPOLL_CTL_DEL, fd, NULL) ? -ofi_syserr() : 0;
123 }
124 
ofi_epoll_wait(int ep,void ** contexts,int max_contexts,int timeout)125 static inline int ofi_epoll_wait(int ep, void **contexts, int max_contexts,
126                                 int timeout)
127 {
128 	struct epoll_event events[max_contexts];
129 	int ret;
130 	int i;
131 
132 	ret = epoll_wait(ep, events, max_contexts, timeout);
133 	if (ret == -1)
134 		return -ofi_syserr();
135 
136 	for (i = 0; i < ret; i++)
137 		contexts[i] = events[i].data.ptr;
138 	return ret;
139 }
140 
ofi_epoll_close(int ep)141 static inline void ofi_epoll_close(int ep)
142 {
143 	close(ep);
144 }
145 
146 #else
147 
148 #define OFI_EPOLL_IN  POLLIN
149 #define OFI_EPOLL_OUT POLLOUT
150 
151 typedef struct ofi_pollfds *ofi_epoll_t;
152 #define OFI_EPOLL_INVALID NULL
153 
154 #define ofi_epoll_create ofi_pollfds_create
155 #define ofi_epoll_add ofi_pollfds_add
156 #define ofi_epoll_mod ofi_pollfds_mod
157 #define ofi_epoll_del ofi_pollfds_del
158 #define ofi_epoll_wait ofi_pollfds_wait
159 #define ofi_epoll_close ofi_pollfds_close
160 
161 #define EPOLL_CTL_ADD POLLFDS_CTL_ADD
162 #define EPOLL_CTL_DEL POLLFDS_CTL_DEL
163 #define EPOLL_CTL_MOD POLLFDS_CTL_MOD
164 
165 #endif /* HAVE_EPOLL */
166 
167 #endif  /* _OFI_EPOLL_H_ */
168