• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • Directories
  • File List
  • Globals

timer_proc.c

Go to the documentation of this file.
00001 /* 
00002  * $Id$
00003  * 
00004  * Copyright (C) 2009 iptelorg GmbH
00005  *
00006  * Permission to use, copy, modify, and distribute this software for any
00007  * purpose with or without fee is hereby granted, provided that the above
00008  * copyright notice and this permission notice appear in all copies.
00009  *
00010  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00011  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00012  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00013  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00014  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00015  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00016  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00017  */
00018 /*
00019  * timer_proc.c  - separate process timers
00020  * (unrelated to the main fast and slow timers)
00021  */
00022 /*
00023  * History:
00024  * --------
00025  *  2009-03-10  initial version (andrei)
00026 */
00027 
00038 #include "timer_proc.h"
00039 #include "cfg/cfg_struct.h"
00040 #include "pt.h"
00041 #include "ut.h"
00042 #include "mem/shm_mem.h"
00043 
00044 #include <unistd.h>
00045 
00046 
00052 int register_basic_timers(int timers)
00053 {
00054         if(register_procs(timers)<0)
00055                 return -1;
00056         cfg_register_child(timers);
00057         return 0;
00058 }
00059 
00077 int fork_basic_timer(int child_id, char* desc, int make_sock,
00078                                                 timer_function* f, void* param, int interval)
00079 {
00080         int pid;
00081         
00082         pid=fork_process(child_id, desc, make_sock);
00083         if (pid<0) return -1;
00084         if (pid==0){
00085                 /* child */
00086                 if (cfg_child_init()) return -1;
00087                 for(;;){
00088                         sleep(interval);
00089                         cfg_update();
00090                         f(get_ticks(), param); /* ticks in s for compatibility with old
00091                                                                           timers */
00092                 }
00093         }
00094         /* parent */
00095         return pid;
00096 }
00097 
00115 int fork_basic_utimer(int child_id, char* desc, int make_sock,
00116                                                 utimer_function* f, void* param, int uinterval)
00117 {
00118         int pid;
00119         ticks_t ts;
00120         
00121         pid=fork_process(child_id, desc, make_sock);
00122         if (pid<0) return -1;
00123         if (pid==0){
00124                 /* child */
00125                 if (cfg_child_init()) return -1;
00126                 for(;;){
00127                         sleep_us(uinterval);
00128                         cfg_update();
00129                         ts = get_ticks_raw();
00130                         f(TICKS_TO_MS(ts), param); /* ticks in mili-seconds */
00131                 }
00132         }
00133         /* parent */
00134         return pid;
00135 }
00136 
00137 
00166 int fork_local_timer_process(int child_id, char* desc, int make_sock,
00167                                                 struct local_timer** lt_h)
00168 {
00169         int pid;
00170         struct local_timer* lt;
00171         
00172         lt=shm_malloc(sizeof(*lt));
00173         if (lt==0) goto error;
00174         if (init_local_timer(lt, get_ticks_raw())<0) goto error;
00175         pid=fork_process(child_id, desc, make_sock);
00176         if (pid<0) goto error;
00177         *lt_h=lt;
00178         return pid;
00179 error:
00180         if (lt) shm_free(lt);
00181         return -1;
00182 }
00183 
00189 int register_sync_timers(int timers)
00190 {
00191         if(register_procs(timers)<0)
00192                 return -1;
00193         cfg_register_child(timers);
00194         return 0;
00195 }
00196 
00214 int fork_sync_timer(int child_id, char* desc, int make_sock,
00215                                                 timer_function* f, void* param, int interval)
00216 {
00217         int pid;
00218         ticks_t ts1 = 0;
00219         ticks_t ts2 = 0;
00220 
00221         pid=fork_process(child_id, desc, make_sock);
00222         if (pid<0) return -1;
00223         if (pid==0){
00224                 /* child */
00225                 ts2 = interval;
00226                 if (cfg_child_init()) return -1;
00227                 for(;;){
00228                         if(ts2>0) sleep(ts2);
00229                         else sleep(1);
00230                         ts1 = get_ticks();
00231                         cfg_update();
00232                         f(get_ticks(), param); /* ticks in s for compatibility with old
00233                                                                           timers */
00234                         ts2 = interval - get_ticks() + ts1;
00235                 }
00236         }
00237         /* parent */
00238         return pid;
00239 }
00240 
00241 
00259 int fork_sync_utimer(int child_id, char* desc, int make_sock,
00260                                                 utimer_function* f, void* param, int uinterval)
00261 {
00262         int pid;
00263         ticks_t ts1 = 0;
00264         ticks_t ts2 = 0;
00265 
00266         pid=fork_process(child_id, desc, make_sock);
00267         if (pid<0) return -1;
00268         if (pid==0){
00269                 /* child */
00270                 ts2 = uinterval;
00271                 if (cfg_child_init()) return -1;
00272                 for(;;){
00273                         if(ts2>0) sleep_us(uinterval);
00274                         else sleep_us(1);
00275                         ts1 = get_ticks_raw();
00276                         cfg_update();
00277                         f(TICKS_TO_MS(ts1), param); /* ticks in mili-seconds */
00278                         ts2 = uinterval - get_ticks_raw() + ts1;
00279                 }
00280         }
00281         /* parent */
00282         return pid;
00283 }
00284 
00285 
00286 /* vi: set ts=4 sw=4 tw=79:ai:cindent: */

Generated on Tue May 22 2012 13:10:17 for SIP Router by  doxygen 1.7.1