aispoll.c 11 KB

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