dispatch_time(3)
π dispatch_time β bestie we're calculating temporal milestones β¨
okay so this is dispatch_time(3) from the Library Functions Manual, and honestly? it's giving time-travel-adjacent. it's giving "when should this thing fire." you're gonna love it, no cap. π°οΈπ¦
πΈ NAME πΈ
dispatch_time, dispatch_walltime β Calculate temporal milestones
β¨ SYNOPSIS β¨
like, here's the whole vibe upfront, bestie:
#include <dispatch/dispatch.h>
static const dispatch_time_t DISPATCH_TIME_NOW = 0ull;
static const dispatch_time_t DISPATCH_WALLTIME_NOW = ~1ull;
static const dispatch_time_t DISPATCH_TIME_FOREVER = ~0ull;
dispatch_time_t
dispatch_time(dispatch_time_t base, int64_t offset);
dispatch_time_t
dispatch_walltime(struct timespec *base, int64_t offset);
π DESCRIPTION π
so dispatch_time() and dispatch_walltime() are the two functions that give you a simple mechanism for expressing temporal milestones for use with dispatch functions that need timeouts or operate on a schedule. that's the whole job. expressing milestones. slay.
okay so the dispatch_time_t type? it's a semi-opaque integer. and here's the tea, the only values that are externally defined are the special ones:
DISPATCH_TIME_NOWDISPATCH_WALLTIME_NOWDISPATCH_TIME_FOREVER
every single other value is represented using an internal format that is not safe for integer arithmetic or comparison. π not safe!! do not do math on it, do not compare it, do not look at it funny. and the internal format is subject to change, so literally don't get attached. periodt.
now, dispatch_time() returns a milestone relative to an existing milestone after adding offset nanoseconds. so like, take a base, add some nanos, boom β new milestone. and the clock situation:
- if the
baseparameter maps internally to a wall clock, or if it isDISPATCH_WALLTIME_NOW, then the returned value is relative to the wall clock. - otherwise, if
baseisDISPATCH_TIME_NOW, then the current time of the default host clock is used. - and on Apple platforms, the value of the default host clock is obtained from
mach_absolute_time(). cute detail, love that for us. π
then there's dispatch_walltime(), which is the one you want when you're creating a milestone relative to a fixed point in time using the wall clock, as specified by the optional base parameter. it's giving "anchor me to an actual moment."
- if
baseisNULL, then the current time of the wall clock is used. - and
dispatch_walltime(NULL, offset)is equivalent todispatch_time(DISPATCH_WALLTIME_NOW, offset). π«Ά
π₯ EDGE CONDITIONS π₯
not me having to tell you about the edge conditions, but we must. dispatch_time() and dispatch_walltime() detect overflow and underflow conditions when applying the offset parameter.
- Overflow causes
DISPATCH_TIME_FOREVERto be returned. and whenbaseisDISPATCH_TIME_FOREVER, then theoffsetparameter is ignored. like it's just not invited to the party. π - Underflow causes the smallest representable value to be returned for a given clock.
bestie these are real edge conditions, not a drill β overflow and underflow are handled for you, but the results are the results. π
π EXAMPLES π
Create a milestone two seconds in the future, relative to the default clock:
milestone = dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC);
Create a milestone two seconds in the future, in wall clock time:
milestone = dispatch_time(DISPATCH_WALLTIME_NOW, 2 * NSEC_PER_SEC);
Create a milestone for use as an infinite timeout:
milestone = DISPATCH_TIME_FOREVER;
Create a milestone on Tuesday, January 19, 2038:
struct timespec ts;
ts.tv_sec = 0x7FFFFFFF;
ts.tv_nsec = 0;
milestone = dispatch_walltime(&ts, 0);
Use a negative delta to create a milestone an hour before the one above:
milestone = dispatch_walltime(&ts, -60 * 60 * NSEC_PER_SEC);
okay the 2038 one is iconic and slightly ominous but we move. π
β RETURN VALUE β
these functions return an abstract value for use with dispatch_after(), dispatch_group_wait(), dispatch_semaphore_wait(), or dispatch_source_set_timer().
so like, don't try to read it, don't try to do arithmetic on it β just hand it to one of those and let them cook. π
π SEE ALSO π
and that's the whole page, bestie!! this one's from Darwin, dated May 1, 2009, and it's dispatch_time(3) forever and ever β DISPATCH_TIME_FOREVER, even. πβ¨π¦ go forth and schedule things responsibly. mwah. π
π¦ where this page comes from
- alpine 3.24: libdispatch-doc 6.1.2-r0 Β· Apache-2.0
- arch rolling: libdispatch 6.2.2-1 Β· Apache-2.0
- epel 10: libdispatch 1:6.0.2-1.el10_0 Β· Apache-2.0
- epel 9: libdispatch 1:6.0.2-1.el9 Β· Apache-2.0
- fedora 43: libdispatch 1:6.0.2-3.fc43 Β· Apache-2.0
- fedora 44: libdispatch 1:6.0.2-3.fc43 Β· Apache-2.0
- fedora 44: libdispatch 1:6.0.2-4.fc44 Β· Apache-2.0
- nixos 26.05: swift-corelibs-libdispatch 5.10.1
- opensuse-leap 16.0: libdispatch-devel 5.6.3-bp160.1.1 Β· Apache-2.0
- Copyright (c) 2008-2013 Apple Inc. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms