📄 posixtimer.c
字号:
/* The oRTP LinPhone RTP library intends to provide basics for a RTP stack. Copyright (C) 2001 Simon MORLAT simon.morlat@linphone.org 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/#include "../config.h"#ifdef BUILD_SCHEDULER#include "rtptimer.h"#include <signal.h>#include <math.h>static guint32 late_ticks;static struct timeval prev,new;static int alarm_received=0; static guint32 posix_timer_time=0; /*in milisecond */void posix_timer_init(){ struct itimerval timer; sigset_t set; /* block the SIGALRM signal */ sigemptyset(&set); sigaddset(&set,SIGALRM); sigprocmask(SIG_BLOCK,&set,NULL); timer.it_value.tv_sec=posix_timer.interval.tv_sec; timer.it_value.tv_usec=posix_timer.interval.tv_usec; timer.it_interval.tv_sec=posix_timer.interval.tv_sec; timer.it_interval.tv_usec=posix_timer.interval.tv_usec; setitimer(ITIMER_REAL,&timer,NULL); posix_timer.state=RTP_TIMER_RUNNING; gettimeofday(&prev,NULL); late_ticks=0; posix_timer_time=0;}void dummy_handler(int signum){ posix_timer_time+=POSIXTIMER_INTERVAL/1000; alarm_received++;}void posix_timer_do(){ sigset_t set,oldset; gint32 diff,time; if (late_ticks>0){ late_ticks--; posix_timer_time+=POSIXTIMER_INTERVAL/1000; return; } gettimeofday(&new,NULL); time=((new.tv_usec-prev.tv_usec)/1000 ) + ((new.tv_sec-prev.tv_sec)*1000 ); diff=time-posix_timer_time; if (diff> (POSIXTIMER_INTERVAL/1000)){ late_ticks=diff/(POSIXTIMER_INTERVAL/1000); g_warning("we must catchup %i ticks.",late_ticks); return; } sigfillset(&set); sigdelset(&set,SIGALRM); alarm_received=0; signal(SIGALRM,dummy_handler); while (1) { sigsuspend(&set); if (alarm_received>1){ g_warning("alarm received=%i",alarm_received); } if (alarm_received==1) return; g_warning("posix_timer_do: we received an unknow signal !!!!\n"); } /* restore the old mask */ //sigprocmask(SIG_SETMASK,&oldset,NULL);}void posix_timer_uninit(){ struct itimerval timer; /* unset the timer */ memset(&timer,0,sizeof(struct itimerval)); setitimer(ITIMER_REAL,&timer,NULL); posix_timer.state=RTP_TIMER_STOPPED;}RtpTimer posix_timer={0, posix_timer_init, posix_timer_do, posix_timer_uninit, {0,POSIXTIMER_INTERVAL}}; #endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -