su_wait.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2005 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. /**@ingroup su_wait
  25. *
  26. * @CFILE su_wait.c
  27. * Implementation of OS-independent socket synchronization interface.
  28. *
  29. * This looks like nth reincarnation of "reactor". It implements the
  30. * (poll()/select()/WaitForMultipleObjects()) functionality.
  31. *
  32. * @author Pekka Pessi <Pekka.Pessi@nokia.com>
  33. * @author Martti Mela <Martti.Mela@nokia.com>
  34. * @date Created: Tue Sep 14 15:51:04 1999 ppessi
  35. *
  36. */
  37. #include "config.h"
  38. #include <stdlib.h>
  39. #include <assert.h>
  40. #include <stdio.h>
  41. #define SU_INTERNAL_P su_root_t *
  42. #include "sofia-sip/su.h"
  43. #include "sofia-sip/su_wait.h"
  44. #include "sofia-sip/su_alloc.h"
  45. /**@defgroup su_wait Syncronization and Threading
  46. * @brief Syncronization and threading interface.
  47. *
  48. * The Sofia utility library provides simple OS-independent synchronization
  49. * interface. The synchronization interface contains primitives for managing
  50. * events, messages, timers and threads.
  51. *
  52. */
  53. /**@ingroup su_wait
  54. * @defgroup su_root_ex Example and test code for syncronization and threads
  55. *
  56. * Example programs demonstrate the su syncronization and threading
  57. * primitives.
  58. */
  59. /**@ingroup su_wait
  60. *
  61. * @page su_wait_t Wait objects
  62. *
  63. * Wait objects are used to signal I/O events to the process.
  64. * The events are as follows:
  65. *
  66. * - SU_WAIT_IN - incoming data is available on socket
  67. * - SU_WAIT_OUT - data can be sent on socket
  68. * - SU_WAIT_ERR - an error occurred on socket
  69. * - SU_WAIT_HUP - the socket connection was closed
  70. * - SU_WAIT_ACCEPT - a listening socket accepted a new connection attempt
  71. *
  72. * It is possible to combine several events with |, binary or operator.
  73. *
  74. * The wait objects can be managed with functions as follows:
  75. * - su_wait_create()
  76. * - su_wait_destroy()
  77. * - su_wait()
  78. * - su_wait_events()
  79. * - su_wait_mask()
  80. *
  81. * @note
  82. * In Unix, the wait object is @c struct @c poll. The structure contains a
  83. * file descriptor, a mask describing expected events, and a mask
  84. * containing the occurred events after calling @c su_wait(), ie. poll().
  85. *
  86. * @note
  87. * In Windows, the wait object is a @c HANDLE (a descriptor of a Windows
  88. * kernel entity).
  89. *
  90. */
  91. /**Initialize a wait object.
  92. *
  93. * The function su_wait_init initializes a memory area of a su_wait_t
  94. * object.
  95. */
  96. void su_wait_init(su_wait_t dst[1])
  97. {
  98. su_wait_t const src = SU_WAIT_INIT;
  99. *dst = src;
  100. }
  101. /**Create a wait object.
  102. *
  103. * The function su_wait_create() creates a new su_wait_t object for an @a
  104. * socket, with given @a events. The new wait object is assigned to the @a
  105. * newwait parameter.
  106. *
  107. * There can be only one wait object per socket. (This is a limitation or
  108. * feature of WinSock interface; the limitation is not enforced on other
  109. * platforms).
  110. *
  111. * As a side-effect the socket is put into non-blocking mode when wait
  112. * object is created.
  113. *
  114. * @param newwait the newly created wait object (output)
  115. * @param socket socket
  116. * @param events mask for events that can signal this wait object
  117. *
  118. * @retval 0 if the call was successful,
  119. * @retval -1 upon an error.
  120. */
  121. int su_wait_create(su_wait_t *newwait, su_socket_t socket, int events)
  122. {
  123. #if SU_HAVE_WINSOCK
  124. HANDLE h = WSACreateEvent();
  125. if (newwait == NULL || events == 0 || socket == INVALID_SOCKET) {
  126. su_seterrno(WSAEINVAL);
  127. return -1;
  128. }
  129. *newwait = 0;
  130. if (WSAEventSelect(socket, h, events) != 0) {
  131. int error = su_errno();
  132. WSACloseEvent(h);
  133. su_seterrno(error);
  134. return -1;
  135. }
  136. *newwait = h;
  137. #elif SU_HAVE_POLL || HAVE_SELECT
  138. int mode;
  139. if (newwait == NULL || events == 0 || socket == INVALID_SOCKET) {
  140. su_seterrno(EINVAL);
  141. return -1;
  142. }
  143. mode = fcntl(socket, F_GETFL, 0);
  144. if (mode < 0)
  145. return -1;
  146. mode |= O_NDELAY | O_NONBLOCK;
  147. if (fcntl(socket, F_SETFL, mode) < 0)
  148. return -1;
  149. newwait->fd = socket;
  150. newwait->events = events;
  151. newwait->revents = 0;
  152. #endif
  153. return 0;
  154. }
  155. /** Destroy a wait object.
  156. *
  157. * The function su_wait_destroy() destroys a su_wait_t object.
  158. *
  159. * @param waitobj pointer to wait object
  160. *
  161. * @retval 0 when successful,
  162. * @retval -1 upon an error.
  163. */
  164. int su_wait_destroy(su_wait_t *waitobj)
  165. {
  166. #if SU_HAVE_WINSOCK
  167. su_wait_t w0 = NULL;
  168. assert(waitobj != NULL);
  169. if (*waitobj) {
  170. WSACloseEvent(*waitobj);
  171. *waitobj = w0;
  172. }
  173. #else
  174. su_wait_t w0 = { INVALID_SOCKET, 0, 0 };
  175. assert(waitobj != NULL);
  176. if (waitobj) {
  177. *waitobj = w0;
  178. }
  179. #endif
  180. return waitobj ? 0 : -1;
  181. }
  182. /**Wait for multiple events.
  183. *
  184. * The function su_wait() blocks until an event specified by wait objects in
  185. * @a wait array. If @a timeout is not SU_WAIT_FOREVER, a timeout occurs
  186. * after @a timeout milliseconds.
  187. *
  188. * In Unix, this is @c poll() or @c select().
  189. *
  190. * In Windows, this is @c WSAWaitForMultipleEvents().
  191. *
  192. * @param waits array of wait objects
  193. * @param n number of wait objects in array waits
  194. * @param timeout timeout in milliseconds
  195. *
  196. * @retval Index of the signaled wait object, if any,
  197. * @retval SU_WAIT_TIMEOUT if timeout occurred, or
  198. * @retval -1 upon an error.
  199. */
  200. int su_wait(su_wait_t waits[], unsigned n, su_duration_t timeout)
  201. {
  202. #if SU_HAVE_WINSOCK
  203. DWORD i;
  204. if (n > 0) {
  205. #define WAIT_EVENT_BLOCK_SIZE WSA_MAXIMUM_WAIT_EVENTS
  206. /* Handle at most WAIT_EVENT_BLOCK_SIZE wait objects at a time */
  207. int blocks = (n + WAIT_EVENT_BLOCK_SIZE - 1) / WAIT_EVENT_BLOCK_SIZE;
  208. int block_index = 0;
  209. int first_wait_index = 0;
  210. int millisec_per_block = timeout / blocks;
  211. if (timeout > 0)
  212. millisec_per_block = max(1, millisec_per_block);
  213. i = WSA_WAIT_TIMEOUT;
  214. for(block_index = 0; block_index < blocks; block_index++,first_wait_index+=WAIT_EVENT_BLOCK_SIZE)
  215. {
  216. int remaining_blocks = n - block_index * WAIT_EVENT_BLOCK_SIZE;
  217. int waits_in_current_block = min( WAIT_EVENT_BLOCK_SIZE, remaining_blocks );
  218. i = WSAWaitForMultipleEvents(waits_in_current_block, waits + first_wait_index, FALSE, millisec_per_block, FALSE);
  219. if (i != WSA_WAIT_TIMEOUT) {
  220. /* Did not timeout, return something NOW, ignore remaining blocks */
  221. if (i != WSA_WAIT_FAILED) {
  222. /* Return the right index */
  223. i += first_wait_index;
  224. }
  225. break;
  226. }
  227. }
  228. } else {
  229. return Sleep(timeout), SU_WAIT_TIMEOUT;
  230. }
  231. if (i == WSA_WAIT_TIMEOUT)
  232. return SU_WAIT_TIMEOUT;
  233. else if (i == WSA_WAIT_FAILED)
  234. return SOCKET_ERROR;
  235. else
  236. return i;
  237. #elif SU_HAVE_POLL || HAVE_SELECT
  238. for (;;) {
  239. int i = poll(waits, n, timeout);
  240. if (i == 0)
  241. return SU_WAIT_TIMEOUT;
  242. if (i > 0) {
  243. unsigned j;
  244. for (j = 0; j < n; j++) {
  245. if (waits[j].revents)
  246. return j;
  247. }
  248. }
  249. if (errno == EINTR)
  250. continue;
  251. return -1;
  252. }
  253. #endif
  254. }
  255. /** Get events.
  256. *
  257. * The function su_wait_events() returns an mask describing events occurred.
  258. *
  259. * @param waitobj pointer to wait object
  260. * @param s socket
  261. *
  262. * @return Binary mask describing the events.
  263. */
  264. int su_wait_events(su_wait_t *waitobj, su_socket_t s)
  265. {
  266. #if SU_HAVE_WINSOCK
  267. WSANETWORKEVENTS net_events;
  268. if (WSAEnumNetworkEvents(s, *waitobj, &net_events) != 0)
  269. return SOCKET_ERROR;
  270. return net_events.lNetworkEvents;
  271. #elif SU_HAVE_POLL || HAVE_SELECT
  272. /* poll(e, 1, 0); */
  273. return waitobj->revents;
  274. #endif
  275. }
  276. /** Set event mask.
  277. *
  278. * The function su_wait_mask() sets the mask describing events that can
  279. * signal the wait object.
  280. *
  281. * @param waitobj pointer to wait object
  282. * @param s socket
  283. * @param events new event mask
  284. *
  285. * @retval 0 when successful,
  286. * @retval -1 upon an error.
  287. */
  288. int su_wait_mask(su_wait_t *waitobj, su_socket_t s, int events)
  289. {
  290. #if SU_HAVE_WINSOCK
  291. HANDLE e = *waitobj;
  292. if (WSAEventSelect(s, e, events) != 0) {
  293. int error = WSAGetLastError();
  294. WSACloseEvent(e);
  295. WSASetLastError(error);
  296. return -1;
  297. }
  298. #elif SU_HAVE_POLL || HAVE_SELECT
  299. waitobj->fd = s;
  300. waitobj->events = events;
  301. waitobj->revents = 0;
  302. #endif
  303. return 0;
  304. }