# zx_counter_create

## Summary

Create a counter.

## Declaration

    #include <zircon/syscalls.h>

    zx_status_t zx_counter_create(uint32_t options, zx_handle_t* out);

## Description

`zx_counter_create()` creates a counter, which is an object that encapsulates
a signed 64-bit integer value that can be incremented, decremented, read, or
written.

When the value is greater than zero, the signal `ZX_COUNTER_POSITIVE` is
asserted. Otherwise `ZX_COUNTER_NON_POSITIVE` is asserted. Exactly one of
these two signals is always asserted, and never both at once.

Additionally, the signal `ZX_COUNTER_SIGNALED` may be set or cleared on the
counter using [`zx_object_signal()`](https://fuchsia.dev/reference/syscalls/object_signal). This is independent of the value of the
counter or the state of the other signals.

The newly-created handle will have rights `ZX_RIGHTS_BASIC`, `ZX_RIGHTS_IO`, and
`ZX_RIGHT_SIGNAL`. The value will be zero and the signal
`ZX_COUNTER_NON_POSITIVE` will be asserted on the newly-created object.

## Rights

TODO(https://fxbug.dev/387324141): Add/enforce ZX_POL_NEW_COUNTER policy.

Caller job policy must allow `ZX_POL_NEW_COUNTER`.

## Return value

`zx_counter_create()` returns `ZX_OK` and a valid counter handle (via *out*) on
success.

On failure, an error value is returned.

## Errors

`ZX_ERR_INVALID_ARGS` *out* is an invalid pointer, or *options* is non-zero.

`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_handle_close()`](https://fuchsia.dev/reference/syscalls/handle_close)
- [`zx_handle_duplicate()`](https://fuchsia.dev/reference/syscalls/handle_duplicate)
- [`zx_handle_replace()`](https://fuchsia.dev/reference/syscalls/handle_replace)
- [`zx_object_wait_async()`](https://fuchsia.dev/reference/syscalls/object_wait_async)
- [`zx_object_wait_many()`](https://fuchsia.dev/reference/syscalls/object_wait_many)
- [`zx_object_wait_one()`](https://fuchsia.dev/reference/syscalls/object_wait_one)
- [`zx_counter_add()`](https://fuchsia.dev/reference/syscalls/counter_add)
- [`zx_counter_read()`](https://fuchsia.dev/reference/syscalls/counter_read)
- [`zx_counter_write()`](https://fuchsia.dev/reference/syscalls/counter_write)