Chinaunix首页 | 论坛 | 博客
  • 博客访问: 17451
  • 博文数量: 3
  • 博客积分: 27
  • 博客等级: 民兵
  • 技术积分: 15
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-15 16:51
文章分类
文章存档

2015年(1)

2013年(1)

2012年(1)

我的朋友
最近访客

分类:

2012-05-02 11:33:31

我在很早以前的一篇文章中介绍过如何用setjmp/longjmp实现简单的协作多任务,并且也提到用非透明的数据结构jmp_buf的内部成员来实现此项操作实属违规。前些天,偶然在一个讨论组上看到有人用ucontext实现用户级线程以规避内核级线程创建及调度的开销和纯异步编程中状态机的复杂性,突然觉得眼前一亮。如果联系到目前Linux内核正在开发的,用户空间程序通过协同的方式确实能够达到高效编程和高效执行的目的。比如读操作:
  1. 设置要读的文件描述符fd为非阻塞方式
  2. 调用读操作,如果成功,函数返回。否则,执行3
  3. 用select系列函数监听fd的可读事件,然后设置当前线程状态为阻塞,调用schedule让出CPU
  4. fd可读时将线程状态变成可运行,以供以后调度
所以,今天又重新实现了用户空间的协作多线程示例:

#include <stdlib.h>
#include <stdio.h>
#include <ucontext.h>

#define STACK_SIZE 4096
#define UTHREAD_MAX_NUM 256

typedef int uthread_t;
typedef void uthread_attr_t;
uthread_t current = 0;

struct uthread_struct
{
        int used;
        ucontext_t context;
        char stack[STACK_SIZE];
        void* (*func)(void *arg);
        void *arg;
        void *exit_status;
};

static struct uthread_struct uthread_slots[UTHREAD_MAX_NUM];

void panic(void)
{
                fprintf(stderr, "Panic, bala bala...\n");
                exit(EXIT_FAILURE);
}

void idle_thread(void)
{
        int i;

        for (i = 1; i < UTHREAD_MAX_NUM; i ++)
                if (uthread_slots[i].used)
                        break;

        if (i == UTHREAD_MAX_NUM)
                panic();
        if (current != 0)
                uthread_slots[current].used = 0;
        current = i;
        swapcontext(&uthread_slots[0].context, &uthread_slots[current].context);
}

void uthread_context_init(int tid)
{
        getcontext(&uthread_slots[tid].context);
        uthread_slots[tid].context.uc_stack.ss_sp = uthread_slots[tid].stack;
        uthread_slots[tid].context.uc_stack.ss_size = sizeof(uthread_slots[tid].stack);
        uthread_slots[tid].context.uc_link = &uthread_slots[0].context;
}

void uthread_init(void)
{
        uthread_context_init(0);
        uthread_slots[0].used = 1;
        makecontext(&uthread_slots[0].context, idle_thread, 0);
}

void uthread_schedule(void);

void uthread_exit(void *exit_status)
{
        uthread_slots[current].exit_status = exit_status;
        uthread_slots[current].used = 0;
        uthread_schedule();
}

void uthread_helper(void)
{
        uthread_exit(uthread_slots[current].func( uthread_slots[current].arg));
}

int uthread_create(uthread_t *thread, const uthread_attr_t *attr,
                        void* (*start_routine)(void*), void *arg)
{
        static int last_used = 0;
        int i;

        for (i = (last_used + 1) % UTHREAD_MAX_NUM; i != last_used;
                        i = (i + 1) % UTHREAD_MAX_NUM)
                if (!uthread_slots[i].used)
                        break;
        if (i == last_used)
                return -1;
        last_used = i;

        if (thread != NULL)
                *thread = i;
        uthread_context_init(i);
        uthread_slots[i].used = 1;
        uthread_slots[i].func = start_routine;
        uthread_slots[i].arg = arg;
        uthread_slots[i].exit_status = 0;
        makecontext(&uthread_slots[i].context, uthread_helper, 0);

        return 0;
}

void uthread_schedule(void)
{
        int i, prev;

        for (i = (current + 1) % UTHREAD_MAX_NUM; i != current;
                        i = (i + 1) % UTHREAD_MAX_NUM)
                if (uthread_slots[i].used)
                        break;
        if (i == current)
                panic();

        prev = current;
        current = i;
        swapcontext(&uthread_slots[prev].context, &uthread_slots[current].context);
}

void* thread(void *arg)
{
        int i;

        for (i = 0; 1; i ++) {
                printf("thread/%d(%s): i = %d\n", current, (char*)arg, i);
                uthread_create(NULL, NULL, thread, arg);
                uthread_schedule();
        }
}

int main(int argc, char *argv[])
{
        uthread_t tid;

        uthread_init();

        uthread_create(&tid, NULL, thread, "hw1");
        printf("tid is %d\n", tid);
        uthread_create(&tid, NULL, thread, "hw2");
        printf("tid is %d\n", tid);

        while (1)
                idle_thread();

        return 0;
}

阅读(1204) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:C语言Label取地址的方法

给主人留下些什么吧!~~