root/maint/gnulib/lib/tss.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. tss_create
  2. tss_set
  3. tss_get
  4. tss_delete
  5. tss_create
  6. tss_set
  7. tss_get
  8. tss_delete

   1 /* ISO C 11 thread-specific storage in multithreaded situations.
   2    Copyright (C) 2005-2021 Free Software Foundation, Inc.
   3 
   4    This file is free software: you can redistribute it and/or modify
   5    it under the terms of the GNU Lesser General Public License as
   6    published by the Free Software Foundation; either version 2.1 of the
   7    License, or (at your option) any later version.
   8 
   9    This file is distributed in the hope that it will be useful,
  10    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12    GNU Lesser General Public License for more details.
  13 
  14    You should have received a copy of the GNU Lesser General Public License
  15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  16 
  17 /* Written by Bruno Haible <bruno@clisp.org>, 2005, 2019.  */
  18 
  19 #include <config.h>
  20 
  21 #include <threads.h>
  22 
  23 #include <string.h>
  24 
  25 #if defined _WIN32 && ! defined __CYGWIN__
  26 /* Use Windows threads.  */
  27 
  28 # define WIN32_LEAN_AND_MEAN  /* avoid including junk */
  29 # include <windows.h>
  30 
  31 #else
  32 /* Use POSIX threads.  */
  33 
  34 # include <pthread.h>
  35 
  36 #endif
  37 
  38 #if defined _WIN32 && ! defined __CYGWIN__
  39 /* Use Windows threads.  */
  40 
  41 int
  42 tss_create (tss_t *keyp, tss_dtor_t destructor)
     /* [previous][next][first][last][top][bottom][index][help] */
  43 {
  44   int err = glwthread_tls_key_create (keyp, destructor);
  45   if (err == 0)
  46     return thrd_success;
  47   else
  48     {
  49       memset (keyp, '\0', sizeof (tss_t));
  50       return thrd_error;
  51     }
  52 }
  53 
  54 int
  55 tss_set (tss_t key, void *value)
     /* [previous][next][first][last][top][bottom][index][help] */
  56 {
  57   int err = glwthread_tls_set (key, value);
  58   return (err == 0 ? thrd_success : thrd_error);
  59 }
  60 
  61 void *
  62 tss_get (tss_t key)
     /* [previous][next][first][last][top][bottom][index][help] */
  63 {
  64   return glwthread_tls_get (key);
  65 }
  66 
  67 void
  68 tss_delete (tss_t key)
     /* [previous][next][first][last][top][bottom][index][help] */
  69 {
  70   glwthread_tls_key_delete (key);
  71 }
  72 
  73 #else
  74 /* Use POSIX threads.  */
  75 
  76 int
  77 tss_create (tss_t *keyp, tss_dtor_t destructor)
     /* [previous][next][first][last][top][bottom][index][help] */
  78 {
  79   int err = pthread_key_create (keyp, destructor);
  80   if (err == 0)
  81     return thrd_success;
  82   else
  83     {
  84       memset (keyp, '\0', sizeof (tss_t));
  85       return thrd_error;
  86     }
  87 }
  88 
  89 int
  90 tss_set (tss_t key, void *value)
     /* [previous][next][first][last][top][bottom][index][help] */
  91 {
  92   int err = pthread_setspecific (key, value);
  93   return (err == 0 ? thrd_success : thrd_error);
  94 }
  95 
  96 void *
  97 tss_get (tss_t key)
     /* [previous][next][first][last][top][bottom][index][help] */
  98 {
  99   return pthread_getspecific (key);
 100 }
 101 
 102 void
 103 tss_delete (tss_t key)
     /* [previous][next][first][last][top][bottom][index][help] */
 104 {
 105   pthread_key_delete (key);
 106 }
 107 
 108 #endif

/* [previous][next][first][last][top][bottom][index][help] */