List:Internals« Previous MessageNext Message »
From:Marc Alff Date:April 27 2011 5:14am
Subject:Re: Use of thread specific data with mysqld
View as plain text  
Hi Hiromichi

The performance schema also uses it's own thread local storage, with a 
dedicated thread key.

Examples of code from storage/perfschema/*.cc:

Init:

   if (pthread_key_create(&THR_PFS, destroy_pfs_thread))
     return NULL;

   THR_PFS_initialized= true;

Anything between init and destroy:

PFS_thread *pfs_thread= my_pthread_getspecific_ptr(PFS_thread*, THR_PFS);
my_pthread_setspecific_ptr(THR_PFS, pfs);

Destroy:

   /*
     Be careful to not delete un-initialized keys,
     this would affect key 0, which is THR_KEY_mysys,
   */
   if (THR_PFS_initialized)
   {
     my_pthread_setspecific_ptr(THR_PFS, NULL);
     pthread_key_delete(THR_PFS);
     THR_PFS_initialized= false;
   }

Note the code around THR_PFS_initialized: this was found the hard way, 
destroying a key that was not properly created caused all sort of chaos, 
so calls to pthread_key_create() and pthread_key_delete() have to be 
*really* well balanced.

I hope this helps.

Regards,
-- Marc


On 4/26/11 1:19 PM, Hiromichi Watari wrote:
> Hi Sergei,
> I tried but I just couldn't make my own thread specific data to coexist with
> structure st_my_thread_var so I ended up moving my data to the structure and used it
> instead.
> I suppose there is really no reason to reinvent the wheel, is there ?
> But anyway, thank you for your help and you are the best.
> Hiromichi
>
>
> --- On Sun, 4/24/11, Sergei Golubchik<serg@stripped>  wrote:
>
>> From: Sergei Golubchik<serg@stripped>
>> Subject: Re: Use of thread specific data with mysqld
>> To: "Hiromichi Watari"<hiromichiwatari@stripped>
>> Cc: internals@stripped
>> Date: Sunday, April 24, 2011, 5:04 PM
>> Hi, Hiromichi!
>>
>> On Apr 24, Hiromichi Watari wrote:
>>> Hi,
>>> I'm trying to use thread specific data with mysqld but
>> ran into a problem with my_errno generating Segmentation
>> fault at the following line.
>>> I know that my_errno is also thread specific data but
>> not familiar with its implementation.
>>> I'm using pthread_key_create(), pthread_setspecific()
>> and pthread_getspecific() for my own thread specific data,
>> is there something I should be aware of ?
>>> Thanks,
>>> Hiromichi
>>>
>>> p.s. I ran into this problem only if I try to use my
>> own thread specific data, of course.
>>
>> probably you forgot to call my_thread_init() in the
>> beginning of your
>> thread function.
>>
>> Anyway, here's the code:
>>
>> #define my_errno my_thread_var->thr_errno
>>
>> #define my_thread_var (_my_thread_var())
>>
>> struct st_my_thread_var *_my_thread_var(void)
>> {
>>    return  my_pthread_getspecific(struct
>> st_my_thread_var*,THR_KEY_mysys);
>> }
>>
>> my_bool my_thread_init(void)
>> {
>>    ...
>>      if (!(tmp= (struct st_my_thread_var *)
>> calloc(1, sizeof(*tmp))))
>>      {
>>        error= 1;
>>        goto end;
>>      }
>>      pthread_setspecific(THR_KEY_mysys,tmp);
>>    ...
>> }
>>
>> Regards,
>> Sergei
>>

Thread
Use of thread specific data with mysqldHiromichi Watari24 Apr
  • Re: Use of thread specific data with mysqldSergei Golubchik24 Apr
    • Re: Use of thread specific data with mysqldHiromichi Watari26 Apr
      • Re: Use of thread specific data with mysqldMarc Alff27 Apr
        • Re: Use of thread specific data with mysqldHiromichi Watari28 Apr
          • Re: Use of thread specific data with mysqldMats Kindahl3 May
            • Re: Use of thread specific data with mysqldHiromichi Watari4 May
            • Re: Use of thread specific data with mysqldMichael Widenius4 May
              • Regression test returning different result each time it's runHiromichi Watari15 Jun
                • Re: Regression test returning different result each time it's runØystein Grøvlen16 Jun
                  • Re: Regression test returning different result each time it's runHiromichi Watari16 Jun
                    • Re: Regression test returning different result each time it's runØystein Grøvlen16 Jun
                      • Re: Regression test returning different result each time it's runRick James16 Jun
                        • Re: Regression test returning different result each time it's runHiromichi Watari18 Jun
                      • Re: Regression test returning different result each time it's runHiromichi Watari18 Jun
                        • Name of function to execute mysql commandHiromichi Watari21 Jan