Blame nptl_db/td_thr_tsd.c

Packit 6c4009
/* Get a thread-specific data pointer for a thread.
Packit 6c4009
   Copyright (C) 1999-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@redhat.com>, 1999.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include "thread_dbP.h"
Packit 6c4009
Packit 6c4009
Packit 6c4009
td_err_e
Packit 6c4009
td_thr_tsd (const td_thrhandle_t *th, const thread_key_t tk, void **data)
Packit 6c4009
{
Packit 6c4009
  td_err_e err;
Packit 6c4009
  psaddr_t tk_seq, level1, level2, seq, value;
Packit 6c4009
  void *copy;
Packit 6c4009
  uint32_t pthread_key_2ndlevel_size, idx1st, idx2nd;
Packit 6c4009
Packit 6c4009
  LOG ("td_thr_tsd");
Packit 6c4009
Packit 6c4009
  /* Get the key entry.  */
Packit 6c4009
  err = DB_GET_VALUE (tk_seq, th->th_ta_p, __pthread_keys, tk);
Packit 6c4009
  if (err == TD_NOAPLIC)
Packit 6c4009
    return TD_BADKEY;
Packit 6c4009
  if (err != TD_OK)
Packit 6c4009
    return err;
Packit 6c4009
Packit 6c4009
  /* Fail if this key is not at all used.  */
Packit 6c4009
  if (((uintptr_t) tk_seq & 1) == 0)
Packit 6c4009
    return TD_BADKEY;
Packit 6c4009
Packit 6c4009
  /* This makes sure we have the size information on hand.  */
Packit 6c4009
  err = DB_GET_FIELD_ADDRESS (level2, th->th_ta_p, 0, pthread_key_data_level2,
Packit 6c4009
			      data, 1);
Packit 6c4009
  if (err != TD_OK)
Packit 6c4009
    return err;
Packit 6c4009
Packit 6c4009
  /* Compute the indeces.  */
Packit 6c4009
  pthread_key_2ndlevel_size
Packit 6c4009
    = DB_DESC_NELEM (th->th_ta_p->ta_field_pthread_key_data_level2_data);
Packit 6c4009
  idx1st = tk / pthread_key_2ndlevel_size;
Packit 6c4009
  idx2nd = tk % pthread_key_2ndlevel_size;
Packit 6c4009
Packit 6c4009
  /* Now fetch the first level pointer.  */
Packit 6c4009
  err = DB_GET_FIELD (level1, th->th_ta_p, th->th_unique, pthread,
Packit 6c4009
		      specific, idx1st);
Packit 6c4009
  if (err == TD_NOAPLIC)
Packit 6c4009
    return TD_DBERR;
Packit 6c4009
  if (err != TD_OK)
Packit 6c4009
    return err;
Packit 6c4009
Packit 6c4009
  /* Check the pointer to the second level array.  */
Packit 6c4009
  if (level1 == 0)
Packit 6c4009
    return TD_NOTSD;
Packit 6c4009
Packit 6c4009
  /* Locate the element within the second level array.  */
Packit 6c4009
  err = DB_GET_FIELD_ADDRESS (level2, th->th_ta_p,
Packit 6c4009
			      level1, pthread_key_data_level2, data, idx2nd);
Packit 6c4009
  if (err == TD_NOAPLIC)
Packit 6c4009
    return TD_DBERR;
Packit 6c4009
  if (err != TD_OK)
Packit 6c4009
    return err;
Packit 6c4009
Packit 6c4009
  /* Now copy in that whole structure.  */
Packit 6c4009
  err = DB_GET_STRUCT (copy, th->th_ta_p, level2, pthread_key_data);
Packit 6c4009
  if (err != TD_OK)
Packit 6c4009
    return err;
Packit 6c4009
Packit 6c4009
  /* Check whether the data is valid.  */
Packit 6c4009
  err = DB_GET_FIELD_LOCAL (seq, th->th_ta_p, copy, pthread_key_data, seq, 0);
Packit 6c4009
  if (err != TD_OK)
Packit 6c4009
    return err;
Packit 6c4009
  if (seq != tk_seq)
Packit 6c4009
    return TD_NOTSD;
Packit 6c4009
Packit 6c4009
  /* Finally, fetch the value.  */
Packit 6c4009
  err = DB_GET_FIELD_LOCAL (value, th->th_ta_p, copy, pthread_key_data,
Packit 6c4009
			    data, 0);
Packit 6c4009
  if (err == TD_OK)
Packit 6c4009
    *data = value;
Packit 6c4009
Packit 6c4009
  return err;
Packit 6c4009
}