]>
Commit | Line | Data |
---|---|---|
83ffe9cd | 1 | /* Copyright (C) 2009-2023 Free Software Foundation, Inc. |
0a35513e AH |
2 | Contributed by Richard Henderson <rth@redhat.com>. |
3 | ||
4 | This file is part of the GNU Transactional Memory Library (libitm). | |
5 | ||
6 | Libitm is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 3 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | Libitm is distributed in the hope that it will be useful, but WITHOUT ANY | |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
13 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
14 | more details. | |
15 | ||
16 | Under Section 7 of GPL version 3, you are granted additional | |
17 | permissions described in the GCC Runtime Library Exception, version | |
18 | 3.1, as published by the Free Software Foundation. | |
19 | ||
20 | You should have received a copy of the GNU General Public License and | |
21 | a copy of the GCC Runtime Library Exception along with this program; | |
22 | see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | <http://www.gnu.org/licenses/>. */ | |
24 | ||
25 | #ifndef GTM_RWLOCK_H | |
26 | #define GTM_RWLOCK_H | |
27 | ||
28 | #include <pthread.h> | |
799142bf | 29 | #include "local_atomic" |
0a35513e AH |
30 | |
31 | namespace GTM HIDDEN { | |
32 | ||
33 | struct gtm_thread; | |
34 | ||
35 | // This datastructure is the blocking, mutex-based side of the Dekker-style | |
36 | // reader-writer lock used to provide mutual exclusion between active and | |
37 | // serial transactions. It has similarities to POSIX pthread_rwlock_t except | |
38 | // that we also provide for upgrading a reader->writer lock, with a | |
39 | // positive indication of failure (another writer acquired the lock | |
40 | // before we were able to acquire). While the writer flag (a_writer below) is | |
41 | // global and protected by the mutex, there are per-transaction reader flags, | |
42 | // which are stored in a transaction's shared state. | |
43 | // See libitm's documentation for further details. | |
44 | // | |
45 | // In this implementation, writers are given highest priority access but | |
46 | // read-to-write upgrades do not have a higher priority than writers. | |
bec9ec3f TR |
47 | // |
48 | // Do not change the layout of this class; it must remain a POD type with | |
6041f70a | 49 | // standard layout, and the summary field must be first (i.e., so the |
bec9ec3f | 50 | // assembler code can assume that its address is equal to the address of the |
6041f70a | 51 | // respective instance of the class), and htm_fastpath must be second. |
0a35513e AH |
52 | |
53 | class gtm_rwlock | |
54 | { | |
0a35513e AH |
55 | static const unsigned a_writer = 1; // An active writer. |
56 | static const unsigned w_writer = 2; // The w_writers field != 0 | |
57 | static const unsigned w_reader = 4; // The w_readers field != 0 | |
58 | ||
799142bf | 59 | std::atomic<unsigned int> summary; // Bitmask of the above. |
bec9ec3f | 60 | |
6041f70a TR |
61 | // We put the HTM fastpath control variable here so that HTM fastpath |
62 | // transactions can check efficiently whether they are allowed to run. | |
63 | // This must be accessed atomically because threads can load this value | |
64 | // when they are neither a registered reader nor writer (i.e., when they | |
65 | // attempt to execute the HTM fastpath). | |
66 | std::atomic<uint32_t> htm_fastpath; | |
67 | ||
bec9ec3f TR |
68 | pthread_mutex_t mutex; // Held if manipulating any field. |
69 | pthread_cond_t c_readers; // Readers wait here | |
70 | pthread_cond_t c_writers; // Writers wait here for writers | |
71 | pthread_cond_t c_confirmed_writers; // Writers wait here for readers | |
72 | ||
0a35513e AH |
73 | unsigned int a_readers; // Nr active readers as observed by a writer |
74 | unsigned int w_readers; // Nr waiting readers | |
75 | unsigned int w_writers; // Nr waiting writers | |
76 | ||
77 | public: | |
78 | gtm_rwlock(); | |
79 | ~gtm_rwlock(); | |
80 | ||
81 | void read_lock (gtm_thread *tx); | |
82 | void read_unlock (gtm_thread *tx); | |
83 | ||
84 | void write_lock (); | |
85 | void write_unlock (); | |
86 | ||
87 | bool write_upgrade (gtm_thread *tx); | |
610e3901 | 88 | void write_upgrade_finish (gtm_thread *tx); |
0a35513e | 89 | |
6041f70a TR |
90 | // Returns true iff there is a concurrent active or waiting writer, or |
91 | // htm_fastpath is zero. This is primarily useful for simple HyTM | |
92 | // approaches, and the values being checked are loaded with | |
93 | // memory_order_relaxed. | |
94 | bool htm_fastpath_disabled () | |
95 | { | |
96 | return (summary.load (memory_order_relaxed) & (a_writer | w_writer)) | |
97 | || htm_fastpath.load (memory_order_relaxed) == 0; | |
98 | } | |
99 | ||
100 | // This does not need to return an exact value, hence relaxed MO is | |
101 | // sufficient. | |
102 | uint32_t get_htm_fastpath () | |
103 | { | |
104 | return htm_fastpath.load (memory_order_relaxed); | |
105 | } | |
106 | // This must only be called while having acquired the write lock, and other | |
107 | // threads do not need to load an exact value; hence relaxed MO is | |
108 | // sufficient. | |
109 | void set_htm_fastpath (uint32_t val) | |
64fbcc74 | 110 | { |
6041f70a | 111 | htm_fastpath.store (val, memory_order_relaxed); |
64fbcc74 TR |
112 | } |
113 | ||
0a35513e AH |
114 | protected: |
115 | bool write_lock_generic (gtm_thread *tx); | |
116 | }; | |
117 | ||
118 | } // namespace GTM | |
119 | ||
120 | #endif // GTM_RWLOCK_H |