前段时间实现的C协程依赖栈传递参数,在开启优化时会导致错误,于是实现了一个ucontext的版本,但ucontext的切换效率太差了,

在我的机器上执行4000W次切换需要11秒左右,这达不到我的要求,所以重新设计了实现,使得在开启优化时也能得到正确的结果.

并且效率也令人满意,4000W次切换仅需要730ms左右,足足比ucontext的实现快乐近15倍。

下面贴出实现:

 #include "uthread.h"
#include <stdlib.h>
#include <ucontext.h>
#include <pthread.h>
#include "link_list.h" struct uthread
{
int32_t reg[];//0:esp,1:ebp,2:eax,3:ebx,4:ecx,5:edx,6:edi,7:esi
void *para;
uthread_t parent;
void*(*main_fun)(void*);
void *stack;
int32_t ssize;
int8_t first_run;
}; #ifdef _DEBUG
//for debug version
void uthread_main_function()
{
int32_t arg;
__asm__ volatile(
"movl %%eax,%0\t\n"
:
:"m"(arg)
); uthread_t u = (uthread_t)arg;
void *ret = u->main_fun(u->para);
if(u->parent)
uthread_switch(u,u->parent,ret);
else
exit();
}
#else
//for release version
void __attribute__((regparm())) uthread_main_function(void *arg)
{
uthread_t u = (uthread_t)arg;
void *ret = u->main_fun(u->para);
if(u->parent)
uthread_switch(u,u->parent,ret);
else
exit();
}
#endif
uthread_t uthread_create(uthread_t parent,void*stack,uint32_t stack_size,void*(*fun)(void*))
{
uthread_t u = (uthread_t)calloc(,sizeof(*u));
u->parent = parent;
u->main_fun = fun;
u->stack = stack;
u->ssize = stack_size;
if(stack)
{
u->reg[] = (int32_t)stack+stack_size-;
u->reg[] = (int32_t)stack+stack_size-;
}
if(u->main_fun)
u->first_run = ;
return u;
} void uthread_destroy(uthread_t *u)
{
free(*u);
*u = NULL;
} #ifdef _DEBUG
void* __attribute__((regparm())) uthread_switch(uthread_t from,uthread_t to,void *para)
{
if(!from)
return NULL;
to->para = para;
int32_t esp,ebp,eax,ebx,ecx,edx,edi,esi;
//save current registers
//the order is important
__asm__ volatile(
"movl %%eax,%2\t\n"
"movl %%ebx,%3\t\n"
"movl %%ecx,%4\t\n"
"movl %%edx,%5\t\n"
"movl %%edi,%6\t\n"
"movl %%esi,%7\t\n"
"movl %%ebp,%1\t\n"
"movl %%esp,%0\t\n"
:
:"m"(esp),"m"(ebp),"m"(eax),"m"(ebx),"m"(ecx),"m"(edx),"m"(edi),"m"(esi)
);
from->reg[] = esp;
from->reg[] = ebp;
from->reg[] = eax;
from->reg[] = ebx;
from->reg[] = ecx;
from->reg[] = edx;
from->reg[] = edi;
from->reg[] = esi;
if(to->first_run)
{
to->first_run = ;
esp = to->reg[];
//use eax to pass arg
eax = (int32_t)to;
__asm__ volatile (
"movl %1,%%eax\t\n"
"movl %0,%%ebp\t\n"
"movl %%ebp,%%esp\t\n"
:
:"m"(esp),"m"(eax)
);
uthread_main_function();
}
else
{
esp = to->reg[];
ebp = to->reg[];
eax = to->reg[];
ebx = to->reg[];
ecx = to->reg[];
edx = to->reg[];
edi = to->reg[];
esi = to->reg[];
//the order is important
__asm__ volatile (
"movl %2,%%eax\t\n"
"movl %3,%%ebx\t\n"
"movl %4,%%ecx\t\n"
"movl %5,%%edx\t\n"
"movl %6,%%edi\t\n"
"movl %7,%%esi\t\n"
"movl %1,%%ebp\t\n"
"movl %0,%%esp\t\n"
:
:"m"(esp),"m"(ebp),"m"(eax),"m"(ebx),"m"(ecx),"m"(edx),"m"(edi),"m"(esi)
);
}
return from->para;
}
#else
void* __attribute__((regparm())) uthread_switch(uthread_t from,uthread_t to,void *para)
{
if(!from)
return NULL;
to->para = para;
int32_t esp,ebp,edi,esi;
//save current registers
//the order is important
__asm__ volatile(
"movl %%eax,%2\t\n"
"movl %%ebx,%3\t\n"
"movl %%ecx,%4\t\n"
"movl %%edx,%5\t\n"
"movl %%edi,%6\t\n"
"movl %%esi,%7\t\n"
"movl %%ebp,%1\t\n"
"movl %%esp,%0\t\n"
:
:"m"(from->reg[]),"m"(from->reg[]),"m"(from->reg[]),"m"(from->reg[])
,"m"(from->reg[]),"m"(from->reg[]),"m"(from->reg[]),"m"(from->reg[])
);
if(to->first_run)
{
to->first_run = ;
//change stack
//the order is important
__asm__ volatile (
"movl %0,%%ebp\t\n"
"movl %%ebp,%%esp\t\n"
:
:"m"(to->reg[])
);
uthread_main_function((void*)to);
}
else
{
esp = to->reg[];
ebp = to->reg[];
edi = to->reg[];
esi = to->reg[];
//the order is important
__asm__ volatile (
"movl %2,%%eax\t\n"
"movl %3,%%ebx\t\n"
"movl %4,%%ecx\t\n"
"movl %5,%%edx\t\n"
"movl %6,%%edi\t\n"
"movl %7,%%esi\t\n"
"movl %1,%%ebp\t\n"
"movl %0,%%esp\t\n"
:
:"m"(esp),"m"(ebp),"m"(to->reg[]),"m"(to->reg[])
,"m"(to->reg[]),"m"(to->reg[]),"m"(edi),"m"(esi)
);
}
return from->para;
}
#endif

