samsquire / preemptible-thread

How to preempt threads in user space

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

preemptible-thread

This idea comes from inspiration from Stephen from this Stackoverflow question : how to preempt a thread from userspace

How to preempt threads in user space?

You set the looping variable to the limit! You can therefore implement an M:N scheduler with this pattern. I've written this multiplexer in Java, C and Rust. See preemtible-thread-evented.c and preemtible-thread.rs and the Java Main.java and Scheduler.java.

This is a userspace thread scheduler. It switches between green threads very fast as frequently as you want it to.

Please note, I would use nanosleep nowadays in C as I do in my other C projects projects.

This scheduler cannot preempt arbitrary code, it requires no cooperation at descheduling time - only you register the memory locations of the loop variable.

These code examples show preempting a hot loop in a green thread in Rust, C and Java.

Rather than introducing an if statement into every iteration of the loop that slows down the loop, we update the loop invariants to cancel the loop. This approach can be used to create cancellable APIs.

api

register_loop takes place of your loop definition by providing an original value and a limit and puts it into a struct visible to other threads.

Warning

This takes avantage of a data race. But you might be fine with it if your algorithm uses a cached value of the loop iterating variable and not checks it otherwise it could change halfway through the loop.

preemptible-thread-evented.c

This is preeemptible threads with threads for IO events. Normally you would use libev or libuv but I thought I could combine my preemptible green threads with IO scheduled on IO threads.

Multiple green threads schedule events to read from the file system and the IO threads handle the blocking reads.

I should probably change IO callbacks to be enqueued onto the RingBuffer as they could currently run at the same time as a lightweight user thread.

LICENCE

With the exception of the ringbuffer code in -evented.c

BSD Zero Clause License

Copyright (C) 2023 by Samuel Michael Squire sam@samsquire.com

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

About

How to preempt threads in user space


Languages

Language:C 74.6%Language:Java 18.2%Language:Rust 4.9%Language:Assembly 1.8%Language:Makefile 0.4%