su_port.h 8.25 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7 8 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
 * * This library is free software; you can redistribute it and/or
 * 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
 *
 */

#ifndef SU_PORT_H /** Defined when su_port.h has been included. */
#define SU_PORT_H "$Id: su_port.h,v 1.1.1.1 2005/07/20 20:35:58 kaiv Exp $"

/**@IFILE su_port.h 
 *
 * @brief Internal OS-independent syncronization interface.
 *
 * This looks like the "reactor" pattern.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * 
 * @date Created: Fri May 12 14:13:34 2000 ppessi
 * $Date: 2005/07/20 20:35:58 $
 */

#ifndef SU_WAIT_H
#include "su_wait.h"
#endif

#ifndef SU_MODULE_DEBUG_H
#include <su_module_debug.h>
#endif

#ifndef SU_ALLOC_H
#include <su_alloc.h>
#endif

/* Minimum array size (size of su_waits, sur_wait_cbs, sur_wait_args) */
#define SU_MIN_WAITS    (16)

#if SU_HAVE_WINSOCK
#define SU_MAX_WAITS    (64)	
#elif SU_HAVE_POLL
/* This may be even bigger on some OSs */
#define SU_MAX_WAITS    (1024)	
#endif

/** Message */
struct su_msg_s {
  int            sum_size;
  su_msg_t      *sum_next;
  su_task_r      sum_to;
  su_task_r      sum_from;
  su_msg_f       sum_func;
  su_msg_f       sum_report;
  su_msg_arg_t   sum_data[1];		/* minimum size, may be extended */
};

struct _GSource;

/** Root structure */
struct su_root_s {
  int              sur_size;
  su_root_magic_t *sur_magic;
  su_root_deinit_f sur_deinit;
  su_task_r        sur_task;
  su_task_r        sur_parent;
  unsigned         sur_threading : 1;
  unsigned         sur_deiniting : 1;
};

#define SU_ROOT_MAGIC(r) ((r) ? (r)->sur_magic : NULL)

/** Virtual function table for port */
typedef struct {
  unsigned su_vtable_size;
  void (*su_port_lock)(su_port_t *port, char const *who);
  void (*su_port_unlock)(su_port_t *port, char const *who);
  void (*su_port_incref)(su_port_t *port, char const *who);
  void (*su_port_decref)(su_port_t *port, int block, char const *who);
  struct _GSource *(*su_port_gsource)(su_port_t *port);
  int (*su_port_send)(su_port_t *self, su_msg_r rmsg);
  int (*su_port_register)(su_port_t *self,
		       su_root_t *root, 
		       su_wait_t *wait, 
		       su_wakeup_f callback,
		       su_wakeup_arg_t *arg,
		       int priority);
  int (*su_port_unregister)(su_port_t *port,
			    su_root_t *root, 
			    su_wait_t *wait,	
			    su_wakeup_f callback, 
			    su_wakeup_arg_t *arg);
  int (*su_port_deregister)(su_port_t *self, int i);
  int (*su_port_unregister_all)(su_port_t *self,
			     su_root_t *root);
  int (*su_port_eventmask)(su_port_t *self, int index, int socket, int events);
  void (*su_port_run)(su_port_t *self);
  void (*su_port_break)(su_port_t *self);
  su_duration_t (*su_port_step)(su_port_t *self, su_duration_t tout);
  
  int (*su_port_own_thread)(su_port_t const *port);
  
  int (*su_port_add_prepoll)(su_port_t *port,
			     su_root_t *root, 
			     su_prepoll_f *, 
			     su_prepoll_magic_t *);
  
  int (*su_port_remove_prepoll)(su_port_t *port,
				su_root_t *root);

  su_timer_t **(*su_port_timers)(su_port_t *port);

  int (*su_port_multishot)(su_port_t *port, int multishot);

  int (*su_port_threadsafe)(su_port_t *port);
} su_port_vtable_t;

su_port_t *su_port_create(void)
     __attribute__((__malloc__));

su_port_t *su_source_create(void)
     __attribute__((__malloc__));

void su_msg_delivery_report(su_msg_r msg);
su_duration_t su_timer_next_expires(su_timer_t const * t, su_time_t now);
su_root_t *su_root_create_with_port(su_root_magic_t *magic,
				    su_port_t *port);

#if SU_PORT_IMPLEMENTATION

#else
struct su_port_s {
  su_home_t               sup_home[1];
  su_port_vtable_t const *sup_vtable;
};

static inline
void su_port_lock(su_port_t *self, char const *who)
{
  if (self) self->sup_vtable->su_port_lock(self, who);
}

