06_clocks_timers.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. Clocks and Timers
  2. *****************
  3. - :c:func:`clock_settime`
  4. - :c:func:`clock_gettime`
  5. - :c:func:`clock_getres`
  6. - :c:func:`mktime`
  7. - :c:func:`gmtime`
  8. - :c:func:`localtime`
  9. - :c:func:`asctime`
  10. - :c:func:`ctime`
  11. - :c:func:`gmtime_r`
  12. - :c:func:`localtime_r`
  13. - :c:func:`asctime_r`
  14. - :c:func:`ctime_r`
  15. - :c:func:`timer_create`
  16. - :c:func:`timer_delete`
  17. - :c:func:`timer_settime`
  18. - :c:func:`timer_gettime`
  19. - :c:func:`timer_getoverrun`
  20. - :c:func:`gettimeofday`
  21. - :c:func:`gethrtime`
  22. .. c:function:: int clock_settime(clockid_t clockid, const struct timespec *tp)
  23. :return: If successful, returns zero (``OK``). Otherwise,
  24. a non-zero error number will be returned to indicate the error.
  25. .. c:function:: int clock_gettime(clockid_t clockid, struct timespec *tp)
  26. :return: If successful, returns zero (``OK``). Otherwise,
  27. a non-zero error number will be returned to indicate the error.
  28. .. c:function:: int clock_getres(clockid_t clockid, struct timespec *res)
  29. :return: If successful, returns zero (``OK``). Otherwise,
  30. a non-zero error number will be returned to indicate the error.
  31. .. c:function:: time_t mktime(struct tm *tp);
  32. :return: If successful, returns zero (``OK``). Otherwise,
  33. a non-zero error number will be returned to indicate the error.
  34. .. c:function:: FAR struct tm *gmtime(FAR const time_t *timep);
  35. Represents GMT date/time in a type ``struct tm``. This
  36. function is not re-entrant.
  37. :param timep: Represents GMT calendar time. This is an absolute time
  38. value representing the number of seconds elapsed since 00:00:00 on
  39. January 1, 1970, Coordinated Universal Time (UTC).
  40. :return: If successful, the function will return the pointer to a statically defined
  41. instance of ``struct tm``. Otherwise, a NULL will be returned to
  42. indicate the error:
  43. .. c:function:: FAR struct tm *localtime(FAR const time_t *timep)
  44. Represents local date/time in a type ``struct tm``.
  45. This function is not re-entrant.
  46. :param timep: Represents GMT calendar time. This is an absolute time
  47. value representing the number of seconds elapsed since 00:00:00 on
  48. January 1, 1970, Coordinated Universal Time (UTC).
  49. :return: If successful, the function will return the pointer to a statically defined
  50. instance of ``struct tm``. Otherwise, a NULL will be returned to
  51. indicate the error:
  52. .. c:function:: FAR char *asctime(FAR const struct tm *tp);
  53. Converts the time provided in a
  54. ``struct tm`` to a string representation. ``asctime()`` is not
  55. re-entrant.
  56. :param tp: Pointer to the time to be converted.
  57. :return: If successful, the function will
  58. return a pointer to a statically defined string holding the converted
  59. time. Otherwise, a NULL will be returned to indicate the error.
  60. .. c:function:: FAR char *ctime(FAR const time_t *timep)
  61. Converts the time provided in seconds since
  62. the epoch to a string representation. ``ctime()`` is not re-entrant.
  63. :param timep: The current time represented as seconds since the epoch.
  64. :return: If successful, the function will return
  65. the pointer to the converted string. Otherwise, a NULL will be returned
  66. to indicate the error.
  67. .. c:function:: struct tm *gmtime_r(const time_t *timep, struct tm *result);
  68. Represents GMT date/time in a type ``struct tm``. This
  69. function is re-entrant.
  70. :param timep: Represents GMT calendar time. This is an absolute time
  71. value representing the number of seconds elapsed since 00:00:00 on
  72. January 1, 1970, Coordinated Universal Time (UTC).
  73. :param result: A user-provided buffer to receive the converted time
  74. structure.
  75. :return: If successful, the ``gmtime_r()`` function will
  76. return the pointer, ``result``, provided by the caller. Otherwise, a
  77. NULL will be returned to indicate the error:
  78. .. c:function:: FAR struct tm *localtime_r(FAR const time_t *timep, FAR struct tm *result)
  79. Represents local date/time in a type ``struct tm``.
  80. This function is re-entrant.
  81. :param timep: Represents GMT calendar time. This is an absolute time
  82. value representing the number of seconds elapsed since 00:00:00 on
  83. January 1, 1970, Coordinated Universal Time (UTC).
  84. :param result: A user-provided buffer to receive the converted time
  85. structure.
  86. :return: If successful, the
  87. ``localtime_r()`` function will return the pointer, ``result``, provided
  88. by the caller. Otherwise, a NULL will be returned to indicate the error:
  89. .. c:function:: FAR char *asctime_r(FAR const struct tm *tp, FAR char *buf)
  90. Converts the time provided in a
  91. ``struct tm`` to a string representation. ``asctime-r()`` is re-entrant.
  92. :param tp: Pointer to the time to be converted.
  93. :param buf: The user provider buffer. of size >= 26 characters, to
  94. receive the converted time.
  95. :return: If successful, the ``asctime_r()`` function will
  96. return the pointer, ``buf``, provided by the caller. Otherwise, a NULL
  97. will be returned to indicate the error.
  98. .. c:function:: FAR char *ctime_r(FAR const time_t *timep, FAR char *buf)
  99. Converts the time provided in seconds
  100. since the epoch to a string representation. ``ctime()`` is re-entrant.
  101. :param timep: The current time represented as seconds since the epoch.
  102. :param buf: The user provider buffer. of size >= 26 characters, to
  103. receive the converted time.
  104. :return: If successful, the ``ctime_r()`` function will
  105. return the pointer, ``buf``, provided by the caller. Otherwise, a NULL
  106. will be returned to indicate the error.
  107. .. c:function:: int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid);
  108. Creates per-thread
  109. timer using the specified clock, ``clock_id``, as the timing base. The
  110. ``timer_create()`` function returns, in the location referenced by
  111. ``timerid``, a timer ID of type timer_t used to identify the timer in
  112. timer requests. This timer ID is unique until the timer is deleted. The
  113. particular clock, ``clock_id``, is defined in ``<time.h>``. The timer
  114. whose ID is returned will be in a disarmed state upon return from
  115. ``timer_create()``.
  116. The ``evp`` argument, if non-NULL, points to a ``sigevent`` structure.
  117. This structure is allocated by the called and defines the asynchronous
  118. notification to occur. If the ``evp`` argument is NULL, the effect is as
  119. if the ``evp`` argument pointed to a ``sigevent`` structure with the
  120. ``sigev_notify`` member having the value ``SIGEV_SIGNAL``, the
  121. ``sigev_signo`` having a default signal number, and the ``sigev_value``
  122. member having the value of the timer ID.
  123. Each implementation defines a set of clocks that can be used as timing
  124. bases for per-thread timers. All implementations will support a
  125. ``clock_id`` of ``CLOCK_REALTIME``.
  126. :param clockid: Specifies the clock to use as the timing base. Must be
  127. ``CLOCK_REALTIME``.
  128. :param ``evp``: Refers to a user allocated sigevent structure that defines
  129. the asynchronous notification. evp may be NULL (see above).
  130. :param ``timerid``: The pre-thread timer created by the call to
  131. timer_create().
  132. :return: If the call succeeds, ``timer_create()`` will return
  133. 0 (``OK``) and update the location referenced by ``timerid`` to a
  134. ``timer_t``, which can be passed to the other per-thread timer calls. If
  135. an error occurs, the function will return a value of -1 (``ERROR``) and
  136. set ``errno`` to indicate the error.
  137. - ``EAGAIN``. The system lacks sufficient signal queuing resources to
  138. honor the request.
  139. - ``EAGAIN``. The calling process has already created all of the timers
  140. it is allowed by this implementation.
  141. - ``EINVAL``. The specified clock ID is not defined.
  142. - ``ENOTSUP``. The implementation does not support the creation of a
  143. timer attached to the CPU-time clock that is specified by clock_id
  144. and associated with a thread different thread invoking
  145. timer_create().
  146. **POSIX Compatibility:** Comparable to the POSIX interface of the same
  147. name. Differences from the full POSIX implementation include:
  148. - Only ``CLOCK_REALTIME`` is supported for the ``clockid`` argument.
  149. .. c:function:: int timer_delete(timer_t timerid);
  150. Deletes the specified
  151. timer, ``timerid``, previously created by the ``timer_create()``
  152. function. If the timer is armed when ``timer_delete()`` is called, the
  153. timer will be automatically disarmed before removal. The disposition of
  154. pending signals for the deleted timer is unspecified.
  155. :param timerid: The pre-thread timer, previously created by the call to
  156. timer_create(), to be deleted.
  157. :return: If successful, the ``timer_delete()`` function will
  158. return zero (``OK``). Otherwise, the function will return a value of -1
  159. (``ERROR``) and set ``errno`` to indicate the error:
  160. - ``EINVAL``. The timer specified timerid is not valid.
  161. **POSIX Compatibility:** Comparable to the POSIX interface of the same
  162. name.
  163. .. c:function:: int timer_settime(timer_t timerid, int flags, const struct itimerspec *value, \
  164. struct itimerspec *ovalue);
  165. Sets the time until
  166. the next expiration of the timer specified by ``timerid`` from the
  167. ``it_value`` member of the value argument and arm the timer if the
  168. ``it_value`` member of value is non-zero. If the specified timer was
  169. already armed when ``timer_settime()`` is called, this call will reset
  170. the time until next expiration to the value specified. If the
  171. ``it_value`` member of value is zero, the timer will be disarmed. The
  172. effect of disarming or resetting a timer with pending expiration
  173. notifications is unspecified.
  174. If the flag ``TIMER_ABSTIME`` is not set in the argument flags,
  175. ``timer_settime()`` will behave as if the time until next expiration is
  176. set to be equal to the interval specified by the ``it_value`` member of
  177. value. That is, the timer will expire in ``it_value`` nanoseconds from
  178. when the call is made. If the flag ``TIMER_ABSTIME`` is set in the
  179. argument flags, ``timer_settime()`` will behave as if the time until
  180. next expiration is set to be equal to the difference between the
  181. absolute time specified by the ``it_value`` member of value and the
  182. current value of the clock associated with ``timerid``. That is, the
  183. timer will expire when the clock reaches the value specified by the
  184. ``it_value`` member of value. If the specified time has already passed,
  185. the function will succeed and the expiration notification will be made.
  186. The reload value of the timer will be set to the value specified by the
  187. ``it_interval`` member of value. When a timer is armed with a non-zero
  188. ``it_interval``, a periodic (or repetitive) timer is specified.
  189. Time values that are between two consecutive non-negative integer
  190. multiples of the resolution of the specified timer will be rounded up to
  191. the larger multiple of the resolution. Quantization error will not cause
  192. the timer to expire earlier than the rounded time value.
  193. If the argument ``ovalue`` is not NULL, the t\ ``imer_settime()``
  194. function will store, in the location referenced by ``ovalue``, a value
  195. representing the previous amount of time before the timer would have
  196. expired, or zero if the timer was disarmed, together with the previous
  197. timer reload value. Timers will not expire before their scheduled time.
  198. **NOTE:**\ At present, the ``ovalue`` argument is ignored.
  199. :param timerid: The pre-thread timer, previously created by the call to
  200. timer_create(), to be be set.
  201. :param flags: Specify characteristics of the timer (see above)
  202. :param value: Specifies the timer value to set
  203. :param ovalue: A location in which to return the time remaining from the
  204. previous timer setting (ignored).
  205. :return: If the timer_gettime() succeeds, a value of 0
  206. (``OK``) will be returned. If an error occurs, the value -1 (``ERROR``)
  207. will be returned, and ```errno`` <#ErrnoAccess>`__ set to indicate the
  208. error.
  209. - ``EINVAL``. The timerid argument does not correspond to an ID
  210. returned by timer_create() but not yet deleted by timer_delete().
  211. - ``EINVAL``. A value structure specified a nanosecond value less than
  212. zero or greater than or equal to 1000 million, and the it_value
  213. member of that structure did not specify zero seconds and
  214. nanoseconds.
  215. **POSIX Compatibility:** Comparable to the POSIX interface of the same
  216. name. Differences from the full POSIX implementation include:
  217. - The ``ovalue`` argument is ignored.
  218. .. c:function:: int timer_gettime(timer_t timerid, struct itimerspec *value);
  219. Stores the amount
  220. of time until the specified timer, ``timerid``, expires and the reload
  221. value of the timer into the space pointed to by the ``value`` argument.
  222. The ``it_value`` member of this structure will contain the amount of
  223. time before the timer expires, or zero if the timer is disarmed. This
  224. value is returned as the interval until timer expiration, even if the
  225. timer was armed with absolute time. The ``it_interval`` member of
  226. ``value`` will contain the reload value last set by ``timer_settime()``.
  227. Due to the asynchronous operation of this function, the time reported by
  228. this function could be significantly more than that actual time
  229. remaining on the timer at any time.
  230. :param timerid: Specifies pre-thread timer, previously created by the
  231. call to ``timer_create()``, whose remaining count will be returned.
  232. :return: If successful, the ``timer_gettime()`` function will
  233. return zero (``OK``). Otherwise, an non-zero error number will be
  234. returned to indicate the error:
  235. - ``EINVAL``. The ``timerid`` argument does not correspond to an ID
  236. returned by ``timer_create()`` but not yet deleted by
  237. ``timer_delete()``.
  238. **POSIX Compatibility:** Comparable to the POSIX interface of the same
  239. name.
  240. .. c:function:: int timer_getoverrun(timer_t timerid);
  241. Only a single signal will be queued to the process for
  242. a given timer at any point in time. When a timer for which a signal is
  243. still pending expires, no signal will be queued, and a timer overrun
  244. will occur. When a timer expiration signal is delivered to or accepted
  245. by a process, if the implementation supports the *Realtime Signals
  246. Extension*, the ``timer_getoverrun()`` function will return the timer
  247. expiration overrun count for the specified timer. The overrun count
  248. returned contains the number of extra timer expirations that occurred
  249. between the time the signal was generated (queued) and when it was
  250. delivered or accepted, up to but not including an implementation-defined
  251. maximum of ``DELAYTIMER_MAX``. If the number of such extra expirations
  252. is greater than or equal to ``DELAYTIMER_MAX``, then the overrun count
  253. will be set to ``DELAYTIMER_MAX``. The value returned by
  254. ``timer_getoverrun()`` will apply to the most recent expiration signal
  255. delivery or acceptance for the timer. If no expiration signal has been
  256. delivered for the timer, or if the *Realtime Signals Extension* is not
  257. supported, the return value of ``timer_getoverrun()`` is unspecified.
  258. **NOTE:** This interface is not currently implemented in NuttX.
  259. :param timerid: Specifies pre-thread timer, previously created by the
  260. call to ``timer_create()``, whose overrun count will be returned.
  261. :return: If the ``timer_getoverrun()`` function succeeds, it
  262. will return the timer expiration overrun count as explained above.
  263. ``timer_getoverrun()`` will fail if:
  264. - ``EINVAL``. The ``timerid`` argument does not correspond to an ID
  265. returned by ``timer_create()`` but not yet deleted by
  266. ``timer_delete()``.
  267. **POSIX Compatibility:** Comparable to the POSIX interface of the same
  268. name. Differences from the full POSIX implementation include:
  269. - This interface is not currently implemented by NuttX.
  270. **Assumptions/Limitations:**
  271. **POSIX Compatibility:** Comparable to the POSIX interface of the same
  272. name.
  273. .. c:function:: int gettimeofday(struct timeval *tp, void *tzp);
  274. This implementation of ``gettimeofday()`` is simply a
  275. thin wrapper around :c:func:`clock_gettime`. It simply
  276. calls ``clock_gettime()`` using the ``CLOCK_REALTIME`` timer and
  277. converts the result to the required ``struct timeval``.
  278. :param tp: The current time will be returned to this user provided
  279. location.
  280. :param tzp: A reference to the timezone -- *IGNORED*.
  281. :return: See :c:func:`clock_gettime`.
  282. .. c:function:: hrtime_t gethrtime(void);
  283. This implementation of ``gethrtime()`` is simply a
  284. thin wrapper around :c:func:`clock_gettime`. It simply
  285. calls ``clock_gettime()`` using the ``CLOCK_REALTIME`` or ``CLOCK_MONOTONIC``,
  286. and converts the result to the required hrtime_t.
  287. :return: current system time in ns