chromium/mojo/public/c/system/types.h

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This file contains types and constants/macros common to different Mojo system
// APIs.
//
// Note: This header should be compilable as C.

#ifndef MOJO_PUBLIC_C_SYSTEM_TYPES_H_
#define MOJO_PUBLIC_C_SYSTEM_TYPES_H_

#include <stdint.h>

#include "mojo/public/c/system/macros.h"

// |MojoTimeTicks|: A time delta, in microseconds, the meaning of which is
// source-dependent.

MojoTimeTicks;

// |MojoHandle|: Handles to Mojo objects.
//   |MOJO_HANDLE_INVALID| - A value that is never a valid handle.

MojoHandle;

#ifdef __cplusplus
inline constexpr MojoHandle MOJO_HANDLE_INVALID =;
#else
#define MOJO_HANDLE_INVALID
#endif

// |MojoResult|: Result codes for Mojo operations. The only success code is zero
// (|MOJO_RESULT_OK|); all non-zero values should be considered as error/failure
// codes (even if the value is not recognized).
//   |MOJO_RESULT_OK| - Not an error; returned on success.
//   |MOJO_RESULT_CANCELLED| - Operation was cancelled, typically by the caller.
//   |MOJO_RESULT_UNKNOWN| - Unknown error (e.g., if not enough information is
//       available for a more specific error).
//   |MOJO_RESULT_INVALID_ARGUMENT| - Caller specified an invalid argument. This
//       differs from |MOJO_RESULT_FAILED_PRECONDITION| in that the former
//       indicates arguments that are invalid regardless of the state of the
//       system.
//   |MOJO_RESULT_DEADLINE_EXCEEDED| - Deadline expired before the operation
//       could complete.
//   |MOJO_RESULT_NOT_FOUND| - Some requested entity was not found (i.e., does
//       not exist).
//   |MOJO_RESULT_ALREADY_EXISTS| - Some entity or condition that we attempted
//       to create already exists.
//   |MOJO_RESULT_PERMISSION_DENIED| - The caller does not have permission to
//       for the operation (use |MOJO_RESULT_RESOURCE_EXHAUSTED| for rejections
//       caused by exhausting some resource instead).
//   |MOJO_RESULT_RESOURCE_EXHAUSTED| - Some resource required for the call
//       (possibly some quota) has been exhausted.
//   |MOJO_RESULT_FAILED_PRECONDITION| - The system is not in a state required
//       for the operation (use this if the caller must do something to rectify
//       the state before retrying).
//   |MOJO_RESULT_ABORTED| - The operation was aborted by the system, possibly
//       due to a concurrency issue (use this if the caller may retry at a
//       higher level).
//   |MOJO_RESULT_OUT_OF_RANGE| - The operation was attempted past the valid
//       range. Unlike |MOJO_RESULT_INVALID_ARGUMENT|, this indicates that the
//       operation may be/become valid depending on the system state. (This
//       error is similar to |MOJO_RESULT_FAILED_PRECONDITION|, but is more
//       specific.)
//   |MOJO_RESULT_UNIMPLEMENTED| - The operation is not implemented, supported,
//       or enabled.
//   |MOJO_RESULT_INTERNAL| - Internal error: this should never happen and
//       indicates that some invariant expected by the system has been broken.
//   |MOJO_RESULT_UNAVAILABLE| - The operation is (temporarily) currently
//       unavailable. The caller may simply retry the operation (possibly with a
//       backoff).
//   |MOJO_RESULT_DATA_LOSS| - Unrecoverable data loss or corruption.
//   |MOJO_RESULT_BUSY| - One of the resources involved is currently being used
//       (possibly on another thread) in a way that prevents the current
//       operation from proceeding, e.g., if the other operation may result in
//       the resource being invalidated.
//   |MOJO_RESULT_SHOULD_WAIT| - The request cannot currently be completed
//       (e.g., if the data requested is not yet available). The caller should
//       wait for it to be feasible using a trap.
//
// The codes from |MOJO_RESULT_OK| to |MOJO_RESULT_DATA_LOSS| come from
// Google3's canonical error codes.

MojoResult;

