aispoll.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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/ais_types.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. error = saHandleInstanceGet (&poll_instance_database, handle,
  96. (void *)&poll_instance);
  97. if (error != SA_OK) {
  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. saHandleDestroy (&poll_instance_database, handle);
  108. saHandleInstancePut (&poll_instance_database, handle);
  109. return (0);
  110. error_exit:
  111. return (-1);
  112. }
  113. int poll_dispatch_add (
  114. poll_handle handle,
  115. int fd,
  116. int events,
  117. void *data,
  118. int (*dispatch_fn) (poll_handle poll_handle, int fd, int revents, void *data, unsigned int *prio),
  119. unsigned int prio)
  120. {
  121. struct poll_instance *poll_instance;
  122. struct poll_entry *poll_entries;
  123. struct pollfd *ufds;
  124. int found = 0;
  125. int install_pos;
  126. SaErrorT error;
  127. error = saHandleInstanceGet (&poll_instance_database, handle,
  128. (void *)&poll_instance);
  129. if (error != SA_OK) {
  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. errno = ENOMEM;
  147. goto error_exit;
  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. errno = ENOMEM;
  155. goto error_exit;
  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. saHandleInstancePut (&poll_instance_database, handle);
  171. return (0);
  172. error_exit:
  173. return (-1);
  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. SaErrorT error;
  185. error = saHandleInstanceGet (&poll_instance_database, handle,
  186. (void *)&poll_instance);
  187. if (error != SA_OK) {
  188. goto error_exit;
  189. }
  190. /*
  191. * Find file descriptor to modify events and dispatch function
  192. */
  193. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  194. if (poll_instance->poll_entries[i].ufd.fd == fd) {
  195. poll_instance->poll_entries[i].ufd.events = events;
  196. poll_instance->poll_entries[i].dispatch_fn = dispatch_fn;
  197. poll_instance->poll_entries[i].prio = prio;
  198. return (0);
  199. }
  200. }
  201. errno = EBADF;
  202. saHandleInstancePut (&poll_instance_database, handle);
  203. error_exit:
  204. return (-1);
  205. }
  206. int poll_dispatch_delete (
  207. poll_handle handle,
  208. int fd)
  209. {
  210. struct poll_instance *poll_instance;
  211. int i;
  212. SaErrorT error;
  213. int found = 0;
  214. error = saHandleInstanceGet (&poll_instance_database, handle,
  215. (void *)&poll_instance);
  216. if (error != SA_OK) {
  217. goto error_exit;
  218. }
  219. /*
  220. * Find dispatch fd to delete
  221. */
  222. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  223. if (poll_instance->poll_entries[i].ufd.fd == fd) {
  224. found = 1;
  225. break;
  226. }
  227. }
  228. if (found) {
  229. poll_instance->poll_entries[i].ufd.fd = -1;
  230. poll_instance->poll_entries[i].ufd.revents = 0;
  231. }
  232. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  233. if (poll_instance->ufds[i].fd == fd) {
  234. found = 1;
  235. break;
  236. }
  237. }
  238. if (found) {
  239. poll_instance->ufds[i].fd = -1;
  240. poll_instance->ufds[i].revents = 0;
  241. }
  242. saHandleInstancePut (&poll_instance_database, handle);
  243. return (0);
  244. error_exit:
  245. errno = EBADF;
  246. return (-1);
  247. }
  248. int poll_timer_add (
  249. poll_handle handle,
  250. int msec_in_future, void *data,
  251. void (*timer_fn) (void *data),
  252. poll_timer_handle *timer_handle_out)
  253. {
  254. struct poll_instance *poll_instance;
  255. int res = -1;
  256. SaErrorT error;
  257. error = saHandleInstanceGet (&poll_instance_database, handle,
  258. (void *)&poll_instance);
  259. if (error != SA_OK) {
  260. goto error_exit;
  261. }
  262. timerlist_add_future (&poll_instance->timerlist,
  263. timer_fn, data, msec_in_future, timer_handle_out);
  264. if (timer_handle_out != 0) {
  265. res = 0;
  266. }
  267. saHandleInstancePut (&poll_instance_database, handle);
  268. error_exit:
  269. return (res);
  270. }
  271. int poll_timer_delete (
  272. poll_handle handle,
  273. poll_timer_handle timer_handle)
  274. {
  275. struct poll_instance *poll_instance;
  276. SaErrorT error;
  277. if (timer_handle == 0) {
  278. return (0);
  279. }
  280. error = saHandleInstanceGet (&poll_instance_database, handle,
  281. (void *)&poll_instance);
  282. if (error != SA_OK) {
  283. goto error_exit;
  284. }
  285. timerlist_del (&poll_instance->timerlist, (void *)timer_handle);
  286. saHandleInstancePut (&poll_instance_database, handle);
  287. return (0);
  288. error_exit:
  289. return (-1);
  290. }
  291. int poll_entry_compare (const void *a, const void *b) {
  292. struct poll_entry *poll_entry_a = (struct poll_entry *)a;
  293. struct poll_entry *poll_entry_b = (struct poll_entry *)b;
  294. return (poll_entry_a->prio < poll_entry_b->prio);
  295. }
  296. int poll_run (
  297. poll_handle handle)
  298. {
  299. struct poll_instance *poll_instance;
  300. int i;
  301. int timeout = -1;
  302. int res;
  303. SaErrorT error;
  304. int poll_entry_count;
  305. error = saHandleInstanceGet (&poll_instance_database, handle,
  306. (void *)&poll_instance);
  307. if (error != SA_OK) {
  308. goto error_exit;
  309. }
  310. for (;;) {
  311. /*
  312. * Sort the poll entries list highest priority to lowest priority
  313. * Then build ufds structure for use with poll system call
  314. */
  315. qsort (poll_instance->poll_entries, poll_instance->poll_entry_count,
  316. sizeof (struct poll_entry), poll_entry_compare);
  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. res = poll_instance->poll_entries[i].dispatch_fn (handle,
  337. poll_instance->ufds[i].fd,
  338. poll_instance->ufds[i].revents,
  339. poll_instance->poll_entries[i].data,
  340. &poll_instance->poll_entries[i].prio);
  341. /*
  342. * Remove dispatch functions that return -1
  343. */
  344. if (res == -1) {
  345. poll_instance->poll_entries[i].ufd.fd = -1; /* empty entry */
  346. }
  347. }
  348. }
  349. timerlist_expire (&poll_instance->timerlist);
  350. } /* for (;;) */
  351. saHandleInstancePut (&poll_instance_database, handle);
  352. error_exit:
  353. return (-1);
  354. }
  355. int poll_stop (
  356. poll_handle handle);