Threadx 释放信号量_tx_semaphore_put

news/2024/7/8 4:38:04 标签: semaphore

semaphore_put_1">释放信号量_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);
}

http://www.niftyadmin.cn/n/1428571.html

相关文章

Threadx 消息队列 queue

文章目录消息传递规则消息大小消息队列控制块消息队列list消息队列API创建消息队列_tx_queue_create删除队列_tx_queue_delete清空消息队列_tx_queue_flushThreadx提供了消息队列进行线程间通信。消息队列中消息通常按照先进先出规则传递,同时提供了把消息直接存储到…

Threadx 消息队列-发送消息_tx_queue_send

消息队列-发送消息_tx_queue_send 1,发送消息会插入到队列尾部。 2,如果消息队列有挂起的接收线程,发送消息时,可以直接把消息放到接收线程的缓冲中,这可以降低消息传递延时。 TX_THREAD线程控制块中tx_additional_su…

Threadx 消息队列-接收消息_tx_queue_receive

消息队列-接收消息_tx_queue_receive 1,如果消息队列有消息,从队列头部取出消息 (1)并且tx_queue_suspension_list有挂起线程,说明发送线程由于消息队列已满而挂起,挂起时把发送的消息 存放到了发送线程的…

Threadx 定时器timer

文章目录定时器管理结构定时器链表定时器激活链表定时器工作原理定时器API定时器创建_tx_timer_create删除定时器_tx_timer_delete修改_tx_timer_changeThreadx 操作系统定时器提供单次定时和周期性定时功能。定时器由周期性定时中断驱动,每一个定时中断称为一个时钟…

Threadx 激活定时器和去激活定时器tx_timer_activate

文章目录tx_timer_activatetx_timer_deactivate_tx_timer_deactivate分析激活定时器和去激活定时器函数。tx_timer_activate _tx_timer_activate_api(TX_TIMER *timer_ptr) 用来激活已经创建的定时器。 UINT _tx_timer_activate_api(TX_TIMER *timer_ptr) {/* Check for a…

Threadx 系统定时器线程_tx_timer_thread_entry

“System Timer Thread” 系统定时器线程用于判断定时器超时,并调用定时器超时处理函数。 _tx_timer_initialize 先来看看定时器线程创建初始化。_tx_timer_initialize初始化系统一系列与时间,定时器相关全局变量,链表,线程等。…

Threadx 内存管理-内存字节池

文章目录内存池控制块内存池链表内存池初始化内存分配内存释放内存整理字节池内存API小结_tx_byte_pool_create_tx_byte_pool_deleteThreadx 提供字节内存池进行内存管理,字节内存池是一块连续字节块,可以以字节为单位申请内存。字节内存池中连续内存初始…

Threadx 字节内存池内存分配_tx_byte_allocate

_tx_byte_allocate _tx_byte_allocate用于内存分配,内存分配原理参考上篇博文:Threadx 内存管理-内存字节池 UINT _tx_byte_allocate(TX_BYTE_POOL *pool_ptr, VOID **memory_ptr,ULONG memory_size, ULONG wait_option) {TX_INTERRUPT_SAVE_AREAREG_1 UINT…