state thread
2015-11-22 17:50
861 查看
State Thread 的官网地址:http://state-threads.sourceforge.net
The State Threads Library is a small application library which provides a foundation for writing fast and highly scalable Internet applications (such as web servers, proxy servers, mail transfer agents, and so on, really any network-data-driven application) on UNIX-like platforms. It combines the simplicity of the multithreaded programming paradigm, in which one thread supports each simultaneous connection, with the performance and scalability of an event-driven state machine architecture. In other words, this library offers a threading API for structuring an Internet application as a state machine.
传统的服务器端开发模型是:采用session异步方式调用来(调用其它好多服务时需要存状态,来完成一个完整的流程调用),这种方式稍稍复杂一点。采用”协程”的好处,常常被人提起的是:回归“同步调用型”的开发模型:程序调用api1(),api1返回之后调api2(),等api2返回之后调api3.. and so on. 这样的开发模式更加容易理解。
每当创建一个微线程时,都会将其加入到RUNQ中。
主要关注点:
微线程的状态机
每个微线程维护的栈空间、私有数据
每个微线程的入口点
我们熟悉的jmp_buf上下文:context
_st_stack的可用空间大小缺省为:128k,即刚开始分配时的大小。当不够用时,会增加一个内存页的大小(getpagesize),当然的大小必须是内存页的整数倍。(详见st_thread_create)。这部分是可用栈空间的大小,实际大小还会加上:2*REDZONE+extra。 在stack的每一端(栈顶、栈底)都有一个REDZONE,其大小也是一个分页;而extra则看是否需要(0或者一个分页大小)
在st_stack中,入栈的方式,有两种:向下增长,向上增加。以向上增长为例(向下增长的话,类似,反之即可):
主要过程如下:
创建栈,分配空间。
初始化sp/bsp、私有数据(private_data)以及微线程自身所需空间(thread),入口函数(start)及参数。
调用setjmp,若成功,则执行_st_thread_main();
将之前的sp恢复.
其中的第三步,_st_thread_main,也就是微线程自身的执行点,过程也很简单:
获取当前微线程
将微线程对应的栈空间位置状态变量置为0(state和flags,转型为valatile,防止longjmp切换时更改值。ps: 这里有个关于二级指针数组[1]的tricks)
执行微线程的入口函数(start)
退出微线程(_st_thread_exit)
退出微线程时,会发生一些很奇妙的事情:
清理工作
对term做检查,是否已退出。若是,添加thread到僵尸队列:zombieq中, 通知term信号,并切换context(_ST_SWITCH_CONTEXT,清理term
清理stack(_st_stack_free)
切换至另一个thread执行(_ST_SWITCH_CONTEXT)
上面就是一个整体的流程。而其中有两个地方,需要特别再关注一下。
如果runq队列有在运行,切换至next; 如果没有,切换至idle_thread
将切换后的当前微线程置为run状态
longjmp置相应微线程的jmp_buf处,恢复栈执行
The State Threads Library is a small application library which provides a foundation for writing fast and highly scalable Internet applications (such as web servers, proxy servers, mail transfer agents, and so on, really any network-data-driven application) on UNIX-like platforms. It combines the simplicity of the multithreaded programming paradigm, in which one thread supports each simultaneous connection, with the performance and scalability of an event-driven state machine architecture. In other words, this library offers a threading API for structuring an Internet application as a state machine.
传统的服务器端开发模型是:采用session异步方式调用来(调用其它好多服务时需要存状态,来完成一个完整的流程调用),这种方式稍稍复杂一点。采用”协程”的好处,常常被人提起的是:回归“同步调用型”的开发模型:程序调用api1(),api1返回之后调api2(),等api2返回之后调api3.. and so on. 这样的开发模式更加容易理解。
一、setjmp/longjmp
ST中使用了setjmp、longjmp来实现协程,内容参考另一篇博文:http://www.cnblogs.com/ym65536/p/4984339.html二、state threads
本文的分析主要基于state threads实现进行分析。为求简单,忽略掉一些平台强相关的逻辑。2.1 事件系统(_st_eventsys_t)
常见的方式:poll/select/epoll/kqueue等等(默认采用select)。实现方式和其它的一些事件系统(ae/libevent)有点类型,所以此文不再详细地做剖析。typedef struct _st_eventsys_ops { const char * name; /* Name of this event system */ int val; /* Type of this event system */ int (*init)(void); /* Initialization */ void (*dispatch)(void); /* Dispatch function */ int (*pollset_add)(struct pollfd *, int); /* Add descriptor set */ void (*pollset_del)(struct pollfd *, int); /* Delete descriptor set */ int (*fd_new)(int); /* New descriptor allocated */ int (*fd_close)(int); /* Descriptor closed */ int (*fd_getlimit)(void); /* Descriptor hard limit */ } _st_eventsys_t;
2.2 协程态(_st_vp_t)
每个vp对应一个idle协程,4个队列,切换回调等。typedef struct _st_vp { _st_thread_t *idle_thread; /* Idle thread for this vp */ st_utime_t last_clock; /* The last time we went into vp_check_clock() */ _st_clist_t run_q; /* run queue for this vp */ _st_clist_t io_q; /* io queue for this vp */ _st_clist_t zombie_q; /* zombie queue for this vp */ #ifdef DEBUG _st_clist_t thread_q; /* all threads of this vp */ #endif int pagesize; _st_thread_t *sleep_q; /* sleep queue for this vp */ int sleepq_size; /* number of threads on sleep queue */ #ifdef ST_SWITCH_CB st_switch_cb_t switch_out_cb; /* called when a thread is switched out */ st_switch_cb_t switch_in_cb; /* called when a thread is switched in */ #endif } _st_vp_t;
2.3 队列(run/io/zombie/sleep queue)
st使用了4个队列, 这个在上面的结构中就已经有,为了访问的方便,定义了4组宏(debug模式下,还有另一个),如下。这4个队列分别对应了4种状态,由此形成自己的状态机体系。每当创建一个微线程时,都会将其加入到RUNQ中。
#define _ST_RUNQ (_st_this_vp.run_q) #define _ST_IOQ (_st_this_vp.io_q) #define _ST_ZOMBIEQ (_st_this_vp.zombie_q) #ifdef DEBUG #define _ST_THREADQ (_st_this_vp.thread_q) #endif #define _ST_SLEEPQ (_st_this_vp.sleep_q)
2.4 微线程(_st_thread_t)
在vp中,我们看到有一个成员:idle_thread. 自然会有疑问:这货是干嘛的?这货长的像个线程,但其实当然不是线程,就以“微线程”来称吧:即用户态下实现的线程。主要关注点:
微线程的状态机
每个微线程维护的栈空间、私有数据
每个微线程的入口点
我们熟悉的jmp_buf上下文:context
typedef struct _st_thread _st_thread_t; struct _st_thread { int state; /* Thread's state */ int flags; /* Thread's flags */ void * (*start)(void *arg); /* The start function of the thread */ void * arg; /* Argument of the start function */ void * retval; /* Return value of the start function */ _st_stack_t * stack; /* Info about thread's stack */ _st_clist_t links; /* For putting on run/sleep/zombie queue */ _st_clist_t wait_links; /* For putting on mutex/condvar wait queue */ #ifdef DEBUG _st_clist_t tlink; /* For putting on thread queue */ #endif st_utime_t due; /* Wakeup time when thread is sleeping */ _st_thread_t * left; /* For putting in timeout heap */ _st_thread_t * right; /* -- see docs/timeout_heap.txt for details */ int heap_index; void ** private_data; /* Per thread private data */ _st_cond_t * term; /* Termination condition variable for join */ jmp_buf context; /* Thread's context */ };
2.5 栈(_st_stack_t)
每个微线程自身单独都会维护自己的一个“栈空间”。栈有自己独立的内存,大小,栈底,栈顶,栈指针(程序级别、非系统内存级别),恢复点。这里的“栈”只是一个概念(实际是堆,通过malloc或mmap来实现),并非我们通常指的栈。typedef struct _st_stack { _st_clist_t links; char * vaddr; /* Base of stack's allocated memory */ int vaddr_size; /* Size of stack's allocated memory */ int stk_size; /* Size of usable portion of the stack */ char * stk_bottom; /* Lowest address of stack's usable portion */ char * stk_top; /* Highest address of stack's usable portion */ void * sp; /* Stack pointer from C's point of view */ #ifdef __ia64__ void * bsp; /* Register stack backing store pointer */ #endif } _st_stack_t;
_st_stack的可用空间大小缺省为:128k,即刚开始分配时的大小。当不够用时,会增加一个内存页的大小(getpagesize),当然的大小必须是内存页的整数倍。(详见st_thread_create)。这部分是可用栈空间的大小,实际大小还会加上:2*REDZONE+extra。 在stack的每一端(栈顶、栈底)都有一个REDZONE,其大小也是一个分页;而extra则看是否需要(0或者一个分页大小)
在st_stack中,入栈的方式,有两种:向下增长,向上增加。以向上增长为例(向下增长的话,类似,反之即可):
2.6 jmp_buf上下文
即然栈是自己实现的,那边对应的jmp_buf里头的sp、bsp也需要跟着变。3 微线程的创建
3.1 整体流程
微线程的创立,通过_st_thread_create()来完成。主要过程如下:
创建栈,分配空间。
初始化sp/bsp、私有数据(private_data)以及微线程自身所需空间(thread),入口函数(start)及参数。
调用setjmp,若成功,则执行_st_thread_main();
将之前的sp恢复.
其中的第三步,_st_thread_main,也就是微线程自身的执行点,过程也很简单:
获取当前微线程
将微线程对应的栈空间位置状态变量置为0(state和flags,转型为valatile,防止longjmp切换时更改值。ps: 这里有个关于二级指针数组[1]的tricks)
执行微线程的入口函数(start)
退出微线程(_st_thread_exit)
退出微线程时,会发生一些很奇妙的事情:
清理工作
对term做检查,是否已退出。若是,添加thread到僵尸队列:zombieq中, 通知term信号,并切换context(_ST_SWITCH_CONTEXT,清理term
清理stack(_st_stack_free)
切换至另一个thread执行(_ST_SWITCH_CONTEXT)
上面就是一个整体的流程。而其中有两个地方,需要特别再关注一下。
3.2 上下文切换
因为涉及到微线程的切换,即然是“切换”,那么这里肯定至少涉及到两个微线程。由于微线程本身处在RUNQ中,切换的话,自然会将next。如果runq队列有在运行,切换至next; 如果没有,切换至idle_thread
将切换后的当前微线程置为run状态
longjmp置相应微线程的jmp_buf处,恢复栈执行
4.小结
state thread的代码很简洁,如果知道原理的话,分析起来不困难。另外,它还简单地实现了一个http server。有兴趣的朋友可以做为参照自己实现功能更复杂的http server。相关文章推荐
- linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结
- 【BZOJ】3922: Karin的弹幕
- k路归并算法的分析和实现
- zzulioj 1780: 和尚特烦恼6——炒股
- OpenGL2 CoreImage初探与Model封装
- 家庭作业汇总
- 一个设置访问权限的实例
- OpenJudge_P7216 Minecraft
- 随笔
- Genymotion安装与使用、Android Studio安装Genymotion插件
- CentOS6.4使用sudo提示用户不在sudoers文件中的解决方法
- java内存区域
- 常用drag控件
- PowerShell GUI 之使用visual studio创建GUI (1)
- iOS--错误集锦--the operation couldn't be completed. (FBSOpenApplicationErrorDomain error 3.)
- FT Introduction
- 单机千万并发连接实战(修订版)
- 数据库优化小技巧总结
- PHP Echo的一些知识,感觉挺有用的
- javascript学习第1节