Thread in SylixOS [2] - thread attribute block and operation

Posted by etsauer on Mon, 20 Dec 2021 08:14:02 +0100

Thread attribute block

Each SylixOS thread has its own properties, including thread priority, stack information, thread parameters, etc. Each thread attribute block consists of a structure LW_CLASS_THREADATTR is composed of the following members:

typedef struct {
 PLW_STACK 	THREADATTR_pstkLowAddr; /* Low memory start address of all stack areas */
 size_t 	THREADATTR_stGuardSize; /* Stack alert area size */
 size_t 	THREADATTR_stStackByteSize;/* Total stack size (bytes) */
 UINT8 		THREADATTR_ucPriority; /* thread priority  */
 ULONG 		THREADATTR_ulOption; /* Task options */
 PVOID 		THREADATTR_pvArg; /* Thread parameters */
 PVOID 		THREADATTR_pvExt; /* Extended segment pointer */
} LW_CLASS_THREADATTR;

Users of SylixOS application development usually only need to care about the stack size, thread priority, task options and thread parameters. Other member operating systems will set them by default.

  • The parameter stStackByteSize is the stack size (bytes);

  • Parameter ucPriority is thread priority;
    In order to reasonably use thread priority, SylixOS sets some common priority values (the smaller the value, the higher the thread priority, and the system gives priority to scheduling), as shown above. In application development, generally, the priority of tasks should be between lw_prio_high and LW_PRIO_LOW, so as not to affect the system kernel threads as much as possible.

    Macro namevalue
    LW_PRIO_HIGHEST0
    LW_PRIO_LOWEST255
    LW_PRIO_EXTREMELW_PRIO_HIGHEST
    LW_PRIO_CRITICAL50
    LW_PRIO_REALTIME100
    LW_PRIO_HIGH150
    LW_PRIO_NORMAL200
    LW_PRIO_LOW250
    LW_PRIO_IDLELW_PRIO_LOWEST
  • Parameter ulOption is thread option;

    Macro nameexplain
    LW_OPTION_THREAD_STK_CHKCheck the thread stack at run time
    LW_OPTION_THREAD_STK_CLRThe stack data is cleared when the thread is established
    LW_OPTION_THREAD_USED_FPSave floating point arithmetic unit
    LW_OPTION_THREAD_SUSPENDBlocking after thread creation
    LW_OPTION_THREAD_INITInitialize thread
    LW_OPTION_THREAD_SAFEThe established thread is in safe mode
    LW_OPTION_THREAD_DETACHEDThreads are not allowed to be merged
    LW_OPTION_THREAD_UNSELECTThis thread does not use the select function
    LW_OPTION_THREAD_NO_MONITORThe kernel tracker has no effect on this thread
    LW_OPTION_THREAD_ UNPREEMPTIVETasks cannot be preempted (currently not supported)
    LW_OPTION_THREAD_SCOPE_PROCESSIn process contention (currently not supported)
  • Parameter pvArg is a thread parameter;

Thread attribute block operation interface

APIexplain
Lw_ThreadAttr_GetDefaultAPI_ThreadAttrGetDefault
Lw_ThreadAttr_GetAPI_ThreadAttrGet
Lw_ThreadAttr_BuildAPI_ThreadAttrBuild
Lw_ThreadAttr_BuildExAPI_ThreadAttrBuildEx
Lw_ThreadAttr_BuildFPAPI_ThreadAttrBuildFP
Lw_ThreadAttr_BuildDefaultAPI_ThreadAttrBuildDefault
Lw_ThreadAttr_SetGuardSizeAPI_ThreadAttrSetGuardSize
Lw_ThreadAttr_SetStackSizeAPI_ThreadAttrSetStackSize
Lw_ThreadAttr_SetArgAPI_ThreadAttrSetArg
LW_API  
LW_CLASS_THREADATTR     API_ThreadAttrGetDefault(VOID);                 /*  Get default thread attribute block          */

LW_API  
LW_CLASS_THREADATTR     API_ThreadAttrGet(LW_OBJECT_HANDLE  ulId);      /*  Gets the current attribute block of the specified thread      */

LW_API ULONG            API_ThreadAttrBuild(PLW_CLASS_THREADATTR    pthreadattr,
                                            size_t                  stStackByteSize, 
                                            UINT8                   ucPriority, 
                                            ULONG                   ulOption, 
                                            PVOID                   pvArg);
                                                                        /*  Generate a thread attribute block          */
                                            
LW_API ULONG            API_ThreadAttrBuildEx(PLW_CLASS_THREADATTR    pthreadattr,
                                              PLW_STACK               pstkStackTop, 
                                              size_t                  stStackByteSize, 
                                              UINT8                   ucPriority, 
                                              ULONG                   ulOption, 
                                              PVOID                   pvArg,
                                              PVOID                   pvExt);
                                                                        /*  Generate an advanced thread attribute block      */

LW_API ULONG            API_ThreadAttrBuildFP(PLW_CLASS_THREADATTR    pthreadattr,
                                              PVOID                   pvFP);
                                                                        /*  Configure floating point operator context        */

LW_API ULONG            API_ThreadAttrBuildDefault(PLW_CLASS_THREADATTR    pthreadattr);
                                                                        /*  Generate a thread default attribute block      */
LW_API ULONG            API_ThreadAttrSetGuardSize(PLW_CLASS_THREADATTR    pthreadattr,
                                                   size_t                  stGuardSize);
                                                                        /*  Set stack alert size          */
LW_API ULONG            API_ThreadAttrSetStackSize(PLW_CLASS_THREADATTR    pthreadattr,
                                                   size_t                  stStackByteSize);
                                                                        /*  Set stack size                */
LW_API ULONG            API_ThreadAttrSetArg(PLW_CLASS_THREADATTR    pthreadattr,
                                             PVOID                   pvArg);
                                                                        /*  Set thread startup parameters            */
  • API_ The threadattrget function is used to obtain the thread attribute block, and other interfaces are used to set the thread attribute block.

  • API_ThreadAttrGetDefault returns a default value of the thread attribute block, while the API_ThreadAttrBuildDefault is to set a thread attribute block as the default value. In fact, the effect is the same.

  • Other interfaces are a member that sets the thread attribute block separately.

  • It is also possible to assign values directly through structure members without using these thread attribute block operation functions.

  • The default thread attribute block values are as follows:

    Thread attribute block memberDefault value
    THREADATTR_pstkLowAddrLW_NULL
    THREADATTR_stGuardSizeLW_CFG_THREAD_DEFAULT_GUARD_SIZE
    THREADATTR_stStackByteSizeLW_CFG_THREAD_DEFAULT_STK_SIZE
    THREADATTR_ucPriorityLW_PRIO_NORMAL
    THREADATTR_ulOptionLW_OPTION_THREAD_STK_CHK
    THREADATTR_pvArgLW_NULL
    THREADATTR_pvExtLW_NULL

Sample code

    LW_CLASS_THREADATTR     threadattr  = API_ThreadAttrGetDefault();
    
    API_ThreadAttrBuild(&threadattr,
                        LW_CFG_THREAD_DISKCACHE_STK_SIZE,
                        LW_PRIO_T_SERVICE,
                        LW_CFG_DISKCACHE_OPTION | LW_OPTION_OBJECT_GLOBAL,
                        pdiskc);

	API_ThreadAttrSetStackSize(&threakattr, __LW_THREAD_MAIN_STK_SIZE);

Topics: thread SylixOS