su_wait.c 7.68 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@ingroup su_wait
 *
27
 * @CFILE su_wait.c
Pekka Pessi's avatar
Pekka Pessi committed
28 29 30 31 32 33
 * Implementation of OS-independent socket synchronization interface.
 *
 * This looks like nth reincarnation of "reactor".  It implements the
 * (poll()/select()/WaitForMultipleObjects()) functionality.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
34
 * @author Martti Mela <Martti.Mela@nokia.com>
Pekka Pessi's avatar
Pekka Pessi committed
35 36 37 38
 * @date Created: Tue Sep 14 15:51:04 1999 ppessi
 *
 */

Michael Jerris's avatar
Michael Jerris committed
39 40
#include "config.h"

Pekka Pessi's avatar
Pekka Pessi committed
41 42 43 44 45
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>

#define SU_INTERNAL_P su_root_t *
Pekka Pessi's avatar
Pekka Pessi committed
46

47 48 49
#include "sofia-sip/su.h"
#include "sofia-sip/su_wait.h"
#include "sofia-sip/su_alloc.h"
Pekka Pessi's avatar
Pekka Pessi committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

/**@defgroup su_wait Syncronization and Threading
 * @brief Syncronization and threading interface.
 *
 * The Sofia utility library provides simple OS-independent synchronization
 * interface. The synchronization interface contains primitives for managing
 * events, messages, timers and threads.
 *
 */

/**@ingroup su_wait
 * @defgroup su_root_ex Example and test code for syncronization and threads
 *
 * Example programs demonstrate the su syncronization and threading
 * primitives.
 */


/**@ingroup su_wait
 *
 * @page su_wait_t Wait objects
 *
 *   Wait objects are used to signal I/O events to the process.
 *   The events are as follows:
 *
 *   - SU_WAIT_IN       - incoming data is available on socket
 *   - SU_WAIT_OUT      - data can be sent on socket
 *   - SU_WAIT_ERR      - an error occurred on socket
 *   - SU_WAIT_HUP      - the socket connection was closed
 *   - SU_WAIT_ACCEPT   - a listening socket accepted a new connection attempt
 *
 *   It is possible to combine several events with |, binary or operator.
 *
 *   The wait objects can be managed with functions as follows:
 *   - su_wait_create()
 *   - su_wait_destroy()
 *   - su_wait()
 *   - su_wait_events()
 *   - su_wait_mask()
 *
 * @note
 *   In Unix, the wait object is @c struct @c poll. The structure contains a
 *   file descriptor, a mask describing expected events, and a mask
 *   containing the occurred events after calling @c su_wait(), ie. poll().
 *
 * @note
 *   In Windows, the wait object is a @c HANDLE (a descriptor of a Windows
 *   kernel entity).
 *
 */

/**Initialize a wait object.
 *
 * The function su_wait_init initializes a memory area of a su_wait_t
 * object.
 */
void su_wait_init(su_wait_t dst[1])
{
  su_wait_t const src = SU_WAIT_INIT;
  *dst = src;
}

/**Create a wait object.
 *
 * The function su_wait_create() creates a new su_wait_t object for an @a
 * socket, with given @a events.  The new wait object is assigned to the @a
 * newwait parameter.
117 118 119 120 121 122 123
 *
 * There can be only one wait object per socket. (This is a limitation or
 * feature of WinSock interface; the limitation is not enforced on other
 * platforms).
 *
 * As a side-effect the socket is put into non-blocking mode when wait
 * object is created.
124
 *
Pekka Pessi's avatar
Pekka Pessi committed
125 126 127
 * @param newwait  the newly created wait object (output)
 * @param socket   socket
 * @param events   mask for events that can signal this wait object
128
 *
Pekka Pessi's avatar
Pekka Pessi committed
129
 * @retval 0 if the call was successful,
130
 * @retval -1 upon an error.
Pekka Pessi's avatar
Pekka Pessi committed
131 132 133 134 135 136
*/
int su_wait_create(su_wait_t *newwait, su_socket_t socket, int events)
{
#if SU_HAVE_WINSOCK
  HANDLE h = WSACreateEvent();

137
  if (newwait == NULL || events == 0 || socket == INVALID_SOCKET) {
Pekka Pessi's avatar
Pekka Pessi committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    su_seterrno(WSAEINVAL);
    return -1;
  }

  *newwait = 0;

  if (WSAEventSelect(socket, h, events) != 0) {
    int error = su_errno();
    WSACloseEvent(h);
    su_seterrno(error);
    return -1;
  }

  *newwait = h;

Martti Mela's avatar
Martti Mela committed
153
#elif SU_HAVE_POLL || HAVE_SELECT
154
  int mode;
Pekka Pessi's avatar
Pekka Pessi committed
155

156
  if (newwait == NULL || events == 0 || socket == INVALID_SOCKET) {
Pekka Pessi's avatar
Pekka Pessi committed
157 158 159 160
    su_seterrno(EINVAL);
    return -1;
  }

161 162 163 164 165 166
  mode = fcntl(socket, F_GETFL, 0);
  if (mode < 0)
     return -1;
  mode |= O_NDELAY | O_NONBLOCK;
  if (fcntl(socket, F_SETFL, mode) < 0)
    return -1;
167

Pekka Pessi's avatar
Pekka Pessi committed
168 169 170 171 172 173 174 175 176 177 178 179
  newwait->fd = socket;
  newwait->events = events;
  newwait->revents = 0;
#endif

  return 0;
}

/** Destroy a wait object.
 *
 * The function su_wait_destroy() destroys a su_wait_t object.
 *
180
 * @param waitobj  pointer to wait object
Pekka Pessi's avatar
Pekka Pessi committed
181 182 183 184 185 186 187 188
 *
 * @retval 0 when successful,
 * @retval -1 upon an error.
 */
