blob: 621fcb6248c81088ce0c68e519edfcce9ece0db5 [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
17#include "benchmark.h"
18
19#include <pthread.h>
20
21static void BM_pthread_self(int iters) {
22 StartBenchmarkTiming();
23
24 for (int i = 0; i < iters; ++i) {
25 pthread_self();
26 }
27
28 StopBenchmarkTiming();
29}
30BENCHMARK(BM_pthread_self);
31
32static void BM_pthread_getspecific(int iters) {
33 StopBenchmarkTiming();
34 pthread_key_t key;
35 pthread_key_create(&key, NULL);
36 StartBenchmarkTiming();
37
38 for (int i = 0; i < iters; ++i) {
39 pthread_getspecific(key);
40 }
41
42 StopBenchmarkTiming();
43 pthread_key_delete(key);
44}
45BENCHMARK(BM_pthread_getspecific);
46
47static void DummyPthreadOnceInitFunction() {
48}
49
50static void BM_pthread_once(int iters) {
51 StopBenchmarkTiming();
52 pthread_once_t once = PTHREAD_ONCE_INIT;
53 pthread_once(&once, DummyPthreadOnceInitFunction);
54 StartBenchmarkTiming();
55
56 for (int i = 0; i < iters; ++i) {
57 pthread_once(&once, DummyPthreadOnceInitFunction);
58 }
59
60 StopBenchmarkTiming();
61}
62BENCHMARK(BM_pthread_once);
63
64static void BM_pthread_mutex_lock(int iters) {
65 StopBenchmarkTiming();
66 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
67 StartBenchmarkTiming();
68
69 for (int i = 0; i < iters; ++i) {
70 pthread_mutex_lock(&mutex);
71 pthread_mutex_unlock(&mutex);
72 }
73
74 StopBenchmarkTiming();
75}
76BENCHMARK(BM_pthread_mutex_lock);
77
78static void BM_pthread_mutex_lock_ERRORCHECK(int iters) {
79 StopBenchmarkTiming();
80 pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER;
81 StartBenchmarkTiming();
82
83 for (int i = 0; i < iters; ++i) {
84 pthread_mutex_lock(&mutex);
85 pthread_mutex_unlock(&mutex);
86 }
87
88 StopBenchmarkTiming();
89}
90BENCHMARK(BM_pthread_mutex_lock_ERRORCHECK);
91
92static void BM_pthread_mutex_lock_RECURSIVE(int iters) {
93 StopBenchmarkTiming();
94 pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
95 StartBenchmarkTiming();
96
97 for (int i = 0; i < iters; ++i) {
98 pthread_mutex_lock(&mutex);
99 pthread_mutex_unlock(&mutex);
100 }
101
102 StopBenchmarkTiming();
103}
104BENCHMARK(BM_pthread_mutex_lock_RECURSIVE);