5 #include <winpr/handle.h>
6 #include <winpr/sysinfo.h>
7 #include <winpr/assert.h>
10 #define TAG WINPR_TAG("sync.pollset")
12 #ifdef WINPR_HAVE_POLL_H
13 static INT16 handle_mode_to_pollevent(ULONG mode)
17 if (mode & WINPR_FD_READ)
20 if (mode & WINPR_FD_WRITE)
27 BOOL pollset_init(WINPR_POLL_SET* set,
size_t nhandles)
30 #ifdef WINPR_HAVE_POLL_H
31 if (nhandles > MAXIMUM_WAIT_OBJECTS)
33 set->isStatic = FALSE;
34 set->pollset = calloc(nhandles,
sizeof(*set->pollset));
40 set->pollset = set->staticSet;
44 set->fdIndex = calloc(nhandles,
sizeof(*set->fdIndex));
48 FD_ZERO(&set->rset_base);
50 FD_ZERO(&set->wset_base);
53 set->nread = set->nwrite = 0;
61 void pollset_uninit(WINPR_POLL_SET* set)
64 #ifdef WINPR_HAVE_POLL_H
72 void pollset_reset(WINPR_POLL_SET* set)
75 #ifndef WINPR_HAVE_POLL_H
76 FD_ZERO(&set->rset_base);
77 FD_ZERO(&set->wset_base);
79 set->nread = set->nwrite = 0;
84 BOOL pollset_add(WINPR_POLL_SET* set,
int fd, ULONG mode)
87 #ifdef WINPR_HAVE_POLL_H
88 struct pollfd* item = NULL;
89 if (set->fillIndex == set->size)
92 item = &set->pollset[set->fillIndex];
95 item->events = handle_mode_to_pollevent(mode);
97 FdIndex* fdIndex = &set->fdIndex[set->fillIndex];
98 if (mode & WINPR_FD_READ)
100 FD_SET(fd, &set->rset_base);
104 if (mode & WINPR_FD_WRITE)
106 FD_SET(fd, &set->wset_base);
114 fdIndex->mode = mode;
120 int pollset_poll(WINPR_POLL_SET* set, DWORD dwMilliseconds)
127 now = GetTickCount64();
128 if (dwMilliseconds == INFINITE)
129 dueTime = 0xFFFFFFFFFFFFFFFF;
131 dueTime = now + dwMilliseconds;
133 #ifdef WINPR_HAVE_POLL_H
138 if (dwMilliseconds == INFINITE)
141 timeout = (int)(dueTime - now);
143 ret = poll(set->pollset, set->fillIndex, timeout);
150 now = GetTickCount64();
151 }
while (now < dueTime);
156 struct timeval staticTimeout;
157 struct timeval* timeout;
162 if (dwMilliseconds == INFINITE)
168 long waitTime = (long)(dueTime - now);
170 timeout = &staticTimeout;
171 timeout->tv_sec = waitTime / 1000;
172 timeout->tv_usec = (waitTime % 1000) * 1000;
178 memcpy(rset, &set->rset_base,
sizeof(*rset));
184 memcpy(wset, &set->wset_base,
sizeof(*wset));
187 ret = select(set->maxFd + 1, rset, wset, NULL, timeout);
194 now = GetTickCount64();
196 }
while (now < dueTime);
205 BOOL pollset_isSignaled(WINPR_POLL_SET* set,
size_t idx)
209 if (idx > set->fillIndex)
211 WLog_ERR(TAG,
"index=%d out of pollset(fillIndex=%" PRIuz
")", idx, set->fillIndex);
215 #ifdef WINPR_HAVE_POLL_H
216 return !!(set->pollset[idx].revents & set->pollset[idx].events);
218 FdIndex* fdIndex = &set->fdIndex[idx];
222 if ((fdIndex->mode & WINPR_FD_READ) && FD_ISSET(fdIndex->fd, &set->rset))
225 if ((fdIndex->mode & WINPR_FD_WRITE) && FD_ISSET(fdIndex->fd, &set->wset))
232 BOOL pollset_isReadSignaled(WINPR_POLL_SET* set,
size_t idx)
236 if (idx > set->fillIndex)
238 WLog_ERR(TAG,
"index=%d out of pollset(fillIndex=%" PRIuz
")", idx, set->fillIndex);
242 #ifdef WINPR_HAVE_POLL_H
243 return !!(set->pollset[idx].revents & POLLIN);
245 FdIndex* fdIndex = &set->fdIndex[idx];
249 return FD_ISSET(fdIndex->fd, &set->rset);
253 BOOL pollset_isWriteSignaled(WINPR_POLL_SET* set,
size_t idx)
257 if (idx > set->fillIndex)
259 WLog_ERR(TAG,
"index=%d out of pollset(fillIndex=%" PRIuz
")", idx, set->fillIndex);
263 #ifdef WINPR_HAVE_POLL_H
264 return !!(set->pollset[idx].revents & POLLOUT);
266 FdIndex* fdIndex = &set->fdIndex[idx];
270 return FD_ISSET(fdIndex->fd, &set->wset);