|
| 1 | +// |
| 2 | +// read_write_lock.hpp |
| 3 | +// read_write_lock |
| 4 | +// |
| 5 | +// Created by herbert koelman on 18/03/2016. |
| 6 | +// |
| 7 | +// |
| 8 | + |
| 9 | +#ifndef pthread_read_write_lock_hpp |
| 10 | +#define pthread_read_write_lock_hpp |
| 11 | + |
| 12 | +// must be include as first hearder file of each source code file (see IBM's |
| 13 | +// recommandation for more info p.285 §8.3.1). |
| 14 | +#include <pthread.h> |
| 15 | + |
| 16 | +#include "pthread/config.h" |
| 17 | +#include "pthread/exceptions.hpp" |
| 18 | + |
| 19 | + |
| 20 | +namespace pthread { |
| 21 | + |
| 22 | + /** \addtogroup concurrency |
| 23 | + * |
| 24 | + * @{ |
| 25 | + */ |
| 26 | + |
| 27 | + /** This class acquires the read lock. |
| 28 | + * |
| 29 | + * This class cannot be instaiated as it's main putpose is to implement read locks. To use a read lock create |
| 30 | + * either a read_write_lock or a write_lock. |
| 31 | + * |
| 32 | + * @author herbert koelman ([email protected]) |
| 33 | + * @since v1.6.0 |
| 34 | + * @see pthread::read_write_lock |
| 35 | + */ |
| 36 | + class read_lock { |
| 37 | + public: |
| 38 | + |
| 39 | + /** The method apply a read lock. |
| 40 | + * |
| 41 | + * The calling thread acquires the write lock if no other thread (reader or writer) holds the read-write lock. Otherwise, the thread shall block until |
| 42 | + * it can acquire the lock. The calling thread may deadlock if at the time the call is made it holds the read-write lock (whether a read or write lock). |
| 43 | + */ |
| 44 | + void lock (); |
| 45 | + |
| 46 | + /** The method shall apply a read lock, if any thread currently holds the lock (for reading or writing) |
| 47 | + * the method throws an exception. |
| 48 | + * |
| 49 | + @see lock |
| 50 | + */ |
| 51 | + void try_lock (); |
| 52 | + |
| 53 | + /** release the read lock. |
| 54 | + @throw read_write_lock_exception if error conditions preventing this method to succeed. |
| 55 | + */ |
| 56 | + void unlock (); |
| 57 | + |
| 58 | + |
| 59 | + /** |
| 60 | + * the descructor, shall destroy the read-write lock object referenced by rwlock and release any resources used by the lock. |
| 61 | + */ |
| 62 | + virtual ~read_lock (); |
| 63 | + |
| 64 | + protected: |
| 65 | + |
| 66 | + /** |
| 67 | + Constructor/Desctructor |
| 68 | + |
| 69 | + this constructor shall allocate any resources required to use the read-write lock referenced by rwlock and initializes the lock to an unlocked state. The read/write lock |
| 70 | + passes NULL attributes. This means default behavior. |
| 71 | + |
| 72 | + @throw read_write_lock_exception if error conditions preventing this method to succeed. |
| 73 | + */ |
| 74 | + read_lock (); |
| 75 | + |
| 76 | + /** read/write lock reference */ |
| 77 | + pthread_rwlock_t _rwlock; |
| 78 | + }; |
| 79 | + |
| 80 | + /** This class acquires the read/write write lock |
| 81 | + * |
| 82 | + * @author herbert koelman ([email protected]) |
| 83 | + * @since v1.6.0 |
| 84 | + */ |
| 85 | + class write_lock: public read_lock { |
| 86 | + public: |
| 87 | + /** The method apply a write lock. |
| 88 | + * |
| 89 | + * The calling thread acquires the write lock if no other thread (reader or writer) holds the read-write lock. Otherwise, the thread shall block until |
| 90 | + * it can acquire the lock. The calling thread may deadlock if at the time the call is made it holds the read-write lock (whether a read or write lock). |
| 91 | + */ |
| 92 | + void lock (); |
| 93 | + |
| 94 | + /** The method shall apply a write lock, if any thread currently holds the lock (for reading or writing) |
| 95 | + * the method throws an exception. |
| 96 | + * |
| 97 | + @see lock |
| 98 | + */ |
| 99 | + void try_lock (); |
| 100 | + |
| 101 | + /** |
| 102 | + Constructor/Desctructor |
| 103 | +
|
| 104 | + this constructor shall allocate any resources required to use the read-write lock referenced by rwlock and initializes the lock to an unlocked state. The read/write lock |
| 105 | + passes NULL attributes. This means default behavior. |
| 106 | +
|
| 107 | + @throw read_write_lock_exception if error conditions preventing this method to succeed. |
| 108 | + */ |
| 109 | + write_lock (); |
| 110 | + |
| 111 | + /** |
| 112 | + * the descructor, shall destroy the read-write lock object referenced by rwlock and release any resources used by the lock. |
| 113 | + */ |
| 114 | + virtual ~write_lock (); |
| 115 | + |
| 116 | + }; |
| 117 | + |
| 118 | + /** A read/write lock. |
| 119 | + * |
| 120 | + * <pre><code> |
| 121 | + * ... |
| 122 | + * pthread::read_write_lock _rwlock; |
| 123 | + * |
| 124 | + * { |
| 125 | + * // get a read lock |
| 126 | + * pthread::lock_guard<pthread::read_lock> lock(_rwlck); |
| 127 | + * ... |
| 128 | + * } // lock_guard unlock lock here |
| 129 | + * |
| 130 | + * { |
| 131 | + * // get write lock |
| 132 | + * pthread::lock_guard<pthread::write_lock> lock(_rwlck); |
| 133 | + * ... |
| 134 | + * } // lock_guard unlock lock here |
| 135 | + * |
| 136 | + * </code></pre> |
| 137 | + * |
| 138 | + */ |
| 139 | + typedef write_lock read_write_lock; |
| 140 | + |
| 141 | + /** @} */ |
| 142 | +} // namespace pthread |
| 143 | + |
| 144 | +#endif /* pthread_read_write_lock_H */ |
0 commit comments