Back to index

glibc  2.9
Functions | Variables
specific.c File Reference
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include "pthread.h"
#include "internals.h"
#include "spinlock.h"
#include "restart.h"
#include <bits/libc-lock.h>
#include <not-cancel.h>

Go to the source code of this file.

Functions

int __pthread_key_create (pthread_key_t *key, destr_function destr)
 strong_alias (__pthread_key_create, pthread_key_create)
static void pthread_key_delete_helper (void *arg, pthread_descr th)
int pthread_key_delete (pthread_key_t key)
int __pthread_setspecific (pthread_key_t key, const void *pointer)
 strong_alias (__pthread_setspecific, pthread_setspecific)
 strong_alias (__pthread_getspecific, pthread_getspecific)
int __pthread_internal_tsd_set (int key, const void *pointer)
void * __pthread_internal_tsd_get (int key)
void ** __attribute__ ((__const__))

Variables

static struct pthread_key_struct [PTHREAD_KEYS_MAX]
const int __linuxthreads_pthread_keys_max = PTHREAD_KEYS_MAX
const int __linuxthreads_pthread_key_2ndlevel_size = PTHREAD_KEY_2NDLEVEL_SIZE
static pthread_mutex_t pthread_keys_mutex = PTHREAD_MUTEX_INITIALIZER

Function Documentation

void** __attribute__ ( (__const__)  )

Definition at line 236 of file specific.c.

{
  pthread_descr self = thread_self();
  return &self->p_libc_specific[key];
}

Definition at line 229 of file specific.c.

int __pthread_internal_tsd_set ( int  key,
const void *  pointer 
)

Definition at line 220 of file specific.c.

{
  pthread_descr self = thread_self();

  THREAD_SETMEM_NC(self, p_libc_specific[key], (void *) pointer);
  return 0;
}

Definition at line 42 of file specific.c.

{
  int i;

  pthread_mutex_lock(&pthread_keys_mutex);
  for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
    if (! pthread_keys[i].in_use) {
      /* Mark key in use */
      pthread_keys[i].in_use = 1;
      pthread_keys[i].destr = destr;
      pthread_mutex_unlock(&pthread_keys_mutex);
      *key = i;
      return 0;
    }
  }
  pthread_mutex_unlock(&pthread_keys_mutex);
  return EAGAIN;
}
int __pthread_setspecific ( pthread_key_t  key,
const void *  pointer 
)

Definition at line 141 of file specific.c.

{
  pthread_descr self = thread_self();
  unsigned int idx1st, idx2nd;

  if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use)
    return EINVAL;
  idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
  idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
  if (THREAD_GETMEM_NC(self, p_specific[idx1st]) == NULL) {
    void *newp = calloc(PTHREAD_KEY_2NDLEVEL_SIZE, sizeof (void *));
    if (newp == NULL)
      return ENOMEM;
    THREAD_SETMEM_NC(self, p_specific[idx1st], newp);
  }
  THREAD_GETMEM_NC(self, p_specific[idx1st])[idx2nd] = (void *) pointer;
  return 0;
}

Definition at line 91 of file specific.c.

{
  pthread_descr self = thread_self();

  pthread_mutex_lock(&pthread_keys_mutex);
  if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use) {
    pthread_mutex_unlock(&pthread_keys_mutex);
    return EINVAL;
  }
  pthread_keys[key].in_use = 0;
  pthread_keys[key].destr = NULL;

  /* Set the value of the key to NULL in all running threads, so
     that if the key is reallocated later by pthread_key_create, its
     associated values will be NULL in all threads.

     If no threads have been created yet, or if we are exiting, clear
     it just in the current thread.  */

  struct pthread_key_delete_helper_args args;
  args.idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
  args.idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
  if (__pthread_manager_request != -1
      && !(__builtin_expect (__pthread_exit_requested, 0)))
    {
      struct pthread_request request;

      args.self = 0;

      request.req_thread = self;
      request.req_kind = REQ_FOR_EACH_THREAD;
      request.req_args.for_each.arg = &args;
      request.req_args.for_each.fn = pthread_key_delete_helper;

      TEMP_FAILURE_RETRY(write_not_cancel(__pthread_manager_request,
                                     (char *) &request, sizeof(request)));
      suspend(self);
    }
  else
    {
      if (self->p_specific[args.idx1st] != NULL)
       self->p_specific[args.idx1st][args.idx2nd] = NULL;
    }

  pthread_mutex_unlock(&pthread_keys_mutex);
  return 0;
}

Here is the caller graph for this function:

static void pthread_key_delete_helper ( void *  arg,
pthread_descr  th 
) [static]

Definition at line 71 of file specific.c.

{
  struct pthread_key_delete_helper_args *args = arg;
  unsigned int idx1st = args->idx1st;
  unsigned int idx2nd = args->idx2nd;
  pthread_descr self = args->self;

  if (self == 0)
    self = args->self = thread_self();

  if (!th->p_terminated) {
    /* pthread_exit() may try to free th->p_specific[idx1st] concurrently. */
    __pthread_lock(th->p_lock, self);
    if (th->p_specific[idx1st] != NULL)
      th->p_specific[idx1st][idx2nd] = NULL;
    __pthread_unlock(th->p_lock);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 60 of file specific.c.

                                      {
  /* Damn, we need lexical closures in C! ;) */
  unsigned int idx1st, idx2nd;
  pthread_descr self;
};

Definition at line 159 of file specific.c.

{
  pthread_descr self = thread_self();
  unsigned int idx1st, idx2nd;

  if (key >= PTHREAD_KEYS_MAX)
    return NULL;
  idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
  idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
  if (THREAD_GETMEM_NC(self, p_specific[idx1st]) == NULL
      || !pthread_keys[key].in_use)
    return NULL;
  return THREAD_GETMEM_NC(self, p_specific[idx1st])[idx2nd];
}

Definition at line 177 of file specific.c.

{
  pthread_descr self = thread_self();
  int i, j, round, found_nonzero;
  destr_function destr;
  void * data;

  for (round = 0, found_nonzero = 1;
       found_nonzero && round < PTHREAD_DESTRUCTOR_ITERATIONS;
       round++) {
    found_nonzero = 0;
    for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++)
      if (THREAD_GETMEM_NC(self, p_specific[i]) != NULL)
        for (j = 0; j < PTHREAD_KEY_2NDLEVEL_SIZE; j++) {
          destr = pthread_keys[i * PTHREAD_KEY_2NDLEVEL_SIZE + j].destr;
          data = THREAD_GETMEM_NC(self, p_specific[i])[j];
          if (destr != NULL && data != NULL) {
            THREAD_GETMEM_NC(self, p_specific[i])[j] = NULL;
            destr(data);
            found_nonzero = 1;
          }
        }
  }
  __pthread_lock(THREAD_GETMEM(self, p_lock), self);
  for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++) {
    if (THREAD_GETMEM_NC(self, p_specific[i]) != NULL) {
      void *p = THREAD_GETMEM_NC(self, p_specific[i]);
      THREAD_SETMEM_NC(self, p_specific[i], NULL);
      free(p);
    }
  }
  __pthread_unlock(THREAD_GETMEM(self, p_lock));
}

Here is the call graph for this function:


Variable Documentation

Definition at line 34 of file specific.c.

Definition at line 33 of file specific.c.

Initial value:
  { { 0, NULL } }

Definition at line 29 of file specific.c.

Definition at line 38 of file specific.c.