1.\" $NetBSD: call_once.3,v 1.2 2019/04/27 10:57:11 wiz Exp $ 2.\" 3.\" Copyright (c) 2016 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Kamil Rytarowski. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.Dd October 16, 2016 31.Dt CALL_ONCE 3 32.Os 33.Sh NAME 34.Nm call_once 35.Nd calls function exactly once 36.Sh LIBRARY 37.Lb libpthread 38.Sh SYNOPSIS 39.In threads.h 40.Ft void 41.Fn call_once "once_flag *flag" "void (*func)(void)" 42.Vt #define ONCE_FLAG_INIT /* implementation specified */ 43.Sh DESCRIPTION 44The 45.Nm 46function uses the 47.Fa flag 48parameter to ensure that 49.Fa func 50is called exactly once, 51even if called from several threads. 52.Pp 53The 54.Dv ONCE_FLAG_INIT 55definition expands to a value that can be used to initialize an object of type 56.Dv once_flag . 57.Pp 58This portable interface is implemented on top of the 59.Xr pthread_once 3 60functionality. 61.Sh RETURN VALUES 62None. 63.Sh EXAMPLES 64The following calls 65.Nm 66from two threads using the portable 67.Xr thrd 3 68interface. 69.Bd -literal -offset indent 70#include <stdio.h> 71#include <threads.h> 72 73static once_flag oflag = ONCE_FLAG_INIT; 74 75void 76called_once(void) 77{ 78 printf("called once\n"); 79} 80 81int 82tfun(void *ptr) 83{ 84 call_once(&oflag, called_once); 85} 86 87int 88main(int argc, char **argv) 89{ 90 thrd_t th1, th2; 91 92 thrd_create(&th1, tfun, NULL); 93 thrd_create(&th2, tfun, NULL); 94 95 thrd_join(th1, NULL); 96 thrd_join(th2, NULL); 97 98 return 0; 99} 100.Ed 101.Sh SEE ALSO 102.Xr pthread_once 3 , 103.Xr threads 3 104.Sh STANDARDS 105The 106.Nm 107function conforms to 108.St -isoC-2011 . 109.Sh HISTORY 110This interface first appeared in 111.Nx 9 . 112.Sh AUTHORS 113.An Kamil Rytarowski Aq Mt kamil@NetBSD.org 114