source: mainline/uspace/lib/posix/src/pthread/keys.c

Last change on this file was da54714, checked in by GitHub <noreply@…>, 4 months ago

partially implement key-based pthread local storage (#245)

  • check for hash section presence in rtld
  • implement pthread thread-local storage keys without destructors and key recycling
  • Property mode set to 100644
File size: 3.3 KB
Line 
1/*
2 * Copyright (c) 2013 Vojtech Horky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libposix
30 * @{
31 */
32/** @file Pthread: keys and thread-specific storage.
33 */
34
35#include <stdlib.h>
36#include <pthread.h>
37#include <errno.h>
38#include <fibril.h>
39#include <stdatomic.h>
40#include "../internal/common.h"
41
42#include <stdio.h>
43#define DPRINTF(format, ...) ((void) 0);
44
45static atomic_ushort next_key = 1; // skip the key 'zero'
46
47/*
48 * For now, we just support maximum of 100 keys. This can be improved
49 * in the future by implementing a dynamically growing array with
50 * reallocations, but that will require more synchronization.
51 */
52#define PTHREAD_KEYS_MAX 100
53
54static fibril_local void *key_data[PTHREAD_KEYS_MAX];
55
56void *pthread_getspecific(pthread_key_t key)
57{
58 assert(key < PTHREAD_KEYS_MAX);
59 assert(key < next_key);
60 assert(key > 0);
61
62 DPRINTF("pthread_getspecific(%d) = %p\n", key, key_data[key]);
63 return key_data[key];
64}
65
66int pthread_setspecific(pthread_key_t key, const void *data)
67{
68 DPRINTF("pthread_setspecific(%d, %p)\n", key, data);
69 assert(key < PTHREAD_KEYS_MAX);
70 assert(key < next_key);
71 assert(key > 0);
72
73 key_data[key] = (void *) data;
74 return EOK;
75}
76
77int pthread_key_delete(pthread_key_t key)
78{
79 /* see https://github.com/HelenOS/helenos/pull/245#issuecomment-2706795848 */
80 not_implemented();
81 return EOK;
82}
83
84int pthread_key_create(pthread_key_t *key, void (*destructor)(void *))
85{
86 unsigned short k = atomic_fetch_add(&next_key, 1);
87 DPRINTF("pthread_key_create(%p, %p) = %d\n", key, destructor, k);
88 if (k >= PTHREAD_KEYS_MAX) {
89 atomic_store(&next_key, PTHREAD_KEYS_MAX + 1);
90 return ELIMIT;
91 }
92 if (destructor != NULL) {
93 /* Inlined not_implemented() macro to add custom message */
94 static int __not_implemented_counter = 0;
95 if (__not_implemented_counter == 0) {
96 fprintf(stderr, "pthread_key_create: destructors not supported\n");
97 }
98 __not_implemented_counter++;
99 }
100
101 *key = k;
102 return EOK;
103}
104
105/** @}
106 */
Note: See TracBrowser for help on using the repository browser.