| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 1 | /* | 
 | 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 Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 17 | #include <pthread.h> | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 18 | #include <semaphore.h> | 
| Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 19 | #include <stdatomic.h> | 
 | 20 | #include <stdio.h> | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 21 | #include <stdlib.h> | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 22 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 23 | #include <benchmark/benchmark.h> | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 24 | #include "util.h" | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 25 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 26 | static void BM_semaphore_sem_getvalue(benchmark::State& state) { | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 27 |   sem_t semaphore; | 
 | 28 |   sem_init(&semaphore, 1, 1); | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 29 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 30 |   while (state.KeepRunning()) { | 
| Elliott Hughes | 68ae6ad | 2020-07-21 16:11:30 -0700 | [diff] [blame] | 31 |     int unused; | 
 | 32 |     sem_getvalue(&semaphore, &unused); | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 33 |   } | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 34 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 35 | BIONIC_BENCHMARK(BM_semaphore_sem_getvalue); | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 36 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 37 | static void BM_semaphore_sem_wait_sem_post(benchmark::State& state) { | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 38 |   sem_t semaphore; | 
 | 39 |   sem_init(&semaphore, 1, 1); | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 40 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 41 |   while (state.KeepRunning()) { | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 42 |     sem_wait(&semaphore); | 
 | 43 |     sem_post(&semaphore); | 
 | 44 |   } | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 45 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 46 | BIONIC_BENCHMARK(BM_semaphore_sem_wait_sem_post); |