summaryrefslogtreecommitdiff
path: root/initfini/impl/_common.h
blob: 1b818b9f17b32fb14967e21947c045195ea49548 (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
/* -*- 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_INITFINI_IMPL_COMMON_H
#define SIMPLEOPS_INITFINI_IMPL_COMMON_H

#ifndef SIMPLEOPS_INITFINI_IMPL_H
#error Private header used directly
#endif

#include "simpleops/atomic/internal/_dispatch.h"
#include "simpleops/tss/internal/_dispatch.h"

static void
_simpleops_init_internals (void)
{
    _simpleops_atomic_init ();
    _simpleops_tss_init ();
}

static void
_simpleops_fini_internals (void)
{
    _simpleops_tss_fini ();
    _simpleops_atomic_fini ();
}


static void _simpleops_init_externals (void);
static void _simpleops_fini_externals (void);

/**
 * This macro is used to specify what function should be called upon
 * initialization.
 */
#define SIMPLEOPS_INIT(f) static void _simpleops_init_externals (void) { f (); }

/**
 * This macro is used to specify what function should be called upon
 * termination.
 */
#define SIMPLEOPS_FINI(f) static void _simpleops_fini_externals (void) { f (); }

static int _simpleops_initfini_counter;

/* Invoke internal/external init/fini in the right order. Init only if
 * initialization has not yet been performed, fini only if every user
 * has already detached.  */

/**
 * Perform the initialization.
 *
 * After this function returns, the initialization is guaranteed to be
 * complete. This is only needed in order to enforce a specific
 * initialization sequence in dependent libraries.
 *
 * This function is not thread-safe. Multiple concurrent calls may
 * lead to undefined behavior.
 */
static void
simpleops_init (void)
{
    if (_simpleops_initfini_counter++)
	return;

    _simpleops_init_internals ();
    _simpleops_init_externals ();
}

/**
 * Perform the termination.
 *
 * After this function returns, the termination is guaranteed to be
 * complete. This is only needed in order to enforce a specific
 * termination sequence in dependent libraries.
 *
 * This function is not thread-safe. Multiple concurrent calls may
 * lead to undefined behavior.
 */
static void
simpleops_fini (void)
{
    if (--_simpleops_initfini_counter)
	return;

    _simpleops_fini_externals ();
    _simpleops_fini_internals ();
}

#endif