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 name value LW_PRIO_HIGHEST 0 LW_PRIO_LOWEST 255 LW_PRIO_EXTREME LW_PRIO_HIGHEST LW_PRIO_CRITICAL 50 LW_PRIO_REALTIME 100 LW_PRIO_HIGH 150 LW_PRIO_NORMAL 200 LW_PRIO_LOW 250 LW_PRIO_IDLE LW_PRIO_LOWEST -
Parameter ulOption is thread option;
Macro name explain LW_OPTION_THREAD_STK_CHK Check the thread stack at run time LW_OPTION_THREAD_STK_CLR The stack data is cleared when the thread is established LW_OPTION_THREAD_USED_FP Save floating point arithmetic unit LW_OPTION_THREAD_SUSPEND Blocking after thread creation LW_OPTION_THREAD_INIT Initialize thread LW_OPTION_THREAD_SAFE The established thread is in safe mode LW_OPTION_THREAD_DETACHED Threads are not allowed to be merged LW_OPTION_THREAD_UNSELECT This thread does not use the select function LW_OPTION_THREAD_NO_MONITOR The kernel tracker has no effect on this thread LW_OPTION_THREAD_ UNPREEMPTIVE Tasks cannot be preempted (currently not supported) LW_OPTION_THREAD_SCOPE_PROCESS In process contention (currently not supported) -
Parameter pvArg is a thread parameter;
Thread attribute block operation interface
API | explain |
---|---|
Lw_ThreadAttr_GetDefault | API_ThreadAttrGetDefault |
Lw_ThreadAttr_Get | API_ThreadAttrGet |
Lw_ThreadAttr_Build | API_ThreadAttrBuild |
Lw_ThreadAttr_BuildEx | API_ThreadAttrBuildEx |
Lw_ThreadAttr_BuildFP | API_ThreadAttrBuildFP |
Lw_ThreadAttr_BuildDefault | API_ThreadAttrBuildDefault |
Lw_ThreadAttr_SetGuardSize | API_ThreadAttrSetGuardSize |
Lw_ThreadAttr_SetStackSize | API_ThreadAttrSetStackSize |
Lw_ThreadAttr_SetArg | API_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 member Default value THREADATTR_pstkLowAddr LW_NULL THREADATTR_stGuardSize LW_CFG_THREAD_DEFAULT_GUARD_SIZE THREADATTR_stStackByteSize LW_CFG_THREAD_DEFAULT_STK_SIZE THREADATTR_ucPriority LW_PRIO_NORMAL THREADATTR_ulOption LW_OPTION_THREAD_STK_CHK THREADATTR_pvArg LW_NULL THREADATTR_pvExt LW_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);