#define SU_PORT_LOCK(p, f)      (su_port_lock(p, #f))
 
static inline
void su_port_unlock(su_port_t *self, char const *who)
{
  if (self) self->sup_vtable->su_port_unlock(self, who);
}

#define SU_PORT_UNLOCK(p, f)    (su_port_unlock(p, #f))

static inline
void su_port_incref(su_port_t *self, char const *who)
{
  if (self) self->sup_vtable->su_port_incref(self, who);
}

#define SU_PORT_INCREF(p, f)    (su_port_incref(p, #f))
 
static inline
void su_port_decref(su_port_t *self, char const *who)
{
  if (self) self->sup_vtable->su_port_decref(self, 0, who);
}

#define SU_PORT_DECREF(p, f)    (su_port_decref(p, #f))

static inline
void su_port_zapref(su_port_t *self, char const *who)
{
  if (self) self->sup_vtable->su_port_decref(self, 1, who);
}

#define SU_PORT_ZAPREF(p, f)    (su_port_zapref(p, #f))

static inline
struct _GSource *su_port_gsource(su_port_t *self)
{
  return self ? self->sup_vtable->su_port_gsource(self) : NULL;
}


static inline
int su_port_send(su_port_t *self, su_msg_r rmsg)
{
  if (self) 
    return self->sup_vtable->su_port_send(self, rmsg);
  errno = EINVAL;
  return -1;
}


static inline
int su_port_register(su_port_t *self,
		     su_root_t *root, 
		     su_wait_t *wait, 
		     su_wakeup_f callback,
		     su_wakeup_arg_t *arg,
		     int priority)
{
  if (self)
    return self->sup_vtable->su_port_register(self, root, wait,
					      callback, arg, priority);
  errno = EINVAL;
  return -1;
}

static inline
int su_port_unregister(su_port_t *self,
		       su_root_t *root, 
		       su_wait_t *wait,	
		       su_wakeup_f callback, 
		       su_wakeup_arg_t *arg)
{
  if (self)
    return self->sup_vtable->
      su_port_unregister(self, root, wait, callback, arg);
  errno = EINVAL;
  return -1;
}

static inline
int su_port_deregister(su_port_t *self, int i)
{
  if (self)
    return self->sup_vtable->
      su_port_deregister(self, i);
  errno = EINVAL;
  return -1;
}

static inline
int su_port_unregister_all(su_port_t *self,
			   su_root_t *root)
{
  if (self)
    return self->sup_vtable->
      su_port_unregister_all(self, root);
  errno = EINVAL;
  return -1;
}

static inline
int su_port_eventmask(su_port_t *self, int index, int socket, int events)
{
  if (self)
    return self->sup_vtable->
      su_port_eventmask(self, index, socket, events);
  assert(self);
  errno = EINVAL;
  return -1;
}

static inline
void su_port_run(su_port_t *self)
{
  if (self)
    self->sup_vtable->su_port_run(self);
}

static inline
void su_port_break(su_port_t *self)
{
  if (self)
    self->sup_vtable->su_port_break(self);
}

static inline
su_duration_t su_port_step(su_port_t *self, su_duration_t tout)
{
  if (self)
    return self->sup_vtable->su_port_step(self, tout);
  errno = EINVAL;
  return (su_duration_t)-1;
}

static inline
int su_port_own_thread(su_port_t const *self)
{
  return self == NULL || self->sup_vtable->su_port_own_thread(self);
}

static inline
int su_port_add_prepoll(su_port_t *self,
			su_root_t *root, 
			su_prepoll_f *prepoll, 
			su_prepoll_magic_t *magic)
{
  if (self)
    return self->sup_vtable->
      su_port_add_prepoll(self, root, prepoll, magic);
  errno = EINVAL;
  return -1;
}

static inline
int su_port_remove_prepoll(su_port_t *self,
			   su_root_t *root)
{
  if (self)
    return self->sup_vtable->su_port_remove_prepoll(self, root);
  errno = EINVAL;
  return -1;
}

static inline
su_timer_t **su_port_timers(su_port_t *self)
{
  if (self)
    return self->sup_vtable->su_port_timers(self);
  errno = EINVAL;
  return NULL;
}

static inline
int su_port_multishot(su_port_t *self, int multishot)
{
  if (self)
    return self->sup_vtable->su_port_multishot(self, multishot);

  assert(self);
  errno = EINVAL;
  return -1;
}

static inline
int su_port_threadsafe(su_port_t *self)
{
  if (self)
    return self->sup_vtable->su_port_threadsafe(self);

  assert(self);
  errno = EINVAL;
  return -1;
}


#endif

#endif /* SU_PORT_H */