Implementing TLS: step 1

Igor Sysoev is at rambler-co.ru
Fri Jun 20 11:27:15 PDT 2003


On Fri, 20 Jun 2003, Julian Elischer wrote:

> On Fri, 20 Jun 2003, Igor Sysoev wrote:
> 
> > If we could sacrifice our current thread compatibility in 5.0-5.1
> > then we could change kse_mailbox from
> > 
> >         struct kse_mailbox {
> >            int                     km_version;     /* Mailbox version */
> >            struct kse_thr_mailbox  *km_curthread;  /* Current thread */
> >            ...
> > 
> > to
> >      
> >         struct kse_mailbox { 
> >            struct kse_thr_mailbox  *km_curthread;  /* Current thread */
> >            int                     km_version;     /* Mailbox version */
> >            ...
> > 
> > then x86's gs would still point to kse_mailbox, and gs:[0] would be
> > and TP pointer.  Also we need to modify
> > 
> >      struct kse_thr_mailbox {
> > +          void              *tls;
> >            ucontext_t        tm_context;     /* User thread context */
> >            ...
> > 
> > And the static TLS must be allocated before kse_thr_mailbox.
> 
> Yes.
> This is what I was thinking...

I'm wrong, this scheme does not allow to use the 1:1 model in libkse
when kse_mailbox.km_curthread is always NULL.

We can implement such scheme on x86:

gs -> [ TP                 ] ---> [ TLS                    ]
      [ struct kse_mailbox ]  +-> [ struct kse_thr_mailbox ]
      [      .km_curthread ] -+

When UTS would switch to the next thread it should set thread's TLS:

     kse_mailbox.km_curthread = NULL;
     gs:[0] = next_thr_tls;
     kse_mailbox.km_curthread = next_kse_thr_mailbox;

kse_mailbox can be accessed via gs register. On amd64 scheme is the same
except the use of fs register instead of gs.

However on sparc64, ia64 and alpha TP is in the register but not
in the memory so we need introduce the new field tm_kse in kse_thr_mailbox
to find kse_mailbox:

TP -> [ TLS                    ]
      [ struct kse_thr_mailbox ]
      [                .tm_kse ] ---> [ struct kse_mailbox ]


By the way how was kse_mailbox being found before gs register was used ?


Igor Sysoev
http://sysoev.ru/en/



More information about the freebsd-threads mailing list