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. 其他方法,待研究。。

更多相关文章

  1. android 时间,日期对话框
  2. Android时间互换代码
  3. Android 监听开机广播启动服务 动态注册时间变化监听广播
  4. Android静态安全检测 -> 敏感函数调用风险
  5. android获取系统当前详细时间,包括星期
  6. Android中调用系统函数查找联系人
  7. Android getActionBar() 函数总是返回 null 的解决
  8. Android中时间标签起点
  9. Android > 浅谈获取时间

随机推荐

  1. Android文档阅读05—Hello, Android!及调
  2. Android:充电状态、轮询、电池状态、Noti
  3. Android SDK Manager:Failed to fetch URL
  4. 【Android开发】Android Studio中进行简
  5. Android静态图片人脸识别的完整demo(附完
  6. android ------- TCP/IP
  7. Android进行设备管理(针对企业开发)
  8. Android(安卓)studio Error:Cause: org/g
  9. 密码设置Android设备管理
  10. Android 上移植 ghostscript-9.04 静态编