xref: /aosp_15_r20/external/skia/src/base/SkSpinlock.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2015 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 
8 #include "src/base/SkSpinlock.h"
9 
10 #include "include/private/base/SkFeatures.h"
11 #include "include/private/base/SkThreadAnnotations.h"
12 
13 #if 0
14     #include "include/private/base/SkMutex.h"
15     #include <execinfo.h>
16     #include <stdio.h>
17 
18     static void debug_trace() {
19         void* stack[64];
20         int len = backtrace(stack, std::size(stack));
21 
22         // As you might imagine, we can't use an SkSpinlock here...
23         static SkMutex lock;
24         {
25             SkAutoMutexExclusive locked(lock);
26             fprintf(stderr, "\n");
27             backtrace_symbols_fd(stack, len, 2/*stderr*/);
28             fprintf(stderr, "\n");
29         }
30     }
31 #else
debug_trace()32     static void debug_trace() {}
33 #endif
34 
35 // Renamed from "pause" to avoid conflict with function defined in unistd.h
36 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
37     #include <emmintrin.h>
do_pause()38     static void do_pause() { _mm_pause(); }
39 #else
do_pause()40     static void do_pause() { /*spin*/ }
41 #endif
42 
contendedAcquire()43 void SkSpinlock::contendedAcquire() {
44     debug_trace();
45 
46     // To act as a mutex, we need an acquire barrier when we acquire the lock.
47     SK_POTENTIALLY_BLOCKING_REGION_BEGIN;
48     while (fLocked.exchange(true, std::memory_order_acquire)) {
49         do_pause();
50     }
51     SK_POTENTIALLY_BLOCKING_REGION_END;
52 }
53