• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
28 
29 #pragma once
30 
31 #include <stdatomic.h>
32 #include "private/bionic_futex.h"
33 #include "private/bionic_macros.h"
34 
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.
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:
init(bool process_shared)48   void init(bool process_shared) {
49     atomic_init(&state, Unlocked);
50     this->process_shared = process_shared;
51   }
52 
trylock()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 
lock()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.
67       __futex_wait_ex(&state, process_shared, LockedWithWaiter);
68     }
69     return;
70   }
71 
unlock()72   void unlock() {
73     bool shared = process_shared; /* cache to local variable */
74     if (atomic_exchange_explicit(&state, Unlocked, memory_order_release) == LockedWithWaiter) {
75       __futex_wake_ex(&state, shared, 1);
76     }
77   }
78 };
79 
80 class LockGuard {
81  public:
LockGuard(Lock & lock)82   explicit LockGuard(Lock& lock) : lock_(lock) {
83     lock_.lock();
84   }
~LockGuard()85   ~LockGuard() {
86     lock_.unlock();
87   }
88 
89   BIONIC_DISALLOW_COPY_AND_ASSIGN(LockGuard);
90 
91  private:
92   Lock& lock_;
93 };
94