文章目录

  • 线程控制块(TCB)
  • _tx_thread_create
  • 示例

线程控制块(TCB)

typedef  struct TX_THREAD_STRUCT
{/* The first section of the control block contains criticalinformation that is referenced by the port-specific assembly language code.  Any changes in this section couldnecessitate changes in the assembly language.  */ULONG       tx_thread_id;           /* Control block ID         */ULONG       tx_run_count;           /* Thread's run counter     */VOID_PTR    tx_stack_ptr;           /* Thread's stack pointer   */VOID_PTR    tx_stack_start;         /* Stack starting address   */VOID_PTR    tx_stack_end;           /* Stack ending address     */ULONG       tx_stack_size;          /* Stack size               */ULONG       tx_time_slice;          /* Current time-slice       */ULONG       tx_new_time_slice;      /* New time-slice           *//* Define pointers to the next and previous ready threads.  */ struct TX_THREAD_STRUCT *tx_ready_next,      *tx_ready_previous;/* Define the port extension field.  This typically is defined to white space, but some ports of ThreadX may need to have additional fields in the thread control block.  This is defined in the file tx_port.h.  */TX_THREAD_PORT_EXTENSION/***************************************************************/  /* Nothing after this point is referenced by the target-specificassembly language.  Hence, information after this point can be added to the control block providing the complete system is recompiled.  */CHAR_PTR    tx_thread_name;         /* Pointer to thread's name */UINT        tx_priority;            /* Priority of thread (0-31)*/UINT        tx_state;               /* Thread's execution state */UINT        tx_delayed_suspend;     /* Delayed suspend flag     */UINT        tx_suspending;          /* Thread suspending flag   */UINT        tx_preempt_threshold;   /* Preemption threshold     */ULONG       tx_priority_bit;        /* Priority ID bit          *//* Define the thread's entry point and input parameter.  */VOID        (*tx_thread_entry)(ULONG);ULONG       tx_entry_parameter;/* Define the thread's timer block.   This is used for thread sleep and timeout requests.  */TX_INTERNAL_TIMERtx_thread_timer;/* Define the thread's cleanup function and associated data.  Thisis used to cleanup various data structures when a thread suspension is lifted or terminated either by the user or a timeout.  */VOID        (*tx_suspend_cleanup)(struct TX_THREAD_STRUCT *);VOID_PTR   tx_suspend_control_block;struct TX_THREAD_STRUCT*tx_suspended_next,*tx_suspended_previous;ULONG       tx_suspend_info;VOID_PTR    tx_additional_suspend_info;UINT        tx_suspend_option;UINT        tx_suspend_status;/* Define a pointer for Green Hills use.  */VOID_PTR    tx_eh_globals;/* Define pointers to the next and previous threads in the created list.  */struct TX_THREAD_STRUCT *tx_created_next,    *tx_created_previous;
} TX_THREAD;

_tx_thread_create

