Android/Linux 的时间和定时器

Linux支持的时钟类型

/*
 * The IDs of the various system clocks (for POSIX.1b interval timers):
 */
/*
 * A settable system-wide real-time clock.
 * 墙上时间,修改系统时间将直接影响该时间,但不受suspend影响
 */
#define CLOCK_REALTIME          0
/*
 * CLOCK_MONOTONIC
 * A nonsettable monotonically increasing clock that measures
 * time from some unspecified point in the past that does not
 * change after system startup.
 * 本时间 在系统suspend期间不计时
 */
#define CLOCK_MONOTONIC         1
/*
 * CLOCK_PROCESS_CPUTIME_ID
 * A clock that measures (user and system) CPU time
 * consumed by(all of the threads in) the calling process.
 */
#define CLOCK_PROCESS_CPUTIME_ID    2
/*
 * CLOCK_THREAD_CPUTIME_ID
 * A clock that measures (user and system) CPU time consumed
 * by(all of the threads in) the calling thread.
 */
#define CLOCK_THREAD_CPUTIME_ID     3


/*
 * CLOCK_MONOTONIC_RAW
 * 和 CLOCK_MONOTONIC很类似,区别点在于:
 * CLOCK_MONOTONIC在系统suspend状态下不计时;
 * CLOCK_MONOTONIC_RAW在系统suspend状态下仍然计时
 */
#define CLOCK_MONOTONIC_RAW     4
/*
 * CLOCK_REALTIME_COARSE
 * 和CLOCK_REALTIME类似,前者是后者的一个简化版本,前者精度较差,但性能比后者要好
 */
#define CLOCK_REALTIME_COARSE       5
/*
 * CLOCK_MONOTONIC_COARSE
 * 和CLOCK_MONOTONIC类似,前者是后者的一个简化版本,前者精度较差,但性能比后者要好
 */
#define CLOCK_MONOTONIC_COARSE      6
/*
 * CLOCK_BOOTTIME
 * Like CLOCK_MONOTONIC, this is a monotonically increasing
 * clock.  However, whereas the CLOCK_MONOTONIC clock does not
 * measure the time while a system is suspended, the
 * CLOCK_BOOTTIME clock does include the time during which the
 * system is suspended.  This is useful for applications that
 * need to be suspend-aware.  CLOCK_REALTIME is not suitable for
 * such applications, since that clock is affected by
 * discontinuous changes to the system clock.
 * 记录启动以来一共经历的时间,不受suspend、时间改变等其他因素影响
 * 一旦开机,该时间就随墙上时间一直单调、递增
 */
#define CLOCK_BOOTTIME          7
/*
 * CLOCK_REALTIME_ALARM
 * This clock is like CLOCK_REALTIME, but will wake the system if
 * it is suspended.  The caller must have the CAP_WAKE_ALARM
 * capability in order to set a timer against this clock.
 * 与CLOCK_REALTIME相似,但是_ALARM版本的CLOCK类型会唤醒系统
 */
#define CLOCK_REALTIME_ALARM        8
/*
 * CLOCK_BOOTTIME_ALARM
 * This clock is like CLOCK_BOOTTIME, but will wake the system if
 * it is suspended.  The caller must have the CAP_WAKE_ALARM
 * capability in order to set a timer against this clock.
 * 与CLOCK_BOOTTIME相似,但是_ALARM版本的CLOCK类型会唤醒系统
 */
#define CLOCK_BOOTTIME_ALARM        9

Android/Linux下实现定时器的方法

1. POSIX提供的一种per-Process定时器

原理:
向kernel申请创建一个posix timer定时器,创建时指定如何处理定时器超出的问题。通常情况下,使用信号量的方式,将定时器超时的消息将通过信号异步发送给创建定时器的线程或其他指定线程,继而进行异步处理。

主要接口列表:
timer_create/ timer_settime/ timer_delete/ timer_getoverrun

参考网址:
http://man7.org/linux/man-pages/man2/timer_create.2.html
http://blog.csdn.net/ustcxiangchun/article/details/6339762

优、缺点:
使用简单,符合POSIX标准,通用性好。
Linux系统定义的信号量个数有限,并且大部分信号都有保留含义,不方便大量扩展;

2. Linux的timerfd机制

Linux特有的一种timer机制,底层实现为kernel的hrtimer。支持poll/epoll等方式进行多路I/O控制,支持ALARM。不使用信号量。Android里的AlarmManager便是基于timerfd机制进行定时及唤醒控制。

参考网址:
http://www.man7.org/linux/man-pages/man2/timerfd_create.2.html

相关源码路径:
/kernel/ fs/timerfd.c
/kernel/ kernel/time/alarmtimer.c
/frameworks/base/services/core/jni/ com_android_server_AlarmManagerService.cpp
/frameworks/base/services/core/java/com/android/server/ AlarmManagerService.java

3. Looper/Handler延时消息

先简单讲下,后续有时间慢慢补充。
大致实现方法是:
a.) Looper->sendMessageDelayed(MESSAGE_CUSTOM_TIMER, 1000)
b.) Handler->handleMessage接收到MESSAGE_CUSTOM_TIMER消息即表示MESSAGE_CUSTOM_TIMER定时器超时。

4. Epoll/poll设置timeout

使用poll/epoll等待I/O消息,在epoll_pwait/poll_wait函数中添加超时时间,在超时后xx_wait函数将返回。在确保poll/epoll监听的描述符无I/O操作时,xx_wait函数返回即可认为定时器超时。

5. sleep系列函数

Linux提供了sleep/usleep/delay等函数,可直接在调用线程中进行延时。

6. 其他方法,待研究。。

你可能感兴趣的:(Android)