sofia-sip/su_wait.h

Go to the documentation of this file.
00001 /*
00002  * This file is part of the Sofia-SIP package
00003  *
00004  * Copyright (C) 2005 Nokia Corporation.
00005  *
00006  * Contact: Pekka Pessi <pekka.pessi@nokia-email.address.hidden>
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation; either version 2.1 of
00011  * the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00021  * 02110-1301 USA
00022  *
00023  */
00024 
00025 #ifndef SU_WAIT_H
00026 
00027 #define SU_WAIT_H
00028 
00037 /* ---------------------------------------------------------------------- */
00038 /* Includes */
00039 
00040 #ifndef SU_H
00041 #include "sofia-sip/su.h"
00042 #endif
00043 
00044 #ifndef SU_TIME_H
00045 #include "sofia-sip/su_time.h"
00046 #endif
00047 #if SU_HAVE_POLL
00048 #include <sys/poll.h>
00049 #endif
00050 
00051 SOFIA_BEGIN_DECLS
00052 
00053 /* ---------------------------------------------------------------------- */
00054 /* Constants */
00055 
00056 #if SU_HAVE_POLL || DOCUMENTATION_ONLY
00057 
00058 #define SU_WAIT_CMP(x, y) \
00059  (((x).fd - (y).fd) ? ((x).fd - (y).fd) : ((x).events - (y).events))
00060 
00062 #define SU_WAIT_IN      (POLLIN)
00063 
00064 #define SU_WAIT_OUT     (POLLOUT)
00065 
00066 #define SU_WAIT_CONNECT (POLLOUT)
00067 
00068 #define SU_WAIT_ERR     (POLLERR)
00069 
00070 #define SU_WAIT_HUP     (POLLHUP)
00071 
00072 #define SU_WAIT_ACCEPT  (POLLIN)
00073 
00075 #define SU_WAIT_FOREVER (-1)
00076 
00077 #define SU_WAIT_TIMEOUT (-2)
00078 
00080 #define SU_WAIT_INIT    { INVALID_SOCKET, 0, 0 }
00081 
00083 #define SU_WAIT_MAX    (0x7fffffff)
00084 
00085 #elif SU_HAVE_WINSOCK
00086 
00087 #define SU_WAIT_CMP(x, y) ((intptr_t)(x) - (intptr_t)(y))
00088 
00089 #define SU_WAIT_IN      (FD_READ)
00090 #define SU_WAIT_OUT     (FD_WRITE)
00091 #define SU_WAIT_CONNECT (FD_CONNECT)
00092 #define SU_WAIT_ERR     (0)     /* fuck this shit */
00093 #define SU_WAIT_HUP     (FD_CLOSE)
00094 #define SU_WAIT_ACCEPT  (FD_ACCEPT)
00095 
00096 #define SU_WAIT_FOREVER (WSA_INFINITE)
00097 #define SU_WAIT_TIMEOUT (WSA_WAIT_TIMEOUT)
00098 
00099 #define SU_WAIT_INIT    NULL
00100 
00101 #define SU_WAIT_MAX    (64)
00102 
00103 #else
00104 #define SU_WAIT_CMP(x, y) 
00105 #define SU_WAIT_IN      
00106 #define SU_WAIT_OUT     
00107 #define SU_WAIT_ERR     
00108 #define SU_WAIT_HUP     
00109 #define SU_WAIT_ACCEPT  
00110 #define SU_WAIT_FOREVER 
00111 #define SU_WAIT_TIMEOUT 
00112 
00113 #define SU_WAIT_INIT
00114 
00115 #endif
00116 
00117 /* ---------------------------------------------------------------------- */
00118 /* Types */
00119 
00120 #if 0
00121 typedef struct _pollfd {
00122   su_socket_t fd;           /* file descriptor */
00123   short events;     /* requested events */
00124   short revents;    /* returned events */
00125 } su_wait_t;
00126 #elif SU_HAVE_POLL
00127 typedef struct pollfd su_wait_t;
00128 #elif SU_HAVE_WINSOCK
00129 typedef HANDLE su_wait_t;
00130 #else
00131 
00132 typedef struct os_specific su_wait_t;
00133 #endif
00134 
00135 /* Used by AD */
00136 typedef int su_success_t;
00137 
00138 /* ---------------------------------------------------------------------- */
00139 
00141 typedef struct su_root_s su_root_t;
00142 
00143 #ifndef SU_ROOT_MAGIC_T
00144 
00154 #define SU_ROOT_MAGIC_T void
00155 #endif
00156 
00167 typedef SU_ROOT_MAGIC_T su_root_magic_t;
00168 
00169 #ifndef SU_WAKEUP_ARG_T
00170 
00180 #define SU_WAKEUP_ARG_T void
00181 #endif
00182 
00192 typedef SU_WAKEUP_ARG_T su_wakeup_arg_t;
00193 
00199 typedef int (*su_wakeup_f)(su_root_magic_t *,
00200                            su_wait_t *,
00201                            su_wakeup_arg_t *arg);
00202 
00203 enum { 
00204   su_pri_normal,                
00205   su_pri_first,                 
00206   su_pri_realtime               
00207 };
00208 
00209 struct _GSource;
00210 
00212 SOFIAPUBVAR int su_root_size_hint;
00213 
00214 /* ---------------------------------------------------------------------- */
00215 /* Pre-poll callback */
00216 
00217 #ifndef SU_PREPOLL_MAGIC_T
00218 
00228 #define SU_PREPOLL_MAGIC_T void
00229 #endif
00230 
00241 typedef SU_PREPOLL_MAGIC_T su_prepoll_magic_t;
00242 
00243 
00248 typedef void su_prepoll_f(su_prepoll_magic_t *, su_root_t *);
00249 
00250 /* ---------------------------------------------------------------------- */
00251 
00252 /* Timers */
00253 #ifdef SU_TIMER_T
00254 #error SU_TIMER_T defined
00255 #endif
00256 
00257 #ifndef SU_TIMER_ARG_T
00258 
00261 #define SU_TIMER_ARG_T void 
00262 #endif
00263 
00265 typedef struct su_timer_s su_timer_t;
00266 
00268 typedef SU_TIMER_ARG_T su_timer_arg_t;
00269 
00271 typedef void (*su_timer_f)(su_root_magic_t *magic, 
00272                            su_timer_t *t,
00273                            su_timer_arg_t *arg);
00274 
00275 /* ---------------------------------------------------------------------- */
00276 
00277 /* Tasks */
00278 
00280 typedef struct su_port_s su_port_t;
00281 
00282 typedef struct { su_port_t *sut_port; su_root_t *sut_root; } _su_task_t;
00283 
00285 typedef _su_task_t su_task_r[1];
00286 
00288 #define SU_TASK_R_INIT  {{ NULL, NULL }}
00289 
00290 /* This must be used instead of su_task_r as return value type. */
00291 typedef _su_task_t const *_su_task_r;
00292 
00293 /* ---------------------------------------------------------------------- */
00294 
00295 /* Messages */
00296 #ifndef SU_MSG_ARG_T
00297 
00300 #define SU_MSG_ARG_T void 
00301 #endif
00302 
00304 typedef SU_MSG_ARG_T su_msg_arg_t;
00305 
00307 typedef struct su_msg_s su_msg_t;
00308 
00310 typedef su_msg_t *su_msg_r[1];
00311 
00313 typedef su_msg_t * const su_msg_cr[1];
00314 
00316 #define SU_MSG_R_INIT   { NULL }
00317 
00319 typedef void (*su_msg_f)(su_root_magic_t *magic, 
00320                          su_msg_r msg,
00321                          su_msg_arg_t *arg);
00322 
00323 /* ---------------------------------------------------------------------- */
00324 
00325 /* Clones */
00326 #ifndef SU_CLONE_T
00327 #define SU_CLONE_T struct su_clone_s
00328 #endif
00329 
00331 typedef SU_CLONE_T *su_clone_r[1];
00332 
00334 #define SU_CLONE_R_INIT  {NULL}
00335 
00337 typedef int (*su_root_init_f)(su_root_t *, su_root_magic_t *);
00338 
00340 typedef void (*su_root_deinit_f)(su_root_t *, su_root_magic_t *);
00341 
00342 /* ---------------------------------------------------------------------- */
00343 /* Functions */
00344 
00345 /* Wait */
00346 SOFIAPUBFUN void su_wait_init(su_wait_t dst[1]);
00347 SOFIAPUBFUN int su_wait_create(su_wait_t *dst, su_socket_t s, int events);
00348 SOFIAPUBFUN int su_wait_destroy(su_wait_t *dst);
00349 SOFIAPUBFUN int su_wait(su_wait_t waits[], unsigned n, su_duration_t timeout);
00350 SOFIAPUBFUN int su_wait_events(su_wait_t *wait, su_socket_t s);
00351 SOFIAPUBFUN int su_wait_mask(su_wait_t *dst, su_socket_t s, int events);
00352 
00353 #if SU_HAVE_POLL
00354 static inline
00355 su_socket_t su_wait_socket(su_wait_t *wait)
00356 {
00357   return wait->fd;
00358 }
00359 #endif
00360 
00361 /* Root */
00362 SOFIAPUBFUN su_root_t *su_root_create(su_root_magic_t *magic)
00363   __attribute__((__malloc__));
00364 SOFIAPUBFUN void su_root_destroy(su_root_t*);
00365 SOFIAPUBFUN int su_root_set_magic(su_root_t *self, su_root_magic_t *magic);
00366 SOFIAPUBFUN su_root_magic_t *su_root_magic(su_root_t *root);
00367 SOFIAPUBFUN int su_root_register(su_root_t*, su_wait_t *, 
00368                                  su_wakeup_f, su_wakeup_arg_t *,
00369                                  int priority);
00370 /* This is slow. Deprecated. */
00371 SOFIAPUBFUN int su_root_unregister(su_root_t*, su_wait_t *, 
00372                                    su_wakeup_f, su_wakeup_arg_t*);
00373 SOFIAPUBFUN int su_root_deregister(su_root_t*, int);
00374 SOFIAPUBFUN int su_root_eventmask(su_root_t *, 
00375                                   int index, int socket, int events);
00376 SOFIAPUBFUN su_duration_t su_root_step(su_root_t *root, su_duration_t timeout);
00377 SOFIAPUBFUN su_duration_t su_root_sleep(su_root_t *root, su_duration_t);
00378 SOFIAPUBFUN int su_root_multishot(su_root_t *root, int multishot);
00379 SOFIAPUBFUN void su_root_run(su_root_t *root);
00380 SOFIAPUBFUN void su_root_break(su_root_t *root);
00381 SOFIAPUBFUN _su_task_r su_root_task(su_root_t const *root);
00382 SOFIAPUBFUN _su_task_r su_root_parent(su_root_t const *root);
00383 
00384 SOFIAPUBFUN int su_root_add_prepoll(su_root_t *root, 
00385                                     su_prepoll_f *, 
00386                                     su_prepoll_magic_t *);
00387 SOFIAPUBFUN int su_root_remove_prepoll(su_root_t *root);
00388 
00389 SOFIAPUBFUN struct _GSource *su_root_gsource(su_root_t *self);
00390 
00391 SOFIAPUBFUN int su_root_yield(su_root_t *root);
00392 
00393 /* Timers */
00394 SOFIAPUBFUN su_timer_t *su_timer_create(su_task_r const, su_duration_t msec)
00395      __attribute__((__malloc__));
00396 SOFIAPUBFUN void su_timer_destroy(su_timer_t *);
00397 SOFIAPUBFUN int su_timer_set(su_timer_t *, su_timer_f, su_timer_arg_t *);
00398 SOFIAPUBFUN int su_timer_set_interval(su_timer_t *t, su_timer_f,
00399                                       su_timer_arg_t *, su_duration_t);
00400 SOFIAPUBFUN int su_timer_set_at(su_timer_t *, su_timer_f,
00401                                 su_timer_arg_t *, su_time_t);
00402 SOFIAPUBFUN int su_timer_run(su_timer_t *, su_timer_f, su_timer_arg_t *);
00403 SOFIAPUBFUN int su_timer_set_for_ever(su_timer_t *, su_timer_f, 
00404                                       su_timer_arg_t *);
00405 SOFIAPUBFUN int su_timer_reset(su_timer_t *);
00406 
00407 SOFIAPUBFUN su_root_t *su_timer_root(su_timer_t const *);
00408 
00409 SOFIAPUBFUN int su_timer_expire(su_timer_t ** const, 
00410                                 su_duration_t *tout,
00411                                 su_time_t now);
00412 
00413 /* Tasks */
00414 
00416 SOFIAPUBVAR su_task_r const su_task_null;
00417 
00418 SOFIAPUBFUN _su_task_r su_task_init(su_task_r task);
00419 SOFIAPUBFUN void su_task_deinit(su_task_r task);
00420 
00421 SOFIAPUBFUN void su_task_copy(su_task_r dst, su_task_r const src);
00422 SOFIAPUBFUN void su_task_move(su_task_r dst, su_task_r src);
00423 SOFIAPUBFUN int su_task_cmp(su_task_r const, su_task_r const);
00424 SOFIAPUBFUN int su_task_is_running(su_task_r const);
00425 
00426 SOFIAPUBFUN su_root_t *su_task_root(su_task_r const self);
00427 SOFIAPUBFUN su_timer_t **su_task_timers(su_task_r const self);
00428 
00429 SOFIAPUBFUN int su_task_execute(su_task_r const task,
00430                                 int (*function)(void *), void *arg,
00431                                 int *return_value);
00432 
00433 /* Messages */
00434 SOFIAPUBFUN int su_msg_create(su_msg_r msg,
00435                               su_task_r const to, su_task_r const from, 
00436                               su_msg_f wakeup, isize_t size);
00437 SOFIAPUBFUN int su_msg_report(su_msg_r msg, su_msg_f report);
00438 SOFIAPUBFUN int su_msg_reply(su_msg_r reply, su_msg_r const msg,
00439                              su_msg_f wakeup, isize_t size);
00440 SOFIAPUBFUN void su_msg_destroy(su_msg_r msg);
00441 SOFIAPUBFUN void su_msg_save(su_msg_r msg, su_msg_r msg0);
00442 SOFIAPUBFUN void su_msg_remove_refs(su_msg_cr msg);
00443 SOFIAPUBFUN su_msg_arg_t *su_msg_data(su_msg_cr msg);
00444 SOFIAPUBFUN isize_t su_msg_size(su_msg_cr msg);
00445 SOFIAPUBFUN _su_task_r su_msg_from(su_msg_cr msg);
00446 SOFIAPUBFUN _su_task_r su_msg_to(su_msg_cr msg);
00447 SOFIAPUBFUN int su_msg_send(su_msg_r msg);
00448 
00450 #if SU_HAVE_INLINE
00451 static SU_INLINE
00452 int su_msg_is_non_null(su_msg_cr msg)
00453 {
00454   return msg && *msg != NULL;
00455 }
00456 #else
00457 #define su_msg_is_non_null(msg) ((msg) && (*(msg)) != NULL)
00458 #endif
00459 
00460 /* Clones */
00461 SOFIAPUBFUN int su_root_threading(su_root_t *self, int enable);
00462 SOFIAPUBFUN int su_clone_start(su_root_t *root, 
00463                                su_clone_r,
00464                                su_root_magic_t *magic,
00465                                su_root_init_f, 
00466                                su_root_deinit_f);
00467 SOFIAPUBFUN _su_task_r su_clone_task(su_clone_r);
00468 SOFIAPUBFUN void su_clone_forget(su_clone_r);
00469 SOFIAPUBFUN void su_clone_stop(su_clone_r);
00470 SOFIAPUBFUN void su_clone_wait(su_root_t *root, su_clone_r clone);
00471 
00472 SOFIAPUBFUN int su_clone_pause(su_clone_r);
00473 SOFIAPUBFUN int su_clone_resume(su_clone_r);
00474 
00475 SOFIA_END_DECLS
00476 
00477 #endif /* SU_WAIT_H */

Sofia-SIP 1.12.4 - Copyright (C) 2006 Nokia Corporation. All rights reserved. Licensed under the terms of the GNU Lesser General Public License.