UINT    _tx_thread_create(TX_THREAD *thread_ptr, CHAR *name, VOID (*entry_function)(ULONG), ULONG entry_input,VOID *stack_start, ULONG stack_size, UINT priority, UINT preempt_threshold,ULONG time_slice, UINT auto_start)
参数 意义
thread_ptr 线程控制块指针
name 线程名字
entry_function 线程入口函数
entry_input 线程入口函数参数
stack_start 线程使用的栈起始地址
stack_size 线程栈大小
priority 线程优先级
preempt_threshold 线程抢占优先级(小于此值线程才能够抢占)
time_slice 时间片 (0表示不使用时间片轮转调度)
auto_start 线程创建后是否自动执行选项
UINT    _tx_thread_create(TX_THREAD *thread_ptr, CHAR *name, VOID (*entry_function)(ULONG), ULONG entry_input,VOID *stack_start, ULONG stack_size, UINT priority, UINT preempt_threshold,ULONG time_slice, UINT auto_start)
{TX_INTERRUPT_SAVE_AREA
REG_1 TX_THREAD_PTR     tail_ptr;       /* Created list tail pointer  */#def 定义局部变量,放在寄存器中#ifndef TX_DISABLE_STACK_CHECKING/* Set the thread stack to a pattern prior to creating the initialstack frame.  This pattern is used by the stack checking routinesto see how much has been used.  */memset(stack_start, TX_STACK_FILL, stack_size);#endif/* Prepare the thread control block prior to placing it on the createdlist.  */#def 设置线程控制块中变量值/* Place the supplied parameters into the thread's control block.  */thread_ptr -> tx_thread_name =      name;thread_ptr -> tx_thread_entry =     entry_function;thread_ptr -> tx_entry_parameter =  entry_input;thread_ptr -> tx_stack_ptr =        TX_NULL;thread_ptr -> tx_stack_start =      stack_start;thread_ptr -> tx_stack_size =       stack_size;thread_ptr -> tx_stack_end =        (VOID_PTR) (((CHAR_PTR) stack_start) + (stack_size-1));thread_ptr -> tx_priority =         priority & TX_THREAD_PRIORITY_MASK;thread_ptr -> tx_preempt_threshold= preempt_threshold & TX_THREAD_PRIORITY_MASK;thread_ptr -> tx_time_slice =       time_slice;thread_ptr -> tx_new_time_slice =   time_slice;/* Now fill in the values that are required for thread initialization.  */thread_ptr -> tx_run_count =        0;#def 初始为挂起状态thread_ptr -> tx_state =            TX_SUSPENDED; thread_ptr -> tx_delayed_suspend =  TX_FALSE;thread_ptr -> tx_suspending =       TX_FALSE;/* Setup the necessary fields in the thread timer block.  */thread_ptr -> tx_thread_timer.tx_timeout_function =     _tx_thread_timeout;thread_ptr -> tx_thread_timer.tx_timeout_param =        (ULONG) thread_ptr;thread_ptr -> tx_thread_timer.tx_list_head =            TX_NULL;thread_ptr -> tx_thread_timer.tx_re_initialize_ticks =  0;/* Clear the suspension information.  */thread_ptr -> tx_suspend_cleanup =        TX_NULL;thread_ptr -> tx_suspend_control_block =  TX_NULL;thread_ptr -> tx_suspended_next =         TX_NULL;thread_ptr -> tx_suspended_previous=      TX_NULL;/* Setup the priority bit.  */thread_ptr -> tx_priority_bit =  (((ULONG) 1) << (priority & TX_THREAD_PRIORITY_MASK));/* Call the target specific stack frame building routine to build the thread's initial stack and to setup the actual stack pointer in thecontrol block.  */#def 为这个线程创建栈空间,初始化栈内存,设置栈指针tx_stack_ptr _tx_thread_stack_build(thread_ptr, _tx_thread_shell_entry);/* Prepare to make this thread a member of the created thread list.  */#def 禁止中断,保证了操作下面全局变量临界资源;禁止中断可以让当前线程不被抢占;当前线程执行,说明当前线程现在是所有就绪队列中优先级最高的,其他更高优先级线程可能被挂起了。中断可能唤醒更高优先级线程TX_DISABLE/* Load the thread ID field in the thread control block.  */thread_ptr -> tx_thread_id =  TX_THREAD_ID;/* Place the thread on the list of created threads.  First,check for an empty list.  */#def 插入到_tx_thread_created_ptr 线程listif (_tx_thread_created_ptr){/* Pickup tail pointer.  */tail_ptr =  _tx_thread_created_ptr -> tx_created_previous;/* Place the new thread in the list.  */_tx_thread_created_ptr -> tx_created_previous =  thread_ptr;tail_ptr -> tx_created_next =  thread_ptr;/* Setup this thread's created links.  */thread_ptr -> tx_created_previous =  tail_ptr;thread_ptr -> tx_created_next =      _tx_thread_created_ptr;   }else{/* The created thread list is empty.  Add thread to empty list.  */_tx_thread_created_ptr =            thread_ptr;thread_ptr -> tx_created_next =     thread_ptr;thread_ptr -> tx_created_previous = thread_ptr;}/* Increment the created thread counter.  */_tx_thread_created_count++;/* Temporarily disable preemption.  */_tx_thread_preempt_disable++;/* Register thread in the thread array structure.  */TX_EL_THREAD_REGISTER(thread_ptr);/* Log this kernel call.  */TX_EL_THREAD_CREATE_INSERT/* Green Hills specific code.  */{#pragma weak __cpp_exception_initextern void __cpp_exception_init(void **);static void (*const cpp_init_funcp)(void **) = __cpp_exception_init;if (cpp_init_funcp) __cpp_exception_init(&(thread_ptr->tx_eh_globals));}/* End of Green Hills specific code.  *//* Restore previous interrupt posture.  */TX_RESTORE/* Determine if an automatic start was requested.  If so, call the resumethread function and then check for a preemption condition.  */#def 入参为自动执行线程if (auto_start){/* Call the resume thread function to make this thread ready.  Upon return, check for a preemption condition caused by creatinga thread of higher priority.  */#def 把线程放入就绪对应优先级list,如果这个新线程需要立马执行(例如优先级最高),返回true,并设置了_tx_thread_execute_ptr线程if (_tx_thread_resume(thread_ptr))/* A preemption condition exists, pass control back to thesystem in order to get the higher priority thread running.  */#def 调度进行线程切换,从_tx_thread_current_ptr当前执行前程切换到执行_tx_thread_execute_ptr线程,有汇编实现,与具体cpu体系相关_tx_thread_system_return();}else{#def 不在动执行线程/* Disable interrupts.  */TX_DISABLE/* Remove temporary disable preemption.  */_tx_thread_preempt_disable--;/* Restore interrupts.  */TX_RESTORE}/* Always return a success.  */return(TX_SUCCESS);
}

