blob: 418b179da8895b566cbb0053560a290a694bf018 [file] [log] [blame]
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
library zx;
@transport("Syscall")
protocol Timer {
/// ## Summary
///
/// Create a timer.
///
/// ## Declaration
///
/// ```c
/// #include <zircon/syscalls.h>
///
/// zx_status_t zx_timer_create(uint32_t options,
/// zx_clock_t clock_id,
/// zx_handle_t* out);
/// ```
///
/// ## Description
///
/// `zx_timer_create()` creates a timer, an object that can signal
/// when a specified point in time has been reached. The only valid
/// *clock_id* is **ZX_CLOCK_MONOTONIC**.
///
/// The *options* value specifies the coalescing behavior, which
/// controls whether the system can fire the time earlier or later
/// depending on other pending timers.
///
/// The possible values are:
///
/// + **ZX_TIMER_SLACK_CENTER**
/// + **ZX_TIMER_SLACK_EARLY**
/// + **ZX_TIMER_SLACK_LATE**
///
/// Passing 0 in options is equivalent to **ZX_TIMER_SLACK_CENTER**.
///
/// See [timer slack](/docs/concepts/kernel/timer_slack.md) for more information.
///
/// The returned handle has the **ZX_RIGHT_DUPLICATE**, **ZX_RIGHT_TRANSFER**,
/// **ZX_RIGHT_WRITE**, **ZX_RIGHT_SIGNAL**, **ZX_RIGHT_WAIT**, and
/// **ZX_RIGHT_INSPECT** rights.
///
/// ## Rights
///
/// Caller job policy must allow **ZX_POL_NEW_TIMER**.
///
/// ## Return value
///
/// `zx_timer_create()` returns **ZX_OK** on success. In the event
/// of failure, a negative error value is returned.
///
/// ## Errors
///
/// **ZX_ERR_INVALID_ARGS** *out* is an invalid pointer or NULL or
/// *options* is not one of the **ZX_TIMER_SLACK** values or *clock_id* is
/// any value other than **ZX_CLOCK_MONOTONIC**.
///
/// **ZX_ERR_NO_MEMORY** Failure due to lack of memory.
/// There is no good way for userspace to handle this (unlikely) error.
/// In a future build this error will no longer occur.
///
/// ## See also
///
/// - [`zx_deadline_after()`]
/// - [`zx_handle_close()`]
/// - [`zx_timer_cancel()`]
/// - [`zx_timer_set()`]
///
/// [`zx_deadline_after()`]: deadline_after.md
/// [`zx_handle_close()`]: handle_close.md
/// [`zx_timer_cancel()`]: timer_cancel.md
/// [`zx_timer_set()`]: timer_set.md
Create(struct {
options uint32;
clock_id Clock;
}) -> (resource struct {
out Handle:TIMER;
}) error Status;
/// ## Summary
///
/// Start a timer.
///
/// ## Declaration
///
/// ```c
/// #include <zircon/syscalls.h>
///
/// zx_status_t zx_timer_set(zx_handle_t handle,
/// zx_time_t deadline,
/// zx_duration_t slack);
/// ```
///
/// ## Description
///
/// `zx_timer_set()` starts a one-shot timer that will fire when
/// *deadline* passes. If a previous call to `zx_timer_set()` was
/// pending, the previous timer is canceled and
/// **ZX_TIMER_SIGNALED** is de-asserted as needed.
///
/// The *deadline* parameter specifies a deadline with respect to
/// **ZX_CLOCK_MONOTONIC**. To wait for a relative interval,
/// use [`zx_deadline_after()`] returned value in *deadline*.
///
/// To fire the timer immediately pass a *deadline* less than or equal to **0**.
///
/// When the timer fires it asserts **ZX_TIMER_SIGNALED**. To de-assert this
/// signal call [`zx_timer_cancel()`] or `zx_timer_set()` again.
///
/// The *slack* parameter specifies a range from *deadline* - *slack* to
/// *deadline* + *slack* during which the timer is allowed to fire. The system
/// uses this parameter as a hint to coalesce nearby timers.
///
/// The precise coalescing behavior is controlled by the *options* parameter
/// specified when the timer was created. **ZX_TIMER_SLACK_EARLY** allows only
/// firing in the *deadline* - *slack* interval and **ZX_TIMER_SLACK_LATE**
/// allows only firing in the *deadline* + *slack* interval. The default
/// option value of 0 is **ZX_TIMER_SLACK_CENTER** and allows both early and
/// late firing with an effective interval of *deadline* - *slack* to
/// *deadline* + *slack*
///
/// ## Rights
///
/// *handle* must be of type **ZX_OBJ_TYPE_TIMER** and have **ZX_RIGHT_WRITE**.
///
/// ## Return value
///
/// `zx_timer_set()` returns **ZX_OK** on success.
/// In the event of failure, a negative error value is returned.
///
/// ## Errors
///
/// **ZX_ERR_BAD_HANDLE** *handle* is not a valid handle.
///
/// **ZX_ERR_ACCESS_DENIED** *handle* lacks the right **ZX_RIGHT_WRITE**.
///
/// **ZX_ERR_OUT_OF_RANGE** *slack* is negative.
///
/// ## See also
///
/// - [`zx_deadline_after()`]
/// - [`zx_timer_cancel()`]
/// - [`zx_timer_create()`]
///
/// [`zx_deadline_after()`]: deadline_after.md
/// [`zx_timer_cancel()`]: timer_cancel.md
/// [`zx_timer_create()`]: timer_create.md
Set(resource struct {
handle Handle:TIMER;
deadline Time;
slack Duration;
}) -> () error Status;
/// ## Summary
///
/// Cancel a timer.
///
/// ## Declaration
///
/// ```c
/// #include <zircon/syscalls.h>
///
/// zx_status_t zx_timer_cancel(zx_handle_t handle);
/// ```
///
/// ## Description
///
/// `zx_timer_cancel()` cancels a pending timer that was started with
/// [`zx_timer_set()`].
///
/// Upon success the pending timer is canceled and the **ZX_TIMER_SIGNALED**
/// signal is de-asserted. If a new pending timer is immediately needed
/// rather than calling `zx_timer_cancel()` first, call [`zx_timer_set()`]
/// with the new deadline.
///
/// ## Rights
///
/// *handle* must be of type **ZX_OBJ_TYPE_TIMER** and have **ZX_RIGHT_WRITE**.
///
/// ## Return value
///
/// `zx_timer_cancel()` returns **ZX_OK** on success.
/// In the event of failure, a negative error value is returned.
///
/// ## Errors
///
/// **ZX_ERR_BAD_HANDLE** *handle* is not a valid handle.
///
/// **ZX_ERR_ACCESS_DENIED** *handle* lacks the right **ZX_RIGHT_WRITE**.
///
/// ## NOTE
///
/// Calling this function before [`zx_timer_set()`] has no effect.
///
/// ## See also
///
/// - [`zx_timer_create()`]
/// - [`zx_timer_set()`]
///
/// [`zx_timer_create()`]: timer_create.md
/// [`zx_timer_set()`]: timer_set.md
Cancel(resource struct {
handle Handle:TIMER;
}) -> () error Status;
};