aispoll.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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);
  45. struct poll_entry {
  46. struct pollfd ufd;
  47. dispatch_fn_t dispatch_fn;
  48. void *data;
  49. };
  50. struct poll_instance {
  51. struct poll_entry *poll_entries;
  52. struct pollfd *ufds;
  53. int poll_entry_count;
  54. struct timerlist timerlist;
  55. pthread_mutex_t *serialize;
  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 (pthread_mutex_t *serialize)
  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. poll_instance->serialize = serialize;
  84. timerlist_init (&poll_instance->timerlist);
  85. return (handle);
  86. error_destroy:
  87. hdb_handle_destroy (&poll_instance_database, handle);
  88. error_exit:
  89. return (-1);
  90. }
  91. int poll_destroy (poll_handle handle)
  92. {
  93. struct poll_instance *poll_instance;
  94. int res = 0;
  95. res = hdb_handle_get (&poll_instance_database, handle,
  96. (void *)&poll_instance);
  97. if (res != 0) {
  98. res = -ENOENT;
  99. goto error_exit;
  100. }
  101. if (poll_instance->poll_entries) {
  102. free (poll_instance->poll_entries);
  103. }
  104. if (poll_instance->ufds) {
  105. free (poll_instance->ufds);
  106. }
  107. timerlist_free (&poll_instance->timerlist);
  108. hdb_handle_destroy (&poll_instance_database, handle);
  109. hdb_handle_put (&poll_instance_database, handle);
  110. error_exit:
  111. return (res);
  112. }
  113. int poll_dispatch_add (
  114. poll_handle handle,
  115. int fd,
  116. int events,
  117. void *data,
  118. int (*dispatch_fn) (
  119. poll_handle poll_handle,
  120. int fd,
  121. int revents,
  122. void *data))
  123. {
  124. struct poll_instance *poll_instance;
  125. struct poll_entry *poll_entries;
  126. struct pollfd *ufds;
  127. int found = 0;
  128. int install_pos;
  129. int res = 0;
  130. res = hdb_handle_get (&poll_instance_database, handle,
  131. (void *)&poll_instance);
  132. if (res != 0) {
  133. res = -ENOENT;
  134. goto error_exit;
  135. }
  136. for (found = 0, install_pos = 0; install_pos < poll_instance->poll_entry_count; install_pos++) {
  137. if (poll_instance->poll_entries[install_pos].ufd.fd == -1) {
  138. found = 1;
  139. break;
  140. }
  141. }
  142. if (found == 0) {
  143. /*
  144. * Grow pollfd list
  145. */
  146. poll_entries = (struct poll_entry *)realloc (poll_instance->poll_entries,
  147. (poll_instance->poll_entry_count + 1) *
  148. sizeof (struct poll_entry));
  149. if (poll_entries == 0) {
  150. res = -ENOMEM;
  151. goto error_put;
  152. }
  153. poll_instance->poll_entries = poll_entries;
  154. ufds = (struct pollfd *)realloc (poll_instance->ufds,
  155. (poll_instance->poll_entry_count + 1) *
  156. sizeof (struct pollfd));
  157. if (ufds == 0) {
  158. res = -ENOMEM;
  159. goto error_put;
  160. }
  161. poll_instance->ufds = ufds;
  162. poll_instance->poll_entry_count += 1;
  163. install_pos = poll_instance->poll_entry_count - 1;
  164. }
  165. /*
  166. * Install new dispatch handler
  167. */
  168. poll_instance->poll_entries[install_pos].ufd.fd = fd;
  169. poll_instance->poll_entries[install_pos].ufd.events = events;
  170. poll_instance->poll_entries[install_pos].ufd.revents = 0;
  171. poll_instance->poll_entries[install_pos].dispatch_fn = dispatch_fn;
  172. poll_instance->poll_entries[install_pos].data = data;
  173. error_put:
  174. hdb_handle_put (&poll_instance_database, handle);
  175. error_exit:
  176. return (res);
  177. }
  178. int poll_dispatch_modify (
  179. poll_handle handle,
  180. int fd,
  181. int events,
  182. int (*dispatch_fn) (
  183. poll_handle poll_handle,
  184. int fd,
  185. int revents,
  186. void *data))
  187. {
  188. struct poll_instance *poll_instance;
  189. int i;
  190. int res = 0;
  191. res = hdb_handle_get (&poll_instance_database, handle,
  192. (void *)&poll_instance);
  193. if (res != 0) {
  194. res = -ENOENT;
  195. goto error_exit;
  196. }
  197. /*
  198. * Find file descriptor to modify events and dispatch function
  199. */
  200. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  201. if (poll_instance->poll_entries[i].ufd.fd == fd) {
  202. poll_instance->poll_entries[i].ufd.events = events;
  203. poll_instance->poll_entries[i].dispatch_fn = dispatch_fn;
  204. goto error_put;
  205. }
  206. }
  207. res = -EBADF;
  208. error_put:
  209. hdb_handle_put (&poll_instance_database, handle);
  210. error_exit:
  211. return (res);
  212. }
  213. int poll_dispatch_delete (
  214. poll_handle handle,
  215. int fd)
  216. {
  217. struct poll_instance *poll_instance;
  218. int i;
  219. int res = 0;
  220. res = hdb_handle_get (&poll_instance_database, handle,
  221. (void *)&poll_instance);
  222. if (res != 0) {
  223. res = -ENOENT;
  224. goto error_exit;
  225. }
  226. /*
  227. * Find dispatch fd to delete
  228. */
  229. res = -EBADF;
  230. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  231. if (poll_instance->poll_entries[i].ufd.fd == fd) {
  232. poll_instance->poll_entries[i].ufd.fd = -1;
  233. poll_instance->poll_entries[i].ufd.revents = 0;
  234. break;
  235. }
  236. }
  237. hdb_handle_put (&poll_instance_database, handle);
  238. error_exit:
  239. return (res);
  240. }
  241. int poll_timer_add (
  242. poll_handle handle,
  243. int msec_in_future, void *data,
  244. void (*timer_fn) (void *data),
  245. poll_timer_handle *timer_handle_out)
  246. {
  247. struct poll_instance *poll_instance;
  248. int res = 0;
  249. res = hdb_handle_get (&poll_instance_database, handle,
  250. (void *)&poll_instance);
  251. if (res != 0) {
  252. res = -ENOENT;
  253. goto error_exit;
  254. }
  255. if (timer_handle_out == 0) {
  256. res = -ENOENT;
  257. }
  258. timerlist_add_future (&poll_instance->timerlist,
  259. timer_fn, data, msec_in_future, timer_handle_out);
  260. hdb_handle_put (&poll_instance_database, handle);
  261. error_exit:
  262. return (res);
  263. }
  264. int poll_timer_delete (
  265. poll_handle handle,
  266. poll_timer_handle timer_handle)
  267. {
  268. struct poll_instance *poll_instance;
  269. int res = 0;
  270. if (timer_handle == 0) {
  271. return (0);
  272. }
  273. res = hdb_handle_get (&poll_instance_database, handle,
  274. (void *)&poll_instance);
  275. if (res != 0) {
  276. res = -ENOENT;
  277. goto error_exit;
  278. }
  279. timerlist_del (&poll_instance->timerlist, (void *)timer_handle);
  280. hdb_handle_put (&poll_instance_database, handle);
  281. error_exit:
  282. return (res);
  283. }
  284. int poll_timer_delete_data (
  285. poll_handle handle,
  286. poll_timer_handle timer_handle) {
  287. struct poll_instance *poll_instance;
  288. int res = 0;
  289. if (timer_handle == 0) {
  290. return (0);
  291. }
  292. res = hdb_handle_get (&poll_instance_database, handle,
  293. (void *)&poll_instance);
  294. if (res != 0) {
  295. res = -ENOENT;
  296. goto error_exit;
  297. }
  298. timerlist_del_data (&poll_instance->timerlist, (void *)timer_handle);
  299. hdb_handle_put (&poll_instance_database, handle);
  300. error_exit:
  301. return (res);
  302. }
  303. int poll_run (
  304. poll_handle handle)
  305. {
  306. struct poll_instance *poll_instance;
  307. int i;
  308. int timeout = -1;
  309. int res;
  310. int poll_entry_count;
  311. res = hdb_handle_get (&poll_instance_database, handle,
  312. (void *)&poll_instance);
  313. if (res != 0) {
  314. goto error_exit;
  315. }
  316. for (;;) {
  317. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  318. memcpy (&poll_instance->ufds[i],
  319. &poll_instance->poll_entries[i].ufd,
  320. sizeof (struct pollfd));
  321. }
  322. timeout = timerlist_timeout_msec (&poll_instance->timerlist);
  323. retry_poll:
  324. res = poll (poll_instance->ufds,
  325. poll_instance->poll_entry_count, timeout);
  326. if (errno == EINTR && res == -1) {
  327. goto retry_poll;
  328. } else
  329. if (res == -1) {
  330. goto error_exit;
  331. }
  332. poll_entry_count = poll_instance->poll_entry_count;
  333. for (i = 0; i < poll_entry_count; i++) {
  334. if (poll_instance->ufds[i].fd != -1 &&
  335. poll_instance->ufds[i].revents) {
  336. pthread_mutex_lock (poll_instance->serialize);
  337. res = poll_instance->poll_entries[i].dispatch_fn (handle,
  338. poll_instance->ufds[i].fd,
  339. poll_instance->ufds[i].revents,
  340. poll_instance->poll_entries[i].data);
  341. pthread_mutex_unlock (poll_instance->serialize);
  342. /*
  343. * Remove dispatch functions that return -1
  344. */
  345. if (res == -1) {
  346. poll_instance->poll_entries[i].ufd.fd = -1; /* empty entry */
  347. }
  348. }
  349. }
  350. pthread_mutex_lock (poll_instance->serialize);
  351. timerlist_expire (&poll_instance->timerlist);
  352. pthread_mutex_unlock (poll_instance->serialize);
  353. } /* for (;;) */
  354. hdb_handle_put (&poll_instance_database, handle);
  355. error_exit:
  356. return (-1);
  357. }
  358. int poll_stop (
  359. poll_handle handle);
  360. #ifdef COMPILE_OUT
  361. void poll_print_state (
  362. poll_handle handle,
  363. int fd)
  364. {
  365. struct poll_instance *poll_instance;
  366. int i;
  367. int res = 0;
  368. res = hdb_handle_get (&poll_instance_database, handle,
  369. (void *)&poll_instance);
  370. if (res != 0) {
  371. res = -ENOENT;
  372. exit (1);
  373. }
  374. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  375. if (poll_instance->poll_entries[i].ufd.fd == fd) {
  376. printf ("fd %d\n", poll_instance->poll_entries[i].ufd.fd);
  377. printf ("events %d\n", poll_instance->poll_entries[i].ufd.events);
  378. printf ("dispatch_fn %p\n", poll_instance->poll_entries[i].dispatch_fn);
  379. }
  380. }
  381. }
  382. #endif