2
* Copyright (c) 2005 Massachusetts Institute of Technology
4
* Permission is hereby granted, free of charge, to any person
5
* obtaining a copy of this software and associated documentation
6
* files (the "Software"), to deal in the Software without
7
* restriction, including without limitation the rights to use, copy,
8
* modify, merge, publish, distribute, sublicense, and/or sell copies
9
* of the Software, and to permit persons to whom the Software is
10
* furnished to do so, subject to the following conditions:
12
* The above copyright notice and this permission notice shall be
13
* included in all copies or substantial portions of the Software.
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
#ifndef __KHIMAIRA_SYNC_H
28
#define __KHIMAIRA_SYNC_H
35
/*! \defgroup util_sync Synchronization
38
/*! \brief A read/write lock
40
A classic read/write lock. Allows multiple readers or a single
41
writer to access a protected object. Readers will wait for any
42
pending writer to release the lock, while a writer will wait for
43
any pending readers to release the lock.
45
typedef struct tag_rwlock {
52
DWORD writer; /* TID of writer thread */
55
typedef rw_lock_t RWLOCK, *PRWLOCK;
57
/*! \brief Initialize a read/write lock.
59
A lock <b>must</b> be initialized before it can be used.
60
Initializing the lock does not grant the caller any locks on the
63
KHMEXP void KHMAPI InitializeRwLock(PRWLOCK pLock);
65
/*! \brief Delete a read/write lock
67
Once the application is done using the read/write lock, it must be
68
deleted with a call to DeleteRwLock()
70
KHMEXP void KHMAPI DeleteRwLock(PRWLOCK pLock);
72
/*! \brief Obtains a read lock on the read/write lock
74
Multiple readers can obtain read locks on the same r/w lock.
75
However, if any thread attempts to obtain a write lock on the
76
object, it will wait until all readers have released the read
79
Call LockReleaseRead() to release the read lock. While the same
80
thread may obtain multiple read locks on the same object, each
81
call to LockObtainRead() must have a corresponding call to
82
LockReleaseRead() to properly relinquish the lock.
84
\see LockReleaseRead()
86
KHMEXP void KHMAPI LockObtainRead(PRWLOCK pLock);
88
/*! \brief Relase a read lock obtained on a read/write lock
90
Each call to LockObtainRead() must have a corresponding call to
91
LockReleaseRead(). Once all read locks are released, any threads
92
waiting on write locks on the object will be woken and assigned a
97
KHMEXP void KHMAPI LockReleaseRead(PRWLOCK pLock);
99
/*! \brief Obtains a write lock on the read/write lock
101
Only a single writer is allowed to lock a single r/w lock.
102
However, if any thread attempts to obtain a read lock on the
103
object, it will wait until the writer has released the lock.
105
Call LockReleaseWrite() to release the write lock. While the same
106
thread may obtain multiple write locks on the same object, each
107
call to LockObtainWrite() must have a corresponding call to
108
LockReleaseWrite() to properly relinquish the lock.
110
\see LockReleaseWrite()
112
KHMEXP void KHMAPI LockObtainWrite(PRWLOCK pLock);
114
/*! \brief Relase a write lock obtained on a read/write lock
116
Each call to LockObtainWrite() must have a corresponding call to
117
LockReleaseWrite(). Once the write lock is released, any threads
118
waiting for read or write locks on the object will be woken and
119
assigned the proper lock.
121
\see LockObtainWrite()
123
KHMEXP void KHMAPI LockReleaseWrite(PRWLOCK pLock);