Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Process Analysis of SylixOS Thread creation

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Overview

The main content of this document is to analyze the process of SylixOS thread creation, and introduce the thread creation function API_ThreadCreate of SylixOS in detail.

Environment and parameter check

In SylixOS, the creation function of a thread cannot be called in an interrupt. And during the creation of the thread, the system will check the validity of the parameters such as the stack size, priority and name of the thread. Once the parameters are wrong, the thread creation will fail. When the parameter validity check is complete, the system calls the _ Allocate_Tcb_Object function to fetch a free TCB resource (TCB is a thread control block) from the free TCB control pool. The specific code implementation is shown in listing 2-1.

Program listing 2-1 the environment and parameter checks created by threads

/ * function name: API_ThreadCreate** Function description: create a thread * * input: pcName thread name * * pfuncThread refers to the starting address of the thread code segment * * pthreadattr thread attribute set pointer * * the ID pointer generated by the pulId thread can output for NULL**: pulId thread Handle has the same concept as ID * / LW_API LW_OBJECT_HANDLE API_ThreadCreate (CPCHAR pcName PTHREAD_START_ROUTINE pfuncThread, PLW_CLASS_THREADATTR pthreadattr LW_OBJECT_ID * pulId) {if (LW_CPU_GET_CUR_NESTING ()) {/ * cannot call * / return (LW_OBJECT_HANDLE_INVALID) in an interrupt } if (threadattrDefault.THREADATTR_stStackByteSize = = 0) {threadattrDefault = API_ThreadAttrGetDefault (); / * initialize the default attribute * /} if (pthreadattr = = LW_NULL) {pthreadattr = & threadattrDefault / * use default properties * /} / * default properties always use auto-allocation stack * / # if LW_CFG_ARG_CHK_EN > 0 if (! pfuncThread) { / * indicates that the starting address of the thread code snippet is empty * / return (LW_OBJECT_HANDLE_INVALID) } if (_ StackSizeCheck (pthreadattr- > THREADATTR_stStackByteSize)) {/ * incorrect stack size * / return (LW_OBJECT_HANDLE_INVALID);} if (_ PriorityCheck (pthreadattr- > THREADATTR_ucPriority)) {/ * priority error * / return (LW_OBJECT_HANDLE_INVALID) } # endif if (_ Object_Name_Invalid (pcName)) {/ * check the validity of names * / return (LW_OBJECT_HANDLE_INVALID);} _ KERNEL_MODE_PROC (ptcb = _ Allocate_Tcb_Object () / * get a TCB * /); if (! ptcb) {/ * check if a thread can be created * / return (LW_OBJECT_HANDLE_INVALID);}

Safety mode

As shown in listing 3-1, in SylixOS, after the system checks the environment and parameters created by the thread, it enters safe mode, which is mainly used to protect the main thread from being deleted when a new thread is created.

In section 2, it is mentioned that "when the parameter validity check is completed, the system calls the _ Allocate_Tcb_Object function to extract a free TCB resource from the free TCB control pool." it should be noted that it is simply to get a TCB resource here. When the system calls the _ TCBBuild function to assign values to the members of the TCB structure, the construction of the TCB is really completed.

Program listing 3-1 safe mode created by threads

If (LW_SYS_STATUS_IS_RUNNING ()) {_ ThreadSafeInternal (); / * enter safe mode * /} lib_bzero (& ptcb- > TCB_pstkStackTop, sizeof (LW_CLASS_TCB)-_ LIST_OFFSETOF (LW_CLASS_TCB, TCB_pstkStackTop)) / * TCB zeroing * / ulIdTemp = _ MakeObjectId (_ OBJECT_THREAD, LW_CFG_PROCESSOR_NUMBER, ptcb- > TCB_usIndex) / * build object id * / / * initialize the stack SHELL * / pstkFristFree = archTaskCtxCreate ((PTHREAD_START_ROUTINE) _ ThreadShell, (PVOID) pfuncThread, / * Real executable code body * / pstkTop, pthreadattr- > THREADATTR_ulOption) UlError = _ TCBBuildExt (ptcb); / * first initialize the extension structure * / if (ulError) {iErrLevel = 2; _ ErrorHandle (ulError); goto _ _ error_handle } _ TCBBuild (pthreadattr- > THREADATTR_ucPriority, / * build TCB * / pstkFristFree, / * address of free stack area * / pstkTop / * address of main stack area * / pstkButtom, / * bottom of stack * / pstkGuard, pthreadattr- > THREADATTR_pvExt, pstkLowAddress StStackSize, / * relative to the stack size of word alignment * / ulIdTemp, pthreadattr- > THREADATTR_ulOption, pfuncThread, ptcb, pthreadattr- > THREADATTR_pvArg) If (! (pthreadattr- > THREADATTR_ulOption & LW_OPTION_THREAD_INIT)) {/ * not just initialize * / _ TCBTryRun (ptcb) / * try to run a new task * /} if (pulId) {* pulId = ulIdTemp / * record ID * /} if (LW_SYS_STATUS_IS_RUNNING ()) {_ ThreadUnsafeInternal (); / * exit safe mode * /} return (LW_OBJECT_HANDLE_INVALID);}

In safe mode, when the TCB is built, the _ TCBTryRun function is called to try to add the newly created thread to the candidate list. Priority winding occurs if the candidate table is not empty and the newly created thread takes precedence over the threads in the candidate table. The next time CPU is scheduled and a priority winding is detected, CPU will find the most suitable thread to run from the ready table.

Disclaimer

Internal communication documents, only for SylixOS platform, if you find relevant errors or suggestions, please contact the document creator for revision and update.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report