diff --git a/compat/posix/src/pprivate.h b/compat/posix/src/pprivate.h index e484c81ad140f908052f32883f9cd9c51860ec1f..8c921f5806406d63e0bac8980c69f15dafbc4807 100644 --- a/compat/posix/src/pprivate.h +++ b/compat/posix/src/pprivate.h @@ -44,24 +44,41 @@ extern "C" { #endif /* __cplusplus */ #endif /* __cplusplus */ +/************************************************************************** +posix 线程属性结构体 + + typedef struct __pthread_attr_s { + unsigned int detachstate; //线程的分离状态 + unsigned int schedpolicy; //线程调度策略 + struct sched_param schedparam; //线程的调度参数 + unsigned int inheritsched; //线程的继承性 + unsigned int scope; //线程的作用域 + unsigned int stackaddr_set; //线程的栈地址设置 + void* stackaddr; //线程栈的位置 + unsigned int stacksize_set; //线程栈的设置 + size_t stacksize; //线程栈的大小 + } pthread_attr_t; + +***************************************************************************/ + #define PTHREAD_DATA_NAME_MAX 20 /* * Thread control data structure * Per-thread information needed by POSIX */ typedef struct { - pthread_attr_t attr; /* Current thread attributes */ - pthread_t id; /* My thread ID */ - LosTaskCB *task; /* pointer to Huawei LiteOS thread object */ - CHAR name[PTHREAD_DATA_NAME_MAX]; /* name string for debugging */ - UINT8 state; /* Thread state */ - UINT8 cancelstate; /* Cancel state of thread */ - volatile UINT8 canceltype; /* Cancel type of thread */ - volatile UINT8 canceled; /* pending cancel flag */ - struct pthread_cleanup_buffer *cancelbuffer; /* stack of cleanup buffers */ - UINT32 freestack; /* stack malloced, must be freed */ - UINT32 stackmem; /* base of stack memory area only valid if freestack == true */ - VOID **thread_data; /* Per-thread data table pointer */ + pthread_attr_t attr; /* Current thread attributes *///当前线程属性 + pthread_t id; /* My thread ID */ //线程PID + LosTaskCB *task; /* pointer to Huawei LiteOS thread object *///指向华为 轻内核线程对象实体 + CHAR name[PTHREAD_DATA_NAME_MAX]; /* name string for debugging */ //名称用于调试 + UINT8 state; /* Thread state */ //线程的状态 + UINT8 cancelstate; /* Cancel state of thread */ //线程的取消状态 + volatile UINT8 canceltype; /* Cancel type of thread */ //线程的取消类型 + volatile UINT8 canceled; /* pending cancel flag */ //阻塞取消标识 + struct pthread_cleanup_buffer *cancelbuffer; /* stack of cleanup buffers */ //清空栈buf + UINT32 freestack; /* stack malloced, must be freed */ //堆栈,必须释放 + UINT32 stackmem; /* base of stack memory area only valid if freestack == true *///堆栈内存区域的有效范围 + VOID **thread_data; /* Per-thread data table pointer *///指向每个线程的数据表的指针 } _pthread_data; /* @@ -70,12 +87,12 @@ typedef struct { * struct _pthread_data about thread object. * Note: numerical order here is important, do not rearrange. */ -#define PTHREAD_STATE_FREE 0 /* This structure is free for reuse */ -#define PTHREAD_STATE_DETACHED 1 /* The thread is running but detached */ -#define PTHREAD_STATE_RUNNING 2 /* The thread is running and will wait to join when it exits */ -#define PTHREAD_STATE_JOIN 3 /* The thread has exited and is waiting to be joined */ -#define PTHREAD_STATE_EXITED 4 /* The thread has exited and is ready to be reaped */ -#define PTHREAD_STATE_ALRDY_JOIN 5 /* The thread state is in join */ +#define PTHREAD_STATE_FREE 0 /* This structure is free for reuse */ //这种结构可以免费重复使用 +#define PTHREAD_STATE_DETACHED 1 /* The thread is running but detached */ //线程正在运行,但已分离 +#define PTHREAD_STATE_RUNNING 2 /* The thread is running and will wait to join when it exits */ //线程正在运行,退出时将等待加入 +#define PTHREAD_STATE_JOIN 3 /* The thread has exited and is waiting to be joined */ //线程已退出,正在等待加入 +#define PTHREAD_STATE_EXITED 4 /* The thread has exited and is ready to be reaped */ //线已退出,准备收割 +#define PTHREAD_STATE_ALRDY_JOIN 5 /* The thread state is in join */ //线程状态为联结 #ifdef __cplusplus diff --git a/compat/posix/src/pthread.c b/compat/posix/src/pthread.c index af8b3ab7a043bdd955266f3068869d45f76208bd..d03b51224b035cffe99d6c53c39f58f527a2a25c 100644 --- a/compat/posix/src/pthread.c +++ b/compat/posix/src/pthread.c @@ -46,7 +46,7 @@ extern "C" { /* * Array of pthread control structures. A pthread_t object is * "just" an index into this array. - */ + */ //pthread控制结构的数组。pthread_t对象只是这个数组的一个索引 STATIC _pthread_data g_pthreadData[LOSCFG_BASE_CORE_TSK_LIMIT + 1]; /* Count of number of threads that have exited and not been reaped. */ @@ -218,7 +218,7 @@ STATIC UINT32 InitPthreadData(pthread_t threadID, pthread_attr_t *userAttr, SetPthreadDataAttr(userAttr, threadID, taskCB, created); return ret; } -//POSIX 创建线程接口 +//POSIX接口之 创建一个线程 int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*startRoutine)(void *), void *arg) { @@ -281,7 +281,7 @@ ERROR_OUT: return map_errno(ret); } - +//POSIX接口之 终止当前线程 void pthread_exit(void *retVal) { _pthread_data *self = pthread_get_self_data(); @@ -358,7 +358,7 @@ STATIC INT32 ProcessByJoinState(_pthread_data *joined) } return err; } - +//阻塞当前的线程,直到另外一个线程运行结束 int pthread_join(pthread_t thread, void **retVal) { INT32 err; @@ -688,7 +688,7 @@ OUT: LOS_TaskUnlock(); return ret; } - +// int pthread_cancel(pthread_t thread) { _pthread_data *data = NULL; diff --git a/compat/posix/src/semaphore.c b/compat/posix/src/semaphore.c index 425259ba952c82965241b927fb6e054697345f3a..1ea02a5fd12118e9994217962b7963c43291f4cd 100644 --- a/compat/posix/src/semaphore.c +++ b/compat/posix/src/semaphore.c @@ -41,7 +41,7 @@ extern "C" { #endif /* __cplusplus */ /* Initialize semaphore to value, shared is not supported in Huawei LiteOS. */ -int sem_init(sem_t *sem, int shared, unsigned int value) +int sem_init(sem_t *sem, int shared, unsigned int value)//初始化信号量的值,华为LiteOS不支持共享。 { UINT32 semHandle = 0; UINT32 ret; diff --git a/syscall/process_syscall.c b/syscall/process_syscall.c index b5cf00d8cadae5af04f506e255cce1cc72bab513..5be528f636827b0cd1feddc97882c95774745c9e 100644 --- a/syscall/process_syscall.c +++ b/syscall/process_syscall.c @@ -197,7 +197,7 @@ int SysSchedGetParam(int id, int flag) return OsGetProcessPriority(LOS_PRIO_PROCESS, id); } -//设置调度参数 +//设置调度参数 id = 线程ID prio:优先级 int SysSchedSetParam(int id, unsigned int prio, int flag) { int ret; @@ -211,10 +211,10 @@ int SysSchedSetParam(int id, unsigned int prio, int flag) } if (id == 0) { - id = (int)LOS_GetCurrProcessID(); + id = (int)LOS_GetCurrProcessID();//当前进程ID } - ret = OsPermissionToCheck(id, LOS_GetCurrProcessID()); + ret = OsPermissionToCheck(id, LOS_GetCurrProcessID());//检查权限 if (ret < 0) { return ret; } @@ -226,20 +226,20 @@ int SysSetProcessPriority(int which, int who, unsigned int prio) { int ret; - if (prio < OS_USER_PROCESS_PRIORITY_HIGHEST) { + if (prio < OS_USER_PROCESS_PRIORITY_HIGHEST) { // 注意这里设置优先不能低于10,[]10:31]是用户进程的优先级范围 return -EINVAL; } if (who == 0) { - who = (int)LOS_GetCurrProcessID(); + who = (int)LOS_GetCurrProcessID();//获取当前进程ID } - ret = OsPermissionToCheck(who, LOS_GetCurrProcessID()); + ret = OsPermissionToCheck(who, LOS_GetCurrProcessID());//检查权限 if (ret < 0) { return ret; } - return OsSetProcessScheduler(which, who, prio, LOS_SCHED_RR, FALSE); + return OsSetProcessScheduler(which, who, prio, LOS_SCHED_RR, FALSE);//设置进程的调度参数 } //获取进程优先级 int SysGetProcessPriority(int which, int who) diff --git a/zzz/git/push.sh b/zzz/git/push.sh index 9784f03bb48e19a43f460441f19adb33546a05bf..a471ea3d84365d7d4214482fb694c5fd8980f879 100644 --- a/zzz/git/push.sh +++ b/zzz/git/push.sh @@ -1,5 +1,5 @@ git add -A -git commit -m '对硬件中断部分源码注释,对CPU各个寄存器功能注释 +git commit -m '开始对 posix 部分代码注释 鸿蒙内核源码分析系列 【 CSDN | OSCHINA | WIKI 】 鸿蒙内核源码注释中文版 【 CSDN仓 | Gitee仓 | Github仓 | Coding仓 】四大仓库每日同步更新代码和wiki 项目给鸿蒙内核源码逐行加上中文注解,详细阐述框架和代码细节, 精读 HarmonyOS 内核源码, 将迅速拔高对计算机整体理解,从此高屋建瓴看问题.'