test.c

 #include <stdio.h>
#include "uthread.h"
#include "SysTime.h"
#include <stdlib.h>
void* ufun2(void *arg)
{
printf("ufun2\n");
char **tmp = (char**)arg;
uthread_t self = (uthread_t)tmp[];
uthread_t parent = (uthread_t)tmp[];
volatile void *ptr = self;
while(ptr)
{
ptr = uthread_switch(self,parent,NULL);
}
return NULL;
} char *stack1;
char *stack2; void* ufun1(void *arg)
{
uthread_t self = (uthread_t)arg;
uthread_t u = uthread_create(self,stack2,,ufun2);
char* _arg[];
_arg[] = (char*)u;
_arg[] = (char*)self;
int i = ;
uint32_t tick = GetSystemMs();
for( ; i < ; ++i)
{
uthread_switch(self,u,&_arg[]);
}
printf("%d\n",GetSystemMs()-tick);
uthread_switch(self,u,NULL);
return arg;
} int main()
{
stack1 = (char*)malloc();
stack2 = (char*)malloc();
/*
* if use ucontext version
char dummy_stack[4096];
uthread_t p = uthread_create(NULL,dummy_stack,0,NULL);
*/
uthread_t p = uthread_create(NULL,NULL,,NULL);
uthread_t u = uthread_create(p,stack1,,ufun1);
uthread_switch(p,u,u);
printf("main end\n");
return ;
};

转自:https://www.cnblogs.com/sniperHW/archive/2012/08/05/2624334.html