示例

void    tx_application_define(void *first_unused_memory)
{CHAR    *pointer;/* Put first available memory address into a character pointer.  */pointer =  (CHAR *) first_unused_memory;/* Put system definition stuff in here, e.g. thread creates and other assortedcreate information.  *//* Create the main thread.  */tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,  pointer, DEMO_STACK_SIZE, 1, 1, TX_NO_TIME_SLICE, TX_AUTO_START);pointer = pointer + DEMO_STACK_SIZE;/* Create threads 1 and 2. These threads pass information through a ThreadX message queue.  It is also interesting to note that these threads have a timeslice.  */tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,  pointer, DEMO_STACK_SIZE, 16, 16, 4, TX_AUTO_START);pointer = pointer + DEMO_STACK_SIZE;tx_thread_create(&thread_2, "thread 2", thread_2_entry, 2,  pointer, DEMO_STACK_SIZE, 16, 16, 4, TX_AUTO_START);pointer = pointer + DEMO_STACK_SIZE;/* Create threads 3 and 4.  These threads compete for a ThreadX counting semaphore.  An interesting thing here is that both threads share the same instruction area.  */tx_thread_create(&thread_3, "thread 3", thread_3_and_4_entry, 3,  pointer, DEMO_STACK_SIZE, 8, 8, TX_NO_TIME_SLICE, TX_AUTO_START);pointer = pointer + DEMO_STACK_SIZE;tx_thread_create(&thread_4, "thread 4", thread_3_and_4_entry, 4,  pointer, DEMO_STACK_SIZE, 8, 8, TX_NO_TIME_SLICE, TX_AUTO_START);pointer = pointer + DEMO_STACK_SIZE;/* Create thread 5.  This thread simply pends on an event flag which will be setby thread_0.  */tx_thread_create(&thread_5, "thread 5", thread_5_entry, 5,  pointer, DEMO_STACK_SIZE, 4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);pointer = pointer + DEMO_STACK_SIZE;/* Create the message queue shared by threads 1 and 2.  */tx_queue_create(&queue_0, "queue 0", TX_1_ULONG, pointer, 100*sizeof(ULONG));pointer = pointer + (100*sizeof(ULONG));/* Create the semaphore used by threads 3 and 4.  */tx_semaphore_create(&semaphore_0, "semaphore 0", 1);/* Create the event flags group used by threads 1 and 5.  */tx_event_flags_create(&event_flags_0, "event flags 0");
}

