mirror of
https://github.com/mpv-player/mpv
synced 2025-04-21 14:49:08 +00:00
POSIX compatible timer's callback
git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@4864 b3059339-0415-0410-9bf9-f77b7e298cf2
This commit is contained in:
parent
1fe9e6e3bb
commit
cb7768f9bb
@ -3,7 +3,7 @@ include ../config.mak
|
|||||||
|
|
||||||
LIBNAME = libosdep.a
|
LIBNAME = libosdep.a
|
||||||
|
|
||||||
SRCS=getch2.c timer-lx.c shmem.c
|
SRCS=getch2.c timer-lx.c timer.c shmem.c
|
||||||
OBJS=$(SRCS:.c=.o)
|
OBJS=$(SRCS:.c=.o)
|
||||||
|
|
||||||
ifeq ($(TARGET_ARCH_X86),yes)
|
ifeq ($(TARGET_ARCH_X86),yes)
|
||||||
|
40
linux/timer.c
Normal file
40
linux/timer.c
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
/* POSIX compatible timer callback */
|
||||||
|
#include <sys/time.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include "timer.h"
|
||||||
|
|
||||||
|
static timer_callback *user_func = NULL;
|
||||||
|
static struct itimerval otimer;
|
||||||
|
static void (*old_alrm)(int) = SIG_DFL;
|
||||||
|
|
||||||
|
static void my_alarm_handler( int signo )
|
||||||
|
{
|
||||||
|
if(user_func) (*user_func)();
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned set_timer_callback(unsigned ms,timer_callback func)
|
||||||
|
{
|
||||||
|
unsigned ret;
|
||||||
|
struct itimerval itimer;
|
||||||
|
user_func = func;
|
||||||
|
getitimer(ITIMER_REAL,&otimer);
|
||||||
|
old_alrm = signal(SIGALRM,my_alarm_handler);
|
||||||
|
signal(SIGALRM,my_alarm_handler);
|
||||||
|
itimer.it_interval.tv_sec = 0;
|
||||||
|
itimer.it_interval.tv_usec = ms*1000;
|
||||||
|
itimer.it_value.tv_sec = 0;
|
||||||
|
itimer.it_value.tv_usec = ms*1000;
|
||||||
|
setitimer(ITIMER_REAL,&itimer,NULL);
|
||||||
|
getitimer(ITIMER_REAL,&itimer);
|
||||||
|
ret = itimer.it_interval.tv_sec*1000 + itimer.it_interval.tv_usec/1000;
|
||||||
|
if(!ret) restore_timer();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void restore_timer(void)
|
||||||
|
{
|
||||||
|
signal(SIGALRM,old_alrm);
|
||||||
|
setitimer(ITIMER_REAL,&otimer,NULL);
|
||||||
|
}
|
@ -1,3 +1,5 @@
|
|||||||
|
#ifndef __TIMER_H
|
||||||
|
#define __TIMER_H
|
||||||
|
|
||||||
void InitTimer();
|
void InitTimer();
|
||||||
unsigned int GetTimer();
|
unsigned int GetTimer();
|
||||||
@ -7,3 +9,9 @@ float GetRelativeTime();
|
|||||||
|
|
||||||
int usec_sleep(int usec_delay);
|
int usec_sleep(int usec_delay);
|
||||||
|
|
||||||
|
/* timer's callback handling */
|
||||||
|
typedef void timer_callback( void );
|
||||||
|
extern unsigned set_timer_callback(unsigned ms,timer_callback func);
|
||||||
|
extern void restore_timer(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user