Gesendet: Donnerstag, 15. Juli 2021 um 21:21 Uhr Von: "Alexander Tormasov via users" users@lists.genode.org An: "Genode users mailing list" users@lists.genode.org Cc: "Alexander Tormasov" a.tormasov@innopolis.ru Betreff: Re: How to switch thread stack between threads?
So, to fix it I need during switch of context to non-local thread (setcontext() or even longjump() functions) I should update these data to current running stack.
user level switching is only valid within the same thread. The only way to do this is to do a local user level switch to a user level thread that immediately blocks the os level thread and wakes the os level thread, that is blocked in the same procedure and corresponds to the target user level thread. At wakeup that user level thread, which was blocked at the os level, reads the target user level thread and makes a local user level switch to it. The Mutex on which the user level threads blocks (at least its address) needs to be part of the context.
probably I give a wrong picture of operations
I did understand you the first time around. But I have to disappoint you. What you want is IMPOSSIBLE! At least in genode. Because you can't create such a context.
- I run arbitrary function with stack associated with first thread
Every stack implies (is bound to) an os level thread.
- I copy current context using getcontext and store it somewhere
You must store a pointer to an os level object that holds the os level thread (mutex is fine) with the context to be able to later resume in the correct context.
- I stop doing function from 1, by switching to another function/stack associated with thread 1
That is possible.
- after some time I create a new os thread and run some code inside it
- then inside 2 thread I take old context from 2 above and perform setcontext from inside 2 thread to replace current function with state in the thread to the first one
You can not use context from 2 in another thread. Alternatively you can reconstruct the call chain from the first thread in the second thread with Duffs Device (https://en.wikipedia.org/wiki/Duff%27s_device#See_also) And then you can construct a mirror context, which is the first context but in the second thread.
So, I don’t need mutexes and wait for something - I have a time gap between suspend of function in os 1 and it continuation on thread 2.
The mutexes have another purpose. They regulate the os level threads when user level threads yield.
during continuation I already switched to thread 2… just need to associate old stack with it (it contains state of function/stack from 1)
Genode users mailing list users@lists.genode.org https://lists.genode.org/listinfo/users