blob: ec179d1b66f7c95dd106f4991afd05356ed38f55 [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"
Yabin Cuife3a83a2015-11-17 16:03:18 -080033#include "private/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) {
49 atomic_init(&state, Unlocked);
50 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) {
Adrian-CJ Hung8c1a14d2019-04-03 10:39:15 +080075 __futex_wake_ex(&state, shared, 1);
Yabin Cui76615da2015-03-17 14:22:09 -070076 }
77 }
78};
79
Tom Cherry6034ef82018-02-02 16:10:07 -080080class LockGuard {
81 public:
Chih-Hung Hsieh770032d2019-01-02 10:59:48 -080082 explicit LockGuard(Lock& lock) : lock_(lock) {
Tom Cherry6034ef82018-02-02 16:10:07 -080083 lock_.lock();
84 }
85 ~LockGuard() {
86 lock_.unlock();
87 }
88
Elliott Hughes5e62b342018-10-25 11:00:00 -070089 BIONIC_DISALLOW_COPY_AND_ASSIGN(LockGuard);
Tom Cherry6034ef82018-02-02 16:10:07 -080090
91 private:
92 Lock& lock_;
93};