Pthreads, setjmp, longjmp. How can you find out when a function is completed?

I am writing a user space thread library. I have a structure that controls each thread. My threads are very simple, they take the ptr function and its arguments and just run that function once.

Each stream has jmp_buf, and I use setjmp and longjmp to switch between streams. One thing I cannot understand is to find out when this function is completed.

For each thread, I modify jmpbuf in two ways.

  • I edit the PC and set it to the function pointer, so the program counter goes there.
  • I also create my own stack and edit the SP to point to this stack

Thus, using my flow control structure, I can switch between flows and maintain the state of each of them, but I don’t know how to say when this function will be completed. When it's finished, I want to call the special function exit () that I have.

+4
source share
3 answers

You can change the return address on the stack to point to the exit () function, or wrap the function call with another function that calls exit () after it.

+2
source

Instead of modifying your computer to a user-defined function, you should actually call some special function (let it be called run_thread() ), which will go to this stream input function. When this input function returns (i.e., the thread exited), run_thread() should do any work to indicate that this thread has completed (possibly removing this thread control block from the planning list and adding it to the connection () cleanup list) . Then it can yield, and when the parent calls join () in its ID, it will be cleared.

+2
source

It will try to return to where it was originally called - presumably your create_thread function.

0
source

All Articles