summaryrefslogtreecommitdiff
path: root/tls/impl/simpleops-tls-impl-common-tail.h
blob: 829a48c65a23ac1eaa6b543a6971eb783d013e1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
/*
 * Copyright 2010-2011 Andrea Canciani
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Author(s): Andrea Canciani <ranma42@gmail.com>
 */

#ifndef SIMPLEOPS_TLS_IMPL_COMMON_TAIL_H
#define SIMPLEOPS_TLS_IMPL_COMMON_TAIL_H

#ifndef SIMPLEOPS_TLS_IMPL_H
#error Private header used directly
#endif

#include <stdlib.h>

static simpleops_tls_t *_simpleops_tls_create (void);
static void _simpleops_tls_destroy (void *tls);

#define _SIMPLEOPS_TLS_INITIALIZED SIMPLEOPS_ADD_PREFIX (_simpleops_tls_initialized)
extern simpleops_bool_t _SIMPLEOPS_TLS_INITIALIZED;

/**
 * Get the pointer to the thread-local storage.
 *
 * \return the pointer to the TLS. If the TLS cannot be initialized,
 * NULL is returned instead.
 */
static simpleops_tls_t *
simpleops_tls_get (void)
{
    simpleops_tls_t *tls;

    if (SIMPLEOPS_UNLIKELY (! _SIMPLEOPS_TLS_INITIALIZED))
	return NULL;

    tls = _simpleops_tls_get ();
    if (SIMPLEOPS_UNLIKELY (tls == NULL)) {
	tls = _simpleops_tls_create ();

	if (SIMPLEOPS_UNLIKELY (tls == NULL))
	    return NULL;

	if (SIMPLEOPS_UNLIKELY (_simpleops_tls_set (tls))) {
	    _simpleops_tls_destroy (tls);
	    return NULL;
	}
    }

    return tls;
}

/**
 * Reset the thread-local storage.
 *
 * Destroy the current content of the TLS and reset it to the state
 * preceding the first simpleops_tls_get() call.
 */
static void
simpleops_tls_reset (void)
{
    simpleops_tls_t *tls;

    if (SIMPLEOPS_UNLIKELY (! _SIMPLEOPS_TLS_INITIALIZED))
	return;

    tls = _simpleops_tls_get ();
    if (SIMPLEOPS_UNLIKELY (tls == NULL))
	return;

    if (SIMPLEOPS_UNLIKELY (_simpleops_tls_set (NULL)))
	return;

    _simpleops_tls_destroy (tls);
}

static simpleops_always_inline void
_simpleops_tls_silence_unused_warnings (void)
{
    if (simpleops_tls_get () != NULL)
	simpleops_tls_reset ();
}

/**
 * This macro is used to specify what function should be used to
 * instantiate and initialize the thread-local storage, when needed.
 *
 * This macro should be invoked as a declaration of a function.  The
 * invocation must be visible to all the places where the TLS
 * functions are called.
 */
#define SIMPLEOPS_TLS_CREATE(f)					\
    static simpleops_tls_t *_simpleops_tls_create (void)	\
    {								\
	return f ();						\
    }

/**
 * This macro is used to specify what function should be used to
 * destroy the content of the thread-local storage, when needed.
 *
 * This macro should be invoked as a declaration of a function.  The
 * invocation must be visible to all the places where the TLS
 * functions are called.
 */
#define SIMPLEOPS_TLS_DESTROY(f)			\
    static void _simpleops_tls_destroy (void *tls)	\
    {							\
	f ((simpleops_tls_t *) tls);			\
    }

/**
 * This macro provides a default implementation for TLS allocation.
 *
 * The create function performs dynamic allocation and initializes
 * the content of the simpleops_tls_t structure to 0 (i.e. it is a
 * calloc()).
 *
 * The destroy function free()s the data.
 */
#define SIMPLEOPS_TLS_DEFAULT_ALLOCATOR			\
    static simpleops_tls_t *				\
    _simpleops_tls_create (void)			\
    {							\
	return calloc (1, sizeof (simpleops_tls_t));	\
    }							\
							\
    static void						\
    _simpleops_tls_destroy (void *tls)			\
    {							\
	free (tls);					\
    }

#endif