| Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 1 | /* | 
 | 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 Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 28 |  | 
 | 29 | #pragma once | 
| Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 30 |  | 
 | 31 | #include <stdatomic.h> | 
 | 32 | #include "private/bionic_futex.h" | 
| Yabin Cui | fe3a83a | 2015-11-17 16:03:18 -0800 | [diff] [blame] | 33 | #include "private/bionic_macros.h" | 
| Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 34 |  | 
| Yabin Cui | d26e780 | 2015-10-22 20:07:56 -0700 | [diff] [blame] | 35 | // 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 Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 37 | class 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 Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 48 |   void init(bool process_shared) { | 
 | 49 |     atomic_init(&state, Unlocked); | 
 | 50 |     this->process_shared = process_shared; | 
 | 51 |   } | 
 | 52 |  | 
| Yabin Cui | fe3a83a | 2015-11-17 16:03:18 -0800 | [diff] [blame] | 53 |   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 Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 59 |   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 Cherry | ac49ced | 2017-08-17 13:18:52 -0700 | [diff] [blame] | 67 |       __futex_wait_ex(&state, process_shared, LockedWithWaiter); | 
| Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 68 |     } | 
 | 69 |     return; | 
 | 70 |   } | 
 | 71 |  | 
 | 72 |   void unlock() { | 
| Adrian-CJ Hung | 8c1a14d | 2019-04-03 10:39:15 +0800 | [diff] [blame] | 73 |     bool shared = process_shared; /* cache to local variable */ | 
| Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 74 |     if (atomic_exchange_explicit(&state, Unlocked, memory_order_release) == LockedWithWaiter) { | 
| Ryan Prichard | aca1101 | 2019-04-18 17:50:22 -0700 | [diff] [blame] | 75 |       // 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 Hung | 8c1a14d | 2019-04-03 10:39:15 +0800 | [diff] [blame] | 81 |       __futex_wake_ex(&state, shared, 1); | 
| Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 82 |     } | 
 | 83 |   } | 
 | 84 | }; | 
 | 85 |  | 
| Tom Cherry | 6034ef8 | 2018-02-02 16:10:07 -0800 | [diff] [blame] | 86 | class LockGuard { | 
 | 87 |  public: | 
| Chih-Hung Hsieh | 770032d | 2019-01-02 10:59:48 -0800 | [diff] [blame] | 88 |   explicit LockGuard(Lock& lock) : lock_(lock) { | 
| Tom Cherry | 6034ef8 | 2018-02-02 16:10:07 -0800 | [diff] [blame] | 89 |     lock_.lock(); | 
 | 90 |   } | 
 | 91 |   ~LockGuard() { | 
 | 92 |     lock_.unlock(); | 
 | 93 |   } | 
 | 94 |  | 
| Elliott Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 95 |   BIONIC_DISALLOW_COPY_AND_ASSIGN(LockGuard); | 
| Tom Cherry | 6034ef8 | 2018-02-02 16:10:07 -0800 | [diff] [blame] | 96 |  | 
 | 97 |  private: | 
 | 98 |   Lock& lock_; | 
 | 99 | }; |