2017年11月1日 星期三

[ThreadX, 4] mutex

特性:
  • 只有一個thread可以Get
  • 同一個thread可以重複Get,但Get幾次就要Put幾次(tx_mutex_ownership_count)
    1. MutexGet()
…..
MutexGet()//還是Get的到喔





MCB(mutex control block):
  • TX_MUTEX

  • Debug:
    • tx_mutex_suspension_list

Delete:
  • 刪除mutex時,被他suspendthread會因此而resume,並得到TX_DELETEDreturn

Obtaining Ownership of a Mutex:
  • TX_INHERIT:當高優先權的請求屬於低優先權擁有的mutex時,低優先權的可以短暫擁有高優先權的優先權,以防止priority inversion.
    • 注意若有設thread priority threshold,其值會被更改如變動的prioritymutex put時,其值不會恢復

Prioritizing the mutex suspension list
  • Mutext被掛起的thread會放在suspension list(in a FIFO),當Mutex Put時,掛起的第一個會GetMutex(無論priority高低)
  • tx_mutex_prioritize(): supspension list中最高優先權的thread放在FIFO最前面,其他不變。
tx_mutex_prioritize(&my_mutex); /*Ensure the highest priority thread will get ownership*/
tx_mutex_put(&my_mutex);

Deadly embrace:
  • 情況:
1. Thread 1 obtains ownership of Mutex 1
2. Thread 2 obtains ownership of Mutex 2
3. Thread 1 suspends because it attempts to obtain ownership of Mutex 2
4. Thread 2 suspends because it attempts to obtain ownership of Mutex 1
  • 避免:
    • 限制一個thread只能get一個mutex
    • 如需多個mutex,需要限制順序,如getmutex1 才能在get mutex2
    • 呼叫get時,設置time out屬性
    • tx_thread_wait_about可以將死鎖的thread恢復


注意:

  • get/put 要在同一個thread
  • get/put 不能操作在ISR
  • 擁有mutexthread不能被終止否則別人get不到

沒有留言:

張貼留言