libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNRWLock.h
Go to the documentation of this file.
1 /* Copyright 2011-2015 CoReNum, INSA-Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNRWLock.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNRWLock_HEADER
23 #define CRNRWLock_HEADER
24 
25 #include <CRN.h>
26 
27 namespace crn
28 {
37  class RWLock
38  {
39  public:
41  RWLock();
43  ~RWLock();
44  RWLock(const RWLock &) = delete;
45  RWLock(RWLock&&) = delete;
46  RWLock& operator=(const RWLock&) = delete;
47  RWLock& operator=(RWLock&&) = delete;
48 
50  void WaitReadLock();
52  void ReadUnlock();
53 
55  void WaitWriteLock();
57  void WriteUnlock();
58 
60  class ReadLock
61  {
62  public:
64  ReadLock(RWLock &l):lock(l) { lock.WaitReadLock(); }
66  ~ReadLock() { lock.ReadUnlock(); }
67  private:
68  RWLock &lock;
69  };
70 
72  class WriteLock
73  {
74  public:
76  WriteLock(RWLock &l):lock(l) { lock.WaitWriteLock(); }
78  ~WriteLock() { lock.WriteUnlock(); }
79  private:
80  RWLock &lock;
81  };
82 
83  private:
84  struct internal_data;
85  std::unique_ptr<internal_data> data;
86  };
87 }
88 
89 #endif
90 
A lock that allows multiple readers or one exclusive writer.
Definition: CRNRWLock.h:37
void WriteUnlock()
Releases write token.
Definition: CRNRWLock.cpp:195
RA2I read auto-lock.
Definition: CRNRWLock.h:60
RWLock()
Constructor.
Definition: CRNRWLock.cpp:47
ReadLock(RWLock &l)
Locks.
Definition: CRNRWLock.h:64
~WriteLock()
Unlocks.
Definition: CRNRWLock.h:78
~RWLock()
Destructor.
Definition: CRNRWLock.cpp:62
RA2I write auto-lock.
Definition: CRNRWLock.h:72
RWLock & operator=(const RWLock &)=delete
void WaitWriteLock()
Requests authorization to write.
Definition: CRNRWLock.cpp:149
void WaitReadLock()
Requests authorization to read.
Definition: CRNRWLock.cpp:74
WriteLock(RWLock &l)
Locks.
Definition: CRNRWLock.h:76
~ReadLock()
Unlocks.
Definition: CRNRWLock.h:66
void ReadUnlock()
Releases read token.
Definition: CRNRWLock.cpp:121