当前位置:网站首页>B_ QuRT_ User_ Guide(17)

B_ QuRT_ User_ Guide(17)

2022-06-11 03:08:00 weixin_ thirty-eight million four hundred and ninety-eight thou

11 Semaphores

Threads use semaphores to synchronously access shared resources . When a semaphore is initialized , It is assigned an integer value . This value indicates the number of threads that can simultaneously access shared resources through the semaphore . The default value is 1.
When a thread executes on a semaphore down In operation , The result depends on the count value of the signal lamp .

  • If the count value is non-zero , Is decremented , The thread gains access to the shared resource and continues execution .
  • If the count value is zero , Will not be decremented , The thread is suspended on the signal . When the count value becomes non-zero ( Because another thread released the signal ), It will be decremented , The suspended thread wakes up and gains access to the shared resource .
    When a thread to a semaphore When working up ,semaphore The count value of is incremented . The result depends on the number of threads waiting on the signal .
  • If no thread is waiting , The current thread releases access to shared resources and continues execution .
  • If one or more threads are waiting , also semaphore The value of is non-zero , The kernel wakes up the waiting thread with the highest priority , And decreasing semaphore The numerical . If the awakened thread has a higher priority than the current thread , Context switching will occur .
    add The operation is similar to up, But you can put semaphore The value of is increased to be greater than 1 The degree of . therefore ,add It is possible to wake up multiple waiting threads in one operation .
    try down Operation enables a thread to attempt to access a shared resource , If it's semaphore The count value of is 0, There is no risk of suspension .
  • If the count is non-zero , Then the attempted lowering operation is the same as the conventional lowering operation .
  • If the count is zero ,try down A value representing the zero count status will be returned .Semaphores It's a shared object , The following operations are supported .
  • qurt_sem_add()
  • qurt_sem_destroy()
  • qurt_sem_down()
  • qurt_sem_get_val()
  • qurt_sem_init()
  • qurt_sem_init_val()
  • qurt_sem_try_down()
  • qurt_sem_up()
  • qurt_sem_down_timed()
  • Data Types

11.1 qurt_sem_add()

11.1.1 Function Documentation

11.1.1.1 int qurt_sem_add ( qurt_sem_t ∗ sem, unsigned int amt )

Release access to a shared resource ( The specified number will increase the number of semaphores ).
When a thread is in a semaphore When performing an add operation on , The specified value increases semaphore Count of . The result depends on the number of threads waiting on the signal .

  • When no thread is waiting , The current thread releases access to shared resources and continues execution .
  • When there is one or more threads waiting , also semaphore When the count is non-zero , The kernel will repeatedly wake up the waiting thread with the highest priority , And decreasing semaphore Count , Until there is no waiting thread or semaphore The count is zero . If any awakened thread has a higher priority than the current thread , Context switching will occur .
    Related data types
    qurt_sem_t
    Parameters
     Insert picture description here
    Return value
    Unused integer value.
    Dependency relationship
    None.

11.2 qurt_sem_destroy()

11.2.1 Function Documentation

11.2.1.1 void qurt_sem_destroy ( qurt_sem_t ∗ sem )

Destroy designated semaphore.
Be careful : When semaphores When it's no longer in use , It must be destroyed . Failure to do so will result in QuRT Kernel resource leak .
Semaphores Cannot be destroyed while still in use . If this happens ,QuRT The behavior of will not be defined .
Related data types
qurt_sem_t
Parameters
 Insert picture description here
Return value
None.
Dependency relationship
None.

11.3 qurt_sem_down()

11.3.1 Function Documentation

11.3.1.1 int qurt_sem_down ( qurt_sem_t ∗ sem )

Request access to a shared resource . When a thread moves down a semaphore , As a result,
Depending on semaphore The count of .

  • When the count value is non-zero , It is decremented , The thread gains access to the shared resource and continues execution .
  • When the count is zero , It will not be decremented , Thread is suspended on semaphore . When the count value becomes non-zero ( Because another thread released the signal ), It is decremented , The suspended thread wakes up and gains access to the shared resource .
    Related data types
    qurt_sem_t
    Parameters
     Insert picture description here
    Return value
    Unused integer value.

Dependency relationship
None.

11.4 qurt_sem_get_val()

11.4.1 Function Documentation

11.4.1.1 static unsigned short qurt_sem_get_val ( qurt_sem_t ∗ sem )

obtain semaphore The count of .
Returns the specified semaphore The current count of .

Related data types
qurt_sem_t
Parameters
 Insert picture description here

Return value
Integer semaphore count value
Dependency relationship
None.

11.5 qurt_sem_init()

11.5.1 Function Documentation

11.5.1.1 void qurt_sem_init ( qurt_sem_t ∗ sem )

Initialize a semaphore object . The default initial value is 1, namely semaphore Count value .
Parameters
 Insert picture description here
Return value
None.
Dependency relationship
None.

原网站

版权声明
本文为[weixin_ thirty-eight million four hundred and ninety-eight thou]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110238526970.html