coropoll.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * Copyright (c) 2003-2004 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2008 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <errno.h>
  36. #include <pthread.h>
  37. #include <sys/poll.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <stdio.h>
  41. #include "coropoll.h"
  42. #include "../include/list.h"
  43. #include "../include/hdb.h"
  44. #include "tlist.h"
  45. typedef int (*dispatch_fn_t) (poll_handle poll_handle, int fd, int revents, void *data);
  46. struct poll_entry {
  47. struct pollfd ufd;
  48. dispatch_fn_t dispatch_fn;
  49. void *data;
  50. };
  51. struct poll_instance {
  52. struct poll_entry *poll_entries;
  53. struct pollfd *ufds;
  54. int poll_entry_count;
  55. struct timerlist timerlist;
  56. void (*serialize_lock_fn) (void);
  57. void (*serialize_unlock_fn) (void);
  58. };
  59. /*
  60. * All instances in one database
  61. */
  62. static struct hdb_handle_database poll_instance_database = {
  63. .handle_count = 0,
  64. .handles = 0,
  65. .iterator = 0
  66. };
  67. poll_handle poll_create (
  68. void (*serialize_lock_fn) (void),
  69. void (*serialize_unlock_fn) (void))
  70. {
  71. poll_handle handle;
  72. struct poll_instance *poll_instance;
  73. unsigned int res;
  74. res = hdb_handle_create (&poll_instance_database,
  75. sizeof (struct poll_instance), &handle);
  76. if (res != 0) {
  77. goto error_exit;
  78. }
  79. res = hdb_handle_get (&poll_instance_database, handle,
  80. (void *)&poll_instance);
  81. if (res != 0) {
  82. goto error_destroy;
  83. }
  84. poll_instance->poll_entries = 0;
  85. poll_instance->ufds = 0;
  86. poll_instance->poll_entry_count = 0;
  87. poll_instance->serialize_lock_fn = serialize_lock_fn;
  88. poll_instance->serialize_unlock_fn = serialize_unlock_fn;
  89. timerlist_init (&poll_instance->timerlist);
  90. return (handle);
  91. error_destroy:
  92. hdb_handle_destroy (&poll_instance_database, handle);
  93. error_exit:
  94. return (-1);
  95. }
  96. int poll_destroy (poll_handle handle)
  97. {
  98. struct poll_instance *poll_instance;
  99. int res = 0;
  100. res = hdb_handle_get (&poll_instance_database, handle,
  101. (void *)&poll_instance);
  102. if (res != 0) {
  103. res = -ENOENT;
  104. goto error_exit;
  105. }
  106. if (poll_instance->poll_entries) {
  107. free (poll_instance->poll_entries);
  108. }
  109. if (poll_instance->ufds) {
  110. free (poll_instance->ufds);
  111. }
  112. hdb_handle_destroy (&poll_instance_database, handle);
  113. hdb_handle_put (&poll_instance_database, handle);
  114. error_exit:
  115. return (res);
  116. }
  117. int poll_dispatch_add (
  118. poll_handle handle,
  119. int fd,
  120. int events,
  121. void *data,
  122. int (*dispatch_fn) (
  123. poll_handle poll_handle,
  124. int fd,
  125. int revents,
  126. void *data))
  127. {
  128. struct poll_instance *poll_instance;
  129. struct poll_entry *poll_entries;
  130. struct pollfd *ufds;
  131. int found = 0;
  132. int install_pos;
  133. int res = 0;
  134. res = hdb_handle_get (&poll_instance_database, handle,
  135. (void *)&poll_instance);
  136. if (res != 0) {
  137. res = -ENOENT;
  138. goto error_exit;
  139. }
  140. for (found = 0, install_pos = 0; install_pos < poll_instance->poll_entry_count; install_pos++) {
  141. if (poll_instance->poll_entries[install_pos].ufd.fd == -1) {
  142. found = 1;
  143. break;
  144. }
  145. }
  146. if (found == 0) {
  147. /*
  148. * Grow pollfd list
  149. */
  150. poll_entries = (struct poll_entry *)realloc (poll_instance->poll_entries,
  151. (poll_instance->poll_entry_count + 1) *
  152. sizeof (struct poll_entry));
  153. if (poll_entries == NULL) {
  154. res = -ENOMEM;
  155. goto error_put;
  156. }
  157. poll_instance->poll_entries = poll_entries;
  158. ufds = (struct pollfd *)realloc (poll_instance->ufds,
  159. (poll_instance->poll_entry_count + 1) *
  160. sizeof (struct pollfd));
  161. if (ufds == NULL) {
  162. res = -ENOMEM;
  163. goto error_put;
  164. }
  165. poll_instance->ufds = ufds;
  166. poll_instance->poll_entry_count += 1;
  167. install_pos = poll_instance->poll_entry_count - 1;
  168. }
  169. /*
  170. * Install new dispatch handler
  171. */
  172. poll_instance->poll_entries[install_pos].ufd.fd = fd;
  173. poll_instance->poll_entries[install_pos].ufd.events = events;
  174. poll_instance->poll_entries[install_pos].ufd.revents = 0;
  175. poll_instance->poll_entries[install_pos].dispatch_fn = dispatch_fn;
  176. poll_instance->poll_entries[install_pos].data = data;
  177. error_put:
  178. hdb_handle_put (&poll_instance_database, handle);
  179. error_exit:
  180. return (res);
  181. }
  182. int poll_dispatch_modify (
  183. poll_handle handle,
  184. int fd,
  185. int events,
  186. int (*dispatch_fn) (
  187. poll_handle poll_handle,
  188. int fd,
  189. int revents,
  190. void *data))
  191. {
  192. struct poll_instance *poll_instance;
  193. int i;
  194. int res = 0;
  195. res = hdb_handle_get (&poll_instance_database, handle,
  196. (void *)&poll_instance);
  197. if (res != 0) {
  198. res = -ENOENT;
  199. goto error_exit;
  200. }
  201. /*
  202. * Find file descriptor to modify events and dispatch function
  203. */
  204. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  205. if (poll_instance->poll_entries[i].ufd.fd == fd) {
  206. poll_instance->poll_entries[i].ufd.events = events;
  207. poll_instance->poll_entries[i].dispatch_fn = dispatch_fn;
  208. goto error_put;
  209. }
  210. }
  211. res = -EBADF;
  212. error_put:
  213. hdb_handle_put (&poll_instance_database, handle);
  214. error_exit:
  215. return (res);
  216. }
  217. int poll_dispatch_delete (
  218. poll_handle handle,
  219. int fd)
  220. {
  221. struct poll_instance *poll_instance;
  222. int i;
  223. int res = 0;
  224. res = hdb_handle_get (&poll_instance_database, handle,
  225. (void *)&poll_instance);
  226. if (res != 0) {
  227. res = -ENOENT;
  228. goto error_exit;
  229. }
  230. /*
  231. * Find dispatch fd to delete
  232. */
  233. res = -EBADF;
  234. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  235. if (poll_instance->poll_entries[i].ufd.fd == fd) {
  236. poll_instance->poll_entries[i].ufd.fd = -1;
  237. poll_instance->poll_entries[i].ufd.revents = 0;
  238. break;
  239. }
  240. }
  241. hdb_handle_put (&poll_instance_database, handle);
  242. error_exit:
  243. return (res);
  244. }
  245. int poll_timer_add (
  246. poll_handle handle,
  247. int msec_duration, void *data,
  248. void (*timer_fn) (void *data),
  249. poll_timer_handle *timer_handle_out)
  250. {
  251. struct poll_instance *poll_instance;
  252. int res = 0;
  253. res = hdb_handle_get (&poll_instance_database, handle,
  254. (void *)&poll_instance);
  255. if (res != 0) {
  256. res = -ENOENT;
  257. goto error_exit;
  258. }
  259. if (timer_handle_out == 0) {
  260. res = -ENOENT;
  261. }
  262. timerlist_add_duration (&poll_instance->timerlist,
  263. timer_fn, data, ((unsigned long long)msec_duration) * 1000000ULL, timer_handle_out);
  264. hdb_handle_put (&poll_instance_database, handle);
  265. error_exit:
  266. return (res);
  267. }
  268. int poll_timer_delete (
  269. poll_handle handle,
  270. poll_timer_handle timer_handle)
  271. {
  272. struct poll_instance *poll_instance;
  273. int res = 0;
  274. if (timer_handle == 0) {
  275. return (0);
  276. }
  277. res = hdb_handle_get (&poll_instance_database, handle,
  278. (void *)&poll_instance);
  279. if (res != 0) {
  280. res = -ENOENT;
  281. goto error_exit;
  282. }
  283. timerlist_del (&poll_instance->timerlist, (void *)timer_handle);
  284. hdb_handle_put (&poll_instance_database, handle);
  285. error_exit:
  286. return (res);
  287. }
  288. int poll_run (
  289. poll_handle handle)
  290. {
  291. struct poll_instance *poll_instance;
  292. int i;
  293. unsigned long long expire_timeout_msec = -1;
  294. int res;
  295. int poll_entry_count;
  296. res = hdb_handle_get (&poll_instance_database, handle,
  297. (void *)&poll_instance);
  298. if (res != 0) {
  299. goto error_exit;
  300. }
  301. for (;;) {
  302. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  303. memcpy (&poll_instance->ufds[i],
  304. &poll_instance->poll_entries[i].ufd,
  305. sizeof (struct pollfd));
  306. }
  307. expire_timeout_msec = timerlist_msec_duration_to_expire (&poll_instance->timerlist);
  308. if (expire_timeout_msec != -1 && expire_timeout_msec > 0xFFFFFFFF) {
  309. expire_timeout_msec = 0xFFFFFFFE;
  310. }
  311. retry_poll:
  312. res = poll (poll_instance->ufds,
  313. poll_instance->poll_entry_count, expire_timeout_msec);
  314. if (errno == EINTR && res == -1) {
  315. goto retry_poll;
  316. } else
  317. if (res == -1) {
  318. goto error_exit;
  319. }
  320. poll_entry_count = poll_instance->poll_entry_count;
  321. for (i = 0; i < poll_entry_count; i++) {
  322. if (poll_instance->ufds[i].fd != -1 &&
  323. poll_instance->ufds[i].revents) {
  324. poll_instance->serialize_lock_fn();
  325. res = poll_instance->poll_entries[i].dispatch_fn (handle,
  326. poll_instance->ufds[i].fd,
  327. poll_instance->ufds[i].revents,
  328. poll_instance->poll_entries[i].data);
  329. poll_instance->serialize_unlock_fn();
  330. /*
  331. * Remove dispatch functions that return -1
  332. */
  333. if (res == -1) {
  334. poll_instance->poll_entries[i].ufd.fd = -1; /* empty entry */
  335. }
  336. }
  337. }
  338. poll_instance->serialize_lock_fn();
  339. timerlist_expire (&poll_instance->timerlist);
  340. poll_instance->serialize_unlock_fn();
  341. } /* for (;;) */
  342. hdb_handle_put (&poll_instance_database, handle);
  343. error_exit:
  344. return (-1);
  345. }
  346. int poll_stop (
  347. poll_handle handle);
  348. #ifdef COMPILE_OUT
  349. void poll_print_state (
  350. poll_handle handle,
  351. int fd)
  352. {
  353. struct poll_instance *poll_instance;
  354. int i;
  355. int res = 0;
  356. res = hdb_handle_get (&poll_instance_database, handle,
  357. (void *)&poll_instance);
  358. if (res != 0) {
  359. res = -ENOENT;
  360. exit (1);
  361. }
  362. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  363. if (poll_instance->poll_entries[i].ufd.fd == fd) {
  364. printf ("fd %d\n", poll_instance->poll_entries[i].ufd.fd);
  365. printf ("events %d\n", poll_instance->poll_entries[i].ufd.events);
  366. printf ("dispatch_fn %p\n", poll_instance->poll_entries[i].dispatch_fn);
  367. }
  368. }
  369. }
  370. #endif