Книга: Real-Time Concepts for Embedded Systems
15.7.6 Implementing Reader-Writer Locks Using Condition Variables
15.7.6 Implementing Reader-Writer Locks Using Condition Variables
This section presents another example of the usage of condition variables. The code shown in Listings 15.7, 15.8, and 15.9 are written in C programming language.
Consider a shared memory region that both readers and writers can access. The example reader-writer lock design has the following properties: multiple readers can simultaneously read the memory content, but only one writer is allowed to write data into the shared memory at any one time. The writer can begin writing to the shared memory when that memory region is not accessed by a task (readers or writers). Readers precede writers because readers have priority over writers in term of accessing the shared memory region.
The implementation that follows can be adapted to other types of synchronization scenarios when prioritized access to shared resources is desired, as shown in Listings 15.7, 15.8, and 15.9.
The following assumptions are made in the program listings:
1. The mutex_t data type represents a mutex object and condvar_t represents a condition variable object; both are provided by the RTOS.
2. lock_mutex, unlock_mutex, wait_cond, signal_cond, and broadcast_cond are functions provided by the RTOS. lock_mutex and unlock_mutex operate on the mutex object. wait_cond, signal_cond, and broadcast_cond operate on the condition variable object.
Listing 15.7 shows the data structure needed to implement the reader-writer lock.
Listing 15.7: Data structure for implementing reader-writer locks.
typedef struct {
mutex_t guard_mutex;
condvar_t read_condvar;
condvar_t write_condvar;
int rw_count;
int read_waiting;
} rwlock_t;
rw_count == -1 indicates a writer is active
Listing 15.8 shows the code that the writer task invokes to acquire and to release the lock.
Listing 15.8: Code called by the writer task to acquire and release locks.
acquire_write(rwlock_t *rwlock) {
lock_mutex(&rwlock-›guard_mutex);
while (rwlock-›rw_count!= 0)
wait_cond(&rwlock-›write_condvar,&rwlock-›guard_mutex);
rwlock-›rw_count = -1;
unlock_mutex(&rwlock-›guard_mutex);
}
release_write(rwlock_t *rwlock) {
lock_mutex(&rwlock-›guard_mutex);
rwlock-›rw_count = 0;
if (rwlock-›r_waiting)
broadcast_cond(&rwlock-›read_condvar,&rwlock-›guard_mutex);
else
signal_cond(&rwlock-›write_condvar,&rwlock-›guard_mutex);
unlock_mutex(&rwlock-›guard_mutex);
}
Listing 15.9 shows the code that the reader task invokes to acquire and release the lock.
Listing 15.9: Code called by the reader task to acquire and release locks.
acquire_read(rwlock_t *rwlock) {
lock_mutex(&rwlock-›guard_mutex);
rwlock-›r_waiting++;
while (rwlock-›rw_count ‹ 0)
wait_cond(&rwlock-›read_condvar,&rwlock-›guard_mutex);
rwlock-›r_waiting = 0;
rwlock-›rw_count++;
unlock_mutex(&rwlock-›guard_mutex);
}
release_read(rwlock_t *rwlock) {
lock_mutex(&rwlock-›guard_mutex);
rwlock-›rw_count--;
if (rwlock-›rw_count == 0)
signal_cond(&rwlock-›write_condvar,&rwlock-›guard_mutex);
unlock_mutex(&rwlock-›guard_mutex);
}
In case broadcast_cond does not exist, use a for loop as follows
for (i = rwlock-›read_waiting; i › 0; i--)
signal_cond(&rwlock-›read_condvar,&rwlock-›guard_mutex);
- 15.7.1 Data Transfer with Flow Control
- 15.7.2 Asynchronous Data Reception from Multiple Data Communication Channels
- 15.7.3 Multiple Input Communication Channels
- 15.7.4 Using Condition Variables to Synchronize between Readers and Writers
- 15.7.5 Sending High Priority Data between Tasks
- 15.7.6 Implementing Reader-Writer Locks Using Condition Variables
- Класс StreamReader
- 8.5.2 Typical Condition Variable Operations
- Caveats using NAT
- Using Double Quotes to Resolve Variables in Strings with Embedded Spaces
- 1. TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
- Data Binding Using the GridView Control
- Using the kill Command to Control Processes
- 15.4.2 Preemption Locks
- В Adobe Acrobat Reader русский текст на экране читается нормально, а при печати получается текст из набора символов. Как...
- Installing Using a Network
- Using X
- Using a Display Manager