su  1.12.11devel
 All Data Structures Files Functions Variables Typedefs Enumerator Macros Groups Pages
su_wait.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Sofia-SIP package
3  *
4  * Copyright (C) 2005 Nokia Corporation.
5  *
6  * Contact: Pekka Pessi <pekka.pessi@nokia-email.address.hidden>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24 
25 #ifndef SU_WAIT_H
26 
27 #define SU_WAIT_H
28 
38 /* ---------------------------------------------------------------------- */
39 /* Includes */
40 
41 #ifndef SU_H
42 #include "sofia-sip/su.h"
43 #endif
44 
45 #ifndef SU_TIME_H
46 #include "sofia-sip/su_time.h"
47 #endif
48 
49 #if SU_HAVE_POLL
50 #include <sys/poll.h>
51 #endif
52 
53 SOFIA_BEGIN_DECLS
54 
55 /* ---------------------------------------------------------------------- */
56 /* Constants */
57 
58 #if SU_HAVE_KQUEUE
59 
60 #define SU_WAIT_CMP(x, y) \
61  (((x).ident - (y).ident) ? ((x).ident - (y).ident) : ((x).flags - (y).flags))
62 
64 #define SU_WAIT_IN (EVFILT_READ)
65 
66 #define SU_WAIT_OUT (EVFILT_WRITE)
67 
68 #define SU_WAIT_CONNECT (EVFILT_WRITE)
69 
70 #define SU_WAIT_ERR (EV_ERROR)
71 
72 #define SU_WAIT_HUP (EV_EOF)
73 
74 #define SU_WAIT_ACCEPT (EVFILT_READ)
75 
77 #define SU_WAIT_FOREVER (-1)
78 
79 #define SU_WAIT_TIMEOUT (-2)
80 
82 #define SU_WAIT_INIT { INVALID_SOCKET, 0, 0, 0, 0, NULL }
83 
85 #define SU_WAIT_MAX (0x7fffffff)
86 
87 #elif SU_HAVE_POLL || DOCUMENTATION_ONLY
88 
89 #define SU_WAIT_CMP(x, y) \
90  (((x).fd - (y).fd) ? ((x).fd - (y).fd) : ((x).events - (y).events))
91 
93 #define SU_WAIT_IN (POLLIN)
94 
95 #define SU_WAIT_OUT (POLLOUT)
96 
97 #define SU_WAIT_CONNECT (POLLOUT)
98 
99 #define SU_WAIT_ERR (POLLERR)
100 
101 #define SU_WAIT_HUP (POLLHUP)
102 
103 #define SU_WAIT_ACCEPT (POLLIN)
104 
106 #define SU_WAIT_FOREVER (-1)
107 
108 #define SU_WAIT_TIMEOUT (-2)
109 
111 #define SU_WAIT_INIT { INVALID_SOCKET, 0, 0 }
112 
114 #define SU_WAIT_MAX (0x7fffffff)
115 
116 #elif SU_HAVE_WINSOCK
117 
118 #define SU_WAIT_CMP(x, y) ((intptr_t)(x) - (intptr_t)(y))
119 
120 #define SU_WAIT_IN (FD_READ)
121 #define SU_WAIT_OUT (FD_WRITE)
122 #define SU_WAIT_CONNECT (FD_CONNECT)
123 #define SU_WAIT_ERR (0) /* let's get it on */
124 #define SU_WAIT_HUP (FD_CLOSE)
125 #define SU_WAIT_ACCEPT (FD_ACCEPT)
126 
127 #define SU_WAIT_FOREVER (WSA_INFINITE)
128 #define SU_WAIT_TIMEOUT (WSA_WAIT_TIMEOUT)
129 
130 #define SU_WAIT_INIT NULL
131 
132 #define SU_WAIT_MAX (64)
133 
134 #else
135 /* If nothing works, try these */
136 
137 #define POLLIN 0x001
138 #define POLLPRI 0x002
139 #define POLLOUT 0x004
140 
141 #ifdef __USE_XOPEN
142 #define POLLRDNORM 0x040
143 #define POLLRDBAND 0x080
144 #define POLLWRNORM 0x100
145 #define POLLWRBAND 0x200
146 #endif
147 
148 /* These for pollfd.revents */
149 #define POLLERR 0x008
150 #define POLLHUP 0x010
151 #define POLLNVAL 0x020
152 
153 #define SU_WAIT_CMP(x, y) \
154  (((x).fd - (y).fd) ? ((x).fd - (y).fd) : ((x).events - (y).events))
155 
156 #define SU_WAIT_IN POLLIN
157 #define SU_WAIT_OUT POLLOUT
158 #define SU_WAIT_CONNECT POLLOUT
159 #define SU_WAIT_ERR POLLERR
160 #define SU_WAIT_HUP POLLHUP
161 #define SU_WAIT_ACCEPT POLLIN
162 #define SU_WAIT_FOREVER (-1)
163 #define SU_WAIT_TIMEOUT (-2)
164 
165 #define SU_WAIT_INIT { INVALID_SOCKET, 0, 0 }
166 
168 #define SU_WAIT_MAX (0x7fffffff)
169 
170 #endif
171 
172 /* ---------------------------------------------------------------------- */
173 /* Types */
174 
176 #if SU_HAVE_KQUEUE
177 typedef struct kevent su_wait_t;
178 #elif SU_HAVE_POLL
179 typedef struct pollfd su_wait_t;
180 #elif SU_HAVE_WINSOCK
181 typedef HANDLE su_wait_t;
182 #else
183 /* typedef struct os_specific su_wait_t; */
184 typedef struct pollfd su_wait_t;
185 struct pollfd {
186  su_socket_t fd; /* file descriptor */
187  short events; /* requested events */
188  short revents; /* returned events */
189 };
190 
191 
192 /* Type used for the number of file descriptors. */
193 typedef unsigned long int nfds_t;
194 
195 /* Poll the file descriptors described by the NFDS structures starting at
196  FDS. If TIMEOUT is nonzero and not -1, allow TIMEOUT milliseconds for
197  an event to occur; if TIMEOUT is -1, block until an event occurs.
198  Returns the number of file descriptors with events, zero if timed out,
199  or -1 for errors. */
200 int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout);
201 
202 #endif
203 
204 /* Used by AD */
205 typedef int su_success_t;
206 
207 /* ---------------------------------------------------------------------- */
208 
210 typedef struct su_root_s su_root_t;
211 
212 #ifndef SU_ROOT_MAGIC_T
213 
223 #define SU_ROOT_MAGIC_T void
224 #endif
225 
237 
238 #ifndef SU_WAKEUP_ARG_T
239 
249 #define SU_WAKEUP_ARG_T void
250 #endif
251 
262 
268 typedef int (*su_wakeup_f)(su_root_magic_t *,
269  su_wait_t *,
270  su_wakeup_arg_t *arg);
271 
272 enum {
276 };
277 
278 struct _GSource;
279 
282 
283 /* ---------------------------------------------------------------------- */
284 /* Pre-poll callback */
285 
286 #ifndef SU_PREPOLL_MAGIC_T
287 
297 #define SU_PREPOLL_MAGIC_T void
298 #endif
299 
311 
312 
318 
319 /* ---------------------------------------------------------------------- */
320 
321 /* Timers */
322 #ifdef SU_TIMER_T
323 #error SU_TIMER_T defined
324 #endif
325 
326 #ifndef SU_TIMER_ARG_T
327 
330 #define SU_TIMER_ARG_T void
331 #endif
332 
334 typedef struct su_timer_s su_timer_t;
335 
338 
340 typedef void (*su_timer_f)(su_root_magic_t *magic,
341  su_timer_t *t,
342  su_timer_arg_t *arg);
343 
344 #ifndef SU_TIMER_QUEUE_T
345 #define SU_TIMER_QUEUE_T su_timer_t *
346 #endif
347 
348 typedef SU_TIMER_QUEUE_T su_timer_queue_t;
349 
350 /* ---------------------------------------------------------------------- */
351 
352 /* Tasks */
353 
355 typedef struct su_port_s su_port_t;
356 
357 typedef struct { su_port_t *sut_port; su_root_t *sut_root; } _su_task_t;
358 
360 typedef _su_task_t su_task_r[1];
361 
363 #define SU_TASK_R_INIT {{ NULL, NULL }}
364 
365 /* This must be used instead of su_task_r as return value type. */
366 typedef _su_task_t const *_su_task_r;
367 
368 /* ---------------------------------------------------------------------- */
369 
370 /* Messages */
371 #ifndef SU_MSG_ARG_T
372 
375 #define SU_MSG_ARG_T void
376 #endif
377 
380 
382 typedef struct su_msg_s su_msg_t;
383 
385 typedef su_msg_t *su_msg_r[1];
386 
391 typedef su_msg_t * const su_msg_cr[1];
392 
394 #define SU_MSG_R_INIT { NULL }
395 
397 typedef void su_msg_function(su_root_magic_t *magic,
398  su_msg_r msg,
399  su_msg_arg_t *arg);
400 
403 
406 
407 
408 /* ---------------------------------------------------------------------- */
409 
410 /* Clones */
411 #ifndef SU_CLONE_T
412 #define SU_CLONE_T struct su_clone_s
413 #endif
414 
416 typedef SU_CLONE_T *su_clone_r[1];
417 
419 #define SU_CLONE_R_INIT {NULL}
420 
423 
426 
427 /* ---------------------------------------------------------------------- */
428 /* Functions */
429 
430 /* Wait */
431 SOFIAPUBFUN void su_wait_init(su_wait_t dst[1]);
432 SOFIAPUBFUN int su_wait_create(su_wait_t *dst, su_socket_t s, int events);
434 SOFIAPUBFUN int su_wait(su_wait_t waits[], unsigned n, su_duration_t timeout);
436 SOFIAPUBFUN int su_wait_mask(su_wait_t *dst, su_socket_t s, int events);
437 
438 #if !HAVE_WIN32 && (SU_HAVE_POLL || HAVE_SELECT)
439 su_inline
440 su_socket_t su_wait_socket(su_wait_t *wait)
441 {
442 #if SU_HAVE_KQUEUE
443  return wait->ident;
444 #else
445  return wait->fd;
446 #endif
447 }
448 #endif
449 
450 /* Root */
452  __attribute__((__malloc__));
454 SOFIAPUBFUN char const *su_root_name(su_root_t *self);
459  int priority);
460 /* This is slow. Deprecated. */
465  int index, int socket, int events);
468 SOFIAPUBFUN int su_root_multishot(su_root_t *root, int multishot);
469 SOFIAPUBFUN void su_root_run(su_root_t *root);
471 SOFIAPUBFUN _su_task_r su_root_task(su_root_t const *root);
472 SOFIAPUBFUN _su_task_r su_root_parent(su_root_t const *root);
473 
475  su_prepoll_f *,
478 
479 SOFIAPUBFUN struct _GSource *su_root_gsource(su_root_t *self);
480 
482 
486 
489 
492 
494  __attribute__((__malloc__));
495 
496 /* Timers */
498  __attribute__((__malloc__));
500 SOFIAPUBFUN int su_timer_is_set(su_timer_t const *t); /* 1.12.11 */
505 SOFIAPUBFUN int su_timer_set_at(su_timer_t *, su_timer_f,
509  su_timer_arg_t *);
511 
513 
514 SOFIAPUBFUN int su_timer_expire(su_timer_queue_t * const,
515  su_duration_t *tout,
516  su_time_t now);
517 SOFIAPUBFUN int su_timer_deferrable(su_timer_t *t, int value); /* 1.12.11 */
518 
519 /* Tasks */
520 
522 
523 SOFIAPUBFUN _su_task_r su_task_init(su_task_r task);
525 
526 SOFIAPUBFUN void su_task_copy(su_task_r dst, su_task_r const src);
528 SOFIAPUBFUN int su_task_cmp(su_task_r const, su_task_r const);
530 
532 SOFIAPUBFUN su_timer_queue_t *su_task_timers(su_task_r const self);
533 SOFIAPUBFUN su_timer_queue_t *su_task_deferrable(su_task_r const task);
534 
535 SOFIAPUBFUN int su_task_wakeup(su_task_r const task);
536 
537 SOFIAPUBFUN int su_task_execute(su_task_r const task,
538  int (*function)(void *), void *arg,
539  int *return_value);
540 
541 /* Messages */
543  su_task_r const to, su_task_r const from,
544  su_msg_f wakeup, isize_t size);
545 SOFIAPUBFUN int su_msg_report(su_msg_r msg, su_msg_f report);
548  su_msg_f wakeup, isize_t size);
550 SOFIAPUBFUN void su_msg_save(su_msg_r msg, su_msg_r msg0);
553 SOFIAPUBFUN isize_t su_msg_size(su_msg_cr msg);
554 SOFIAPUBFUN _su_task_r su_msg_from(su_msg_cr msg);
555 SOFIAPUBFUN _su_task_r su_msg_to(su_msg_cr msg);
557 
558 SOFIAPUBFUN int su_msg_new(su_msg_r msg, size_t size);
560  su_task_r const to,
561  su_msg_f wakeup);
562 
564 #if SU_HAVE_INLINE
565 static SU_INLINE
566 int su_msg_is_non_null(su_msg_cr msg)
567 {
568  return msg && *msg != NULL;
569 }
570 #else
571 #define su_msg_is_non_null(msg) ((msg) && (*(msg)) != NULL)
572 #endif
573 
574 /* Clones */
575 SOFIAPUBFUN int su_root_threading(su_root_t *self, int enable);
577  su_clone_r,
578  su_root_magic_t *magic,
584 SOFIAPUBFUN void su_clone_wait(su_root_t *root, su_clone_r clone);
585 
588 
589 /* ---------------------------------------------------------------------- */
590 /* Different su_root_t implementations */
591 
592 typedef su_port_t *su_port_create_f(void);
593 typedef int su_clone_start_f(su_root_t *parent,
594  su_clone_r return_clone,
595  su_root_magic_t *magic,
596  su_root_init_f init,
597  su_root_deinit_f deinit);
598 
599 SOFIAPUBFUN void su_port_prefer(su_port_create_f *f, su_clone_start_f *);
600 
601 SOFIAPUBFUN su_port_create_f su_default_port_create;
602 SOFIAPUBFUN su_port_create_f su_epoll_port_create;
603 SOFIAPUBFUN su_port_create_f su_poll_port_create;
604 SOFIAPUBFUN su_port_create_f su_wsaevent_port_create;
605 SOFIAPUBFUN su_port_create_f su_select_port_create;
606 SOFIAPUBFUN su_port_create_f su_kqueue_port_create;
607 SOFIAPUBFUN su_port_create_f su_devpoll_port_create;
608 
609 SOFIAPUBFUN su_clone_start_f su_default_clone_start;
610 SOFIAPUBFUN su_clone_start_f su_epoll_clone_start;
611 SOFIAPUBFUN su_clone_start_f su_poll_clone_start;
612 SOFIAPUBFUN su_clone_start_f su_wsaevent_clone_start;
613 SOFIAPUBFUN su_clone_start_f su_select_clone_start;
614 SOFIAPUBFUN su_clone_start_f su_kqueue_clone_start;
615 SOFIAPUBFUN su_clone_start_f su_devpoll_clone_start;
616 
617 SOFIA_END_DECLS
618 
619 #endif /* SU_WAIT_H */

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