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 static void BM_pthread_self(int iters) {
22 StartBenchmarkTiming();
24 for (int i = 0; i < iters; ++i) {
25 pthread_self();
26 }
28 StopBenchmarkTiming();
29 }
30 BENCHMARK(BM_pthread_self);
32 static void BM_pthread_getspecific(int iters) {
33 StopBenchmarkTiming();
34 pthread_key_t key;
35 pthread_key_create(&key, NULL);
36 StartBenchmarkTiming();
38 for (int i = 0; i < iters; ++i) {
39 pthread_getspecific(key);
40 }
42 StopBenchmarkTiming();
43 pthread_key_delete(key);
44 }
45 BENCHMARK(BM_pthread_getspecific);
47 static void DummyPthreadOnceInitFunction() {
48 }
50 static void BM_pthread_once(int iters) {
51 StopBenchmarkTiming();
52 pthread_once_t once = PTHREAD_ONCE_INIT;
53 pthread_once(&once, DummyPthreadOnceInitFunction);
54 StartBenchmarkTiming();
56 for (int i = 0; i < iters; ++i) {
57 pthread_once(&once, DummyPthreadOnceInitFunction);
58 }
60 StopBenchmarkTiming();
61 }
62 BENCHMARK(BM_pthread_once);
64 static void BM_pthread_mutex_lock(int iters) {
65 StopBenchmarkTiming();
66 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
67 StartBenchmarkTiming();
69 for (int i = 0; i < iters; ++i) {
70 pthread_mutex_lock(&mutex);
71 pthread_mutex_unlock(&mutex);
72 }
74 StopBenchmarkTiming();
75 }
76 BENCHMARK(BM_pthread_mutex_lock);
78 static void BM_pthread_mutex_lock_ERRORCHECK(int iters) {
79 StopBenchmarkTiming();
80 pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER;
81 StartBenchmarkTiming();
83 for (int i = 0; i < iters; ++i) {
84 pthread_mutex_lock(&mutex);
85 pthread_mutex_unlock(&mutex);
86 }
88 StopBenchmarkTiming();
89 }
90 BENCHMARK(BM_pthread_mutex_lock_ERRORCHECK);
92 static void BM_pthread_mutex_lock_RECURSIVE(int iters) {
93 StopBenchmarkTiming();
94 pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
95 StartBenchmarkTiming();
97 for (int i = 0; i < iters; ++i) {
98 pthread_mutex_lock(&mutex);
99 pthread_mutex_unlock(&mutex);
100 }
102 StopBenchmarkTiming();
103 }
104 BENCHMARK(BM_pthread_mutex_lock_RECURSIVE);