rwlock.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* Copyright (C) 2009-2015 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@redhat.com>.
  3. This file is part of the GNU Transactional Memory Library (libitm).
  4. Libitm is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. Libitm is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #ifndef GTM_RWLOCK_H
  20. #define GTM_RWLOCK_H
  21. #include <pthread.h>
  22. #include "local_atomic"
  23. namespace GTM HIDDEN {
  24. struct gtm_thread;
  25. // This datastructure is the blocking, mutex-based side of the Dekker-style
  26. // reader-writer lock used to provide mutual exclusion between active and
  27. // serial transactions. It has similarities to POSIX pthread_rwlock_t except
  28. // that we also provide for upgrading a reader->writer lock, with a
  29. // positive indication of failure (another writer acquired the lock
  30. // before we were able to acquire). While the writer flag (a_writer below) is
  31. // global and protected by the mutex, there are per-transaction reader flags,
  32. // which are stored in a transaction's shared state.
  33. // See libitm's documentation for further details.
  34. //
  35. // In this implementation, writers are given highest priority access but
  36. // read-to-write upgrades do not have a higher priority than writers.
  37. //
  38. // Do not change the layout of this class; it must remain a POD type with
  39. // standard layout, and the SUMMARY field must be first (i.e., so the
  40. // assembler code can assume that its address is equal to the address of the
  41. // respective instance of the class).
  42. class gtm_rwlock
  43. {
  44. static const unsigned a_writer = 1; // An active writer.
  45. static const unsigned w_writer = 2; // The w_writers field != 0
  46. static const unsigned w_reader = 4; // The w_readers field != 0
  47. std::atomic<unsigned int> summary; // Bitmask of the above.
  48. pthread_mutex_t mutex; // Held if manipulating any field.
  49. pthread_cond_t c_readers; // Readers wait here
  50. pthread_cond_t c_writers; // Writers wait here for writers
  51. pthread_cond_t c_confirmed_writers; // Writers wait here for readers
  52. unsigned int a_readers; // Nr active readers as observed by a writer
  53. unsigned int w_readers; // Nr waiting readers
  54. unsigned int w_writers; // Nr waiting writers
  55. public:
  56. gtm_rwlock();
  57. ~gtm_rwlock();
  58. void read_lock (gtm_thread *tx);
  59. void read_unlock (gtm_thread *tx);
  60. void write_lock ();
  61. void write_unlock ();
  62. bool write_upgrade (gtm_thread *tx);
  63. void write_upgrade_finish (gtm_thread *tx);
  64. // Returns true iff there is a concurrent active or waiting writer.
  65. // This is primarily useful for simple HyTM approaches, and the value being
  66. // checked is loaded with memory_order_relaxed.
  67. bool is_write_locked()
  68. {
  69. return summary.load (memory_order_relaxed) & (a_writer | w_writer);
  70. }
  71. protected:
  72. bool write_lock_generic (gtm_thread *tx);
  73. };
  74. } // namespace GTM
  75. #endif // GTM_RWLOCK_H