1*68463fd2Suwe /* $NetBSD: tss.c,v 1.2 2019/12/15 22:32:29 uwe Exp $ */
2a9ca1710Skamil
3a9ca1710Skamil /*-
4a9ca1710Skamil * Copyright (c) 2016 The NetBSD Foundation, Inc.
5a9ca1710Skamil * All rights reserved.
6a9ca1710Skamil *
7a9ca1710Skamil * This code is derived from software contributed to The NetBSD Foundation
8a9ca1710Skamil * by Kamil Rytarowski.
9a9ca1710Skamil *
10a9ca1710Skamil * Redistribution and use in source and binary forms, with or without
11a9ca1710Skamil * modification, are permitted provided that the following conditions
12a9ca1710Skamil * are met:
13a9ca1710Skamil * 1. Redistributions of source code must retain the above copyright
14a9ca1710Skamil * notice, this list of conditions and the following disclaimer.
15a9ca1710Skamil * 2. Redistributions in binary form must reproduce the above copyright
16a9ca1710Skamil * notice, this list of conditions and the following disclaimer in the
17a9ca1710Skamil * documentation and/or other materials provided with the distribution.
18a9ca1710Skamil *
19a9ca1710Skamil * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20a9ca1710Skamil * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21a9ca1710Skamil * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22a9ca1710Skamil * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23a9ca1710Skamil * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24a9ca1710Skamil * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25a9ca1710Skamil * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26a9ca1710Skamil * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27a9ca1710Skamil * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28a9ca1710Skamil * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29a9ca1710Skamil * POSSIBILITY OF SUCH DAMAGE.
30a9ca1710Skamil */
31a9ca1710Skamil
32a9ca1710Skamil #include <sys/cdefs.h>
33*68463fd2Suwe __RCSID("$NetBSD: tss.c,v 1.2 2019/12/15 22:32:29 uwe Exp $");
34a9ca1710Skamil
35a9ca1710Skamil #include <assert.h>
36a9ca1710Skamil #include <errno.h>
37a9ca1710Skamil #include <pthread.h>
38a9ca1710Skamil #include <threads.h>
39a9ca1710Skamil
40a9ca1710Skamil int
tss_create(tss_t * key,tss_dtor_t dtor)41a9ca1710Skamil tss_create(tss_t *key, tss_dtor_t dtor)
42a9ca1710Skamil {
43a9ca1710Skamil
44a9ca1710Skamil _DIAGASSERT(key != NULL);
45a9ca1710Skamil
46a9ca1710Skamil if (pthread_key_create(key, dtor) == 0)
47a9ca1710Skamil return thrd_success;
48a9ca1710Skamil
49a9ca1710Skamil return thrd_error;
50a9ca1710Skamil }
51a9ca1710Skamil
52a9ca1710Skamil void
tss_delete(tss_t key)53a9ca1710Skamil tss_delete(tss_t key)
54a9ca1710Skamil {
55a9ca1710Skamil
56a9ca1710Skamil /*
57a9ca1710Skamil * The tss_delete(3) function that conforms to C11 returns no value.
58a9ca1710Skamil */
59a9ca1710Skamil (void)pthread_key_delete(key);
60a9ca1710Skamil }
61a9ca1710Skamil
62a9ca1710Skamil void *
tss_get(tss_t key)63a9ca1710Skamil tss_get(tss_t key)
64a9ca1710Skamil {
65a9ca1710Skamil
66a9ca1710Skamil return pthread_getspecific(key);
67a9ca1710Skamil }
68a9ca1710Skamil
69a9ca1710Skamil int
tss_set(tss_t key,void * val)70a9ca1710Skamil tss_set(tss_t key, void *val)
71a9ca1710Skamil {
72a9ca1710Skamil
73a9ca1710Skamil if (pthread_setspecific(key, val) == 0)
74a9ca1710Skamil return thrd_success;
75a9ca1710Skamil
76a9ca1710Skamil return thrd_error;
77a9ca1710Skamil }
78