dispatch_time.3 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. .\" Copyright (c) 2008-2013 Apple Inc. All rights reserved.
  2. .Dd May 1, 2009
  3. .Dt dispatch_time 3
  4. .Os Darwin
  5. .Sh NAME
  6. .Nm dispatch_time ,
  7. .Nm dispatch_walltime
  8. .Nd Calculate temporal milestones
  9. .Sh SYNOPSIS
  10. .Fd #include <dispatch/dispatch.h>
  11. .Vt static const dispatch_time_t DISPATCH_TIME_NOW = 0ull ;
  12. .Vt static const dispatch_time_t DISPATCH_WALLTIME_NOW = ~1ull ;
  13. .Vt static const dispatch_time_t DISPATCH_TIME_FOREVER = ~0ull ;
  14. .Ft dispatch_time_t
  15. .Fo dispatch_time
  16. .Fa "dispatch_time_t base" "int64_t offset"
  17. .Fc
  18. .Ft dispatch_time_t
  19. .Fo dispatch_walltime
  20. .Fa "struct timespec *base" "int64_t offset"
  21. .Fc
  22. .Sh DESCRIPTION
  23. The
  24. .Fn dispatch_time
  25. and
  26. .Fn dispatch_walltime
  27. functions provide a simple mechanism for expressing temporal milestones for use
  28. with dispatch functions that need timeouts or operate on a schedule.
  29. .Pp
  30. The
  31. .Fa dispatch_time_t
  32. type is a semi-opaque integer, with only the special values
  33. .Vt DISPATCH_TIME_NOW ,
  34. .Vt DISPATCH_WALLTIME_NOW
  35. and
  36. .Vt DISPATCH_TIME_FOREVER
  37. being externally defined. All other values are represented using an internal
  38. format that is not safe for integer arithmetic or comparison.
  39. The internal format is subject to change.
  40. .Pp
  41. The
  42. .Fn dispatch_time
  43. function returns a milestone relative to an existing milestone after adding
  44. .Fa offset
  45. nanoseconds.
  46. If the
  47. .Fa base
  48. parameter maps internally to a wall clock or is
  49. .Vt DISPATCH_WALLTIME_NOW ,
  50. then the returned value is relative to the wall clock.
  51. Otherwise, if
  52. .Fa base
  53. is
  54. .Vt DISPATCH_TIME_NOW ,
  55. then the current time of the default host clock is used. On Apple platforms,
  56. the value of the default host clock is obtained from
  57. .Vt mach_absolute_time() .
  58. .Pp
  59. The
  60. .Fn dispatch_walltime
  61. function is useful for creating a milestone relative to a fixed point in time
  62. using the wall clock, as specified by the optional
  63. .Fa base
  64. parameter. If
  65. .Fa base
  66. is NULL, then the current time of the wall clock is used.
  67. .Vt dispatch_walltime(NULL, offset)
  68. is equivalent to
  69. .Vt dispatch_time(DISPATCH_WALLTIME_NOW, offset) .
  70. .Sh EDGE CONDITIONS
  71. The
  72. .Fn dispatch_time
  73. and
  74. .Fn dispatch_walltime
  75. functions detect overflow and underflow conditions when applying the
  76. .Fa offset
  77. parameter.
  78. .Pp
  79. Overflow causes
  80. .Vt DISPATCH_TIME_FOREVER
  81. to be returned. When
  82. .Fa base
  83. is
  84. .Vt DISPATCH_TIME_FOREVER ,
  85. then the
  86. .Fa offset
  87. parameter is ignored.
  88. .Pp
  89. Underflow causes the smallest representable value to be
  90. returned for a given clock.
  91. .Sh EXAMPLES
  92. Create a milestone two seconds in the future, relative to the default clock:
  93. .Bd -literal -offset indent
  94. milestone = dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC);
  95. .Ed
  96. .Pp
  97. Create a milestone two seconds in the future, in wall clock time:
  98. .Bd -literal -offset indent
  99. milestone = dispatch_time(DISPATCH_WALLTIME_NOW, 2 * NSEC_PER_SEC);
  100. .Ed
  101. .Pp
  102. Create a milestone for use as an infinite timeout:
  103. .Bd -literal -offset indent
  104. milestone = DISPATCH_TIME_FOREVER;
  105. .Ed
  106. .Pp
  107. Create a milestone on Tuesday, January 19, 2038:
  108. .Bd -literal -offset indent
  109. struct timespec ts;
  110. ts.tv_sec = 0x7FFFFFFF;
  111. ts.tv_nsec = 0;
  112. milestone = dispatch_walltime(&ts, 0);
  113. .Ed
  114. .Pp
  115. Use a negative delta to create a milestone an hour before the one above:
  116. .Bd -literal -offset indent
  117. milestone = dispatch_walltime(&ts, -60 * 60 * NSEC_PER_SEC);
  118. .Ed
  119. .Sh RETURN VALUE
  120. These functions return an abstract value for use with
  121. .Fn dispatch_after ,
  122. .Fn dispatch_group_wait ,
  123. .Fn dispatch_semaphore_wait ,
  124. or
  125. .Fn dispatch_source_set_timer .
  126. .Sh SEE ALSO
  127. .Xr dispatch 3 ,
  128. .Xr dispatch_after 3 ,
  129. .Xr dispatch_group_create 3 ,
  130. .Xr dispatch_semaphore_create 3