sipxportlib  Version 3.3
OsRWMutexShared.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2006 SIPfoundry Inc.
3 // Licensed by SIPfoundry under the LGPL license.
4 //
5 // Copyright (C) 2004-2006 Pingtel Corp. All rights reserved.
6 // Licensed to SIPfoundry under a Contributor Agreement.
7 //
8 // $$
10 
11 
12 #ifndef _OsRWMutexShared_h_
13 #define _OsRWMutexShared_h_
14 
15 // SYSTEM INCLUDES
16 
17 // APPLICATION INCLUDES
18 #include "os/OsRWMutex.h"
19 #include "os/OsBSem.h"
20 #include "os/OsCSem.h"
21 
22 // DEFINES
23 // MACROS
24 // EXTERNAL FUNCTIONS
25 // EXTERNAL VARIABLES
26 // CONSTANTS
27 // STRUCTS
28 // TYPEDEFS
29 // FORWARD DECLARATIONS
30 
31 //:Mutual exclusion semaphore handling multiple readers and writers
32 // Two kinds of concurrent tasks, called "readers" and "writers", share a
33 // single resource. The readers can use the resource simultaneously, but each
34 // writer must have exclusive access to it. When a writer is ready to use the
35 // resource, it should be enabled to do so as soon as possible.
37 {
38 /* //////////////////////////// PUBLIC //////////////////////////////////// */
39 public:
40 
42  {
43  Q_FIFO = 0x0, // queue blocked tasks on a first-in, first-out basis
44  Q_PRIORITY = 0x1 // queue blocked tasks based on their priority
45  };
46 
47 /* ============================ CREATORS ================================== */
48 
49  OsRWMutexShared(const int queueOptions);
50  //:Constructor
51 
53  //:Destructor
54 
55 /* ============================ MANIPULATORS ============================== */
56 
57  OsStatus acquireRead(void);
58  //:Block (if necessary) until the task acquires the resource for reading
59  // Multiple simultaneous readers are allowed.
60 
61  OsStatus acquireWrite(void);
62  //:Block (if necessary) until the task acquires the resource for writing
63  // Only one writer at a time is allowed (and no readers).
64 
66  //:Conditionally acquire the resource for reading (i.e., don't block)
67  // Multiple simultaneous readers are allowed.
68  // Return OS_BUSY if the resource is held for writing by some other task
69 
71  //:Conditionally acquire the resource for writing (i.e., don't block).
72  // Only one writer at a time is allowed (and no readers).
73  // Return OS_BUSY if the resource is held for writing by some other task
74  // or if there are running readers.
75 
76  OsStatus releaseRead(void);
77  //:Release the resource for reading
78 
79  OsStatus releaseWrite(void);
80  //:Release the resource for writing
81 
82 /* ============================ ACCESSORS ================================= */
83 
84 /* ============================ INQUIRY =================================== */
85 
86 /* //////////////////////////// PROTECTED ///////////////////////////////// */
87 protected:
88 
89 /* //////////////////////////// PRIVATE /////////////////////////////////// */
90 private:
91  OsBSem mGuard; // binary semaphore used to implement the
92  // critical section for protecting the RWMutex
93  // structure from concurrent access
94  OsCSem mReadSem; // semaphore used to signal readers
95  OsCSem mWriteSem; // semaphore used to signal writers
96  OsBSem mWriteExclSem; // binary semaphore used to ensure mutual
97  // exclusion among multiple concurrent writers
98  int mActiveReadersCnt; // number of active reader tasks
99  int mActiveWritersCnt; // number of active writer tasks (always <= 1)
100  int mRunningReadersCnt; // number of running reader tasks
101  int mRunningWritersCnt; // number of running writer tasks (always <= 1)
102 
103  OsStatus doAcquireRead(UtlBoolean dontBlock);
104  //:Helper function used to acquire the resource for reading
105 
106  OsStatus doAcquireWrite(UtlBoolean dontBlock);
107  //:Helper function used to acquire the resource for writing
108 
109  OsStatus doAcquireExclWrite(UtlBoolean dontBlock);
110  //:Helper function used to acquire a semaphore for exclusive writing.
111  // A binary semaphore is used to ensure that there is only a single
112  // writer operating on the resource at any one time.
113 
114  OsStatus doReleaseRead(void);
115  //:Helper function allowing a reader to give up access to the resource.
116  // The mGuard object must be acquired (and ultimately be released) by
117  // callers of this method.
118 
119  OsStatus doReleaseNonExclWrite(UtlBoolean guardIsHeld);
120  //:Helper function to release non-exclusive write access to the resource
121  // The mGuard object must be acquired (and ultimately be released) by
122  // callers of this method.
123 
124  OsStatus doReleaseExclWrite(void);
125  //:Helper function to release exclusive write access to the resource
126 
127  void grantReadTickets(void);
128  // Determine whether the resource can be granted immediately to readers
129  // and, if so, grant sufficient read "tickets" for all active but not
130  // yet running readers.
131 
132  void grantWriteTickets(void);
133  // Determine whether the resource can be granted immediately to writers
134  // and, if so, grant sufficient write "tickets" for all active but not
135  // yet running writers.
136 
137  OsRWMutexShared();
138  //:Default constructor (not implemented for this class)
139 
140  OsRWMutexShared(const OsRWMutexShared& rOsRWMutexShared);
141  //:Copy constructor (not implemented for this class)
142 
143  OsRWMutexShared& operator=(const OsRWMutexShared& rhs);
144  //:Assignment operator (not implemented for this class)
145 
146 };
147 
148 /* ============================ INLINE METHODS ============================ */
149 
150 #endif // _OsRWMutexShared_h_
OsStatus tryAcquireWrite(void)
Definition: OsRWMutexShared.cpp:94
Definition: OsRWMutexShared.h:43
OsStatus releaseRead(void)
Definition: OsRWMutexShared.cpp:100
OsStatus
Definition: OsStatus.h:27
Definition: OsRWMutexShared.h:44
OsStatus acquireWrite(void)
Definition: OsRWMutexShared.cpp:78
Definition: OsRWMutexShared.h:36
~OsRWMutexShared()
Definition: OsRWMutexShared.cpp:62
OsStatus tryAcquireRead(void)
Definition: OsRWMutexShared.cpp:86
int UtlBoolean
Definition: UtlDefs.h:41
OsStatus releaseWrite(void)
Definition: OsRWMutexShared.cpp:121
OsStatus acquireRead(void)
Definition: OsRWMutexShared.cpp:71
QueueOptions
Definition: OsRWMutexShared.h:41