Mail Archives: djgpp/2000/06/28/18:15:28
Cherniavsky Beni <cben AT crosswinds DOT net> wrote:
>
>Maybe you can setup the stack of the new thread in spawn_thread
()
>so that it will return into spawn_thread() - on the new stack.
I
>think that copying spawn_thread()'s stack frame to the new
stack would
>do the job, more or less. Then you have put the thread
termination
>code in spawn_thread() and have the thread's first function
just return
>to indicate termination. This resembles a unix-style fork()
called
>from spawn_thread(), which is clean (INHO) but I'm not sure
what's the
>implementation burden of this idea.
>
I like this idea. Here's a pseudo-code of what I'm thinking
spawn_thread(void (*da_func)(), int stack_size)
{
check if the caller of this function is the main thread
if it isn't then return;
modify the main thread's saved EIP to the content of the
current stack position;
allocate the new thread's stack;
change the new thread saved ESP to the end of the allocated
block;
place the address of end_thread() in the end of the allocated
block;
call da_func();
}
end_thread()
{
set uninterruptible flag;
do some cleaning up;
}
task_switcher()
{
check uninterruptible flag
if it's true then return;
do task switch;
}
There are some loose ends though. First, in spawn_thread() there
are no stack cleaning before calling the new thread, so when
there is a task switch to the main thread, the main thread will
likely fail to execute, 'cause C uses stacks for variables if
I'm not mistaken.
Second, if a task switch occurs between the return of a thread
and setting uninterruptible flag in end_trhread(), I dunno what
will happen.
Third, at the end of end_thread(), how to return to the current
working position of the main thread? I'm thinking of calling the
task_switcher() manually at this point.
I'm not familiar with *NIX multitasking. What yield() do? Where
is the difference between external and internal signals?
Thanks for the idea.
Got questions? Get answers over the phone at Keen.com.
Up to 100 minutes free!
http://www.keen.com
- Raw text -