Environment and parameter checkingIn Sylixos, the creation function of a thread cannot be called in an interrupt. And when the thread is created, the system will check the parameters such as stack size, priority and name, and the thread creation fails once the parameter is faulted. When the parameter validity check is complete, the system calls the _allocate_tcb_object function to remove an idle TCB resource from the free TCB control pool (the TCB is the thread control block). The specific code implementation is shown in listing 2-1 of the program.
List of programs 2-1 environment and parameter checking for thread creation
/*********************************************************************************************************** Function name: api_threadcreate** function Description: Create a thread ** input : pcName Thread Name ** pfuncThread refers to the thread code snippet start address ** pthreadattr Thread Properties collection pointer ** pulid thread-generated ID pointer can be considered NULL** Output : pulId thread handles same id A concept ****************/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 be called in an interrupt */ return (lw_object_handle_invalid); } if (threadattrdefault.threadattr_ststackbytesize == 0) { threadattrdefault = api_ Threadattrgetdefault (); /* initializationDefault Properties */ } if (pthreadattr == lw_null) { pthreadattr = &threadattrDefault; /* using default Properties */ } /* Default properties always use the automatic allocation stack */ #if lw_cfg_ arg_chk_en > 0 if (!pfuncthread) { /* Thread Code Snippet start address is empty */ return (lw_object_handle_invalid); } if (_stacksizecheck (pthreadattr->threadattr_ ststackbytesize) { /* stack size not correct */ return (LW_OBJECT_HANDLE_INVAlid); } if (_PriorityCheck ( pthreadattr->threadattr_ucpriority)) { /* Priority Error */ return (Lw_object_handle_ INVALID);  , #endif if (_object_name_invalid (pcname)) { /* Check name validity */ return (lw_object_handle_invalid); } &nbsP; __kernel_mode_proc ( ptcb = _allocate_tcb_ Object (); /* get a TCB */ ); if (!PTCB) { /* Check if threads can be established */ return (lw_object_handle_invalid); }
Safe ModeAs shown in Listing 3-1, in Sylixos, when the system checks the environment and parameters created by the thread, it enters safe mode, and the primary function of the security mode is to protect the main thread from being deleted when creating new threads.
In the 2nd section, "When the parameter validity check is complete, the system calls the _allocate_tcb_object function to remove an idle TCB resource from the free TCB control pool", it is important to note that this is simply a TCB resource. When the system calls the _tcbbuild function to assign values to the members of the TCB struct, the TCB construction is actually completed.
List of programs 3-1 thread-created security mode
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 0 */ ulidtemp = _makeobjectid (_object_thread, LW_CFG_PROCESSOR_NUMBER, ptcb->tcb_usindex); /* Build Objects id */ /* Initialize 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, /* Free Stack address */ pstktop, /* Main Stack Address */ pstkButtom, /* Stack Bottom */ pstkGuard, pthreadattr->threadattr_pvext, pstkLowAddress, stStackSize, /* stack size relative to Word alignment */ ulIdTemp, Pthreadattr->threadattr_uloption, pfuncthread, ptcb,         PTHREADATTR->THREADATTR_PVARG); if (!) ( pthreadattr->threadattr_uloption & lw_option_thread_init)) { /* non-initialization only */ _tcbtryrun (PTCB); /* try to run a new task */ } if (pulid) { *pulid = ulIdTemp; /* Records id */ } if (Lw_sys_ Status_is_running ())  { &NBsp; _threadunsafeinternal (); /* Exit Safe Mode */ } return (lw_object_handle_invalid);}
In Safe mode, when the TCB build is complete, the _tcbtryrun function is called, attempting to join the newly created thread to the candidate table. If the candidate table is not empty and the newly created thread has a higher priority than the candidate table thread, a priority winding occurs. When the CPU is scheduled for the next time, a priority winding is detected, and the CPU looks for the most suitable thread to run from the Ready table.