├── .dir-locals.el ├── .gitignore ├── Makefile ├── README.md ├── examples ├── pingpong.c ├── primes.c ├── spin.c └── yield.c ├── vireo.c └── vireo.h /.dir-locals.el: -------------------------------------------------------------------------------- 1 | ((nil 2 | (indent-tabs-mode . t) 3 | (tab-width . 8)) 4 | (c-mode 5 | (c-file-style . "bsd") 6 | (c-basic-offset . 8)) 7 | (shell-mode 8 | (sh-basic-offset . 8) 9 | (sh-indentation . 8)) 10 | (python-mode 11 | (indent-tabs-mode . nil)) 12 | ) 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | libvireo.so 2 | examples/* 3 | !examples/*.c 4 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -ggdb3 -Wall -Wextra -Werror 2 | 3 | targets = libvireo.so $(basename $(wildcard examples/*.c)) 4 | all: $(targets) 5 | 6 | libvireo.so: vireo.c vireo.h 7 | $(CC) $(CFLAGS) -fPIC -shared -o $@ $< -lrt 8 | 9 | examples/%: examples/%.c vireo.h | libvireo.so 10 | $(CC) $(CFLAGS) -I. -o $@ $< -L. -lvireo -Wl,-rpath,\$$ORIGIN/.. 11 | 12 | clean: 13 | rm -f $(targets) 14 | .PHONY: clean 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Vireo, a green thread library 2 | ============================= 3 | 4 | Vireo is a tiny C library implementing preemptible green threads 5 | (greenlets) for Linux. It is designed as an example of how to implement 6 | userspace threading, that is, without special kernel support or 7 | awareness of multiple threads. It is also a good example of how to use 8 | the setcontext(3) family of functions. 9 | 10 | Vireo's focus is on readability and pedagogy, not performance or 11 | complete correctness. In particular, the library does no error-checking 12 | for most system calls. 13 | 14 | This library was written for MIT's operating systems class, and the 15 | examples are taken from the examples / test cases in JOS, the teaching 16 | OS used in that class. The library itself is licensed under the 2-clause 17 | BSD license. 18 | -------------------------------------------------------------------------------- /examples/pingpong.c: -------------------------------------------------------------------------------- 1 | // Ping-pong a counter between two processes. 2 | // Only need to start one of these -- splits into two with fork. 3 | 4 | #include 5 | 6 | #include 7 | 8 | void pingpong(void); 9 | 10 | void 11 | umain(void) 12 | { 13 | int who = vireo_create(pingpong); 14 | 15 | // get the ball rolling 16 | printf("send 0 from %x to %x\n", vireo_getid(), who); 17 | vireo_send(who, 0); 18 | 19 | pingpong(); 20 | } 21 | 22 | void 23 | pingpong(void) 24 | { 25 | int who; 26 | while (1) { 27 | int i = vireo_recv(&who); 28 | printf("%x got %d from %x\n", vireo_getid(), i, who); 29 | if (i == 10) 30 | return; 31 | i++; 32 | vireo_send(who, i); 33 | if (i == 10) 34 | return; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /examples/primes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | void 7 | primeproc(void) 8 | { 9 | int i, id, p; 10 | 11 | // fetch a prime from our left neighbor 12 | p = vireo_recv(NULL); 13 | printf("%d ", p); 14 | 15 | // fork a right neighbor to continue the chain 16 | id = vireo_create(primeproc); 17 | if (id == -1) 18 | exit(0); 19 | 20 | // filter out multiples of our prime 21 | while (1) { 22 | i = vireo_recv(NULL); 23 | if (i % p) 24 | vireo_send(id, i); 25 | } 26 | } 27 | 28 | void 29 | umain(void) 30 | { 31 | int i, id; 32 | // fork the first prime procss in the chain 33 | id = vireo_create(primeproc); 34 | 35 | // feed all the integers through 36 | for (i = 2; ; i++) 37 | vireo_send(id, i); 38 | } 39 | -------------------------------------------------------------------------------- /examples/spin.c: -------------------------------------------------------------------------------- 1 | // Test preemption by forking off a child process that just spins forever. 2 | // Let it run for a couple time slices, then kill it. 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | void 10 | child(void) 11 | { 12 | printf("I am the child. Spinning...\n"); 13 | while (1) 14 | /* do nothing */; 15 | } 16 | 17 | void 18 | umain(void) 19 | { 20 | int env; 21 | 22 | printf("I am the parent. Forking the child...\n"); 23 | env = vireo_create(child); 24 | 25 | printf("I am the parent. Running the child...\n"); 26 | vireo_yield(); 27 | vireo_yield(); 28 | vireo_yield(); 29 | vireo_yield(); 30 | vireo_yield(); 31 | vireo_yield(); 32 | vireo_yield(); 33 | vireo_yield(); 34 | 35 | printf("I am the parent. Killing the child...\n"); 36 | vireo_destroy(env); 37 | } 38 | -------------------------------------------------------------------------------- /examples/yield.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | static void 6 | yield_thread(void) 7 | { 8 | int i; 9 | 10 | printf("Hello, I am environment %08x.\n", vireo_getid()); 11 | for (i = 0; i < 5; i++) { 12 | vireo_yield(); 13 | printf("Back in environment %08x, iteration %d.\n", 14 | vireo_getid(), i); 15 | } 16 | printf("All done in environment %08x.\n", vireo_getid()); 17 | } 18 | 19 | void 20 | umain(void) 21 | { 22 | int i; 23 | for (i = 0; i < 3; i++) { 24 | vireo_create(yield_thread); 25 | } 26 | vireo_exit(); 27 | } 28 | -------------------------------------------------------------------------------- /vireo.c: -------------------------------------------------------------------------------- 1 | /* vireo - Preemptible green threads in C for Linux 2 | * https://github.com/geofft/vireo 3 | * 4 | * Copyright (c) 2014 Alexander Chernyakhovsky 5 | * Copyright (c) 2014 Geoffrey Thomas 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #define _GNU_SOURCE 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | #include "vireo.h" 37 | 38 | #define UNUSED __attribute__((unused)) 39 | 40 | #define TIMERSIG SIGRTMIN 41 | 42 | // Environment structure, containing a status and a jump buffer. 43 | typedef struct Env { 44 | int status; 45 | ucontext_t state; 46 | int state_reentered; 47 | int ipc_sender; 48 | int ipc_value; 49 | } Env; 50 | 51 | // Increase NENV to get more greenlets 52 | #define NENV 1024 53 | 54 | // Status codes for the Env 55 | #define ENV_UNUSED 0 56 | #define ENV_RUNNABLE 1 57 | #define ENV_WAITING 2 58 | 59 | #define ENV_STACK_SIZE 16384 60 | 61 | static Env envs[NENV]; 62 | static int curenv; 63 | 64 | void umain(void); /* provided by user */ 65 | 66 | /* Define a "successor context" for the purpose of calling env_exit */ 67 | static ucontext_t exiter = {0}; 68 | 69 | /* Preemption timer */ 70 | timer_t timer; 71 | const struct itimerspec ts = { 72 | {0, 0}, 73 | {0, 100000000}, 74 | }; 75 | 76 | static void 77 | make_stack(ucontext_t *ucp) 78 | { 79 | // Reuse existing stack if any 80 | if (ucp->uc_stack.ss_sp) 81 | return; 82 | 83 | ucp->uc_stack.ss_sp = mmap( 84 | NULL, ENV_STACK_SIZE, PROT_READ | PROT_WRITE, 85 | MAP_ANONYMOUS | MAP_GROWSDOWN | MAP_PRIVATE, 86 | -1, 0); 87 | ucp->uc_stack.ss_size = ENV_STACK_SIZE; 88 | } 89 | 90 | int 91 | vireo_create(vireo_entry entry) 92 | { 93 | // Find an available environment 94 | int env; 95 | for (env = 0; (env < NENV); env++) { 96 | if (envs[env].status == ENV_UNUSED) { 97 | // This one is usable! 98 | break; 99 | } 100 | } 101 | if (env == NENV) { 102 | // No available environments found 103 | return -1; 104 | } 105 | envs[env].status = ENV_RUNNABLE; 106 | 107 | getcontext(&envs[env].state); 108 | make_stack(&envs[env].state); 109 | envs[env].state.uc_link = &exiter; 110 | makecontext(&envs[env].state, entry, 0); 111 | // Creation worked. Yay. 112 | return env; 113 | } 114 | 115 | static void 116 | vireo_schedule(void) 117 | { 118 | int attempts = 0; 119 | int candidate; 120 | while (attempts < NENV) { 121 | candidate = (curenv + attempts + 1) % NENV; 122 | if (envs[candidate].status == ENV_RUNNABLE) { 123 | curenv = candidate; 124 | /* Request delivery of TIMERSIG after 10 ms */ 125 | timer_settime(timer, 0, &ts, NULL); 126 | setcontext(&envs[curenv].state); 127 | } 128 | attempts++; 129 | } 130 | exit(0); 131 | } 132 | 133 | void 134 | vireo_yield(void) 135 | { 136 | envs[curenv].state_reentered = 0; 137 | getcontext(&envs[curenv].state); 138 | if (envs[curenv].state_reentered++ == 0) { 139 | // Save successful, find the next process to run. 140 | vireo_schedule(); 141 | } 142 | // We've re-entered. Do nothing. 143 | } 144 | 145 | void 146 | vireo_exit(void) 147 | { 148 | envs[curenv].status = ENV_UNUSED; 149 | vireo_schedule(); 150 | } 151 | 152 | void 153 | vireo_destroy(int env) 154 | { 155 | envs[env].status = ENV_UNUSED; 156 | } 157 | 158 | int 159 | vireo_getid(void) 160 | { 161 | return curenv; 162 | } 163 | 164 | int 165 | vireo_recv(int *who) 166 | { 167 | envs[curenv].status = ENV_WAITING; 168 | vireo_yield(); 169 | if (who) 170 | *who = envs[curenv].ipc_sender; 171 | return envs[curenv].ipc_value; 172 | } 173 | 174 | void 175 | vireo_send(int toenv, int val) 176 | { 177 | while (envs[toenv].status != ENV_WAITING) 178 | vireo_yield(); 179 | envs[toenv].ipc_sender = curenv; 180 | envs[toenv].ipc_value = val; 181 | envs[toenv].status = ENV_RUNNABLE; 182 | } 183 | 184 | static void 185 | preempt(int signum UNUSED, siginfo_t *si UNUSED, void *context UNUSED) 186 | { 187 | vireo_yield(); 188 | } 189 | 190 | static void 191 | enable_preemption(void) 192 | { 193 | struct sigaction act = { 194 | .sa_sigaction = preempt, 195 | .sa_flags = SA_SIGINFO, 196 | }; 197 | struct sigevent sigev = { 198 | .sigev_notify = SIGEV_SIGNAL, 199 | .sigev_signo = TIMERSIG, 200 | .sigev_value.sival_int = 0, 201 | }; 202 | 203 | sigemptyset(&act.sa_mask); 204 | sigaction(TIMERSIG, &act, NULL); 205 | timer_create(CLOCK_PROCESS_CPUTIME_ID, &sigev, &timer); 206 | } 207 | 208 | static void 209 | initialize_threads(vireo_entry new_main) 210 | { 211 | curenv = 0; 212 | 213 | getcontext(&exiter); 214 | make_stack(&exiter); 215 | makecontext(&exiter, vireo_exit, 0); 216 | 217 | vireo_create(new_main); 218 | setcontext(&envs[curenv].state); 219 | } 220 | 221 | int 222 | main(int argc UNUSED, char* argv[] UNUSED) 223 | { 224 | enable_preemption(); 225 | initialize_threads(umain); 226 | return 0; 227 | } 228 | -------------------------------------------------------------------------------- /vireo.h: -------------------------------------------------------------------------------- 1 | /* vireo - Preemptible green threads in C for Linux 2 | * https://github.com/geofft/vireo 3 | */ 4 | 5 | #ifndef VIREO_H 6 | #define VIREO_H 7 | 8 | #include 9 | 10 | // Start point of the green thread 11 | typedef void (*vireo_entry)(void); 12 | 13 | // Create (and mark as runnable) a new green thread. Will not be run 14 | // until at least the next call to env_yield(). Returns an identifier 15 | // for the green thread, or -1 if no new green thread can be created. 16 | int vireo_create(vireo_entry entry); 17 | 18 | // Yield to the next available green thread, possibly the current one 19 | void vireo_yield(void); 20 | 21 | // Indicate that we're done running 22 | void vireo_exit(void); 23 | 24 | // Kill another green thread 25 | void vireo_destroy(int id); 26 | 27 | // Get this environment's ID 28 | int vireo_getid(void); 29 | 30 | // Receive a value from another green thread 31 | int vireo_recv(int *who); 32 | 33 | // Send a value to another green thread 34 | void vireo_send(int dest, int val); 35 | 36 | #endif 37 | --------------------------------------------------------------------------------