aispoll.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 "aispoll.h"
  40. #include "../include/list.h"
  41. #include "hdb.h"
  42. #include "../include/ais_types.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 saHandleDatabase poll_instance_database = {
  61. .handleCount = 0,
  62. .handles = 0,
  63. .handleInstanceDestructor = 0
  64. };
  65. poll_handle poll_create (void)
  66. {
  67. poll_handle handle;
  68. struct poll_instance *poll_instance;
  69. SaErrorT error;
  70. error = saHandleCreate (&poll_instance_database,
  71. sizeof (struct poll_instance), &handle);
  72. if (error != SA_OK) {
  73. goto error_exit;
  74. }
  75. error = saHandleInstanceGet (&poll_instance_database, handle,
  76. (void *)&poll_instance);
  77. if (error != SA_OK) {
  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. saHandleDestroy (&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. SaErrorT error;
  94. error = saHandleInstanceGet (&poll_instance_database, handle,
  95. (void *)&poll_instance);
  96. if (error != SA_OK) {
  97. goto error_exit;
  98. }
  99. if (poll_instance->poll_entries) {
  100. free (poll_instance->poll_entries);
  101. }
  102. if (poll_instance->ufds) {
  103. free (poll_instance->ufds);
  104. }
  105. timerlist_free (&poll_instance->timerlist);
  106. saHandleDestroy (&poll_instance_database, handle);
  107. saHandleInstancePut (&poll_instance_database, handle);
  108. return (0);
  109. error_exit:
  110. return (-1);
  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. SaErrorT error;
  126. error = saHandleInstanceGet (&poll_instance_database, handle,
  127. (void *)&poll_instance);
  128. if (error != SA_OK) {
  129. goto error_exit;
  130. }
  131. for (found = 0, install_pos = 0; install_pos < poll_instance->poll_entry_count; install_pos++) {
  132. if (poll_instance->poll_entries[install_pos].ufd.fd == -1) {
  133. found = 1;
  134. break;
  135. }
  136. }
  137. if (found == 0) {
  138. /*
  139. * Grow pollfd list
  140. */
  141. poll_entries = (struct poll_entry *)realloc (poll_instance->poll_entries,
  142. (poll_instance->poll_entry_count + 1) *
  143. sizeof (struct poll_entry));
  144. if (poll_entries == 0) {
  145. errno = ENOMEM;
  146. goto error_exit;
  147. }
  148. poll_instance->poll_entries = poll_entries;
  149. ufds = (struct pollfd *)realloc (poll_instance->ufds,
  150. (poll_instance->poll_entry_count + 1) *
  151. sizeof (struct pollfd));
  152. if (ufds == 0) {
  153. errno = ENOMEM;
  154. goto error_exit;
  155. }
  156. poll_instance->ufds = ufds;
  157. poll_instance->poll_entry_count += 1;
  158. install_pos = poll_instance->poll_entry_count - 1;
  159. }
  160. /*
  161. * Install new dispatch handler
  162. */
  163. poll_instance->poll_entries[install_pos].prio = prio;
  164. poll_instance->poll_entries[install_pos].ufd.fd = fd;
  165. poll_instance->poll_entries[install_pos].ufd.events = events;
  166. poll_instance->poll_entries[install_pos].ufd.revents = 0;
  167. poll_instance->poll_entries[install_pos].dispatch_fn = dispatch_fn;
  168. poll_instance->poll_entries[install_pos].data = data;
  169. saHandleInstancePut (&poll_instance_database, handle);
  170. return (0);
  171. error_exit:
  172. return (-1);
  173. }
  174. int poll_dispatch_modify (
  175. poll_handle handle,
  176. int fd,
  177. int events,
  178. int (*dispatch_fn) (poll_handle poll_handle, int fd, int revents, void *data, unsigned int *prio),
  179. unsigned int prio)
  180. {
  181. struct poll_instance *poll_instance;
  182. int i;
  183. SaErrorT error;
  184. error = saHandleInstanceGet (&poll_instance_database, handle,
  185. (void *)&poll_instance);
  186. if (error != SA_OK) {
  187. goto error_exit;
  188. }
  189. /*
  190. * Find file descriptor to modify events and dispatch function
  191. */
  192. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  193. if (poll_instance->poll_entries[i].ufd.fd == fd) {
  194. poll_instance->poll_entries[i].ufd.events = events;
  195. poll_instance->poll_entries[i].dispatch_fn = dispatch_fn;
  196. poll_instance->poll_entries[i].prio = prio;
  197. return (0);
  198. }
  199. }
  200. errno = EBADF;
  201. saHandleInstancePut (&poll_instance_database, handle);
  202. error_exit:
  203. return (-1);
  204. }
  205. int poll_dispatch_delete (
  206. poll_handle handle,
  207. int fd)
  208. {
  209. struct poll_instance *poll_instance;
  210. int i;
  211. SaErrorT error;
  212. int found = 0;
  213. error = saHandleInstanceGet (&poll_instance_database, handle,
  214. (void *)&poll_instance);
  215. if (error != SA_OK) {
  216. goto error_exit;
  217. }
  218. /*
  219. * Find dispatch fd to delete
  220. */
  221. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  222. if (poll_instance->poll_entries[i].ufd.fd == fd) {
  223. found = 1;
  224. break;
  225. }
  226. }
  227. if (found) {
  228. poll_instance->poll_entries[i].ufd.fd = -1;
  229. saHandleInstancePut (&poll_instance_database, handle);
  230. return (0);
  231. }
  232. saHandleInstancePut (&poll_instance_database, handle);
  233. error_exit:
  234. errno = EBADF;
  235. return (-1);
  236. }
  237. int poll_timer_add (
  238. poll_handle handle,
  239. int msec_in_future, void *data,
  240. void (*timer_fn) (void *data),
  241. poll_timer_handle *timer_handle_out)
  242. {
  243. struct poll_instance *poll_instance;
  244. int res = -1;
  245. SaErrorT error;
  246. error = saHandleInstanceGet (&poll_instance_database, handle,
  247. (void *)&poll_instance);
  248. if (error != SA_OK) {
  249. goto error_exit;
  250. }
  251. timerlist_add_future (&poll_instance->timerlist,
  252. timer_fn, data, msec_in_future, timer_handle_out);
  253. if (timer_handle_out != 0) {
  254. res = 0;
  255. }
  256. saHandleInstancePut (&poll_instance_database, handle);
  257. error_exit:
  258. return (res);
  259. }
  260. int poll_timer_delete (
  261. poll_handle handle,
  262. poll_timer_handle timer_handle)
  263. {
  264. struct poll_instance *poll_instance;
  265. SaErrorT error;
  266. if (timer_handle == 0) {
  267. return (0);
  268. }
  269. error = saHandleInstanceGet (&poll_instance_database, handle,
  270. (void *)&poll_instance);
  271. if (error != SA_OK) {
  272. goto error_exit;
  273. }
  274. timerlist_del (&poll_instance->timerlist, (void *)timer_handle);
  275. saHandleInstancePut (&poll_instance_database, handle);
  276. return (0);
  277. error_exit:
  278. return (-1);
  279. }
  280. int poll_entry_compare (const void *a, const void *b) {
  281. struct poll_entry *poll_entry_a = (struct poll_entry *)a;
  282. struct poll_entry *poll_entry_b = (struct poll_entry *)b;
  283. return (poll_entry_a->prio < poll_entry_b->prio);
  284. }
  285. int poll_run (
  286. poll_handle handle)
  287. {
  288. struct poll_instance *poll_instance;
  289. int i;
  290. int timeout = -1;
  291. int res;
  292. SaErrorT error;
  293. int poll_entry_count;
  294. error = saHandleInstanceGet (&poll_instance_database, handle,
  295. (void *)&poll_instance);
  296. if (error != SA_OK) {
  297. goto error_exit;
  298. }
  299. for (;;) {
  300. /*
  301. * Sort the poll entries list highest priority to lowest priority
  302. * Then build ufds structure for use with poll system call
  303. */
  304. qsort (poll_instance->poll_entries, poll_instance->poll_entry_count,
  305. sizeof (struct poll_entry), poll_entry_compare);
  306. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  307. memcpy (&poll_instance->ufds[i],
  308. &poll_instance->poll_entries[i].ufd,
  309. sizeof (struct pollfd));
  310. }
  311. timeout = timerlist_timeout_msec (&poll_instance->timerlist);
  312. retry_poll:
  313. res = poll (poll_instance->ufds,
  314. poll_instance->poll_entry_count, timeout);
  315. if (errno == EINTR && res == -1) {
  316. goto retry_poll;
  317. } else
  318. if (res == -1) {
  319. goto error_exit;
  320. }
  321. poll_entry_count = poll_instance->poll_entry_count;
  322. for (i = 0; i < poll_entry_count; i++) {
  323. if (poll_instance->ufds[i].fd != -1 &&
  324. poll_instance->ufds[i].revents) {
  325. res = poll_instance->poll_entries[i].dispatch_fn (handle,
  326. poll_instance->ufds[i].fd,
  327. poll_instance->ufds[i].revents,
  328. poll_instance->poll_entries[i].data,
  329. &poll_instance->poll_entries[i].prio);
  330. /*
  331. * Remove dispatch functions that return -1
  332. */
  333. if (res == -1) {
  334. poll_instance->poll_entries[i].ufd.fd = -1; /* empty entry */
  335. }
  336. }
  337. }
  338. timerlist_expire (&poll_instance->timerlist);
  339. } /* for (;;) */
  340. saHandleInstancePut (&poll_instance_database, handle);
  341. error_exit:
  342. return (-1);
  343. }
  344. int poll_stop (
  345. poll_handle handle);