OSDN Git Service

359991ab45b852864e69cbca253291fd3a47c4ce
[pf3gnuchains/gcc-fork.git] / libitm / config / posix / rwlock.h
1 /* Copyright (C) 2009, 2011 Free Software Foundation, Inc.
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>
29 #include "local_atomic"
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.
47
48 class gtm_rwlock
49 {
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
54
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
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
63
64  public:
65   gtm_rwlock();
66   ~gtm_rwlock();
67
68   void read_lock (gtm_thread *tx);
69   void read_unlock (gtm_thread *tx);
70
71   void write_lock ();
72   void write_unlock ();
73
74   bool write_upgrade (gtm_thread *tx);
75
76  protected:
77   bool write_lock_generic (gtm_thread *tx);
78 };
79
80 } // namespace GTM
81
82 #endif // GTM_RWLOCK_H