OSDN Git Service

f538bd059a2ce7b6ccd7d79e0a6b0cc8283431c4
[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
30 namespace GTM HIDDEN {
31
32 struct gtm_thread;
33
34 // This datastructure is the blocking, mutex-based side of the Dekker-style
35 // reader-writer lock used to provide mutual exclusion between active and
36 // serial transactions. It has similarities to POSIX pthread_rwlock_t except
37 // that we also provide for upgrading a reader->writer lock, with a
38 // positive indication of failure (another writer acquired the lock
39 // before we were able to acquire). While the writer flag (a_writer below) is
40 // global and protected by the mutex, there are per-transaction reader flags,
41 // which are stored in a transaction's shared state.
42 // See libitm's documentation for further details.
43 //
44 // In this implementation, writers are given highest priority access but
45 // read-to-write upgrades do not have a higher priority than writers.
46
47 class gtm_rwlock
48 {
49   pthread_mutex_t mutex;                // Held if manipulating any field.
50   pthread_cond_t c_readers;             // Readers wait here
51   pthread_cond_t c_writers;             // Writers wait here for writers
52   pthread_cond_t c_confirmed_writers;   // Writers wait here for readers
53
54   static const unsigned a_writer  = 1;  // An active writer.
55   static const unsigned w_writer  = 2;  // The w_writers field != 0
56   static const unsigned w_reader  = 4;  // The w_readers field != 0
57
58   unsigned int summary;         // Bitmask of the above.
59   unsigned int a_readers;       // Nr active readers as observed by a writer
60   unsigned int w_readers;       // Nr waiting readers
61   unsigned int w_writers;       // Nr waiting writers
62
63  public:
64   gtm_rwlock();
65   ~gtm_rwlock();
66
67   void read_lock (gtm_thread *tx);
68   void read_unlock (gtm_thread *tx);
69
70   void write_lock ();
71   void write_unlock ();
72
73   bool write_upgrade (gtm_thread *tx);
74
75  protected:
76   bool write_lock_generic (gtm_thread *tx);
77 };
78
79 } // namespace GTM
80
81 #endif // GTM_RWLOCK_H