xref: /netbsd-src/share/man/man9/RUN_ONCE.9 (revision a5e84fe7234aba4d33e74bc85425fb3840653c49)
1.\"	$NetBSD: RUN_ONCE.9,v 1.11 2019/03/19 10:17:35 wiz Exp $
2.\"
3.\" Copyright (c)2005 YAMAMOTO Takashi,
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25.\" SUCH DAMAGE.
26.\"
27.\" ------------------------------------------------------------
28.Dd March 19, 2019
29.Dt RUN_ONCE 9
30.Os
31.\" ------------------------------------------------------------
32.Sh NAME
33.Nm RUN_ONCE ,
34.Nm INIT_ONCE ,
35.Nm FINI_ONCE
36.Nd run a function exactly once
37.\" ------------------------------------------------------------
38.Sh SYNOPSIS
39.In sys/once.h
40.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
41.Vt ONCE_DECL(control);
42.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
43.Ft int
44.Fn RUN_ONCE \
45"once_t *control" "int (*init_func)(void)"
46.Ft int
47.Fn INIT_ONCE \
48"once_t *control" "int (*init_func)(void)"
49.Ft void
50.Fn FINI_ONCE \
51"once_t *control" "void (*fini_func)(void)"
52.\" ------------------------------------------------------------
53.Sh DESCRIPTION
54.Fn RUN_ONCE
55provides a functionality similar to
56.Xr pthread_once 3 .
57It ensures that, for a given
58.Fa control ,
59.Fn init_func
60is executed (successfully) exactly once.
61It is considered as a successful execution if and only if
62.Fn init_func
63returned 0.
64As long as there was no successful execution,
65.Fn RUN_ONCE
66will try again each time it is called.
67.Pp
68.Fn RUN_ONCE
69can sleep if it's called concurrently.
70.Pp
71.Fn INIT_ONCE
72is used in pair with
73.Fn FINI_ONCE .
74.Fn INIT_ONCE
75will only be run once similar to
76.Fn RUN_ONCE .
77.Fn FINI_ONCE
78will only be run at last time if it is called as many times as calling
79.Fn INIT_ONCE .
80When
81.Fn FINI_ONCE
82is executed, the next call to
83.Fn INIT_ONCE
84will be executed again.
85That is,
86.Fn INIT_ONCE
87and
88.Fn FINI_ONCE
89can be nested.
90.\" ------------------------------------------------------------
91.Sh RETURN VALUES
92On failure,
93.Fn RUN_ONCE
94returns what
95.Fn init_func
96returned.
97Otherwise, it returns 0.
98.\" ------------------------------------------------------------
99.Sh EXAMPLES
100The following example shows how
101.Fn RUN_ONCE
102is used.
103Regardless of how many times
104.Fn some_func
105is executed,
106.Fn init_func
107will be executed exactly once.
108.Bd -literal
109static int
110init_func(void)
111{
112
113	/*
114	 * do some initialization.
115	 */
116
117	return 0; /* success */
118}
119
120int
121some_func(void)
122{
123	static ONCE_DECL(control);
124
125	RUN_ONCE(&control, init_func);
126
127	/*
128	 * we are sure that init_func has already been completed here.
129	 */
130}
131.Ed
132.\" ------------------------------------------------------------
133.Sh SEE ALSO
134.Xr pthread_once 3 ,
135.Xr condvar 9 ,
136.Xr intro 9
137