提交 40e67ad2 编写于 作者: 鸿蒙内核源码分析's avatar 鸿蒙内核源码分析

开始对 posix 部分代码注释

鸿蒙内核源码分析系列 【 CSDN | OSCHINA | WIKI 】
鸿蒙内核源码注释中文版 【 CSDN仓 | Gitee仓 | Github仓 | Coding仓 】四大仓库每日同步更新代码和wiki
项目给鸿蒙内核源码逐行加上中文注解,详细阐述框架和代码细节, 精读 HarmonyOS 内核源码, 将迅速拔高对计算机整体理解,从此高屋建瓴看问题.
上级 a93d19be
...@@ -44,24 +44,41 @@ extern "C" { ...@@ -44,24 +44,41 @@ extern "C" {
#endif /* __cplusplus */ #endif /* __cplusplus */
#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 #define PTHREAD_DATA_NAME_MAX 20
/* /*
* Thread control data structure * Thread control data structure
* Per-thread information needed by POSIX * Per-thread information needed by POSIX
*/ */
typedef struct { typedef struct {
pthread_attr_t attr; /* Current thread attributes */ pthread_attr_t attr; /* Current thread attributes *///当前线程属性
pthread_t id; /* My thread ID */ pthread_t id; /* My thread ID */ //线程PID
LosTaskCB *task; /* pointer to Huawei LiteOS thread object */ LosTaskCB *task; /* pointer to Huawei LiteOS thread object *///指向华为 轻内核线程对象实体
CHAR name[PTHREAD_DATA_NAME_MAX]; /* name string for debugging */ CHAR name[PTHREAD_DATA_NAME_MAX]; /* name string for debugging */ //名称用于调试
UINT8 state; /* Thread state */ UINT8 state; /* Thread state */ //线程的状态
UINT8 cancelstate; /* Cancel state of thread */ UINT8 cancelstate; /* Cancel state of thread */ //线程的取消状态
volatile UINT8 canceltype; /* Cancel type of thread */ volatile UINT8 canceltype; /* Cancel type of thread */ //线程的取消类型
volatile UINT8 canceled; /* pending cancel flag */ volatile UINT8 canceled; /* pending cancel flag */ //阻塞取消标识
struct pthread_cleanup_buffer *cancelbuffer; /* stack of cleanup buffers */ struct pthread_cleanup_buffer *cancelbuffer; /* stack of cleanup buffers */ //清空栈buf
UINT32 freestack; /* stack malloced, must be freed */ UINT32 freestack; /* stack malloced, must be freed */ //堆栈,必须释放
UINT32 stackmem; /* base of stack memory area only valid if freestack == true */ UINT32 stackmem; /* base of stack memory area only valid if freestack == true *///堆栈内存区域的有效范围
VOID **thread_data; /* Per-thread data table pointer */ VOID **thread_data; /* Per-thread data table pointer *///指向每个线程的数据表的指针
} _pthread_data; } _pthread_data;
/* /*
...@@ -70,12 +87,12 @@ typedef struct { ...@@ -70,12 +87,12 @@ typedef struct {
* struct _pthread_data about thread object. * struct _pthread_data about thread object.
* Note: numerical order here is important, do not rearrange. * Note: numerical order here is important, do not rearrange.
*/ */
#define PTHREAD_STATE_FREE 0 /* This structure is free for reuse */ #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_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_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_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_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_ALRDY_JOIN 5 /* The thread state is in join */ //线程状态为联结
#ifdef __cplusplus #ifdef __cplusplus
......
...@@ -46,7 +46,7 @@ extern "C" { ...@@ -46,7 +46,7 @@ extern "C" {
/* /*
* Array of pthread control structures. A pthread_t object is * Array of pthread control structures. A pthread_t object is
* "just" an index into this array. * "just" an index into this array.
*/ */ //pthread控制结构的数组。pthread_t对象只是这个数组的一个索引
STATIC _pthread_data g_pthreadData[LOSCFG_BASE_CORE_TSK_LIMIT + 1]; STATIC _pthread_data g_pthreadData[LOSCFG_BASE_CORE_TSK_LIMIT + 1];
/* Count of number of threads that have exited and not been reaped. */ /* 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, ...@@ -218,7 +218,7 @@ STATIC UINT32 InitPthreadData(pthread_t threadID, pthread_attr_t *userAttr,
SetPthreadDataAttr(userAttr, threadID, taskCB, created); SetPthreadDataAttr(userAttr, threadID, taskCB, created);
return ret; return ret;
} }
//POSIX 创建线程接口 //POSIX接口之 创建一个线程
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*startRoutine)(void *), void *arg) void *(*startRoutine)(void *), void *arg)
{ {
...@@ -281,7 +281,7 @@ ERROR_OUT: ...@@ -281,7 +281,7 @@ ERROR_OUT:
return map_errno(ret); return map_errno(ret);
} }
//POSIX接口之 终止当前线程
void pthread_exit(void *retVal) void pthread_exit(void *retVal)
{ {
_pthread_data *self = pthread_get_self_data(); _pthread_data *self = pthread_get_self_data();
...@@ -358,7 +358,7 @@ STATIC INT32 ProcessByJoinState(_pthread_data *joined) ...@@ -358,7 +358,7 @@ STATIC INT32 ProcessByJoinState(_pthread_data *joined)
} }
return err; return err;
} }
//阻塞当前的线程,直到另外一个线程运行结束
int pthread_join(pthread_t thread, void **retVal) int pthread_join(pthread_t thread, void **retVal)
{ {
INT32 err; INT32 err;
...@@ -688,7 +688,7 @@ OUT: ...@@ -688,7 +688,7 @@ OUT:
LOS_TaskUnlock(); LOS_TaskUnlock();
return ret; return ret;
} }
//
int pthread_cancel(pthread_t thread) int pthread_cancel(pthread_t thread)
{ {
_pthread_data *data = NULL; _pthread_data *data = NULL;
......
...@@ -41,7 +41,7 @@ extern "C" { ...@@ -41,7 +41,7 @@ extern "C" {
#endif /* __cplusplus */ #endif /* __cplusplus */
/* Initialize semaphore to value, shared is not supported in Huawei LiteOS. */ /* 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 semHandle = 0;
UINT32 ret; UINT32 ret;
......
...@@ -197,7 +197,7 @@ int SysSchedGetParam(int id, int flag) ...@@ -197,7 +197,7 @@ int SysSchedGetParam(int id, int flag)
return OsGetProcessPriority(LOS_PRIO_PROCESS, id); return OsGetProcessPriority(LOS_PRIO_PROCESS, id);
} }
//设置调度参数 //设置调度参数 id = 线程ID prio:优先级
int SysSchedSetParam(int id, unsigned int prio, int flag) int SysSchedSetParam(int id, unsigned int prio, int flag)
{ {
int ret; int ret;
...@@ -211,10 +211,10 @@ int SysSchedSetParam(int id, unsigned int prio, int flag) ...@@ -211,10 +211,10 @@ int SysSchedSetParam(int id, unsigned int prio, int flag)
} }
if (id == 0) { 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) { if (ret < 0) {
return ret; return ret;
} }
...@@ -226,20 +226,20 @@ int SysSetProcessPriority(int which, int who, unsigned int prio) ...@@ -226,20 +226,20 @@ int SysSetProcessPriority(int which, int who, unsigned int prio)
{ {
int ret; int ret;
if (prio < OS_USER_PROCESS_PRIORITY_HIGHEST) { if (prio < OS_USER_PROCESS_PRIORITY_HIGHEST) { // 注意这里设置优先不能低于10,[]10:31]是用户进程的优先级范围
return -EINVAL; return -EINVAL;
} }
if (who == 0) { 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) { if (ret < 0) {
return ret; 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) int SysGetProcessPriority(int which, int who)
......
git add -A git add -A
git commit -m '对硬件中断部分源码注释,对CPU各个寄存器功能注释 git commit -m '开始对 posix 部分代码注释
鸿蒙内核源码分析系列 【 CSDN | OSCHINA | WIKI 】 鸿蒙内核源码分析系列 【 CSDN | OSCHINA | WIKI 】
鸿蒙内核源码注释中文版 【 CSDN仓 | Gitee仓 | Github仓 | Coding仓 】四大仓库每日同步更新代码和wiki 鸿蒙内核源码注释中文版 【 CSDN仓 | Gitee仓 | Github仓 | Coding仓 】四大仓库每日同步更新代码和wiki
项目给鸿蒙内核源码逐行加上中文注解,详细阐述框架和代码细节, 精读 HarmonyOS 内核源码, 将迅速拔高对计算机整体理解,从此高屋建瓴看问题.' 项目给鸿蒙内核源码逐行加上中文注解,详细阐述框架和代码细节, 精读 HarmonyOS 内核源码, 将迅速拔高对计算机整体理解,从此高屋建瓴看问题.'
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册