实现coroutine的symmetric context switch

main.c

#include <stdio.h>

#define STACK_SIZE 65536
#define REG_SIZE sizeof(long)

typedef void (*cothread_func)();

typedef struct
{
    unsigned long regs[3]; /* [0]:rip [1]:rbp [2]:rsp */
    char stack[STACK_SIZE];
} cothread_t;

void cothread_yield(cothread_t* from, cothread_t* to);

void cothread_create(cothread_t* thread, cothread_func func)
{
    thread->regs[0] = (unsigned long)func;
    thread->regs[1] = (unsigned long)&thread->stack[STACK_SIZE];
    thread->regs[2] = (unsigned long)&thread->stack[STACK_SIZE - REG_SIZE];
    *((unsigned long*)&thread->stack[STACK_SIZE - REG_SIZE]) = (unsigned long)func;
}

cothread_t mainThread;
cothread_t fThread;

#define TIMES 5

void f()
{
    int i;
    for (i = 0; i < TIMES; i++)
    {
        printf("f(): %d\n", i);
        cothread_yield(&fThread, &mainThread);
    }
}

int main()
{
    cothread_create(&fThread, f);
    int i;
    for (i = 0; i < TIMES; i++)
    {
        printf("main(): %d\n", i);
        cothread_yield(&mainThread, &fThread);
    }
    return 0;
}

yield.x86_64.s

.section        .text
.globl cothread_yield
cothread_yield:
        // save current rip/rbp/rsp
        mov (%rsp), %rax
        mov %rax, (%rdi)
        mov %rbp, 8(%rdi)
        mov %rsp, 16(%rdi)
        // load new rbp/rsp/rip
        mov 8(%rsi), %rbp
        mov 16(%rsi), %rsp
        mov (%rsi), %rax
        mov %rax, (%rsp)
        ret

测试

$ gcc -Wall main.c yield.x86_64.s -o test

$ ./test

main(): 0

f(): 0

main(): 1

f(): 1

main(): 2

f(): 2

main(): 3

f(): 3

main(): 4

f(): 4

时间: 2024-08-29 07:06:43

实现coroutine的symmetric context switch的相关文章

Context Switch Definition

A context switch (also sometimes referred to as a process switch or a task switch) is the switching of the CPU (central processing unit) from one process or thread to another. A process (also sometimes referred to as a task) is an executing (i.e., ru

压力测试衡量CPU的三个指标:CPU Utilization、Load Average和Context Switch Rate

分类: 4.软件设计/架构/测试 2010-01-12 19:58 34241人阅读 评论(4) 收藏 举报 测试loadrunnerlinux服务器firebugthread 上篇讲如何用LoadRunner监控Linux的性能指标 ,但是关于CPU的几个指标没有搞清楚,下面就详细说说. CPU Utilization 好理解,就是CPU的利用率,75%以上就比较高了(也有说法是80%或者更高).除了这个指标外,还要结合Load Average和Context Switch Rate来看,有可

context switch

In computing, a context switch is the process of storing and restoring the state (more specifically, the execution context) of a process or thread so that execution can be resumed from the same point at a later time. This enables multiple processes t

查看上下文切换的多的进程(find which process take the most context switch)

这是原文链接http://serverfault.com/questions/190049/find-out-which-task-is-generating-a-lot-of-context-switches-on-linux 主要就是用pidstat -w 查看上下文切换, [email protected]:~# pidstat -w 1 Linux 4.2.0-16-generic (wis-virtual-machine)    2016年04月08日  _i686_  (1 CPU)

Linux Context , Interrupts 和 Context Switching 说明【转】

转自:http://blog.csdn.net/tianlesoftware/article/details/6461207 一. 进程Context 定义 当一个进程在执行时, CPU的所有寄存器中的值.进程的状态以及堆栈中的内容,比如各个变量和数据,包括所有的寄存器变量.进程打开的文件.内存信息等.这些信息被称为该进程的上下文(Context). 一个进程的Context可以分为三个部分:用户级上下文.寄存器上下文以及系统级上下文: (1)用户级上下文: 正文.数据.用户堆栈以及共享存储区:

Context Switching on the Cortex-M3

http://coactionos.com/embedded%20design%20tips/2013/10/09/Tips-Context-Switching-on-the-Cortex-M3/ The ARM Cortex-M3 architecture is designed with special features to facilitate implementing a pre-emptive RTOS. The system code takes advantage of thes

Lua基础 coroutine —— Lua的多线程编程

Lua的coroutine 跟thread 的概念比较相似,但是也不完全相同.一个multi-thread的程序,可以同时有多个thread 在运行,但是一个multi-coroutines的程序,同一时间只能有一个coroutine 在运行,而且当前正在运行的coroutine 只有在被显式地要求挂起时,才会挂起.Lua的coroutine 是一个强大的概念,尽管它的几个主要应用都比较复杂. 1. Coroutine 基础 Lua将coroutine相关的所有函数封装在表coroutine 中

Multi-tasking RTOS for microprocessors with limited memory by saving only a single return address per task during context switching

A real-time operating system (RTOS) for use with minimal-memory controllers has a kernel for managing task execution, including context switching, a plurality of defined tasks, individual ones of the tasks having subroutines callable in nested levels

DYNAMIC CONTEXT SWITCHING BETWEEN ARCHITECTURALLY DISTINCT GRAPHICS PROCESSORS

FIELD OF INVENTION This invention relates to computer graphics processing, and more specifically to computer graphics processing using two or more architecturally distinct graphics processors. BACKGROUND OF INVENTION Many computing devices utilize hi