Home | History | Annotate | Line # | Download | only in base
      1  1.1  christos /*	$NetBSD: dll.c,v 1.3 2023/06/19 21:41:42 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /***********************************************************************
      4  1.1  christos  * Copyright (c) 2016 Kungliga Tekniska Hgskolan
      5  1.1  christos  * (Royal Institute of Technology, Stockholm, Sweden).
      6  1.1  christos  * All rights reserved.
      7  1.1  christos  *
      8  1.1  christos  * Redistribution and use in source and binary forms, with or without
      9  1.1  christos  * modification, are permitted provided that the following conditions
     10  1.1  christos  * are met:
     11  1.1  christos  *
     12  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     14  1.1  christos  *
     15  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  christos  *    documentation and/or other materials provided with the distribution.
     18  1.1  christos  *
     19  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20  1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21  1.1  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     22  1.1  christos  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     23  1.1  christos  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     24  1.1  christos  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     25  1.1  christos  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     26  1.1  christos  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     28  1.1  christos  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
     30  1.1  christos  * OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1  christos  *
     32  1.1  christos  **********************************************************************/
     33  1.1  christos 
     34  1.1  christos /*
     35  1.1  christos  * This is an implementation of thread-specific storage with
     36  1.1  christos  * destructors.  WIN32 doesn't quite have this.  Instead it has
     37  1.1  christos  * DllMain(), an entry point in every DLL that gets called to notify the
     38  1.1  christos  * DLL of thread/process "attach"/"detach" events.
     39  1.1  christos  *
     40  1.1  christos  * We use __thread (or __declspec(thread)) for the thread-local itself
     41  1.1  christos  * and DllMain() DLL_THREAD_DETACH events to drive destruction of
     42  1.1  christos  * thread-local values.
     43  1.1  christos  *
     44  1.1  christos  * When building in maintainer mode on non-Windows pthread systems this
     45  1.1  christos  * uses a single pthread key instead to implement multiple keys.  This
     46  1.1  christos  * keeps the code from rotting when modified by non-Windows developers.
     47  1.1  christos  */
     48  1.1  christos 
     49  1.1  christos #include "baselocl.h"
     50  1.1  christos 
     51  1.1  christos #ifdef WIN32
     52  1.1  christos #include <windows.h>
     53  1.1  christos #endif
     54  1.1  christos 
     55  1.1  christos #ifdef HEIM_WIN32_TLS
     56  1.1  christos #include <assert.h>
     57  1.1  christos #include <err.h>
     58  1.1  christos #include <heim_threads.h>
     59  1.1  christos 
     60  1.1  christos #ifndef WIN32
     61  1.1  christos #include <pthread.h>
     62  1.1  christos #endif
     63  1.1  christos 
     64  1.1  christos /* Logical array of keys that grows lock-lessly */
     65  1.1  christos typedef struct tls_keys tls_keys;
     66  1.1  christos struct tls_keys {
     67  1.1  christos     void (**keys_dtors)(void *);    /* array of destructors         */
     68  1.1  christos     size_t keys_start_idx;          /* index of first destructor    */
     69  1.1  christos     size_t keys_num;
     70  1.1  christos     tls_keys *keys_next;
     71  1.1  christos };
     72  1.1  christos 
     73  1.1  christos /*
     74  1.1  christos  * Well, not quite locklessly.  We need synchronization primitives to do
     75  1.1  christos  * this locklessly.  An atomic CAS will do.
     76  1.1  christos  */
     77  1.1  christos static HEIMDAL_MUTEX tls_key_defs_lock = HEIMDAL_MUTEX_INITIALIZER;
     78  1.1  christos static tls_keys *tls_key_defs;
     79  1.1  christos 
     80  1.1  christos /* Logical array of values (per-thread; no locking needed here) */
     81  1.1  christos struct tls_values {
     82  1.1  christos     void **values; /* realloc()ed */
     83  1.1  christos     size_t values_num;
     84  1.1  christos };
     85  1.1  christos 
     86  1.1  christos static HEIMDAL_THREAD_LOCAL struct tls_values values;
     87  1.1  christos 
     88  1.3  christos static char dead_key[1];
     89  1.3  christos static void no_dtor(void *d) { (void)d; }
     90  1.1  christos 
     91  1.1  christos void
     92  1.1  christos heim_w32_service_thread_detach(void *unused)
     93  1.1  christos {
     94  1.1  christos     tls_keys *key_defs;
     95  1.1  christos     void (*dtor)(void*);
     96  1.1  christos     size_t i;
     97  1.1  christos 
     98  1.1  christos     HEIMDAL_MUTEX_lock(&tls_key_defs_lock);
     99  1.1  christos     key_defs = tls_key_defs;
    100  1.1  christos     HEIMDAL_MUTEX_unlock(&tls_key_defs_lock);
    101  1.1  christos 
    102  1.1  christos     if (key_defs == NULL)
    103  1.1  christos         return;
    104  1.1  christos 
    105  1.1  christos     for (i = 0; i < values.values_num; i++) {
    106  1.1  christos         assert(i >= key_defs->keys_start_idx);
    107  1.1  christos         if (i >= key_defs->keys_start_idx + key_defs->keys_num) {
    108  1.1  christos             HEIMDAL_MUTEX_lock(&tls_key_defs_lock);
    109  1.1  christos             key_defs = key_defs->keys_next;
    110  1.1  christos             HEIMDAL_MUTEX_unlock(&tls_key_defs_lock);
    111  1.1  christos 
    112  1.1  christos             assert(key_defs != NULL);
    113  1.1  christos             assert(i >= key_defs->keys_start_idx);
    114  1.1  christos             assert(i < key_defs->keys_start_idx + key_defs->keys_num);
    115  1.1  christos         }
    116  1.1  christos         dtor = key_defs->keys_dtors[i - key_defs->keys_start_idx];
    117  1.3  christos         if (values.values[i] != NULL && dtor != NULL && dtor != no_dtor)
    118  1.1  christos             dtor(values.values[i]);
    119  1.1  christos         values.values[i] = NULL;
    120  1.1  christos     }
    121  1.1  christos }
    122  1.1  christos 
    123  1.1  christos #if !defined(WIN32)
    124  1.1  christos static pthread_key_t pt_key;
    125  1.1  christos pthread_once_t pt_once = PTHREAD_ONCE_INIT;
    126  1.1  christos 
    127  1.1  christos static void
    128  1.1  christos atexit_del_tls_for_thread(void)
    129  1.1  christos {
    130  1.1  christos     heim_w32_service_thread_detach(NULL);
    131  1.1  christos }
    132  1.1  christos 
    133  1.1  christos static void
    134  1.1  christos create_pt_key(void)
    135  1.1  christos {
    136  1.1  christos     int ret;
    137  1.1  christos 
    138  1.1  christos     /* The main thread may not execute TLS destructors */
    139  1.1  christos     atexit(atexit_del_tls_for_thread);
    140  1.1  christos     ret = pthread_key_create(&pt_key, heim_w32_service_thread_detach);
    141  1.1  christos     if (ret != 0)
    142  1.1  christos         err(1, "pthread_key_create() failed");
    143  1.1  christos }
    144  1.1  christos 
    145  1.1  christos #endif
    146  1.1  christos 
    147  1.1  christos int
    148  1.1  christos heim_w32_key_create(HEIM_PRIV_thread_key *key, void (*dtor)(void *))
    149  1.1  christos {
    150  1.1  christos     tls_keys *key_defs, *new_key_defs;
    151  1.1  christos     size_t i, k;
    152  1.1  christos     int ret = ENOMEM;
    153  1.1  christos 
    154  1.1  christos #if !defined(WIN32)
    155  1.1  christos     (void) pthread_once(&pt_once, create_pt_key);
    156  1.3  christos     (void) pthread_setspecific(pt_key, dead_key);
    157  1.1  christos #endif
    158  1.1  christos 
    159  1.1  christos     HEIMDAL_MUTEX_lock(&tls_key_defs_lock);
    160  1.1  christos     if (tls_key_defs == NULL) {
    161  1.1  christos         /* First key */
    162  1.1  christos         new_key_defs = calloc(1, sizeof(*new_key_defs));
    163  1.1  christos         if (new_key_defs == NULL) {
    164  1.1  christos             HEIMDAL_MUTEX_unlock(&tls_key_defs_lock);
    165  1.1  christos             return ENOMEM;
    166  1.1  christos         }
    167  1.1  christos         new_key_defs->keys_num = 8;
    168  1.1  christos         new_key_defs->keys_dtors = calloc(new_key_defs->keys_num,
    169  1.1  christos                                           sizeof(*new_key_defs->keys_dtors));
    170  1.1  christos         if (new_key_defs->keys_dtors == NULL) {
    171  1.1  christos             HEIMDAL_MUTEX_unlock(&tls_key_defs_lock);
    172  1.1  christos             free(new_key_defs);
    173  1.1  christos             return ENOMEM;
    174  1.1  christos         }
    175  1.1  christos         tls_key_defs = new_key_defs;
    176  1.1  christos         new_key_defs->keys_dtors[0] = dtor;
    177  1.1  christos         for (i = 1; i < new_key_defs->keys_num; i++)
    178  1.1  christos             new_key_defs->keys_dtors[i] = NULL;
    179  1.1  christos         HEIMDAL_MUTEX_unlock(&tls_key_defs_lock);
    180  1.1  christos         return 0;
    181  1.1  christos     }
    182  1.1  christos 
    183  1.1  christos     for (key_defs = tls_key_defs;
    184  1.1  christos          key_defs != NULL;
    185  1.1  christos          key_defs = key_defs->keys_next) {
    186  1.1  christos         k = key_defs->keys_start_idx;
    187  1.1  christos         for (i = 0; i < key_defs->keys_num; i++, k++) {
    188  1.1  christos             if (key_defs->keys_dtors[i] == NULL) {
    189  1.1  christos                 /* Found free slot; use it */
    190  1.1  christos                 key_defs->keys_dtors[i] = dtor;
    191  1.1  christos                 *key = k;
    192  1.1  christos                 HEIMDAL_MUTEX_unlock(&tls_key_defs_lock);
    193  1.1  christos                 return 0;
    194  1.1  christos             }
    195  1.1  christos         }
    196  1.1  christos         if (key_defs->keys_next != NULL)
    197  1.1  christos             continue;
    198  1.1  christos 
    199  1.1  christos         /* Grow the registration array */
    200  1.1  christos         /* XXX DRY */
    201  1.1  christos         new_key_defs = calloc(1, sizeof(*new_key_defs));
    202  1.1  christos         if (new_key_defs == NULL)
    203  1.1  christos             break;
    204  1.1  christos 
    205  1.1  christos         new_key_defs->keys_dtors =
    206  1.1  christos             calloc(key_defs->keys_num + key_defs->keys_num / 2,
    207  1.1  christos                    sizeof(*new_key_defs->keys_dtors));
    208  1.1  christos         if (new_key_defs->keys_dtors == NULL) {
    209  1.1  christos             free(new_key_defs);
    210  1.1  christos             break;
    211  1.1  christos         }
    212  1.1  christos         new_key_defs->keys_start_idx = key_defs->keys_start_idx +
    213  1.1  christos             key_defs->keys_num;
    214  1.1  christos         new_key_defs->keys_num = key_defs->keys_num + key_defs->keys_num / 2;
    215  1.1  christos         new_key_defs->keys_dtors[i] = dtor;
    216  1.1  christos         for (i = 1; i < new_key_defs->keys_num; i++)
    217  1.1  christos             new_key_defs->keys_dtors[i] = NULL;
    218  1.1  christos         key_defs->keys_next = new_key_defs;
    219  1.1  christos         ret = 0;
    220  1.1  christos         break;
    221  1.1  christos     }
    222  1.1  christos     HEIMDAL_MUTEX_unlock(&tls_key_defs_lock);
    223  1.1  christos     return ret;
    224  1.1  christos }
    225  1.1  christos 
    226  1.1  christos static void
    227  1.1  christos key_lookup(HEIM_PRIV_thread_key key, tls_keys **kd,
    228  1.1  christos            size_t *dtor_idx, void (**dtor)(void *))
    229  1.1  christos {
    230  1.1  christos     tls_keys *key_defs;
    231  1.1  christos 
    232  1.1  christos     if (kd != NULL)
    233  1.1  christos         *kd = NULL;
    234  1.1  christos     if (dtor_idx != NULL)
    235  1.1  christos         *dtor_idx = 0;
    236  1.1  christos     if (dtor != NULL)
    237  1.1  christos         *dtor = NULL;
    238  1.1  christos 
    239  1.1  christos     HEIMDAL_MUTEX_lock(&tls_key_defs_lock);
    240  1.1  christos     key_defs = tls_key_defs;
    241  1.1  christos     HEIMDAL_MUTEX_unlock(&tls_key_defs_lock);
    242  1.1  christos 
    243  1.1  christos     while (key_defs != NULL) {
    244  1.1  christos         if (key >= key_defs->keys_start_idx &&
    245  1.1  christos             key < key_defs->keys_start_idx + key_defs->keys_num) {
    246  1.1  christos             if (kd != NULL)
    247  1.1  christos                 *kd = key_defs;
    248  1.1  christos             if (dtor_idx != NULL)
    249  1.1  christos                 *dtor_idx = key - key_defs->keys_start_idx;
    250  1.1  christos             if (dtor != NULL)
    251  1.1  christos                 *dtor = key_defs->keys_dtors[key - key_defs->keys_start_idx];
    252  1.1  christos             return;
    253  1.1  christos         }
    254  1.1  christos 
    255  1.1  christos         HEIMDAL_MUTEX_lock(&tls_key_defs_lock);
    256  1.1  christos         key_defs = key_defs->keys_next;
    257  1.1  christos         HEIMDAL_MUTEX_unlock(&tls_key_defs_lock);
    258  1.1  christos         assert(key_defs != NULL);
    259  1.1  christos         assert(key >= key_defs->keys_start_idx);
    260  1.1  christos     }
    261  1.1  christos }
    262  1.1  christos 
    263  1.1  christos int
    264  1.1  christos heim_w32_delete_key(HEIM_PRIV_thread_key key)
    265  1.1  christos {
    266  1.1  christos     tls_keys *key_defs;
    267  1.1  christos     size_t dtor_idx;
    268  1.1  christos 
    269  1.1  christos     key_lookup(key, &key_defs, &dtor_idx, NULL);
    270  1.1  christos     if (key_defs == NULL)
    271  1.1  christos         return EINVAL;
    272  1.3  christos     key_defs->keys_dtors[dtor_idx] = no_dtor;
    273  1.1  christos     return 0;
    274  1.1  christos }
    275  1.1  christos 
    276  1.1  christos int
    277  1.1  christos heim_w32_setspecific(HEIM_PRIV_thread_key key, void *value)
    278  1.1  christos {
    279  1.1  christos     void **new_values;
    280  1.1  christos     size_t new_num;
    281  1.1  christos     void (*dtor)(void *);
    282  1.1  christos     size_t i;
    283  1.1  christos 
    284  1.1  christos #if !defined(WIN32)
    285  1.3  christos     (void) pthread_setspecific(pt_key, dead_key);
    286  1.1  christos #endif
    287  1.1  christos 
    288  1.1  christos     key_lookup(key, NULL, NULL, &dtor);
    289  1.1  christos     if (dtor == NULL)
    290  1.1  christos         return EINVAL;
    291  1.1  christos 
    292  1.1  christos     if (key >= values.values_num) {
    293  1.1  christos         if (values.values_num == 0) {
    294  1.1  christos             values.values = NULL;
    295  1.1  christos             new_num = 8;
    296  1.1  christos         } else {
    297  1.1  christos             new_num = (values.values_num + values.values_num / 2);
    298  1.1  christos         }
    299  1.1  christos         new_values = realloc(values.values, sizeof(void *) * new_num);
    300  1.1  christos         if (new_values == NULL)
    301  1.1  christos             return ENOMEM;
    302  1.1  christos         for (i = values.values_num; i < new_num; i++)
    303  1.1  christos             new_values[i] = NULL;
    304  1.1  christos         values.values = new_values;
    305  1.1  christos         values.values_num = new_num;
    306  1.1  christos     }
    307  1.1  christos 
    308  1.1  christos     assert(key < values.values_num);
    309  1.1  christos 
    310  1.3  christos     if (values.values[key] != NULL && dtor != NULL && dtor != no_dtor)
    311  1.1  christos         dtor(values.values[key]);
    312  1.1  christos 
    313  1.1  christos     values.values[key] = value;
    314  1.1  christos     return 0;
    315  1.1  christos }
    316  1.1  christos 
    317  1.1  christos void *
    318  1.1  christos heim_w32_getspecific(HEIM_PRIV_thread_key key)
    319  1.1  christos {
    320  1.1  christos     if (key >= values.values_num)
    321  1.1  christos         return NULL;
    322  1.1  christos     return values.values[key];
    323  1.1  christos }
    324  1.1  christos 
    325  1.1  christos #else
    326  1.1  christos static char dummy;
    327  1.1  christos #endif /* HEIM_WIN32_TLS */
    328