Threadx tx_thread_create创建线程相关推荐

  1. java多线程-线程创建-线程池-java内存模型

    文章目录 ==多线程基础== 进程 线程 浏览器的进程和线程(案例) 线程的异步和同步 多线程的优势 ==多线程的实现方式== 第一种:继承Thread类 第二种:实现Runnable接口 第三种:通 ...

  2. C++:多线程中的小白(2)线程启动、结束、创建线程、join、detach

    目录 一.范例演示:线程运行的开始和结束 二.其他创建线程的手法 一.范例演示:线程运行的开始和结束 (1)thrad (2)join()函数 (3)detach()函数 (4)joinable()函 ...

  3. Linux创建线程时 内存分配的那些事

    文章目录 问题描述 问题分析 针对问题1 的猜测: 针对问题2 的猜测: 原理追踪 总结 问题描述 事情开始于一段内存问题,通过gperf工具抓取进程运行过程中的内存占用情况. 分析结果时发现一个有趣 ...

  4. C++多线程:thread类创建线程的多种方式

    文章目录 描述 函数成员简介 总结 描述 头文件 <thread> 声明方式:std::thread <obj> 简介 线程在构造关联的线程对象时立即开始执行,从提供给作为构造 ...

  5. python tcp server分包_如何创建线程池来监听tcpserver包python

    我试图创建线程池来同时对传入的tcp包执行一些操作.在 我在python3中找不到任何内置线程池.我也读了一些关于multiprocessing.Pool的文章,但是它不支持内存共享.所以我使用Que ...

  6. 新来了个技术总监:谁再用 @Async 创建线程以后就不用来了!!

    欢迎关注方志朋的博客,回复"666"获面试宝典 在很久很久之前,我有一段痛苦的记忆.那种被故障所驱使的感觉,在我脑海里久久无法驱散. 原因无它,有小伙伴开启了线程池的暴力使用模式. ...

  7. 为什么阿里巴巴要禁用 Executors 创建线程池?

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 转自:掘金,作者:何甜甜在吗 juejin.im/post/5dc ...

  8. 创建线程那么容易,为什么非要让我使用线程池?(深深深入剖析)

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 一.概述 1.问题 先看我们遇到的问题:我们创建线程的方式很简单, ...

  9. 为什么阿里巴巴要禁用Executors创建线程池?

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试资料 作者:何甜甜在吗 juejin.im/post/5dc41c165 ...

最新文章

  1. Django博客系统(博客保存)
  2. Mysql服务器问题(2013.3.5日发现)
  3. 修改mysql数据库的编码格式
  4. 锤子剪刀布 (20)
  5. Deep Learning---py-faster-rcnn基于PASCAL VOC数据集训练模型
  6. 如何通过Windows Server 2008 R2建立NFS存储
  7. 云上快速搭建Serverless AI实验室
  8. ZooKeeper与Eureka作为注册中心的比较
  9. 【云栖大会】“天”与“人”的大数据
  10. 二分图最大匹配的经典匈牙利算法
  11. 腾讯云分布式高可靠消息队列CMQ架构最佳实践
  12. 龙之谷冰龙linux手工服务端,【网游】【龙之谷】v300冰龙巢穴一键服务端+客户端+GM工具+视频教程...
  13. java 定义整数数组_定义一个由整数组成的数组,要求求出其中的奇数个数和偶数个数...
  14. 鸽巢排序Pigeonhole Sort----(排序算法八)
  15. Spring Cloud Gateway Session
  16. 远程IT运维的升级,“团队协作”
  17. 【Bio】基础生物学 - 蛋白质结构 protein structure
  18. duet连win10_在Windows PC上使用Duet Display时连接不上Apple设备的解决方法之一
  19. 3.3.6 全微分与复合求导
  20. 揭露丑恶:中国社会的职业道德集体缺失?

热门文章

  1. C# TCP/IP通信(存小问题/大体欧克)
  2. Oracle EBS教学视频
  3. mysql 0xc0000005_duilib菜单开发遇见“0xC0000005: 读取位置 0xFFFFFFFFFFFFFFFF 时发生访问冲突”...
  4. iptables 设置端口转发/映射
  5. Android在外接物理键盘时,如何强制调用系统软键盘
  6. 逻辑强化(02)复合推理 答案解析
  7. 奇梦达何以“贱卖”西安研发中心
  8. 银行网点数字化怎么做?打造“线上+线下”服务新模式
  9. 翼码张波O2O分享9:O2O的其他商务行为
  10. 如何在CodeIgniter中使用会话数据