headers/bsd: Add sys/queue.h.
[haiku.git] / src / tests / servers / app / newerClipping / MultiLocker.h
blob22406dfc32eabc465daf9caf8050218cfc9d42b1
1 /* MultiLocker.h */
2 /*
3 Copyright 1999, Be Incorporated. All Rights Reserved.
4 This file may be used under the terms of the Be Sample Code License.
5 */
7 /* multiple-reader single-writer locking class */
9 #ifndef MULTI_LOCKER_H
10 #define MULTI_LOCKER_H
12 //#define TIMING 1
14 #include <OS.h>
16 const int32 LARGE_NUMBER = 100000;
18 class MultiLocker {
19 public:
20 MultiLocker(const char* semaphoreBaseName);
21 virtual ~MultiLocker();
23 status_t InitCheck();
25 //locking for reading or writing
26 bool ReadLock();
27 bool WriteLock();
29 //unlocking after reading or writing
30 bool ReadUnlock();
31 bool WriteUnlock();
33 //does the current thread hold a write lock ?
34 bool IsWriteLocked(uint32 *stack_base = NULL, thread_id *thread = NULL);
35 //in DEBUG mode returns whether the lock is held
36 //in non-debug mode returns true
37 bool IsReadLocked();
39 private:
40 //functions for managing the DEBUG reader array
41 void register_thread();
42 void unregister_thread();
44 status_t fInit;
45 //readers adjust count and block on fReadSem when a writer
46 //hold the lock
47 int32 fReadCount;
48 sem_id fReadSem;
49 //writers adjust the count and block on fWriteSem
50 //when readers hold the lock
51 int32 fWriteCount;
52 sem_id fWriteSem;
53 //writers must acquire fWriterLock when acquiring a write lock
54 int32 fLockCount;
55 sem_id fWriterLock;
56 int32 fWriterNest;
58 thread_id fWriterThread;
59 uint32 fWriterStackBase;
61 int32 * fDebugArray;
62 int32 fMaxThreads;
64 #if TIMING
65 uint32 rl_count;
66 bigtime_t rl_time;
67 uint32 ru_count;
68 bigtime_t ru_time;
69 uint32 wl_count;
70 bigtime_t wl_time;
71 uint32 wu_count;
72 bigtime_t wu_time;
73 uint32 islock_count;
74 bigtime_t islock_time;
75 uint32 reg_count;
76 bigtime_t reg_time;
77 uint32 unreg_count;
78 bigtime_t unreg_time;
79 #endif
82 class AutoWriteLocker {
83 public:
84 AutoWriteLocker(MultiLocker* lock)
85 : fLock(lock)
87 fLock->WriteLock();
89 ~AutoWriteLocker()
91 fLock->WriteUnlock();
93 private:
94 MultiLocker* fLock;
97 class AutoReadLocker {
98 public:
99 AutoReadLocker(MultiLocker* lock)
100 : fLock(lock)
102 fLock->ReadLock();
104 ~AutoReadLocker()
106 fLock->ReadUnlock();
108 private:
109 MultiLocker* fLock;
114 #endif