(转)C协程实现的效率对比的更多相关文章

  1. python3 - 多线程和协程速率测试对比

    多线程和协程都属于IO密集型,我通过以下用例测试多线程和协程的实际速率对比. 实例:通过socket客户端以多线程并发模式请求不同服务器端(这里服务器端分2种写法:第一种服务器通过协程实现,第二种服务 ...

  2. 初学Python——协程

    进程.线程和协程区分 我们通常所说的协程Coroutine其实是corporate routine的缩写,直接翻译为协同的例程,一般我们都简称为协程. 在linux系统中,线程就是轻量级的进程,而我们 ...

  3. {python之协程}一 引子 二 协程介绍 三 Greenlet 四 Gevent介绍 五 Gevent之同步与异步 六 Gevent之应用举例一 七 Gevent之应用举例二

    python之协程 阅读目录 一 引子 二 协程介绍 三 Greenlet 四 Gevent介绍 五 Gevent之同步与异步 六 Gevent之应用举例一 七 Gevent之应用举例二 一 引子 本 ...

  4. python2.0_s12_day9_协程&Gevent协程

    Python之路,Day9 - 异步IO\数据库\队列\缓存 本节内容 Gevent协程 Select\Poll\Epoll异步IO与事件驱动 Python连接Mysql数据库操作 协程 1.协程,又 ...

  5. python爬虫——多线程+协程(threading+gevent)

    上一篇博客中我介绍了如何将爬虫改造为多进程爬虫,但是这种方法对爬虫效率的提升不是非常明显,而且占用电脑cpu较高,不是非常适用于爬虫.这篇博客中,我将介绍在爬虫中广泛运用的多线程+协程的解决方案,亲测 ...

  6. Python多线程、多进程和协程的实例讲解

    线程.进程和协程是什么 线程.进程和协程的详细概念解释和原理剖析不是本文的重点,本文重点讲述在Python中怎样实际使用这三种东西 参考: 进程.线程.协程之概念理解 进程(Process)是计算机中 ...

  7. 百万年薪python之路 -- 并发编程之 协程

    协程 一. 协程的引入 本节的主题是基于单线程来实现并发,即只用一个主线程(很明显可利用的cpu只有一个)情况下实现并发,为此我们需要先回顾下并发的本质:切换+保存状态 cpu正在运行一个任务,会在两 ...

  8. tbox新增stackless协程支持

    tbox之前提供的stackfull协程库,虽然切换效率已经非常高了,但是由于每个协程都需要维护一个独立的堆栈, 内存空间利用率不是很高,在并发量非常大的时候,内存使用量会相当大. 之前考虑过采用st ...

  9. Python 原生协程------asyncio

    协程 在python3.5以前,写成的实现都是通过生成器的yield from原理实现的, 这样实现的缺点是代码看起来会很乱,于是3.5版本之后python实现了原生的协程,并且引入了async和aw ...

随机推荐

  1. 使用Highcharts实现柱状图展示

    第一步 新建页面line.html,引入HighCharts核心js文件 <script type="text/javascript" src="../../js/ ...

  2. urllib.urlencode() 无法encode中文, UnicodeEncodeError

    urllib.urlencode() 无法encode中文, UnicodeEncodeError, 具体错误内容如下:File "/System/Library/Frameworks/Py ...

  3. php解析xml文件为数组

    $xml = simplexml_load_file($fullfilename); $arr = json_decode(json_encode($xml),true); echo "&l ...

  4. Unknown Entity namespace alias 'BaseMemberBundle'.

    $em = $this->getDoctrine()->getManager('member');//要记得写上member $repo = $em->getRepository(' ...

  5. 磁盘检测SMART工具

    题记: 做过一些关于硬盘的调研任务,当时搜集很多资料,不过现在没有,从网上找了一篇关于SMART的介绍,感觉基本上都是比较全面了. 首先各大硬盘厂商生产的硬盘基本都是会遵循SMART的技术标准的,当然 ...

  6. python3 计算文件夹中所有py文件里面代码行数,注释行数,空行数

    import os,re #代码所在位置 FILE_PATH = './' def analyze_code(codefilesource): ''' 打开一个py文件统计其中的代码行数,包括空格和注 ...

  7. Linux基础系列:常用命令(8)_shell script

    一.什么是shell script 将OS命令堆积到可执行的文件里,由上至下的顺序执行文本里的OS命令 就是脚本了. 再加上些智能(条件/流控)控制,就变成了智能化脚本了 二.变量命名规则 以字母或下 ...

  8. Redis缓存全自动安装shell脚本

    我只是把命令放到shell文件中了,方便安装,代码如下: #!/bin/bash # shell的执行选项: # -n 只读取shell脚本,但不实际执行 # -x 进入跟踪方式,显示所执行的每一条命 ...

  9. jquery 如何获取单选框的值

    jquery 如何获取单选框的值   获取单选框的值有三种方式: 1.$('input:radio:checked').val():2.$("input[type='radio']:chec ...

  10. HTTP基础概念讲解

    HTTP基础概念讲解 作者:Danbo 时间:2016-03-17 1.1.http协议头部:curl -I www.meituan.com 1.2.静态和动态 静态网页:纯HTML格式的网页,后台没 ...