#ifndef __PTIMER_H__
#define __PTIMER_H__

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <error.h>
#include <errno.h>
#include <time.h>
#include <signal.h> 
#include <pthread.h>
#include <sys/time.h>  //struct timeval
#include "tc_system.h"

// CLOCK_REALTIME :Systemwide realtime clock.
// CLOCK_MONOTONIC:Represents monotonic time. Cannot be set.
// CLOCK_PROCESS_CPUTIME_ID :High resolution per-process timer.
// CLOCK_THREAD_CPUTIME_ID :Thread-specific timer.
// CLOCK_REALTIME_HR :High resolution version of CLOCK_REALTIME.
// CLOCK_MONOTONIC_HR :High resolution version of CLOCK_MONOTONIC.

// union sigval
// {
//     int sival_int; //integer value
//     void *sival_ptr; //pointer value
// }

// struct sigevent
// {
//     int sigev_notify; //notification type
//     int sigev_signo; //signal number
//     union sigval   sigev_value; //signal value
//     void (*sigev_notify_function)(union sigval);
//     pthread_attr_t *sigev_notify_attributes;
// }

// 通过将evp->sigev_notify设定为如下值来定制定时器到期后的行为：
// SIGEV_NONE：什么都不做，只提供通过timer_gettime和timer_getoverrun查询超时信息。
// SIGEV_SIGNAL: 当定时器到期，内核会将sigev_signo所指定的信号传送给进程。在信号处理程序中，si_value会被设定会sigev_value。
// SIGEV_THREAD: 当定时器到期，内核会(在此进程内)以sigev_notification_attributes为线程属性创建一个线程，并且让它执行sigev_notify_function，传入sigev_value作为为一个参数。
// sigev_signo的值是SIGALRM以及sigev_value的值是定时器的标识符
 
// struct timespec{
//     time_t tv_sec;
//     long tv_nsec; 
// };
// struct itimespec{
//     struct timespec it_interval;
//     struct timespec it_value;
// };

// int timer_create(clockid_t clock_id, struct sigevent *evp, timer_t *timerid)
// int timer_settime(timer_t timerid, int flags, const struct itimerspec *value, struct itimerspec *ovalue);
// int timer_gettime(timer_t timerid, struct itimerspec *value);
// int timer_getoverrun(timer_t timerid);
// int timer_delete (timer_t timerid);

typedef struct ptimer
{
    timer_t  timerid;

    struct sigevent evp;

    struct itimerspec it;
    struct itimerspec save;

    void *priv;
    void (*timeout_callback)(void* priv);

}ptimer_t;

void com_ptimer_init(ptimer_t *ptimer, const char* type, void (*timeout_callback)(void* priv), void *priv);

void com_ptimer_reset(ptimer_t *ptimer, int start_ms, int interval_ms);

void com_ptimer_exit(ptimer_t *ptimer);


#endif//__PTIMER_H__
