su_wait.c 7.71 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 27 28 29 30 31 32 33
 * 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
 *
 * @CFILE su_wait.c  
 * 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.
Pekka Pessi's avatar
Pekka Pessi committed
124 125 126 127 128 129 130 131 132 133 134 135 136
 * 
 * @param newwait  the newly created wait object (output)
 * @param socket   socket
 * @param events   mask for events that can signal this wait object
 * 
 * @retval 0 if the call was successful,
 * @retval -1 upon an error.  
*/
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 167
  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;
  
Pekka Pessi's avatar
Pekka Pessi committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
  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.
 *
 * @param waitobj  pointer to wait object   
 *
 * @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;
  if (*waitobj)
    WSACloseEvent(*waitobj);
Martti Mela's avatar
Martti Mela committed
191
#elif SU_HAVE_POLL || HAVE_SELECT
Pekka Pessi's avatar
Pekka Pessi committed
192
  su_wait_t w0 = { INVALID_SOCKET, 0, 0 };
193 194
#else
  su_wait_t w0 = { INVALID_SOCKET, 0, 0 };
Pekka Pessi's avatar
Pekka Pessi committed
195 196 197 198 199 200 201 202 203
#endif
  assert(waitobj != NULL);
  *waitobj = w0;

  return waitobj ? 0 : -1;
}

/**Wait for multiple events.
 *
204
 * The function su_wait() blocks until an event specified by wait objects in
Pekka Pessi's avatar
Pekka Pessi committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
 * @a wait array.  If @a timeout is not SU_WAIT_FOREVER, a timeout occurs
 * after @a timeout milliseconds.
 * 
 * In Unix, this is @c poll() or @c select().
 * 
 * In Windows, this is @c WSAWaitForMultipleEvents().
 * 
 * @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
223 224 225 226 227 228
  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
229 230 231 232 233 234 235 236

  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
237
#elif SU_HAVE_POLL || HAVE_SELECT
Pekka Pessi's avatar
Pekka Pessi committed
238 239 240 241 242 243 244
  for (;;) {
    int i = poll(waits, n, timeout);

    if (i == 0)
      return SU_WAIT_TIMEOUT;

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

255
    return -1;
Pekka Pessi's avatar
Pekka Pessi committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
  }  
#endif
}

/** Get events.
 * 
 *   The function su_wait_events() returns an mask describing events occurred.
 *
 * @param waitobj  pointer to wait object   
 * @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
279
#elif SU_HAVE_POLL || HAVE_SELECT
Pekka Pessi's avatar
Pekka Pessi committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
  /* poll(e, 1, 0); */
  return waitobj->revents;
#endif
}

/** Set event mask.
 * 
 *   The function su_wait_mask() sets the mask describing events that can
 *   signal the wait object.
 *
 * @param waitobj  pointer to wait object   
 * @param s        socket
 * @param events   new event mask
 *
 * @retval  0 when successful, 
 * @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;
  }
308

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

  return 0;
}
317