int su_wait_destroy(su_wait_t *waitobj)
{
#if SU_HAVE_WINSOCK
  su_wait_t w0 = NULL;
189
  assert(waitobj != NULL);
190
  if (*waitobj) {
Pekka Pessi's avatar
Pekka Pessi committed
191
    WSACloseEvent(*waitobj);
192 193
    *waitobj = w0;
  }
194 195
#else
  su_wait_t w0 = { INVALID_SOCKET, 0, 0 };
Pekka Pessi's avatar
Pekka Pessi committed
196
  assert(waitobj != NULL);
197 198 199
  if (waitobj) {
    *waitobj = w0;
  }
200
#endif
Pekka Pessi's avatar
Pekka Pessi committed
201 202 203 204 205
  return waitobj ? 0 : -1;
}

/**Wait for multiple events.
 *
206
 * The function su_wait() blocks until an event specified by wait objects in
Pekka Pessi's avatar
Pekka Pessi committed
207 208
 * @a wait array.  If @a timeout is not SU_WAIT_FOREVER, a timeout occurs
 * after @a timeout milliseconds.
209
 *
Pekka Pessi's avatar
Pekka Pessi committed
210
 * In Unix, this is @c poll() or @c select().
211
 *
Pekka Pessi's avatar
Pekka Pessi committed
212
 * In Windows, this is @c WSAWaitForMultipleEvents().
213
 *
Pekka Pessi's avatar
Pekka Pessi committed
214 215 216 217 218 219 220 221 222 223 224
 * @param waits    array of wait objects
 * @param n        number of wait objects in array waits
 * @param timeout  timeout in milliseconds
 *
 * @retval Index of the signaled wait object, if any,
 * @retval SU_WAIT_TIMEOUT if timeout occurred, or
 * @retval -1 upon an error.
 */
int su_wait(su_wait_t waits[], unsigned n, su_duration_t timeout)
{
#if SU_HAVE_WINSOCK
225 226 227 228 229 230
  DWORD i;

  if (n > 0)
    i = WSAWaitForMultipleEvents(n, waits, FALSE, timeout, FALSE);
  else
    return Sleep(timeout), SU_WAIT_TIMEOUT;
Pekka Pessi's avatar
Pekka Pessi committed
231 232 233 234 235 236 237 238

  if (i == WSA_WAIT_TIMEOUT)
    return SU_WAIT_TIMEOUT;
  else if (i == WSA_WAIT_FAILED)
    return SOCKET_ERROR;
  else
    return i;

Martti Mela's avatar
Martti Mela committed
239
#elif SU_HAVE_POLL || HAVE_SELECT
Pekka Pessi's avatar
Pekka Pessi committed
240 241 242 243 244 245 246
  for (;;) {
    int i = poll(waits, n, timeout);

    if (i == 0)
      return SU_WAIT_TIMEOUT;

    if (i > 0) {
247 248 249 250
      unsigned j;
      for (j = 0; j < n; j++) {
	if (waits[j].revents)
	  return j;
Pekka Pessi's avatar
Pekka Pessi committed
251 252
      }
    }
253

Pekka Pessi's avatar
Pekka Pessi committed
254 255 256
    if (errno == EINTR)
      continue;

257
    return -1;
258
  }
Pekka Pessi's avatar
Pekka Pessi committed
259 260 261 262
#endif
}

/** Get events.
263
 *
Pekka Pessi's avatar
Pekka Pessi committed
264 265
 *   The function su_wait_events() returns an mask describing events occurred.
 *
266
 * @param waitobj  pointer to wait object
Pekka Pessi's avatar
Pekka Pessi committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280
 * @param s        socket
 *
 * @return Binary mask describing the events.
 */
int su_wait_events(su_wait_t *waitobj, su_socket_t s)
{
#if SU_HAVE_WINSOCK
  WSANETWORKEVENTS net_events;

  if (WSAEnumNetworkEvents(s, *waitobj, &net_events) != 0)
    return SOCKET_ERROR;

  return net_events.lNetworkEvents;

Martti Mela's avatar
Martti Mela committed
281
#elif SU_HAVE_POLL || HAVE_SELECT
Pekka Pessi's avatar
Pekka Pessi committed
282 283 284 285 286 287
  /* poll(e, 1, 0); */
  return waitobj->revents;
#endif
}

/** Set event mask.
288
 *
Pekka Pessi's avatar
Pekka Pessi committed
289 290 291
 *   The function su_wait_mask() sets the mask describing events that can
 *   signal the wait object.
 *
292
 * @param waitobj  pointer to wait object
Pekka Pessi's avatar
Pekka Pessi committed
293 294 295
 * @param s        socket
 * @param events   new event mask
 *
296
 * @retval  0 when successful,
Pekka Pessi's avatar
Pekka Pessi committed
297 298 299 300 301 302 303 304 305 306 307 308 309
 * @retval -1 upon an error.
 */
int su_wait_mask(su_wait_t *waitobj, su_socket_t s, int events)
{
#if SU_HAVE_WINSOCK
  HANDLE e = *waitobj;

  if (WSAEventSelect(s, e, events) != 0) {
    int error = WSAGetLastError();
    WSACloseEvent(e);
    WSASetLastError(error);
    return -1;
  }
310

Martti Mela's avatar
Martti Mela committed
311
#elif SU_HAVE_POLL || HAVE_SELECT
Pekka Pessi's avatar
Pekka Pessi committed
312 313 314 315 316 317 318
  waitobj->fd = s;
  waitobj->events = events;
  waitobj->revents = 0;
#endif

  return 0;
}
319