* ChangeLog: Fix my email address.
[official-gcc.git] / libitm / config / posix / rwlock.h
blob2e415286aeb19afdaadfb2cede7068646e3561cc
1 /* Copyright (C) 2009-2013 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@redhat.com>.
4 This file is part of the GNU Transactional Memory Library (libitm).
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.
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.
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.
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/>. */
25 #ifndef GTM_RWLOCK_H
26 #define GTM_RWLOCK_H
28 #include <pthread.h>
29 #include "local_atomic"
31 namespace GTM HIDDEN {
33 struct gtm_thread;
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.
45 // In this implementation, writers are given highest priority access but
46 // read-to-write upgrades do not have a higher priority than writers.
48 class gtm_rwlock
50 pthread_mutex_t mutex; // Held if manipulating any field.
51 pthread_cond_t c_readers; // Readers wait here
52 pthread_cond_t c_writers; // Writers wait here for writers
53 pthread_cond_t c_confirmed_writers; // Writers wait here for readers
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
59 std::atomic<unsigned int> summary; // Bitmask of the above.
60 unsigned int a_readers; // Nr active readers as observed by a writer
61 unsigned int w_readers; // Nr waiting readers
62 unsigned int w_writers; // Nr waiting writers
64 public:
65 gtm_rwlock();
66 ~gtm_rwlock();
68 void read_lock (gtm_thread *tx);
69 void read_unlock (gtm_thread *tx);
71 void write_lock ();
72 void write_unlock ();
74 bool write_upgrade (gtm_thread *tx);
75 void write_upgrade_finish (gtm_thread *tx);
77 // Returns true iff there is a concurrent active or waiting writer.
78 // This is primarily useful for simple HyTM approaches, and the value being
79 // checked is loaded with memory_order_relaxed.
80 bool is_write_locked()
82 return summary.load (memory_order_relaxed) & (a_writer | w_writer);
85 protected:
86 bool write_lock_generic (gtm_thread *tx);
89 } // namespace GTM
91 #endif // GTM_RWLOCK_H