aispoll.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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);
  45. struct poll_instance {
  46. struct pollfd *ufds;
  47. int nfds;
  48. dispatch_fn_t *dispatch_fns;
  49. void **data;
  50. struct timerlist timerlist;
  51. };
  52. /*
  53. * All instances in one database
  54. */
  55. static struct saHandleDatabase poll_instance_database = {
  56. .handleCount = 0,
  57. .handles = 0,
  58. .handleInstanceDestructor = 0
  59. };
  60. poll_handle poll_create (void)
  61. {
  62. poll_handle handle;
  63. struct poll_instance *poll_instance;
  64. SaErrorT error;
  65. error = saHandleCreate (&poll_instance_database,
  66. sizeof (struct poll_instance), &handle);
  67. if (error != SA_OK) {
  68. goto error_exit;
  69. }
  70. error = saHandleInstanceGet (&poll_instance_database, handle,
  71. (void *)&poll_instance);
  72. if (error != SA_OK) {
  73. goto error_destroy;
  74. }
  75. poll_instance->ufds = 0;
  76. poll_instance->nfds = 0;
  77. poll_instance->dispatch_fns = 0;
  78. poll_instance->data = 0;
  79. timerlist_init (&poll_instance->timerlist);
  80. return (handle);
  81. error_destroy:
  82. saHandleDestroy (&poll_instance_database, handle);
  83. error_exit:
  84. return (-1);
  85. }
  86. int poll_destroy (poll_handle handle)
  87. {
  88. struct poll_instance *poll_instance;
  89. SaErrorT error;
  90. error = saHandleInstanceGet (&poll_instance_database, handle,
  91. (void *)&poll_instance);
  92. if (error != SA_OK) {
  93. goto error_exit;
  94. }
  95. if (poll_instance->ufds) {
  96. free (poll_instance->ufds);
  97. }
  98. if (poll_instance->dispatch_fns) {
  99. free (poll_instance->dispatch_fns);
  100. }
  101. if (poll_instance->data) {
  102. free (poll_instance->data);
  103. }
  104. timerlist_free (&poll_instance->timerlist);
  105. saHandleDestroy (&poll_instance_database, handle);
  106. saHandleInstancePut (&poll_instance_database, handle);
  107. return (0);
  108. error_exit:
  109. return (-1);
  110. }
  111. int poll_dispatch_add (
  112. poll_handle handle,
  113. int fd,
  114. int events,
  115. void *data,
  116. int (*dispatch_fn) (poll_handle poll_handle, int fd, int revents, void *data))
  117. {
  118. struct poll_instance *poll_instance;
  119. struct pollfd *ufds;
  120. dispatch_fn_t *dispatch_fns;
  121. void **data_list;
  122. int found = 0;
  123. int install_pos;
  124. SaErrorT error;
  125. error = saHandleInstanceGet (&poll_instance_database, handle,
  126. (void *)&poll_instance);
  127. if (error != SA_OK) {
  128. goto error_exit;
  129. }
  130. for (found = 0, install_pos = 0; install_pos < poll_instance->nfds; install_pos++) {
  131. if (poll_instance->ufds[install_pos].fd == -1) {
  132. found = 1;
  133. break;
  134. }
  135. }
  136. if (found == 0) {
  137. /*
  138. * Grow pollfd list
  139. */
  140. ufds = (struct pollfd *)realloc (poll_instance->ufds,
  141. (poll_instance->nfds + 1) * sizeof (struct pollfd));
  142. if (ufds == 0) {
  143. errno = ENOMEM;
  144. goto error_exit;
  145. }
  146. poll_instance->ufds = ufds;
  147. /*
  148. * Grow dispatch functions list
  149. */
  150. dispatch_fns = (dispatch_fn_t *)realloc (poll_instance->dispatch_fns,
  151. (poll_instance->nfds + 1) * sizeof (dispatch_fn_t));
  152. if (dispatch_fns == 0) {
  153. errno = ENOMEM;
  154. goto error_exit;
  155. }
  156. poll_instance->dispatch_fns = dispatch_fns;
  157. /*
  158. * Grow data list
  159. */
  160. data_list = (void **)realloc (poll_instance->data,
  161. (poll_instance->nfds + 1) * sizeof (void *));
  162. if (data_list == 0) {
  163. errno = ENOMEM;
  164. goto error_exit;
  165. }
  166. poll_instance->data = data_list;
  167. poll_instance->nfds += 1;
  168. install_pos = poll_instance->nfds - 1;
  169. }
  170. /*
  171. * Install new dispatch handler
  172. */
  173. poll_instance->ufds[install_pos].fd = fd;
  174. poll_instance->ufds[install_pos].events = events;
  175. poll_instance->ufds[install_pos].revents = 0;
  176. poll_instance->dispatch_fns[install_pos] = dispatch_fn;
  177. poll_instance->data[install_pos] = data;
  178. saHandleInstancePut (&poll_instance_database, handle);
  179. return (0);
  180. error_exit:
  181. return (-1);
  182. }
  183. int poll_dispatch_modify (
  184. poll_handle handle,
  185. int fd,
  186. int events,
  187. int (*dispatch_fn) (poll_handle poll_handle, int fd, int revents, void *data))
  188. {
  189. struct poll_instance *poll_instance;
  190. int i;
  191. SaErrorT error;
  192. error = saHandleInstanceGet (&poll_instance_database, handle,
  193. (void *)&poll_instance);
  194. if (error != SA_OK) {
  195. goto error_exit;
  196. }
  197. /*
  198. * Find file descriptor to modify events and dispatch function
  199. */
  200. for (i = 0; i < poll_instance->nfds; i++) {
  201. if (poll_instance->ufds[i].fd == fd) {
  202. poll_instance->ufds[i].events = events;
  203. poll_instance->dispatch_fns[i] = dispatch_fn;
  204. return (0);
  205. }
  206. }
  207. errno = EBADF;
  208. saHandleInstancePut (&poll_instance_database, handle);
  209. error_exit:
  210. return (-1);
  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. error = saHandleInstanceGet (&poll_instance_database, handle,
  221. (void *)&poll_instance);
  222. if (error != SA_OK) {
  223. goto error_exit;
  224. }
  225. /*
  226. * Find dispatch fd to delete
  227. */
  228. for (i = 0; i < poll_instance->nfds; i++) {
  229. if (poll_instance->ufds[i].fd == fd) {
  230. found = 1;
  231. break;
  232. }
  233. }
  234. if (found) {
  235. poll_instance->ufds[i].fd = -1;
  236. saHandleInstancePut (&poll_instance_database, handle);
  237. return (0);
  238. }
  239. saHandleInstancePut (&poll_instance_database, handle);
  240. error_exit:
  241. errno = EBADF;
  242. return (-1);
  243. }
  244. int poll_timer_add (
  245. poll_handle handle,
  246. int msec_in_future, void *data,
  247. void (*timer_fn) (void *data),
  248. poll_timer_handle *timer_handle_out)
  249. {
  250. struct poll_instance *poll_instance;
  251. poll_timer_handle timer_handle;
  252. int res = -1;
  253. SaErrorT error;
  254. error = saHandleInstanceGet (&poll_instance_database, handle,
  255. (void *)&poll_instance);
  256. if (error != SA_OK) {
  257. goto error_exit;
  258. }
  259. timer_handle = (poll_timer_handle)timerlist_add_future (&poll_instance->timerlist,
  260. timer_fn, data, msec_in_future);
  261. if (timer_handle != 0) {
  262. *timer_handle_out = timer_handle;
  263. res = 0;
  264. }
  265. saHandleInstancePut (&poll_instance_database, handle);
  266. error_exit:
  267. return (res);
  268. }
  269. int poll_timer_delete (
  270. poll_handle handle,
  271. poll_timer_handle timer_handle)
  272. {
  273. struct poll_instance *poll_instance;
  274. SaErrorT error;
  275. if (timer_handle == 0) {
  276. return (0);
  277. }
  278. error = saHandleInstanceGet (&poll_instance_database, handle,
  279. (void *)&poll_instance);
  280. if (error != SA_OK) {
  281. goto error_exit;
  282. }
  283. timerlist_del (&poll_instance->timerlist, (void *)timer_handle);
  284. saHandleInstancePut (&poll_instance_database, handle);
  285. return (0);
  286. error_exit:
  287. return (-1);
  288. }
  289. int poll_run (
  290. poll_handle handle)
  291. {
  292. struct poll_instance *poll_instance;
  293. int i;
  294. int timeout = -1;
  295. int res;
  296. SaErrorT error;
  297. error = saHandleInstanceGet (&poll_instance_database, handle,
  298. (void *)&poll_instance);
  299. if (error != SA_OK) {
  300. goto error_exit;
  301. }
  302. for (;;) {
  303. timeout = timerlist_timeout_msec (&poll_instance->timerlist);
  304. retry_poll:
  305. res = poll (poll_instance->ufds, poll_instance->nfds, timeout);
  306. if (errno == EINTR && res == -1) {
  307. goto retry_poll;
  308. } else
  309. if (res == -1) {
  310. goto error_exit;
  311. }
  312. for (i = 0; i < poll_instance->nfds; i++) {
  313. if (poll_instance->ufds[i].fd != -1 &&
  314. poll_instance->ufds[i].revents) {
  315. res = poll_instance->dispatch_fns[i] (handle, poll_instance->ufds[i].fd,
  316. poll_instance->ufds[i].revents, poll_instance->data[i]);
  317. /*
  318. * Remove dispatch functions that return -1
  319. */
  320. if (res == -1) {
  321. poll_instance->ufds[i].fd = -1; /* empty entry */
  322. }
  323. }
  324. }
  325. timerlist_expire (&poll_instance->timerlist);
  326. } /* for (;;) */
  327. saHandleInstancePut (&poll_instance_database, handle);
  328. error_exit:
  329. return (-1);
  330. }
  331. int poll_stop (
  332. poll_handle handle);