• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
7 #include "bench/Benchmark.h"
8 #include "include/core/SkString.h"
9 #include "include/private/base/SkMutex.h"
10 #include "src/base/SkSpinlock.h"
11 
12 #include <shared_mutex>
13 
14 template <typename Mutex>
15 class MutexBench : public Benchmark {
16 public:
MutexBench(SkString benchPrefix)17     MutexBench(SkString benchPrefix) : fBenchName(benchPrefix += "UncontendedBenchmark") { }
isSuitableFor(Backend backend)18     bool isSuitableFor(Backend backend) override {
19         return backend == Backend::kNonRendering;
20     }
21 
22 protected:
onGetName()23     const char* onGetName() override {
24         return fBenchName.c_str();
25     }
26 
onDraw(int loops,SkCanvas *)27     void onDraw(int loops, SkCanvas*) override {
28         for (int i = 0; i < loops; i++) {
29             fMu.acquire();
30             fMu.release();
31         }
32     }
33 
34 private:
35     SkString fBenchName;
36     Mutex fMu;
37 };
38 
39 
40 // Wrappers around a possibly null shared mutex that acquire the lock for
41 // as long as the object is in scope.
42 class SK_SCOPED_CAPABILITY Exclusive {
43 public:
Exclusive(std::shared_mutex * maybe_lock)44     explicit Exclusive(std::shared_mutex* maybe_lock)
45         : fLock(maybe_lock) {
46         if (fLock) {
47             fLock->lock();
48         }
49     }
~Exclusive()50     ~Exclusive() {
51         if (fLock) {
52             fLock->unlock();
53         }
54     }
55 
56 private:
57     std::shared_mutex* fLock;
58 };
59 class SK_SCOPED_CAPABILITY Shared {
60 public:
Shared(std::shared_mutex * maybe_lock)61     explicit Shared(std::shared_mutex* maybe_lock)
62         : fLock(maybe_lock)  {
63         if (fLock) {
64             fLock->lock_shared();
65         }
66     }
67 
~Shared()68     ~Shared() {
69         if (fLock) {
70             fLock->unlock_shared();
71         }
72     }
73 
74 private:
75     std::shared_mutex* fLock;
76 };
77 
maybe_dw_mutex(size_t typeface)78 static std::shared_mutex* maybe_dw_mutex(size_t typeface) {
79     static std::shared_mutex mutex;
80     return typeface > 60 ? nullptr : &mutex;
81 }
82 
is_hinted(size_t typeface)83 bool is_hinted(size_t typeface) {
84     Exclusive l(maybe_dw_mutex(typeface));
85     Exclusive l2(maybe_dw_mutex(typeface));
86     if (typeface > 20) {
87         return false;
88     }
89     return true;
90 }
91 
92 ///////////////////////////////////////////////////////////////////////////////
93 
94 DEF_BENCH( return new MutexBench<SkMutex>(SkString("SkMutex")); )
95 DEF_BENCH( return new MutexBench<SkSpinlock>(SkString("SkSpinlock")); )
96