1// Copyright 2018 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#include <threads.h>
6
7#include <perftest/perftest.h>
8
9namespace {
10
11// Measure the times taken to lock and unlock a C11 mutex in the
12// uncontended case.
13bool MutexLockUnlockTest(perftest::RepeatState* state) {
14    state->DeclareStep("lock");
15    state->DeclareStep("unlock");
16    mtx_t mutex;
17    ZX_ASSERT(mtx_init(&mutex, mtx_plain) == thrd_success);
18    while (state->KeepRunning()) {
19        ZX_ASSERT(mtx_lock(&mutex) == thrd_success);
20        state->NextStep();
21        ZX_ASSERT(mtx_unlock(&mutex) == thrd_success);
22    }
23    mtx_destroy(&mutex);
24    return true;
25}
26
27void RegisterTests() {
28    perftest::RegisterTest("MutexLockUnlock", MutexLockUnlockTest);
29}
30PERFTEST_CTOR(RegisterTests);
31
32}  // namespace
33