co_routine(第一部分 : 辅助函数)—— libco源码分析、学习笔记

博客一级目录

二级目录——libco源码分析/学习笔记

由于本源代码蛮长的,所以按照功能划分模块来分析,分为若干部分,详见二级目录↑

代码文件:co_routine.hco_routine.cppco_routine_inner.h

二、辅助函数

static unsigned long long counter(void);//主要是调用rdtscp这条汇编指令,将计数(来一个时钟脉冲+1)读出来。 将总共的时钟脉冲数读出再除以cpu的频率(每秒时钟脉冲)就是时间。虽然对于变频来说频率会变的,但这里不会有影响,原因在表格下面详细说明。

static unsigned long long getCpuKhz();//读取配置文件,获得cpu频率,返回配置文件mhz*1000

static unsigned long long GetTickMS();//读取1970年1月1日到现在的时间,单位毫秒
static pid_t GetPid();//读取tid  获取线程id

template <class T,class TLink>

void RemoveFromLink(T *ap);//将一个节点从链表中删除,此节点数据结构中包含前驱、链表等必要信息

template <class TNode,class TLink>

void inline AddTail(TLink*apLink,TNode *ap);//push_back,要注意pLink(原来链表)一定为NULL,不然不会做任何操作。

template <class TNode,class TLink>

void inline PopHead( TLink*apLink );//如函数名

template <class TNode,class TLink>

void inline Join( TLink*apLink,TLink *apOther );//将链表apOther插入到apLink之后

stStackMem_t* co_alloc_stackmem(unsigned int stack_size);//向os申请stack_size大小的栈并初始化,返回一个栈结构体指针

stShareStack_t* co_alloc_sharestack(int count, int stack_size);//向os申请一个share栈并初始化,返回相应类型的结构体指针,参数表示内部的栈队列的数量和大小。

static stStackMem_t* co_get_stackmem(stShareStack_t* share_stack);//从一个share栈的栈队列中取下一个栈并返回指针。

ps:share栈推测是栈的池结构。

未完待续。。。

对于counter()中rdtscp汇编指令频率不恒定的问题:

查询INTEL,有:

  1.The time stamp counter in newer processors may support an enhancement, referred to as invariant TSC. 

Processor’s support for invariant TSC is indicated by CPUID.80000007H:EDX[8].   

  2.The invariant TSC will run at a constant rate in all ACPI P-, C-. and Tstates. This is the architectural behavior 

moving forward. On processors with invariant TSC support, the OS may use the TSC for wall clock timer 

services (instead of ACPI or HPET timers). TSC reads are much more efficient and do not incur the overhead 

associated with a ring transition or access to a platform resource.  

也就是说rdtsc或rdtscp不用考虑CPU的变频问题,他会以固定的速率增加。

stCoRoutine_t *co_self();

stCoRoutine_t *GetCurrThreadCo( );

两个函数功能相同,其中co_self直接调用GetCurrThreadCo。

获取正在执行的(当然就是调用者自己喽)协程控制块。一般不会在main协程里调用(无意义)。

void OnCoroutineEvent( stTimeoutItem_t * ap );
将ap中pArg保存的stCoRoutine_t*取出,赋予执行权。pArg用于保存回到co_poll_inner的协程控制字指针。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值