1113595Snectar/*-
2113595Snectar * Copyright (c) 2003 Networks Associates Technology, Inc.
3113595Snectar * All rights reserved.
4113595Snectar *
5113595Snectar * This software was developed for the FreeBSD Project by
6113595Snectar * Jacques A. Vidrine, Safeport Network Services, and Network
7113595Snectar * Associates Laboratories, the Security Research Division of Network
8113595Snectar * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
9113595Snectar * ("CBOSS"), as part of the DARPA CHATS research program.
10113595Snectar *
11113595Snectar * Redistribution and use in source and binary forms, with or without
12113595Snectar * modification, are permitted provided that the following conditions
13113595Snectar * are met:
14113595Snectar * 1. Redistributions of source code must retain the above copyright
15113595Snectar *    notice, this list of conditions and the following disclaimer.
16113595Snectar * 2. Redistributions in binary form must reproduce the above copyright
17113595Snectar *    notice, this list of conditions and the following disclaimer in the
18113595Snectar *    documentation and/or other materials provided with the distribution.
19113595Snectar *
20113595Snectar * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21113595Snectar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22113595Snectar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23113595Snectar * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24113595Snectar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25113595Snectar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26113595Snectar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27113595Snectar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28113595Snectar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29113595Snectar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30113595Snectar * SUCH DAMAGE.
31113595Snectar *
32113595Snectar * $FreeBSD$
33113595Snectar *
34113595Snectar * Macros which generate thread local storage handling code in NSS modules.
35113595Snectar */
36113595Snectar#ifndef _NSS_TLS_H_
37113595Snectar#define _NSS_TLS_H_
38113595Snectar
39113595Snectar#define NSS_TLS_HANDLING(name)					\
40113595Snectarstatic pthread_key_t name##_state_key;				\
41113595Snectarstatic	void	 name##_keyinit(void);				\
42113595Snectarstatic	int	 name##_getstate(struct name##_state **);	\
43113595Snectar\
44113595Snectarstatic void								\
45113595Snectarname##_keyinit(void)							\
46113595Snectar{									\
47113595Snectar	(void)_pthread_key_create(&name##_state_key, name##_endstate);	\
48113595Snectar}									\
49113595Snectar\
50113595Snectarstatic int							\
51113595Snectarname##_getstate(struct name##_state **p)			\
52113595Snectar{								\
53113595Snectar	static struct name##_state st;				\
54113595Snectar	static pthread_once_t	keyinit = PTHREAD_ONCE_INIT;	\
55113595Snectar	int			rv;				\
56113595Snectar								\
57113595Snectar	if (!__isthreaded || _pthread_main_np() != 0) {		\
58113595Snectar		*p = &st;					\
59113595Snectar		return (0);					\
60113595Snectar	}							\
61113595Snectar	rv = _pthread_once(&keyinit, name##_keyinit);		\
62113595Snectar	if (rv != 0)						\
63113595Snectar		return (rv);					\
64113595Snectar	*p = _pthread_getspecific(name##_state_key);		\
65113595Snectar	if (*p != NULL)						\
66113595Snectar		return (0);					\
67113798Snectar	*p = calloc(1, sizeof(**p));				\
68113595Snectar	if (*p == NULL)						\
69113595Snectar		return (ENOMEM);				\
70113595Snectar	rv = _pthread_setspecific(name##_state_key, *p);	\
71113595Snectar	if (rv != 0) {						\
72113595Snectar		free(*p);					\
73113595Snectar		*p = NULL;					\
74113595Snectar	}							\
75113595Snectar	return (rv);						\
76113595Snectar}								\
77113595Snectar/* allow the macro invocation to end with a semicolon */	\
78127625Snectarstruct _clashproof_bmVjdGFy
79113595Snectar
80113595Snectar#endif /* _NSS_TLS_H_ */
81