| // Licensed to the Apache Software Foundation (ASF) under one |
| // or more contributor license agreements. See the NOTICE file |
| // distributed with this work for additional information |
| // regarding copyright ownership. The ASF licenses this file |
| // to you under the Apache License, Version 2.0 (the |
| // "License"); you may not use this file except in compliance |
| // with the License. You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, |
| // software distributed under the License is distributed on an |
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| // KIND, either express or implied. See the License for the |
| // specific language governing permissions and limitations |
| // under the License. |
| |
| #include <stdlib.h> |
| #include <unistd.h> |
| #include <stdio.h> |
| #include <signal.h> |
| #include <gtest/gtest.h> |
| #include "butil/time.h" |
| #include "butil/macros.h" |
| |
| namespace { |
| void* read_thread(void* arg) { |
| const size_t N = 10000; |
| #ifdef CHECK_RWLOCK |
| pthread_rwlock_t* lock = (pthread_rwlock_t*)arg; |
| #else |
| pthread_mutex_t* lock = (pthread_mutex_t*)arg; |
| #endif |
| const long t1 = butil::cpuwide_time_ns(); |
| for (size_t i = 0; i < N; ++i) { |
| #ifdef CHECK_RWLOCK |
| pthread_rwlock_rdlock(lock); |
| pthread_rwlock_unlock(lock); |
| #else |
| pthread_mutex_lock(lock); |
| pthread_mutex_unlock(lock); |
| #endif |
| } |
| const long t2 = butil::cpuwide_time_ns(); |
| return new long((t2 - t1)/N); |
| } |
| |
| void* write_thread(void*) { |
| return NULL; |
| } |
| |
| TEST(RWLockTest, rdlock_performance) { |
| #ifdef CHECK_RWLOCK |
| pthread_rwlock_t lock1; |
| ASSERT_EQ(0, pthread_rwlock_init(&lock1, NULL)); |
| #else |
| pthread_mutex_t lock1; |
| ASSERT_EQ(0, pthread_mutex_init(&lock1, NULL)); |
| #endif |
| pthread_t rth[16]; |
| pthread_t wth; |
| for (size_t i = 0; i < ARRAY_SIZE(rth); ++i) { |
| ASSERT_EQ(0, pthread_create(&rth[i], NULL, read_thread, &lock1)); |
| } |
| ASSERT_EQ(0, pthread_create(&wth, NULL, write_thread, &lock1)); |
| |
| for (size_t i = 0; i < ARRAY_SIZE(rth); ++i) { |
| long* res = NULL; |
| pthread_join(rth[i], (void**)&res); |
| printf("read thread %lu = %ldns\n", i, *res); |
| } |
| pthread_join(wth, NULL); |
| #ifdef CHECK_RWLOCK |
| pthread_rwlock_destroy(&lock1); |
| #else |
| pthread_mutex_destroy(&lock1); |
| #endif |
| } |
| } // namespace |