aispoll.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. saHandleInstancePut (&poll_instance_database, handle);
  231. return (0);
  232. }
  233. saHandleInstancePut (&poll_instance_database, handle);
  234. error_exit:
  235. errno = EBADF;
  236. return (-1);
  237. }
  238. int poll_timer_add (
  239. poll_handle handle,
  240. int msec_in_future, void *data,
  241. void (*timer_fn) (void *data),
  242. poll_timer_handle *timer_handle_out)
  243. {
  244. struct poll_instance *poll_instance;
  245. int res = -1;
  246. SaErrorT error;
  247. error = saHandleInstanceGet (&poll_instance_database, handle,
  248. (void *)&poll_instance);
  249. if (error != SA_OK) {
  250. goto error_exit;
  251. }
  252. timerlist_add_future (&poll_instance->timerlist,
  253. timer_fn, data, msec_in_future, timer_handle_out);
  254. if (timer_handle_out != 0) {
  255. res = 0;
  256. }
  257. saHandleInstancePut (&poll_instance_database, handle);
  258. error_exit:
  259. return (res);
  260. }
  261. int poll_timer_delete (
  262. poll_handle handle,
  263. poll_timer_handle timer_handle)
  264. {
  265. struct poll_instance *poll_instance;
  266. SaErrorT error;
  267. if (timer_handle == 0) {
  268. return (0);
  269. }
  270. error = saHandleInstanceGet (&poll_instance_database, handle,
  271. (void *)&poll_instance);
  272. if (error != SA_OK) {
  273. goto error_exit;
  274. }
  275. timerlist_del (&poll_instance->timerlist, (void *)timer_handle);
  276. saHandleInstancePut (&poll_instance_database, handle);
  277. return (0);
  278. error_exit:
  279. return (-1);
  280. }
  281. int poll_entry_compare (const void *a, const void *b) {
  282. struct poll_entry *poll_entry_a = (struct poll_entry *)a;
  283. struct poll_entry *poll_entry_b = (struct poll_entry *)b;
  284. return (poll_entry_a->prio < poll_entry_b->prio);
  285. }
  286. int poll_run (
  287. poll_handle handle)
  288. {
  289. struct poll_instance *poll_instance;
  290. int i;
  291. int timeout = -1;
  292. int res;
  293. SaErrorT error;
  294. int poll_entry_count;
  295. error = saHandleInstanceGet (&poll_instance_database, handle,
  296. (void *)&poll_instance);
  297. if (error != SA_OK) {
  298. goto error_exit;
  299. }
  300. for (;;) {
  301. /*
  302. * Sort the poll entries list highest priority to lowest priority
  303. * Then build ufds structure for use with poll system call
  304. */
  305. qsort (poll_instance->poll_entries, poll_instance->poll_entry_count,
  306. sizeof (struct poll_entry), poll_entry_compare);
  307. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  308. memcpy (&poll_instance->ufds[i],
  309. &poll_instance->poll_entries[i].ufd,
  310. sizeof (struct pollfd));
  311. }
  312. timeout = timerlist_timeout_msec (&poll_instance->timerlist);
  313. retry_poll:
  314. res = poll (poll_instance->ufds,
  315. poll_instance->poll_entry_count, timeout);
  316. if (errno == EINTR && res == -1) {
  317. goto retry_poll;
  318. } else
  319. if (res == -1) {
  320. goto error_exit;
  321. }
  322. poll_entry_count = poll_instance->poll_entry_count;
  323. for (i = 0; i < poll_entry_count; i++) {
  324. if (poll_instance->ufds[i].fd != -1 &&
  325. poll_instance->ufds[i].revents) {
  326. res = poll_instance->poll_entries[i].dispatch_fn (handle,
  327. poll_instance->ufds[i].fd,
  328. poll_instance->ufds[i].revents,
  329. poll_instance->poll_entries[i].data,
  330. &poll_instance->poll_entries[i].prio);
  331. /*
  332. * Remove dispatch functions that return -1
  333. */
  334. if (res == -1) {
  335. poll_instance->poll_entries[i].ufd.fd = -1; /* empty entry */
  336. }
  337. }
  338. }
  339. timerlist_expire (&poll_instance->timerlist);
  340. } /* for (;;) */
  341. saHandleInstancePut (&poll_instance_database, handle);
  342. error_exit:
  343. return (-1);
  344. }
  345. int poll_stop (
  346. poll_handle handle);