走神一会

你生而有翼,为何竟愿一生匍匐前进,形如虫蚁?

「学而不思则罔、思而不学则殆」的意思是:

想顺利的学会「内思」要与「关系」思考方式结合起来练。

否则,直接学「内思」,初学者会陷入练习的困境,这就是罔。

而只依靠「关系」思考方式,又会带来很大的危害,这就是殆。 作者:心智玩家的简书链接:https://www.jianshu.com/p/2940cf732b94来源

「内思」,用元神意识直接「看」意识影像,是需要经过一段时间的练习以后,才能达到的状态。没有经过练习,不容易做到。

历史渊源

首先介绍一下这个库的历史渊源,从代码贡献者来看,ST不是个人作品,而是有着雄厚的商业支持和应用背景,比如服务器领域,在这里你可以看到ST曾作为Apache的多核应用模块发布。

其诞生最初是由网景(Netscape)公司的MSPR(Netscape Portable Runtime library)项目中剥离出来,后由SGI(Silicon Graphic Inc)还有Yahoo!公司(前者是主力)开发维护的独立线程库。

版本方面,作为SourceForge上开源项目,

由2001年发布v1.0以来一直到2009年v1.9稳定版后未再变动。

在平台移植方面,从Makefile的配置选项中可知ST支持多种Unix-like平台,还有专门针对Win32的源码改写。

源码例子中,提供了web server、proxy以及dns三种编程实例供参考。

可以说代码质量应该是相当的稳定和可靠的。

卡片

协程库 语言 开发者
state threads library 3000行C代码 历史渊源
Libtask a Coroutine Library for C and Unix  

学习记录

一、doc

阅读文档1 ,累计盘茄次数 5 耗时 130分钟

理解 ST本质上仍然是基于EDSM模型,但旨在取代传统的异步回调方式 ,为互联网应用程序而生的State Threads

涉及内容

State Threads Library Documentation

关键词:

  • ST scheduler (不懂)

the ST scheduler is hidden inside the library and invisible to an application writer.

  • traditional EDSM (不懂)

event-driven state machines (EDSM)

  • virtual processor (不懂)

ST的多核架构

  • ,ST的threads可以并发地线性地处理I/O事件 (不懂)
  • execute state event wait queue

  • setjmp/longjmp (不懂)

  • 看了一下代码和运行一个例子 懂了

image.png

阅读文档2 ,累计盘茄次数 3 耗时 100分钟

理解 Libtask: a Coroutine Library for C and Unix 含义

涉及内容

二、 code

 1
 2
 3
 4
 5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 
void *_st_idle_thread_start(void *arg) { _st_thread_t *me = _ST_CURRENT_THREAD(); while (_st_active_count > 0) { /* Idle vp till I/O is ready or the smallest timeout expired */ _ST_VP_IDLE(); /* Check sleep queue for expired threads */ _st_vp_check_clock(); me->state = _ST_ST_RUNNABLE; _ST_SWITCH_CONTEXT(me); } /* No more threads */ exit(0); /* NOTREACHED */ return NULL; } /*  * Switch away from the current thread context by saving its state  * and calling the thread scheduler  */ #define _ST_SWITCH_CONTEXT(_thread) \  ST_BEGIN_MACRO \  if (!MD_SETJMP((_thread)->context)) { \  _st_vp_schedule(); \  } \  ST_END_MACRO  /*  * Restore a thread context that was saved by _ST_SWITCH_CONTEXT  * or initialized by _ST_INIT_CONTEXT  */ #define _ST_RESTORE_CONTEXT(_thread) \  ST_BEGIN_MACRO \  _ST_SET_CURRENT_THREAD(_thread); \  MD_LONGJMP((_thread)->context, 1); \  ST_END_MACRO  void _st_vp_schedule(void) { _st_thread_t *thread; if (_ST_RUNQ.next != &_ST_RUNQ) { /* Pull thread off of the run queue */ thread = _ST_THREAD_PTR(_ST_RUNQ.next); _ST_DEL_RUNQ(thread); } else { /* If there are no threads to run, switch to the idle thread */ thread = _st_this_vp.idle_thread; } ST_ASSERT(thread->state == _ST_ST_RUNNABLE); /* Resume the thread */ thread->state = _ST_ST_RUNNING; _ST_RESTORE_CONTEXT(thread); } 

三、talk

  • task 编译成动态库和静态库 30分钟

     1
     2
     3
     4
     5  6  7  8  9 10 11 12 13 14 
    git clone https://github.com/wangcy6/state-threads.git cd state-threads make linux-debug //不同平台 state-threads/obj/ libst.so.1.9 libst.a cd examples/ make linux-debug //不同平台 EXAMPLES = $(OBJDIR)/lookupdns $(OBJDIR)/proxy $(OBJDIR)/server ./lookupdns www.baidu.com www.google.com www.bing.com 

项目应用

暂无

参考