semphr. h
xSemaphoreTake( xSemaphoreHandle xSemaphore, portTickType xBlockTime )
セマフォを得るマクロ。 このセマフォはvSemaphoreCreateBinary () 、 xSemaphoreCreateMutex ()
あるいは xSemaphoreCreateCounting () で作成されたものです。
このマクロは ISR での使用はできません。通常操作ではないが、必要とされるなら、 割り込みの中からセマフォをtakeするため、xQueueReceiveFromISR
() を使用することができる。 Semaphores は待ち行列を基本的なメカニズムとして使用します、従って関数はある程度協同使用可能です。
xSemaphoreTake () はタスク間通信 API の一部です。 xSemaphoreAltTake () は同等な代替APIです。
この二つは同じパラメータを必要とし、同じ値を返します。
パラメータ:
xSemaphore
セマフォを作成したとき得られた、あるいは「take]したセマフォへのハンドル。
xBlockTime
セマフォが利用可能になるのを待つチック時間。 マクロ portTICK_RATE_MS はこれをリアルタイムに変換するために使用できます。 ゼロのブロックタイムはセマフォを
pollするために使われます。もし INCLUDE_vTaskSuspend を1にセットした場合、ブロックタイムを portMAX_DELAY
として明示することによりタスクを(タイムアウトなしで)いつまでもブロックさせることが出来る。
リターン:
pdTRUE
もしセマフォが得られた時。
pdFALSE
もし、セマフォが利用可能にならないで、 xBlockTime の期限が切れた場合。
使用例:
xSemaphoreHandle xSemaphore = NULL;
// A task that creates a semaphore.
void vATask( void * pvParameters )
{
// Create the semaphore to guard a shared resource. As we are using
// the semaphore for mutual exclusion we create a mutex semaphore
// rather than a binary semaphore.
xSemaphore = xSemaphoreCreateMutex();
}
// A task that uses the semaphore.
void vAnotherTask( void * pvParameters )
{
// ... Do other things.
if( xSemaphore != NULL ) {
// See if we can obtain the semaphore. If the semaphore is not available
// wait 10 ticks to see if it becomes free.
if( xSemaphoreTake( xSemaphore, ( portTickType ) 10 ) == pdTRUE ) {
// We were able to obtain the semaphore and can now access the
// shared resource.
// ...
// We have finished accessing the shared resource. Release the
// semaphore.
xSemaphoreGive( xSemaphore );
} else {
// We could not obtain the semaphore and can therefore not access
// the shared resource safely.
}
}
}