#ifdef __cplusplus
inline constexpr MojoResult MOJO_RESULT_OK =;
inline constexpr MojoResult MOJO_RESULT_CANCELLED =;
inline constexpr MojoResult MOJO_RESULT_UNKNOWN =;
inline constexpr MojoResult MOJO_RESULT_INVALID_ARGUMENT =;
inline constexpr MojoResult MOJO_RESULT_DEADLINE_EXCEEDED =;
inline constexpr MojoResult MOJO_RESULT_NOT_FOUND =;
inline constexpr MojoResult MOJO_RESULT_ALREADY_EXISTS =;
inline constexpr MojoResult MOJO_RESULT_PERMISSION_DENIED =;
inline constexpr MojoResult MOJO_RESULT_RESOURCE_EXHAUSTED =;
inline constexpr MojoResult MOJO_RESULT_FAILED_PRECONDITION =;
inline constexpr MojoResult MOJO_RESULT_ABORTED =;
inline constexpr MojoResult MOJO_RESULT_OUT_OF_RANGE =;
inline constexpr MojoResult MOJO_RESULT_UNIMPLEMENTED =;
inline constexpr MojoResult MOJO_RESULT_INTERNAL =;
inline constexpr MojoResult MOJO_RESULT_UNAVAILABLE =;
inline constexpr MojoResult MOJO_RESULT_DATA_LOSS =;
inline constexpr MojoResult MOJO_RESULT_BUSY =;
inline constexpr MojoResult MOJO_RESULT_SHOULD_WAIT =;
#else
#define MOJO_RESULT_OK
#define MOJO_RESULT_CANCELLED
#define MOJO_RESULT_UNKNOWN
#define MOJO_RESULT_INVALID_ARGUMENT
#define MOJO_RESULT_DEADLINE_EXCEEDED
#define MOJO_RESULT_NOT_FOUND
#define MOJO_RESULT_ALREADY_EXISTS
#define MOJO_RESULT_PERMISSION_DENIED
#define MOJO_RESULT_RESOURCE_EXHAUSTED
#define MOJO_RESULT_FAILED_PRECONDITION
#define MOJO_RESULT_ABORTED
#define MOJO_RESULT_OUT_OF_RANGE
#define MOJO_RESULT_UNIMPLEMENTED
#define MOJO_RESULT_INTERNAL
#define MOJO_RESULT_UNAVAILABLE
#define MOJO_RESULT_DATA_LOSS
#define MOJO_RESULT_BUSY
#define MOJO_RESULT_SHOULD_WAIT
#endif

// Flags passed to |MojoInitialize()| via |MojoInitializeOptions|.
MojoInitializeFlags;

// No flags.
#define MOJO_INITIALIZE_FLAG_NONE

// The calling process will be initialized as the broker process for its IPC
// network. Any connected graph of Mojo consumers must have exactly one broker
// process. That process is always the first member of the network and it should
// set this flag during initialization. Attempts to invite a broker process into
// an existing network will always fail.
//
// This flag is ignored when |MOJO_INITIALIZE_FLAG_LOAD_ONLY| is set.
#define MOJO_INITIALIZE_FLAG_AS_BROKER

// Even if not initialized as the broker process, the calling process will be
// configured for direct shared memory allocation. This can be used for
// non-broker processes which are still sufficiently privileged to allocate
// their own shared memory.
//
// This flag is ignored when |MOJO_INITIALIZE_FLAG_LOAD_ONLY| is set.
#define MOJO_INITIALIZE_FLAG_FORCE_DIRECT_SHARED_MEMORY_ALLOCATION

// This call to |MojoInitialize()| should NOT fully initialize Mojo's internal
// IPC support engine. Initialization is essentially a two-phase operation:
// first the library is loaded and its global state is initialized, and then
// full IPC support is initialized. The latter phase may spawn a background
// thread, thus making it hostile to certain scenarios (e.g. prior to a fork()
// on Linux et al); meanwhile the former phase may still need to be completed
// early, e.g. prior to some sandbox configuration which may precede a fork().
//
// Applications wishing to separate initialization into two phases can set
// this flag during an initial call to |MojoInitialize()|. To subsequently
// enable use of all Mojo APIs, |MojoInitialize()| must be called another time,
// without this flag set.
//
// Note that various MojoInitializeOptions may be ignored on the second call
// to |MojoInitialize()|, while others may actually override options passed to
// the first call. Documentation on individual option fields and flags clarifies
// this behavior.
#define MOJO_INITIALIZE_FLAG_LOAD_ONLY

