aispoll.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 "hdb.h"
  43. #include "../include/saAis.h"
  44. #include "tlist.h"
  45. typedef int (*dispatch_fn_t) (poll_handle poll_handle, int fd, int revents, void *data, unsigned int *prio);
  46. struct poll_entry {
  47. unsigned int prio;
  48. struct pollfd ufd;
  49. dispatch_fn_t dispatch_fn;
  50. void *data;
  51. };
  52. struct poll_instance {
  53. struct poll_entry *poll_entries;
  54. struct pollfd *ufds;
  55. int poll_entry_count;
  56. struct timerlist timerlist;
  57. };
  58. /*
  59. * All instances in one database
  60. */
  61. static struct saHandleDatabase poll_instance_database = {
  62. .handleCount = 0,
  63. .handles = 0,
  64. .handleInstanceDestructor = 0
  65. };
  66. poll_handle poll_create (void)
  67. {
  68. poll_handle handle;
  69. struct poll_instance *poll_instance;
  70. SaErrorT error;
  71. error = saHandleCreate (&poll_instance_database,
  72. sizeof (struct poll_instance), &handle);
  73. if (error != SA_OK) {
  74. goto error_exit;
  75. }
  76. error = saHandleInstanceGet (&poll_instance_database, handle,
  77. (void *)&poll_instance);
  78. if (error != SA_OK) {
  79. goto error_destroy;
  80. }
  81. poll_instance->poll_entries = 0;
  82. poll_instance->ufds = 0;
  83. poll_instance->poll_entry_count = 0;
  84. timerlist_init (&poll_instance->timerlist);
  85. return (handle);
  86. error_destroy:
  87. saHandleDestroy (&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. SaErrorT error;
  95. int res = 0;
  96. error = saHandleInstanceGet (&poll_instance_database, handle,
  97. (void *)&poll_instance);
  98. if (error != SA_OK) {
  99. res = -ENOENT;
  100. goto error_exit;
  101. }
  102. if (poll_instance->poll_entries) {
  103. free (poll_instance->poll_entries);
  104. }
  105. if (poll_instance->ufds) {
  106. free (poll_instance->ufds);
  107. }
  108. timerlist_free (&poll_instance->timerlist);
  109. saHandleDestroy (&poll_instance_database, handle);
  110. saHandleInstancePut (&poll_instance_database, handle);
  111. error_exit:
  112. return (res);
  113. }
  114. int poll_dispatch_add (
  115. poll_handle handle,
  116. int fd,
  117. int events,
  118. void *data,
  119. int (*dispatch_fn) (poll_handle poll_handle, int fd, int revents, void *data, unsigned int *prio),
  120. unsigned int prio)
  121. {
  122. struct poll_instance *poll_instance;
  123. struct poll_entry *poll_entries;
  124. struct pollfd *ufds;
  125. int found = 0;
  126. int install_pos;
  127. SaErrorT error;
  128. int res = 0;
  129. error = saHandleInstanceGet (&poll_instance_database, handle,
  130. (void *)&poll_instance);
  131. if (error != SA_OK) {
  132. res = -ENOENT;
  133. goto error_exit;
  134. }
  135. for (found = 0, install_pos = 0; install_pos < poll_instance->poll_entry_count; install_pos++) {
  136. if (poll_instance->poll_entries[install_pos].ufd.fd == -1) {
  137. found = 1;
  138. break;
  139. }
  140. }
  141. if (found == 0) {
  142. /*
  143. * Grow pollfd list
  144. */
  145. poll_entries = (struct poll_entry *)realloc (poll_instance->poll_entries,
  146. (poll_instance->poll_entry_count + 1) *
  147. sizeof (struct poll_entry));
  148. if (poll_entries == 0) {
  149. res = -ENOMEM;
  150. goto error_put;
  151. }
  152. poll_instance->poll_entries = poll_entries;
  153. ufds = (struct pollfd *)realloc (poll_instance->ufds,
  154. (poll_instance->poll_entry_count + 1) *
  155. sizeof (struct pollfd));
  156. if (ufds == 0) {
  157. res = -ENOMEM;
  158. goto error_put;
  159. }
  160. poll_instance->ufds = ufds;
  161. poll_instance->poll_entry_count += 1;
  162. install_pos = poll_instance->poll_entry_count - 1;
  163. }
  164. /*
  165. * Install new dispatch handler
  166. */
  167. poll_instance->poll_entries[install_pos].prio = prio;
  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. saHandleInstancePut (&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) (poll_handle poll_handle, int fd, int revents, void *data, unsigned int *prio),
  183. unsigned int prio)
  184. {
  185. struct poll_instance *poll_instance;
  186. int i;
  187. SaErrorT error;
  188. int res = 0;
  189. error = saHandleInstanceGet (&poll_instance_database, handle,
  190. (void *)&poll_instance);
  191. if (error != SA_OK) {
  192. res = -ENOENT;
  193. goto error_exit;
  194. }
  195. /*
  196. * Find file descriptor to modify events and dispatch function
  197. */
  198. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  199. if (poll_instance->poll_entries[i].ufd.fd == fd) {
  200. poll_instance->poll_entries[i].ufd.events = events;
  201. poll_instance->poll_entries[i].dispatch_fn = dispatch_fn;
  202. poll_instance->poll_entries[i].prio = prio;
  203. goto error_put;
  204. }
  205. }
  206. res = -EBADF;
  207. error_put:
  208. saHandleInstancePut (&poll_instance_database, handle);
  209. error_exit:
  210. return (res);
  211. }
  212. int poll_dispatch_delete (
  213. poll_handle handle,
  214. int fd)
  215. {
  216. struct poll_instance *poll_instance;
  217. int i;
  218. SaErrorT error;
  219. int found = 0;
  220. int res = 0;
  221. error = saHandleInstanceGet (&poll_instance_database, handle,
  222. (void *)&poll_instance);
  223. if (error != SA_OK) {
  224. res = -ENOENT;
  225. goto error_exit;
  226. }
  227. /*
  228. * Find dispatch fd to delete
  229. */
  230. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  231. if (poll_instance->poll_entries[i].ufd.fd == fd) {
  232. found = 1;
  233. break;
  234. }
  235. }
  236. if (found) {
  237. poll_instance->poll_entries[i].ufd.fd = -1;
  238. poll_instance->poll_entries[i].ufd.revents = 0;
  239. }
  240. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  241. if (poll_instance->ufds[i].fd == fd) {
  242. found = 1;
  243. break;
  244. }
  245. }
  246. res = -EBADF;
  247. if (found) {
  248. poll_instance->ufds[i].fd = -1;
  249. poll_instance->ufds[i].revents = 0;
  250. res = 0;
  251. }
  252. saHandleInstancePut (&poll_instance_database, handle);
  253. error_exit:
  254. return (res);
  255. }
  256. int poll_timer_add (
  257. poll_handle handle,
  258. int msec_in_future, void *data,
  259. void (*timer_fn) (void *data),
  260. poll_timer_handle *timer_handle_out)
  261. {
  262. struct poll_instance *poll_instance;
  263. int res = 0;
  264. SaErrorT error;
  265. error = saHandleInstanceGet (&poll_instance_database, handle,
  266. (void *)&poll_instance);
  267. if (error != SA_OK) {
  268. res = -ENOENT;
  269. goto error_exit;
  270. }
  271. timerlist_add_future (&poll_instance->timerlist,
  272. timer_fn, data, msec_in_future, timer_handle_out);
  273. if (timer_handle_out == 0) {
  274. res = -ENOENT;
  275. }
  276. saHandleInstancePut (&poll_instance_database, handle);
  277. error_exit:
  278. return (res);
  279. }
  280. int poll_timer_delete (
  281. poll_handle handle,
  282. poll_timer_handle timer_handle)
  283. {
  284. struct poll_instance *poll_instance;
  285. SaErrorT error;
  286. int res = 0;
  287. if (timer_handle == 0) {
  288. return (0);
  289. }
  290. error = saHandleInstanceGet (&poll_instance_database, handle,
  291. (void *)&poll_instance);
  292. if (error != SA_OK) {
  293. res = -ENOENT;
  294. goto error_exit;
  295. }
  296. timerlist_del (&poll_instance->timerlist, (void *)timer_handle);
  297. saHandleInstancePut (&poll_instance_database, handle);
  298. error_exit:
  299. return (res);
  300. }
  301. int poll_timer_delete_data (
  302. poll_handle handle,
  303. poll_timer_handle timer_handle) {
  304. struct poll_instance *poll_instance;
  305. SaErrorT error;
  306. int res = 0;
  307. if (timer_handle == 0) {
  308. return (0);
  309. }
  310. error = saHandleInstanceGet (&poll_instance_database, handle,
  311. (void *)&poll_instance);
  312. if (error != SA_OK) {
  313. res = -ENOENT;
  314. goto error_exit;
  315. }
  316. timerlist_del_data (&poll_instance->timerlist, (void *)timer_handle);
  317. saHandleInstancePut (&poll_instance_database, handle);
  318. error_exit:
  319. return (res);
  320. }
  321. int poll_entry_compare (const void *a, const void *b) {
  322. struct poll_entry *poll_entry_a = (struct poll_entry *)a;
  323. struct poll_entry *poll_entry_b = (struct poll_entry *)b;
  324. return (poll_entry_a->prio < poll_entry_b->prio);
  325. }
  326. int poll_run (
  327. poll_handle handle)
  328. {
  329. struct poll_instance *poll_instance;
  330. int i;
  331. int timeout = -1;
  332. int res;
  333. SaErrorT error;
  334. int poll_entry_count;
  335. error = saHandleInstanceGet (&poll_instance_database, handle,
  336. (void *)&poll_instance);
  337. if (error != SA_OK) {
  338. goto error_exit;
  339. }
  340. for (;;) {
  341. /*
  342. * Sort the poll entries list highest priority to lowest priority
  343. * Then build ufds structure for use with poll system call
  344. */
  345. qsort (poll_instance->poll_entries, poll_instance->poll_entry_count,
  346. sizeof (struct poll_entry), poll_entry_compare);
  347. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  348. memcpy (&poll_instance->ufds[i],
  349. &poll_instance->poll_entries[i].ufd,
  350. sizeof (struct pollfd));
  351. }
  352. timeout = timerlist_timeout_msec (&poll_instance->timerlist);
  353. retry_poll:
  354. res = poll (poll_instance->ufds,
  355. poll_instance->poll_entry_count, timeout);
  356. if (errno == EINTR && res == -1) {
  357. goto retry_poll;
  358. } else
  359. if (res == -1) {
  360. goto error_exit;
  361. }
  362. poll_entry_count = poll_instance->poll_entry_count;
  363. for (i = 0; i < poll_entry_count; i++) {
  364. if (poll_instance->ufds[i].fd != -1 &&
  365. poll_instance->ufds[i].revents) {
  366. res = poll_instance->poll_entries[i].dispatch_fn (handle,
  367. poll_instance->ufds[i].fd,
  368. poll_instance->ufds[i].revents,
  369. poll_instance->poll_entries[i].data,
  370. &poll_instance->poll_entries[i].prio);
  371. /*
  372. * Remove dispatch functions that return -1
  373. */
  374. if (res == -1) {
  375. poll_instance->poll_entries[i].ufd.fd = -1; /* empty entry */
  376. }
  377. }
  378. }
  379. timerlist_expire (&poll_instance->timerlist);
  380. } /* for (;;) */
  381. saHandleInstancePut (&poll_instance_database, handle);
  382. error_exit:
  383. return (-1);
  384. }
  385. int poll_stop (
  386. poll_handle handle);
  387. #ifdef COMPILE_OUT
  388. void poll_print_state (
  389. poll_handle handle,
  390. int fd)
  391. {
  392. struct poll_instance *poll_instance;
  393. int i;
  394. SaErrorT error;
  395. int res = 0;
  396. error = saHandleInstanceGet (&poll_instance_database, handle,
  397. (void *)&poll_instance);
  398. if (error != SA_OK) {
  399. res = -ENOENT;
  400. exit (1);
  401. }
  402. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  403. if (poll_instance->poll_entries[i].ufd.fd == fd) {
  404. printf ("fd %d\n", poll_instance->poll_entries[i].ufd.fd);
  405. printf ("events %d\n", poll_instance->poll_entries[i].ufd.events);
  406. printf ("dispatch_fn %x\n", poll_instance->poll_entries[i].dispatch_fn);
  407. printf ("prio %d\n", poll_instance->poll_entries[i].prio);
  408. }
  409. }
  410. }
  411. #endif