前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Zephyr 内核数据结构

Zephyr 内核数据结构

原创
作者头像
无限之生
修改2020-06-29 17:26:16
5220
修改2020-06-29 17:26:16
举报
文章被收录于专栏:技术日记技术日记

基于zephyr-2.2.0-rc3

1 双向链表(_dnode)

代码语言:c
复制
struct _dnode {
	union {
		struct _dnode *head; /* ptr to head of list (sys_dlist_t) */
		struct _dnode *next; /* ptr to next node    (sys_dnode_t) */
	};
	union {
		struct _dnode *tail; /* ptr to tail of list (sys_dlist_t) */
		struct _dnode *prev; /* ptr to previous node (sys_dnode_t) */
	};
}

等价于sys_dlist_t sys_dnode_t

代码语言:c
复制
typedef struct _dnode sys_dlist_t;
typedef struct _dnode sys_dnode_t;

注:

等待队列(_wait_q_t),也是通过_dnode 实现的

2 红黑树节点(rbnode )

位于sys/rb.h

代码语言:c
复制
struct rbnode {
 struct rbnode *children[2];
};)

进一步,红黑树的结构定义

代码语言:c
复制
struct rbtree {
 struct rbnode *root;              //根节点
 rb_lessthan_t lessthan_fn;        //比较函数
 int max_depth;
#ifdef CONFIG_MISRA_SANE
 struct rbnode *iter_stack[Z_MAX_RBTREE_DEPTH];
 unsigned char iter_left[Z_MAX_RBTREE_DEPTH];
#endif
};

3 _thread_base

有了前面_dnoderbnode 的结构基础,接下来看看线程的基础结构_thread_base 的定义,如下:

代码语言:c
复制
/* can be used for creating 'dummy' threads, e.g. for pending on objects */
struct _thread_base {

	/* this thread's entry in a ready/wait queue */
	union {
		sys_dnode_t qnode_dlist;
		struct rbnode qnode_rb;
	};

	/* wait queue on which the thread is pended (needed only for
	 * trees, not dumb lists)
	 */
	_wait_q_t *pended_on;                             //挂起的任务队列

	/* user facing 'thread options'; values defined in include/kernel.h */
	u8_t user_options;

	/* thread state */
	u8_t thread_state;                               //线程状态

	/*
	 * scheduler lock count and thread priority
	 *
	 * These two fields control the preemptibility of a thread.
	 *
	 * When the scheduler is locked, sched_locked is decremented, which
	 * means that the scheduler is locked for values from 0xff to 0x01. A
	 * thread is coop if its prio is negative, thus 0x80 to 0xff when
	 * looked at the value as unsigned.
	 *
	 * By putting them end-to-end, this means that a thread is
	 * non-preemptible if the bundled value is greater than or equal to
	 * 0x0080.
	 */
	union {
		struct {
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__            //对preempt字段大小端处理
			u8_t sched_locked;
			s8_t prio;
#else /* LITTLE and PDP */
			s8_t prio;
			u8_t sched_locked;
#endif
		};
		u16_t preempt;                  //preempt 包含prio和sched_locked两个字段,用于控制线程的抢占性
	};

#ifdef CONFIG_SCHED_DEADLINE
	int prio_deadline;
#endif

	u32_t order_key;

#ifdef CONFIG_SMP                                   //注:多核CPU支持才需要
	/* True for the per-CPU idle threads */
	u8_t is_idle;

	/* CPU index on which thread was last run */
	u8_t cpu;

	/* Recursive count of irq_lock() calls */
	u8_t global_lock_count;

#endif

#ifdef CONFIG_SCHED_CPU_MASK
	/* "May run on" bits for each CPU */
	u8_t cpu_mask;
#endif

	/* data returned by APIs */
	void *swap_data;

#ifdef CONFIG_SYS_CLOCK_EXISTS
	/* this thread's entry in a timeout queue */
	struct _timeout timeout;
#endif
};

4 线程结构(k_thread)

代码语言:javascript
复制
/**
 * @ingroup thread_apis
 * Thread Structure
 */
struct k_thread {

	struct _thread_base base;

	/** defined by the architecture, but all archs need these */
	struct _callee_saved callee_saved;

	/** static thread init data */
	void *init_data;

	/**
	 * abort function
	 * @req K-THREAD-002
	 * */
	void (*fn_abort)(void);

#if defined(CONFIG_THREAD_MONITOR)
	/** thread entry and parameters description */
	struct __thread_entry entry;

	/** next item in list of all threads */
	struct k_thread *next_thread;
#endif

#if defined(CONFIG_THREAD_NAME)
	/** Thread name */
	char name[CONFIG_THREAD_MAX_NAME_LEN];
#endif

#ifdef CONFIG_THREAD_CUSTOM_DATA
	/** crude thread-local storage */
	void *custom_data;
#endif

#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
	struct _thread_userspace_local_data *userspace_local_data;
#endif

#ifdef CONFIG_ERRNO
#ifndef CONFIG_USERSPACE
	/** per-thread errno variable */
	int errno_var;
#endif
#endif

#if defined(CONFIG_THREAD_STACK_INFO)
	/** Stack Info */
	struct _thread_stack_info stack_info;
#endif /* CONFIG_THREAD_STACK_INFO */

#if defined(CONFIG_USERSPACE)
	/** memory domain info of the thread */
	struct _mem_domain_info mem_domain_info;
	/** Base address of thread stack */
	k_thread_stack_t *stack_obj;
	/** current syscall frame pointer */
	void *syscall_frame;
#endif /* CONFIG_USERSPACE */


#if defined(CONFIG_USE_SWITCH)
	/* When using __switch() a few previously arch-specific items
	 * become part of the core OS
	 */

	/** z_swap() return value */
	int swap_retval;

	/** Context handle returned via arch_switch() */
	void *switch_handle;
#endif
	/** resource pool */
	struct k_mem_pool *resource_pool;

	/** arch-specifics: must always be at the end */
	struct _thread_arch arch;
};

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 双向链表(_dnode)
  • 2 红黑树节点(rbnode )
  • 3 _thread_base
  • 4 线程结构(k_thread)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档