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 
00082 #elif SU_HAVE_WINSOCK
00083 
00084 #define SU_WAIT_CMP(x, y) ((long)(x) - (long)(y))
00085 
00086 #define SU_WAIT_IN      (FD_READ)
00087 #define SU_WAIT_OUT     (FD_WRITE)
00088 #define SU_WAIT_CONNECT (FD_CONNECT)
00089 #define SU_WAIT_ERR     (0)     /* fuck this shit */
00090 #define SU_WAIT_HUP     (FD_CLOSE)
00091 #define SU_WAIT_ACCEPT  (FD_ACCEPT)
00092 
00093 #define SU_WAIT_FOREVER (WSA_INFINITE)
00094 #define SU_WAIT_TIMEOUT (WSA_WAIT_TIMEOUT)
00095 
00096 #define SU_WAIT_INIT    NULL
00097 
00098 #else
00099 #define SU_WAIT_CMP(x, y) 
00100 #define SU_WAIT_IN      
00101 #define SU_WAIT_OUT     
00102 #define SU_WAIT_ERR     
00103 #define SU_WAIT_HUP     
00104 #define SU_WAIT_ACCEPT  
00105 #define SU_WAIT_FOREVER 
00106 #define SU_WAIT_TIMEOUT 
00107 
00108 #define SU_WAIT_INIT
00109 
00110 #endif
00111 
00112 /* ---------------------------------------------------------------------- */
00113 /* Types */
00114 
00115 #if SU_HAVE_BSDSOCK
00116 typedef struct pollfd su_wait_t;
00117 #elif SU_HAVE_WINSOCK
00118 typedef HANDLE su_wait_t;
00119 #else
00120 
00121 typedef struct os_specific su_wait_t;
00122 #endif
00123 
00124 /* Used by AD */
00125 typedef int su_success_t;
00126 
00127 /* ---------------------------------------------------------------------- */
00128 
00130 typedef struct su_root_s su_root_t;
00131 
00132 #ifndef SU_ROOT_MAGIC_T
00133 
00143 #define SU_ROOT_MAGIC_T void
00144 #endif
00145 
00156 typedef SU_ROOT_MAGIC_T su_root_magic_t;
00157 
00158 #ifndef SU_WAKEUP_ARG_T
00159 
00169 #define SU_WAKEUP_ARG_T void
00170 #endif
00171 
00181 typedef SU_WAKEUP_ARG_T su_wakeup_arg_t;
00182 
00188 typedef int (*su_wakeup_f)(su_root_magic_t *,
00189                            su_wait_t *,
00190                            su_wakeup_arg_t *arg);
00191 
00192 enum { 
00193   su_pri_normal,                
00194   su_pri_first,                 
00195   su_pri_realtime               
00196 };
00197 
00198 struct _GSource;
00199 
00201 SOFIAPUBVAR int su_root_size_hint;
00202 
00203 /* ---------------------------------------------------------------------- */
00204 /* Pre-poll callback */
00205 
00206 #ifndef SU_PREPOLL_MAGIC_T
00207 
00217 #define SU_PREPOLL_MAGIC_T void
00218 #endif
00219 
00230 typedef SU_PREPOLL_MAGIC_T su_prepoll_magic_t;
00231 
00232 
00237 typedef void su_prepoll_f(su_prepoll_magic_t *, su_root_t *);
00238 
00239 /* ---------------------------------------------------------------------- */
00240 
00241 /* Timers */
00242 #ifdef SU_TIMER_T
00243 #error SU_TIMER_T defined
00244 #endif
00245 
00246 #ifndef SU_TIMER_ARG_T
00247 
00250 #define SU_TIMER_ARG_T void 
00251 #endif
00252 
00254 typedef struct su_timer_s su_timer_t;
00255 
00257 typedef SU_TIMER_ARG_T su_timer_arg_t;
00258 
00260 typedef void (*su_timer_f)(su_root_magic_t *magic, 
00261                            su_timer_t *t,
00262                            su_timer_arg_t *arg);
00263 
00264 /* ---------------------------------------------------------------------- */
00265 
00266 /* Tasks */
00267 
00269 typedef struct su_port_s su_port_t;
00270 
00271 typedef struct { su_port_t *sut_port; su_root_t *sut_root; } _su_task_t;
00272 
00274 typedef _su_task_t su_task_r[1];
00275 
00277 #define SU_TASK_R_INIT  {{ NULL, NULL }}
00278 
00279 /* This must be used instead of su_task_r as return value type. */
00280 typedef _su_task_t const *_su_task_r;
00281 
00282 /* ---------------------------------------------------------------------- */
00283 
00284 /* Messages */
00285 #ifndef SU_MSG_ARG_T
00286 
00289 #define SU_MSG_ARG_T void 
00290 #endif
00291 
00293 typedef SU_MSG_ARG_T su_msg_arg_t;
00294 
00296 typedef struct su_msg_s su_msg_t;
00297 
00299 typedef su_msg_t *su_msg_r[1];
00300 
00302 typedef su_msg_t * const su_msg_cr[1];
00303 
00305 #define SU_MSG_R_INIT   { NULL }
00306 
00308 typedef void (*su_msg_f)(su_root_magic_t *magic, 
00309                          su_msg_r msg,
00310                          su_msg_arg_t *arg);
00311 
00312 /* ---------------------------------------------------------------------- */
00313 
00314 /* Clones */
00315 #ifndef SU_CLONE_T
00316 #define SU_CLONE_T struct su_clone_s
00317 #endif
00318 
00320 typedef SU_CLONE_T *su_clone_r[1];
00321 
00323 #define SU_CLONE_R_INIT  {NULL}
00324 
00326 typedef int (*su_root_init_f)(su_root_t *, su_root_magic_t *);
00327 
00329 typedef void (*su_root_deinit_f)(su_root_t *, su_root_magic_t *);
00330 
00331 /* ---------------------------------------------------------------------- */
00332 /* Functions */
00333 
00334 /* Wait */
00335 SOFIAPUBFUN void su_wait_init(su_wait_t dst[1]);
00336 SOFIAPUBFUN int su_wait_create(su_wait_t *dst, su_socket_t s, int events);
00337 SOFIAPUBFUN int su_wait_destroy(su_wait_t *dst);
00338 SOFIAPUBFUN int su_wait(su_wait_t waits[], unsigned n, su_duration_t timeout);
00339 SOFIAPUBFUN int su_wait_events(su_wait_t *wait, su_socket_t s);
00340 SOFIAPUBFUN int su_wait_mask(su_wait_t *dst, su_socket_t s, int events);
00341 
00342 #if SU_HAVE_BSDSOCK
00343 static inline
00344 su_socket_t su_wait_socket(su_wait_t *wait)
00345 {
00346   return wait->fd;
00347 }
00348 #endif
00349 
00350 /* Root */
00351 SOFIAPUBFUN su_root_t *su_root_create(su_root_magic_t *magic)
00352   __attribute__((__malloc__));
00353 SOFIAPUBFUN void su_root_destroy(su_root_t*);
00354 SOFIAPUBFUN int su_root_set_magic(su_root_t *self, su_root_magic_t *magic);
00355 SOFIAPUBFUN su_root_magic_t *su_root_magic(su_root_t *root);
00356 SOFIAPUBFUN int su_root_register(su_root_t*, su_wait_t *, 
00357                                  su_wakeup_f, su_wakeup_arg_t *,
00358                                  int priority);
00359 SOFIAPUBFUN int su_root_unregister(su_root_t*, su_wait_t *, 
00360                                    su_wakeup_f, su_wakeup_arg_t*);
00361 SOFIAPUBFUN int su_root_deregister(su_root_t*, int);
00362 SOFIAPUBFUN int su_root_eventmask(su_root_t *, 
00363                                   int index, int socket, int events);
00364 SOFIAPUBFUN su_duration_t su_root_step(su_root_t *root, su_duration_t timeout);
00365 SOFIAPUBFUN su_duration_t su_root_sleep(su_root_t *root, su_duration_t);
00366 SOFIAPUBFUN int su_root_multishot(su_root_t *root, int multishot);
00367 SOFIAPUBFUN void su_root_run(su_root_t *root);
00368 SOFIAPUBFUN void su_root_break(su_root_t *root);
00369 SOFIAPUBFUN _su_task_r su_root_task(su_root_t const *root);
00370 SOFIAPUBFUN _su_task_r su_root_parent(su_root_t const *root);
00371 
00372 SOFIAPUBFUN int su_root_add_prepoll(su_root_t *root, 
00373                                     su_prepoll_f *, 
00374                                     su_prepoll_magic_t *);
00375 SOFIAPUBFUN int su_root_remove_prepoll(su_root_t *root);
00376 
00377 SOFIAPUBFUN struct _GSource *su_root_gsource(su_root_t *self);
00378 
00379 SOFIAPUBFUN int su_root_yield(su_root_t *root);
00380 
00381 /* Timers */
00382 SOFIAPUBFUN su_timer_t *su_timer_create(su_task_r const, su_duration_t msec)
00383      __attribute__((__malloc__));
00384 SOFIAPUBFUN void su_timer_destroy(su_timer_t *);
00385 SOFIAPUBFUN int su_timer_set(su_timer_t *, su_timer_f, su_timer_arg_t *);
00386 SOFIAPUBFUN int su_timer_set_interval(su_timer_t *t, su_timer_f,
00387                                       su_timer_arg_t *, su_duration_t);
00388 SOFIAPUBFUN int su_timer_set_at(su_timer_t *, su_timer_f,
00389                                 su_timer_arg_t *, su_time_t);
00390 SOFIAPUBFUN int su_timer_run(su_timer_t *, su_timer_f, su_timer_arg_t *);
00391 SOFIAPUBFUN int su_timer_set_for_ever(su_timer_t *, su_timer_f, 
00392                                       su_timer_arg_t *);
00393 SOFIAPUBFUN int su_timer_reset(su_timer_t *);
00394 
00395 SOFIAPUBFUN su_root_t *su_timer_root(su_timer_t const *);
00396 
00397 SOFIAPUBFUN int su_timer_expire(su_timer_t ** const, 
00398                                 su_duration_t *tout,
00399                                 su_time_t now);
00400 
00401 /* Tasks */
00402 
00404 SOFIAPUBVAR su_task_r const su_task_null;
00405 
00406 SOFIAPUBFUN _su_task_r su_task_init(su_task_r task);
00407 SOFIAPUBFUN void su_task_deinit(su_task_r task);
00408 
00409 SOFIAPUBFUN void su_task_copy(su_task_r dst, su_task_r const src);
00410 SOFIAPUBFUN void su_task_move(su_task_r dst, su_task_r src);
00411 SOFIAPUBFUN int su_task_cmp(su_task_r const, su_task_r const);
00412 SOFIAPUBFUN int su_task_is_running(su_task_r const);
00413 
00414 SOFIAPUBFUN su_root_t *su_task_root(su_task_r const self);
00415 SOFIAPUBFUN su_timer_t **su_task_timers(su_task_r const self);
00416 
00417 SOFIAPUBFUN int su_task_execute(su_task_r const task,
00418                                 int (*function)(void *), void *arg,
00419                                 int *return_value);
00420 
00421 /* Messages */
00422 SOFIAPUBFUN int su_msg_create(su_msg_r msg,
00423                               su_task_r const to, su_task_r const from, 
00424                               su_msg_f wakeup, int size);
00425 SOFIAPUBFUN int su_msg_report(su_msg_r msg, su_msg_f report);
00426 SOFIAPUBFUN int su_msg_reply(su_msg_r reply, su_msg_r const msg,
00427                              su_msg_f wakeup, int size);
00428 SOFIAPUBFUN void su_msg_destroy(su_msg_r msg);
00429 SOFIAPUBFUN void su_msg_save(su_msg_r msg, su_msg_r msg0);
00430 SOFIAPUBFUN void su_msg_remove_refs(su_msg_cr msg);
00431 SOFIAPUBFUN su_msg_arg_t *su_msg_data(su_msg_cr msg);
00432 SOFIAPUBFUN int su_msg_size(su_msg_cr msg);
00433 SOFIAPUBFUN _su_task_r su_msg_from(su_msg_cr msg);
00434 SOFIAPUBFUN _su_task_r su_msg_to(su_msg_cr msg);
00435 SOFIAPUBFUN int su_msg_send(su_msg_r msg);
00436 
00438 #if SU_HAVE_INLINE
00439 static SU_INLINE
00440 int su_msg_is_non_null(su_msg_cr msg)
00441 {
00442   return msg && *msg != NULL;
00443 }
00444 #else
00445 #define su_msg_is_non_null(msg) ((msg) && (*(msg)) != NULL)
00446 #endif
00447 
00448 /* Clones */
00449 SOFIAPUBFUN int su_root_threading(su_root_t *self, int enable);
00450 SOFIAPUBFUN int su_clone_start(su_root_t *root, 
00451                                su_clone_r,
00452                                su_root_magic_t *magic,
00453                                su_root_init_f, 
00454                                su_root_deinit_f);
00455 SOFIAPUBFUN _su_task_r su_clone_task(su_clone_r);
00456 SOFIAPUBFUN void su_clone_forget(su_clone_r);
00457 SOFIAPUBFUN void su_clone_stop(su_clone_r);
00458 SOFIAPUBFUN void su_clone_wait(su_root_t *root, su_clone_r clone);
00459 
00460 SOFIAPUBFUN int su_clone_pause(su_clone_r);
00461 SOFIAPUBFUN int su_clone_resume(su_clone_r);
00462 
00463 SOFIA_END_DECLS
00464 
00465 #endif /* SU_WAIT_H */

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