setjmpとlongjmpでユーザレベルスレッドを作る
「C言語のsetjmpとlongjmpがあればスレッドは作れる」
という話を聞いたので実際にやってみました。
まずは、完成品の使用例から。
#include <stdio.h>
#include <stdlib.h>
#include "ahothread.h"
void f1(void *args)
{
int i, j;
char *str = (char*)args;
for (i=0; i<3; i++) {
puts(str);
for (j=0; j<10000000; j+= 1 + rand()%2);
}
}
int main(int argc, char **argv)
{
int t1, t2;
ahothread_init();
t1 = ahothread_create(f1, "aho");
t2 = ahothread_create(f1, "baka");
ahothread_join(t1);
ahothread_join(t2);
return 0;
}
ahoとbakaが交互に出ます。多分。
ループの中でrand()を呼ぶことにより時間稼ぎをしているので、
環境によっては交互じゃない可能性もあります。
次は、スレッドの中身で、もっとも重要な部分。
int ahothread_create(void (*func)(void*), void *args)
{
int id = search_free_thread();
thread_table[id].state = ALIVE;
if (setjmp(thread_table[current_thread].jb) == 0) {
if (id > current_thread) {
alloca((id-current_thread) * FRAME_SIZE);
}
else {
assert(0);
}
current_thread = id;
func(args);
}
return id;
}
void ahothread_yield()
{
if (setjmp(thread_table[current_thread].jb) == 0) {
int next = search_alive_thread();
current_thread = next;
longjmp(thread_table[next].jb, 1);
}
}
ahothread_createは、setjmpで現在の状態を記憶し、
allocaでスタックポインタを適当に押し上げた後に、(別のスレッドとして)目的の関数を呼びます。
ahothread_yieldは、setjmpで現在の状態を記憶し、
適当な(多くの場合、現在とは別の)スレッドを探し、longjmpで制御を移します。
ahothread_yieldを定期的に呼ぶことにより、実行するスレッドが定期的に切り替わります。
最後に、全部のソースを載せておきますが、
かなりやっつけで作ったのでバグが沢山埋もれている可能性があります。
#include <stdlib.h>
#include <setjmp.h>
#include <assert.h>
#include <signal.h>
#include <sys/time.h>
#define MAX_THREAD 32
#define FRAME_SIZE 10240
#define DEAD 0
#define ALIVE 1
#define JOIN 2
struct thread_data {
jmp_buf jb;
int state;
int wait;
};
struct thread_data thread_table[MAX_THREAD];
static int current_thread = 0;
static int num_thread = 1;
static int search_alive_thread()
{
int i;
for (i=current_thread+1; i<num_thread; i++) {
if (thread_table[i].state == ALIVE) {
return i;
}
}
for (i=0; i<=current_thread; i++) {
if (thread_table[i].state == ALIVE) {
return i;
}
}
assert(0);
return -1;
}
static int search_free_thread()
{
int i = num_thread++;
assert(i < MAX_THREAD);
return i;
}
static int search_join_thread(int id)
{
int i;
for (i=0; i<num_thread; i++) {
if (thread_table[i].state == JOIN &&
thread_table[i].wait == id) {
return i;
}
}
return -1;
}
void ahothread_yield()
{
if (setjmp(thread_table[current_thread].jb) == 0) {
int next = search_alive_thread();
current_thread = next;
longjmp(thread_table[next].jb, 1);
}
}
void ahothread_join(int id)
{
if (thread_table[id].state != DEAD) {
thread_table[current_thread].state = JOIN;
thread_table[current_thread].wait = id;
ahothread_yield();
}
}
void ahothread_exit()
{
int join;
if (current_thread == 0) {
exit(0);
}
join = search_join_thread(current_thread);
if (join != -1) {
thread_table[join].state = ALIVE;
}
thread_table[current_thread].state = DEAD;
ahothread_yield();
}
int ahothread_create(void (*func)(void*), void *args)
{
int id = search_free_thread();
thread_table[id].state = ALIVE;
if (setjmp(thread_table[current_thread].jb) == 0) {
if (id > current_thread) {
alloca((id-current_thread) * FRAME_SIZE);
}
else {
assert(0);
}
current_thread = id;
func(args);
}
return id;
}
static void ahothread_handler(int sig)
{
if (num_thread > 1) {
ahothread_yield();
}
}
void ahothread_init()
{
struct sigaction sa = {
.sa_handler = ahothread_handler,
.sa_flags = SA_RESTART
};
struct itimerval it = {};
thread_table[0].state = ALIVE;
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 100000;
it.it_value = it.it_interval;
sigemptyset(&sa.sa_mask);
sigaction(SIGALRM, &sa, NULL);
setitimer(ITIMER_REAL, &it, 0);
}