1// Copyright 2016 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#pragma once
6
7#include <zircon/compiler.h>
8#include <zircon/types.h>
9
10#include <stdatomic.h>
11
12__BEGIN_CDECLS
13
14typedef struct {
15    atomic_int futex;
16} zxr_mutex_t;
17
18#define ZXR_MUTEX_INIT ((zxr_mutex_t){})
19
20#pragma GCC visibility push(hidden)
21
22// Attempts to take the lock without blocking. Returns ZX_OK if the
23// lock is obtained, and ZX_ERR_BAD_STATE if not.
24zx_status_t zxr_mutex_trylock(zxr_mutex_t* mutex);
25
26// Attempts to take the lock before the timeout expires. This takes an
27// absolute time. Returns ZX_OK if the lock is acquired, and
28// ZX_ERR_TIMED_OUT if the timeout expires.
29//
30// This function is only for use by mtx_timedlock().
31zx_status_t __zxr_mutex_timedlock(zxr_mutex_t* mutex, zx_time_t abstime);
32
33// Blocks until the lock is obtained.
34void zxr_mutex_lock(zxr_mutex_t* mutex);
35
36// Unlocks the lock.
37void zxr_mutex_unlock(zxr_mutex_t* mutex);
38
39// This is the same as zxr_mutex_lock() except that it always marks the
40// mutex as having a waiter.  This is intended for use by condvar
41// implementations.  This means that a thread waiting on a condvar futex
42// can be requeued onto the mutex's futex, so that a later call to
43// zxr_mutex_unlock() will wake that thread.
44void zxr_mutex_lock_with_waiter(zxr_mutex_t* mutex);
45
46#pragma GCC visibility pop
47
48__END_CDECLS
49