tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: ucontext



On Tue Dec 15 2009 at 01:51:02 +0000, Sad Clouds wrote:
> On Tuesday 15 December 2009 00:32:21 Joerg Sonnenberger wrote:
> > On Mon, Dec 14, 2009 at 10:51:46PM +0000, Sad Clouds wrote:
> > > Can anyone tell me what ucontext(2) and related functions can be used
> > > for? Reading man pages it seems to suggest that you can get and set
> > > threads context. Would this be similar to creating a new thread with
> > > pthread_create()?

pthread threads are context + scheduling.

> Below is the extract from makecontext(3). Can anyone elaborate on that? What 
> limitations and what would happen when calling makecontext from a threaded 
> program?
> 
> Due to limitations in the current pthread implementation, makecontext should 
> not be used in programs which link against the pthread(3) library (whether 
> threads are used or not).

You could simply try it out with a few-line program to be sure.
But here's what I recall:

libpthread expects to find pthread_t for a thread at the top of a stack.
makecontext() itself won't do anything, but when you swapcontext()
or setcontext() to the new context, your stack will change to somewhere
without a pthread_t at the top.  When the program hits e.g. a libc routine
requiring locking, pthread_mutex is called (due to how libc mutexes are
implemented) and boom happens.


Home | Main Index | Thread Index | Old Index