xref: /freebsd/lib/libstdthreads/tss.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1fc6f0665SEd Schouten /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni  *
4fc6f0665SEd Schouten  * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
5fc6f0665SEd Schouten  * All rights reserved.
6fc6f0665SEd Schouten  *
7fc6f0665SEd Schouten  * Redistribution and use in source and binary forms, with or without
8fc6f0665SEd Schouten  * modification, are permitted provided that the following conditions
9fc6f0665SEd Schouten  * are met:
10fc6f0665SEd Schouten  * 1. Redistributions of source code must retain the above copyright
11fc6f0665SEd Schouten  *    notice, this list of conditions and the following disclaimer.
12fc6f0665SEd Schouten  * 2. Redistributions in binary form must reproduce the above copyright
13fc6f0665SEd Schouten  *    notice, this list of conditions and the following disclaimer in the
14fc6f0665SEd Schouten  *    documentation and/or other materials provided with the distribution.
15fc6f0665SEd Schouten  *
16fc6f0665SEd Schouten  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17fc6f0665SEd Schouten  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18fc6f0665SEd Schouten  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19fc6f0665SEd Schouten  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20fc6f0665SEd Schouten  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21fc6f0665SEd Schouten  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22fc6f0665SEd Schouten  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23fc6f0665SEd Schouten  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24fc6f0665SEd Schouten  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25fc6f0665SEd Schouten  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26fc6f0665SEd Schouten  * SUCH DAMAGE.
27fc6f0665SEd Schouten  */
28fc6f0665SEd Schouten 
29fc6f0665SEd Schouten #include <sys/cdefs.h>
30fc6f0665SEd Schouten #include <pthread.h>
31fc6f0665SEd Schouten 
32fc6f0665SEd Schouten #include "threads.h"
33fc6f0665SEd Schouten 
34fc6f0665SEd Schouten int
tss_create(tss_t * key,tss_dtor_t dtor)35fc6f0665SEd Schouten tss_create(tss_t *key, tss_dtor_t dtor)
36fc6f0665SEd Schouten {
37fc6f0665SEd Schouten 
38fc6f0665SEd Schouten 	if (pthread_key_create(key, dtor) != 0)
39fc6f0665SEd Schouten 		return (thrd_error);
40fc6f0665SEd Schouten 	return (thrd_success);
41fc6f0665SEd Schouten }
42fc6f0665SEd Schouten 
43fc6f0665SEd Schouten void
tss_delete(tss_t key)44fc6f0665SEd Schouten tss_delete(tss_t key)
45fc6f0665SEd Schouten {
46fc6f0665SEd Schouten 
47fc6f0665SEd Schouten 	(void)pthread_key_delete(key);
48fc6f0665SEd Schouten }
49fc6f0665SEd Schouten 
50fc6f0665SEd Schouten void *
tss_get(tss_t key)51fc6f0665SEd Schouten tss_get(tss_t key)
52fc6f0665SEd Schouten {
53fc6f0665SEd Schouten 
54fc6f0665SEd Schouten 	return (pthread_getspecific(key));
55fc6f0665SEd Schouten }
56fc6f0665SEd Schouten 
57fc6f0665SEd Schouten int
tss_set(tss_t key,void * val)58fc6f0665SEd Schouten tss_set(tss_t key, void *val)
59fc6f0665SEd Schouten {
60fc6f0665SEd Schouten 
61fc6f0665SEd Schouten 	if (pthread_setspecific(key, val) != 0)
62fc6f0665SEd Schouten 		return (thrd_error);
63fc6f0665SEd Schouten 	return (thrd_success);
64fc6f0665SEd Schouten }
65fc6f0665SEd Schouten 
66fc6f0665SEd Schouten _Static_assert(TSS_DTOR_ITERATIONS == PTHREAD_DESTRUCTOR_ITERATIONS,
67fc6f0665SEd Schouten     "TSS_DTOR_ITERATIONS must be identical to PTHREAD_DESTRUCTOR_ITERATIONS");
68