Threadx 释放信号量_tx_semaphore_put

26 篇文章 3 订阅

释放信号量_tx_semaphore_put

1,如果tx_semaphore_suspension_list挂起队列为空,那么直接把tx_semaphore_count计数器加一
2,如果tx_semaphore_suspension_list挂起队列不为空,那么tx_semaphore_suspension_list最前面线程获取释放的信号量,
并恢复线程。这里采用的FIFO,并没有按照优先级高低选择恢复线程。
_tx_semaphore_put函数在释放信号量是并没有检查当前释放信号量的线程是不是线程的拥有者,信号量并没有所有权属性。
这种特性可以用来进行事件通知,比如一个线程不断等待信号量,成功后处理任务;另一个线程,进行信号量释放。
这种特性,任何线程都可以释放信号量。

UINT    _tx_semaphore_put(TX_SEMAPHORE *semaphore_ptr)
{

    TX_INTERRUPT_SAVE_AREA

    REG_1   TX_THREAD   *thread_ptr;            /* Working thread pointer  */


    /* Disable interrupts to put an instance back to the semaphore.  */
    TX_DISABLE

    /* Determine if there are any threads suspended on the semaphore.  */
    thread_ptr =  semaphore_ptr -> tx_semaphore_suspension_list;
    #def tx_semaphore_suspension_list不为空,队列头部线程获得信号量,恢复线程
    if (thread_ptr)
    {

        /* Remove the suspended thread from the list.  */

        /* See if this is the only suspended thread on the list.  */
        if (thread_ptr == thread_ptr -> tx_suspended_next)
        {

            /* Yes, the only suspended thread.  */

            /* Update the head pointer.  */
            semaphore_ptr -> tx_semaphore_suspension_list =  TX_NULL;
        }
        else
        {

            /* At least one more thread is on the same expiration list.  */

            /* Update the list head pointer.  */
            semaphore_ptr -> tx_semaphore_suspension_list =  thread_ptr -> tx_suspended_next;

            /* Update the links of the adjacent threads.  */
            (thread_ptr -> tx_suspended_next) -> tx_suspended_previous =
                thread_ptr -> tx_suspended_previous;
            (thread_ptr -> tx_suspended_previous) -> tx_suspended_next =
                thread_ptr -> tx_suspended_next;
        }

        /* Decrement the suspension count.  */
        semaphore_ptr -> tx_semaphore_suspended_count--;

        /* Prepare for resumption of the first thread.  */

        /* Clear cleanup routine to avoid timeout.  */
        thread_ptr -> tx_suspend_cleanup =  TX_NULL;

        /* Temporarily disable preemption.  */
        _tx_thread_preempt_disable++;

        /* Restore interrupts.  */
        TX_RESTORE

        /* Deactivate the timeout timer if necessary.  */
        #def 关闭定时器
        if (thread_ptr -> tx_thread_timer.tx_list_head)
        {

            /* Deactivate the thread's timeout timer.  */
            _tx_timer_deactivate(&(thread_ptr -> tx_thread_timer));
        }
        else
        {

            /* Clear the remaining time to ensure timer doesn't get activated.  */
            thread_ptr -> tx_thread_timer.tx_remaining_ticks =  0;
        }

        /* Put return status into the thread control block.  */
        thread_ptr -> tx_suspend_status =  TX_SUCCESS;

        semaphore_ptr->sema_last_owner = thread_ptr;
        /* Resume thread.  */
        #def 恢复线程
        if (_tx_thread_resume(thread_ptr))

            /* Preemption is required, transfer control back to
               system.  */
            _tx_thread_system_return();

        /* Return success.  */
        return (TX_SUCCESS);
    }
    else
    {
	#def 没有线程挂起等待信号量,信号量计数器加1
        /* Increment the semaphore count.  */
        semaphore_ptr -> tx_semaphore_count++;

        semaphore_ptr->sema_last_owner = 0;
    }

    /* Restore interrupts.  */
    TX_RESTORE

    /* Return successful completion status.  */
    return (TX_SUCCESS);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
my_semaphore是一个信号量,它用于在多任务或多进程环境,实现对共享资源的互斥访问。信号量是一个计数器,用于控制对共享资源的访问。当一个任务或进程需要访问共享资源时,它会请求信号量,如果信号量计数器的值大于0,该任务或进程就可以访问共享资源,并将信号量计数器减1。如果信号量计数器的值为0,该任务或进程就必须等待,直到有其他任务或进程释放信号量。 在C语言,可以使用sem_init、sem_wait和sem_post等函数来操作信号量。sem_init函数用于初始化信号量,sem_wait函数用于等待信号量,并将信号量计数器减1,sem_post函数用于释放信号量,并将信号量计数器加1。以下是一个信号量的示例代码: ``` #include <semaphore.h> #include <stdio.h> #include <pthread.h> sem_t my_semaphore; void *my_thread(void *arg) { sem_wait(&my_semaphore); printf("Thread %d is accessing the shared resource.\n", *(int *)arg); sem_post(&my_semaphore); pthread_exit(NULL); } int main() { int i; pthread_t threads[5]; sem_init(&my_semaphore, 0, 1); for (i = 0; i < 5; i++) { pthread_create(&threads[i], NULL, my_thread, (void *)&i); } for (i = 0; i < 5; i++) { pthread_join(threads[i], NULL); } sem_destroy(&my_semaphore); return 0; } ``` 在上面的例子,我们创建了5个线程,并使用信号量来控制这些线程对于共享资源的访问。由于我们初始化信号量的值为1,因此只有一个线程可以访问共享资源。每个线程在访问共享资源前都会调用sem_wait函数来等待信号量,然后在访问完共享资源后调用sem_post函数来释放信号量。这样可以保证同一时间只有一个线程可以访问共享资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值