I was curious how user-space threads library could be implemented so i built a minimalistic library that does so. i was inspired by this great blog post that helped me to get the idea of one of the ways this can be achieved. this is an educational project of course and not practical for production usage.
- Non-blocking IO:
- write()
- read()
- open()
- close()
- Preemptive scheduling
- Multiple IO workers
- Scheduler per core
void coop2(void *args) {
int fd = coop_open("example.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
coop_print("coop2: writing to file\n");
const char* buf = "Hello";
coop_write(fd, buf, 5);
coop_close(fd);
fd = coop_open("example.txt", O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
coop_print("coop2: reading from file\n");
char res[6];
coop_read(fd, res, 6);
coop_close(fd);
coop_print(res);
}
void coop1(void* args) {
coop(coop2, NULL);
for (int i = 0; i < 4; i++) {
coop_print("coop1: Hey\n");
}
}
int main(int argc, char**argv) {
coop(coop1, NULL);
}
// Output:
// coop1: Hey
// coop1: Hey
// coop2: writing to file
// coop1: Hey
// coop1: Hey
// coop2: reading from file
// Hello