Книга: Real-Time Concepts for Embedded Systems

6.4.5 Recursive Shared-Resource-Access Synchronization

6.4.5 Recursive Shared-Resource-Access Synchronization

Sometimes a developer might want a task to access a shared resource recursively. This situation might exist if tAccessTask calls Routine A that calls Routine B, and all three need access to the same shared resource, as shown in Figure 6.9.


Figure 6.9: Recursive shared- resource-access synchronization.

If a semaphore were used in this scenario, the task would end up blocking, causing a deadlock. When a routine is called from a task, the routine effectively becomes a part of the task. When Routine A runs, therefore, it is running as a part of tAccessTask. Routine A trying to acquire the semaphore is effectively the same as tAccessTask trying to acquire the same semaphore. In this case, tAccessTask would end up blocking while waiting for the unavailable semaphore that it already has.

One solution to this situation is to use a recursive mutex. After tAccessTask locks the mutex, the task owns it. Additional attempts from the task itself or from routines that it calls to lock the mutex succeed. As a result, when Routines A and B attempt to lock the mutex, they succeed without blocking. The pseudo code for tAccessTask, Routine A, and Routine B are similar to Listing 6.5.

Listing 6.5: Pseudo code for recursively accessing a shared resource.

tAccessTask () {
 :
 Acquire mutex
 Access shared resource
 Call Routine A
 Release mutex
 :
}
Routine A () {
 :
 Acquire mutex
 Access shared resource
 Call Routine B
 Release mutex
 :
}
Routine B () {
 :
 Acquire mutex
 Access shared resource
 Release mutex
 :
}

Оглавление книги


Генерация: 0.071. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз