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 */
17 #include "benchmark.h"
19 #include <pthread.h>
21 // Stop GCC optimizing out our pure function.
22 /* Must not be static! */ pthread_t (*pthread_self_fp)() = pthread_self;
24 static void BM_pthread_self(int iters) {
25 StartBenchmarkTiming();
27 for (int i = 0; i < iters; ++i) {
28 pthread_self_fp();
29 }
31 StopBenchmarkTiming();
32 }
33 BENCHMARK(BM_pthread_self);
35 static void BM_pthread_getspecific(int iters) {
36 StopBenchmarkTiming();
37 pthread_key_t key;
38 pthread_key_create(&key, NULL);
39 StartBenchmarkTiming();
41 for (int i = 0; i < iters; ++i) {
42 pthread_getspecific(key);
43 }
45 StopBenchmarkTiming();
46 pthread_key_delete(key);
47 }
48 BENCHMARK(BM_pthread_getspecific);
50 static void DummyPthreadOnceInitFunction() {
51 }
53 static void BM_pthread_once(int iters) {
54 StopBenchmarkTiming();
55 pthread_once_t once = PTHREAD_ONCE_INIT;
56 pthread_once(&once, DummyPthreadOnceInitFunction);
57 StartBenchmarkTiming();
59 for (int i = 0; i < iters; ++i) {
60 pthread_once(&once, DummyPthreadOnceInitFunction);
61 }
63 StopBenchmarkTiming();
64 }
65 BENCHMARK(BM_pthread_once);
67 static void BM_pthread_mutex_lock(int iters) {
68 StopBenchmarkTiming();
69 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
70 StartBenchmarkTiming();
72 for (int i = 0; i < iters; ++i) {
73 pthread_mutex_lock(&mutex);
74 pthread_mutex_unlock(&mutex);
75 }
77 StopBenchmarkTiming();
78 }
79 BENCHMARK(BM_pthread_mutex_lock);
81 static void BM_pthread_mutex_lock_ERRORCHECK(int iters) {
82 StopBenchmarkTiming();
83 pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER;
84 StartBenchmarkTiming();
86 for (int i = 0; i < iters; ++i) {
87 pthread_mutex_lock(&mutex);
88 pthread_mutex_unlock(&mutex);
89 }
91 StopBenchmarkTiming();
92 }
93 BENCHMARK(BM_pthread_mutex_lock_ERRORCHECK);
95 static void BM_pthread_mutex_lock_RECURSIVE(int iters) {
96 StopBenchmarkTiming();
97 pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
98 StartBenchmarkTiming();
100 for (int i = 0; i < iters; ++i) {
101 pthread_mutex_lock(&mutex);
102 pthread_mutex_unlock(&mutex);
103 }
105 StopBenchmarkTiming();
106 }
107 BENCHMARK(BM_pthread_mutex_lock_RECURSIVE);