// Options passed to |MojoInitialize()|.
struct MOJO_ALIGNAS(8) MojoInitializeOptions {};
MOJO_STATIC_ASSERT();

// Flags passed to |MojoShutdown()| via |MojoShutdownOptions|.
MojoShutdownFlags;

// No flags.
#define MOJO_SHUTDOWN_FLAG_NONE

// Options passed to |MojoShutdown()|.
struct MOJO_ALIGNAS(8) MojoShutdownOptions {};
MOJO_STATIC_ASSERT();

// |MojoHandleSignals|: Used to specify signals that can be watched for on a
// handle (and which can be triggered), e.g., the ability to read or write to
// the handle.
//   |MOJO_HANDLE_SIGNAL_NONE| - No flags. A registered watch will always fail
//       to arm with |MOJO_RESULT_FAILED_PRECONDITION| when watching for this.
//   |MOJO_HANDLE_SIGNAL_READABLE| - Can read (e.g., a message) from the handle.
//   |MOJO_HANDLE_SIGNAL_WRITABLE| - Can write (e.g., a message) to the handle.
//   |MOJO_HANDLE_SIGNAL_PEER_CLOSED| - The peer handle is closed.
//   |MOJO_HANDLE_SIGNAL_NEW_DATA_READABLE| - Can read data from a data pipe
//       consumer handle (implying MOJO_HANDLE_SIGNAL_READABLE is also set),
//       AND there is some nonzero quantity of new data available on the pipe
//       since the last |MojoReadData()| or |MojoBeginReadData()| call on the
//       handle.
//   |MOJO_HANDLE_SIGNAL_PEER_REMOTE| - The peer handle exists in a remote
//       execution context (e.g. in another process.) Note that this signal is
//       maintained with best effort but may at any time be slightly out of sync
//       with the actual location of the peer handle.
//   |MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED| - One or more quotas set on the handle
//       is currently exceeded.

MojoHandleSignals;

#ifdef __cplusplus
inline constexpr MojoHandleSignals MOJO_HANDLE_SIGNAL_NONE =;
inline constexpr MojoHandleSignals MOJO_HANDLE_SIGNAL_READABLE =;
inline constexpr MojoHandleSignals MOJO_HANDLE_SIGNAL_WRITABLE =;
inline constexpr MojoHandleSignals MOJO_HANDLE_SIGNAL_PEER_CLOSED =;
inline constexpr MojoHandleSignals MOJO_HANDLE_SIGNAL_NEW_DATA_READABLE =;
inline constexpr MojoHandleSignals MOJO_HANDLE_SIGNAL_PEER_REMOTE =;
inline constexpr MojoHandleSignals MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED =;
#else
#define MOJO_HANDLE_SIGNAL_NONE
#define MOJO_HANDLE_SIGNAL_READABLE
#define MOJO_HANDLE_SIGNAL_WRITABLE
#define MOJO_HANDLE_SIGNAL_PEER_CLOSED
#define MOJO_HANDLE_SIGNAL_NEW_DATA_READABLE
#define MOJO_HANDLE_SIGNAL_PEER_REMOTE
#define MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED
#endif

// |MojoHandleSignalsState|: Returned by watch notification callbacks and
// |MojoQueryHandleSignalsState| functions to indicate the signaling state of
// handles. Members are as follows:
//   - |satisfied signals|: Bitmask of signals that were satisfied at some time
//         before the call returned.
//   - |satisfiable signals|: These are the signals that are possible to
//         satisfy. For example, if the return value was
//         |MOJO_RESULT_FAILED_PRECONDITION|, you can use this field to
//         determine which, if any, of the signals can still be satisfied.
// Note: This struct is not extensible (and only has 32-bit quantities), so it's
// 32-bit-aligned.
MOJO_STATIC_ASSERT();
struct MOJO_ALIGNAS(4) MojoHandleSignalsState {};
MOJO_STATIC_ASSERT();

// TODO(crbug.com/40565809): Remove these aliases.
#define MOJO_WATCH_CONDITION_SATISFIED
#define MOJO_WATCH_CONDITION_NOT_SATISFIED

#endif  // MOJO_PUBLIC_C_SYSTEM_TYPES_H_