I am writing a user space thread library. I have a struct that manages each thread. My threads are very simple, they take a function ptr and its arguments, and just run that function one time.
Each thread has a jmp_buf and I use setjmp and longjmp to switch between threads. One thing I cant figure out is how to tell when this function is finished.
For each thread I modify the jmpbuf in 2 ways.
- I edit the PC and set it to the function pointer, so the program counter goes there next.
- I also make each one have its own stack and edit SP so it points to that stack
So using my thread control struct I can switch between threads and have each one maintain state, but do not know how to tell when that function is finished. When it is finished i want to call a special exit() function I have.