blob: ad9a92a90b3146ea047690833e973dcfb13a8423 [file] [log] [blame]
tomhudson@google.com410e9dc2011-11-14 17:30:08 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
tfarinaf168b862014-06-19 12:32:29 -07007#include "Benchmark.h"
mtklein1b249332015-07-07 12:21:21 -07008#include "SkMutex.h"
herbbcfd5112015-09-18 09:24:18 -07009#include "SkSharedMutex.h"
10#include "SkSpinlock.h"
11#include "SkString.h"
tomhudson@google.com410e9dc2011-11-14 17:30:08 +000012
herbbcfd5112015-09-18 09:24:18 -070013template <typename Mutex>
tfarinaf168b862014-06-19 12:32:29 -070014class MutexBench : public Benchmark {
tomhudson@google.com410e9dc2011-11-14 17:30:08 +000015public:
herbbcfd5112015-09-18 09:24:18 -070016 MutexBench(SkString benchPrefix) : fBenchName(benchPrefix += "UncontendedBenchmark") { }
mtklein36352bf2015-03-25 18:17:31 -070017 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000018 return backend == kNonRendering_Backend;
tomhudson@google.com410e9dc2011-11-14 17:30:08 +000019 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000020
tomhudson@google.com410e9dc2011-11-14 17:30:08 +000021protected:
mtkleinf0599002015-07-13 06:18:39 -070022 const char* onGetName() override {
herbbcfd5112015-09-18 09:24:18 -070023 return fBenchName.c_str();
tomhudson@google.com410e9dc2011-11-14 17:30:08 +000024 }
25
mtkleinf0599002015-07-13 06:18:39 -070026 void onDraw(const int loops, SkCanvas*) override {
herbbcfd5112015-09-18 09:24:18 -070027 Mutex mu;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000028 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +000029 mu.acquire();
30 mu.release();
tomhudson@google.com410e9dc2011-11-14 17:30:08 +000031 }
32 }
33
34private:
tfarinaf168b862014-06-19 12:32:29 -070035 typedef Benchmark INHERITED;
herbbcfd5112015-09-18 09:24:18 -070036 SkString fBenchName;
tomhudson@google.com410e9dc2011-11-14 17:30:08 +000037};
38
39///////////////////////////////////////////////////////////////////////////////
40
herbbcfd5112015-09-18 09:24:18 -070041DEF_BENCH( return new MutexBench<SkSharedMutex>(SkString("SkSharedMutex")); )
42DEF_BENCH( return new MutexBench<SkMutex>(SkString("SkMutex")); )
43DEF_BENCH( return new MutexBench<SkSpinlock>(SkString("SkSpinlock")); )