blob: ffccc82d782b1e9f0bf9df8c1598df681402e532 [file] [log] [blame]
Elliott Hughesb28e4902014-03-11 11:19:06 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mark Salyzyn7e50fb22015-02-09 08:18:10 -080017#include <pthread.h>
Elliott Hughesb28e4902014-03-11 11:19:06 -070018#include <semaphore.h>
Mark Salyzyn7e50fb22015-02-09 08:18:10 -080019#include <stdatomic.h>
20#include <stdio.h>
Elliott Hughes281e06b2016-02-17 10:23:52 -080021#include <stdlib.h>
Elliott Hughesb28e4902014-03-11 11:19:06 -070022
Elliott Hughes281e06b2016-02-17 10:23:52 -080023#include <benchmark/benchmark.h>
Anders Lewisa7b0f882017-07-24 20:01:13 -070024#include "util.h"
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080025
Elliott Hughes281e06b2016-02-17 10:23:52 -080026static void BM_semaphore_sem_getvalue(benchmark::State& state) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070027 sem_t semaphore;
28 sem_init(&semaphore, 1, 1);
Elliott Hughesb28e4902014-03-11 11:19:06 -070029
Elliott Hughes281e06b2016-02-17 10:23:52 -080030 while (state.KeepRunning()) {
Elliott Hughes68ae6ad2020-07-21 16:11:30 -070031 int unused;
32 sem_getvalue(&semaphore, &unused);
Elliott Hughesb28e4902014-03-11 11:19:06 -070033 }
Elliott Hughesb28e4902014-03-11 11:19:06 -070034}
Anders Lewisa7b0f882017-07-24 20:01:13 -070035BIONIC_BENCHMARK(BM_semaphore_sem_getvalue);
Elliott Hughesb28e4902014-03-11 11:19:06 -070036
Elliott Hughes281e06b2016-02-17 10:23:52 -080037static void BM_semaphore_sem_wait_sem_post(benchmark::State& state) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070038 sem_t semaphore;
39 sem_init(&semaphore, 1, 1);
Elliott Hughesb28e4902014-03-11 11:19:06 -070040
Elliott Hughes281e06b2016-02-17 10:23:52 -080041 while (state.KeepRunning()) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070042 sem_wait(&semaphore);
43 sem_post(&semaphore);
44 }
Elliott Hughesb28e4902014-03-11 11:19:06 -070045}
Anders Lewisa7b0f882017-07-24 20:01:13 -070046BIONIC_BENCHMARK(BM_semaphore_sem_wait_sem_post);