blob: d0c6d5e22f24524d59d140b11bc313f6d8781a02 [file] [log] [blame]
Yabin Cui76615da2015-03-17 14:22:09 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
Elliott Hughes5e62b342018-10-25 11:00:00 -070028
29#pragma once
Yabin Cui76615da2015-03-17 14:22:09 -070030
31#include <stdatomic.h>
32#include "private/bionic_futex.h"
Josh Gao4956c372019-12-19 16:35:51 -080033#include "platform/bionic/macros.h"
Yabin Cui76615da2015-03-17 14:22:09 -070034
Yabin Cuid26e7802015-10-22 20:07:56 -070035// Lock is used in places like pthread_rwlock_t, which can be initialized without calling
36// an initialization function. So make sure Lock can be initialized by setting its memory to 0.
Yabin Cui76615da2015-03-17 14:22:09 -070037class Lock {
38 private:
39 enum LockState {
40 Unlocked = 0,
41 LockedWithoutWaiter,
42 LockedWithWaiter,
43 };
44 _Atomic(LockState) state;
45 bool process_shared;
46
47 public:
Yabin Cui76615da2015-03-17 14:22:09 -070048 void init(bool process_shared) {
Nick Desaulniers2e65afe2024-11-19 09:27:06 -080049 atomic_store_explicit(&state, Unlocked, memory_order_relaxed);
Yabin Cui76615da2015-03-17 14:22:09 -070050 this->process_shared = process_shared;
51 }
52
Yabin Cuife3a83a2015-11-17 16:03:18 -080053 bool trylock() {
54 LockState old_state = Unlocked;
55 return __predict_true(atomic_compare_exchange_strong_explicit(&state, &old_state,
56 LockedWithoutWaiter, memory_order_acquire, memory_order_relaxed));
57 }
58
Yabin Cui76615da2015-03-17 14:22:09 -070059 void lock() {
60 LockState old_state = Unlocked;
61 if (__predict_true(atomic_compare_exchange_strong_explicit(&state, &old_state,
62 LockedWithoutWaiter, memory_order_acquire, memory_order_relaxed))) {
63 return;
64 }
65 while (atomic_exchange_explicit(&state, LockedWithWaiter, memory_order_acquire) != Unlocked) {
66 // TODO: As the critical section is brief, it is a better choice to spin a few times befor sleeping.
Tom Cherryac49ced2017-08-17 13:18:52 -070067 __futex_wait_ex(&state, process_shared, LockedWithWaiter);
Yabin Cui76615da2015-03-17 14:22:09 -070068 }
69 return;
70 }
71
72 void unlock() {
Adrian-CJ Hung8c1a14d2019-04-03 10:39:15 +080073 bool shared = process_shared; /* cache to local variable */
Yabin Cui76615da2015-03-17 14:22:09 -070074 if (atomic_exchange_explicit(&state, Unlocked, memory_order_release) == LockedWithWaiter) {
Ryan Prichardaca11012019-04-18 17:50:22 -070075 // The Lock object may have been deallocated between the atomic exchange and the futex wake
76 // call, so avoid accessing any fields of Lock here. In that case, the wake call may target
77 // unmapped memory or trigger a spurious futex wakeup. The same situation happens with
78 // pthread mutexes. References:
79 // - https://lkml.org/lkml/2014/11/27/472
80 // - http://austingroupbugs.net/view.php?id=811#c2267
Adrian-CJ Hung8c1a14d2019-04-03 10:39:15 +080081 __futex_wake_ex(&state, shared, 1);
Yabin Cui76615da2015-03-17 14:22:09 -070082 }
83 }
84};
85
Tom Cherry6034ef82018-02-02 16:10:07 -080086class LockGuard {
87 public:
Chih-Hung Hsieh770032d2019-01-02 10:59:48 -080088 explicit LockGuard(Lock& lock) : lock_(lock) {
Tom Cherry6034ef82018-02-02 16:10:07 -080089 lock_.lock();
90 }
91 ~LockGuard() {
92 lock_.unlock();
93 }
94
Elliott Hughes5e62b342018-10-25 11:00:00 -070095 BIONIC_DISALLOW_COPY_AND_ASSIGN(LockGuard);
Tom Cherry6034ef82018-02-02 16:10:07 -080096
97 private:
98 Lock& lock_;
99};