1*7adb4107Sriastradh /* $NetBSD: pthread_once.c,v 1.4 2022/02/12 14:59:32 riastradh Exp $ */
277bfe129Sjoerg
377bfe129Sjoerg /*-
477bfe129Sjoerg * Copyright (c) 2001, 2003 The NetBSD Foundation, Inc.
577bfe129Sjoerg * All rights reserved.
677bfe129Sjoerg *
777bfe129Sjoerg * This code is derived from software contributed to The NetBSD Foundation
877bfe129Sjoerg * by Nathan J. Williams, and by Jason R. Thorpe.
977bfe129Sjoerg *
1077bfe129Sjoerg * Redistribution and use in source and binary forms, with or without
1177bfe129Sjoerg * modification, are permitted provided that the following conditions
1277bfe129Sjoerg * are met:
1377bfe129Sjoerg * 1. Redistributions of source code must retain the above copyright
1477bfe129Sjoerg * notice, this list of conditions and the following disclaimer.
1577bfe129Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1677bfe129Sjoerg * notice, this list of conditions and the following disclaimer in the
1777bfe129Sjoerg * documentation and/or other materials provided with the distribution.
1877bfe129Sjoerg * 3. All advertising materials mentioning features or use of this software
1977bfe129Sjoerg * must display the following acknowledgement:
2077bfe129Sjoerg * This product includes software developed by the NetBSD
2177bfe129Sjoerg * Foundation, Inc. and its contributors.
2277bfe129Sjoerg * 4. Neither the name of The NetBSD Foundation nor the names of its
2377bfe129Sjoerg * contributors may be used to endorse or promote products derived
2477bfe129Sjoerg * from this software without specific prior written permission.
2577bfe129Sjoerg *
2677bfe129Sjoerg * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2777bfe129Sjoerg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2877bfe129Sjoerg * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2977bfe129Sjoerg * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
3077bfe129Sjoerg * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3177bfe129Sjoerg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3277bfe129Sjoerg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3377bfe129Sjoerg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3477bfe129Sjoerg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3577bfe129Sjoerg * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3677bfe129Sjoerg * POSSIBILITY OF SUCH DAMAGE.
3777bfe129Sjoerg */
3877bfe129Sjoerg
3977bfe129Sjoerg #include <sys/cdefs.h>
40*7adb4107Sriastradh __RCSID("$NetBSD: pthread_once.c,v 1.4 2022/02/12 14:59:32 riastradh Exp $");
41*7adb4107Sriastradh
42*7adb4107Sriastradh /* Need to use libc-private names for atomic operations. */
43*7adb4107Sriastradh #include "../../common/lib/libc/atomic/atomic_op_namespace.h"
4477bfe129Sjoerg
4577bfe129Sjoerg #include "pthread.h"
4671d484f9Schristos #include "pthread_int.h"
4771d484f9Schristos #include "reentrant.h"
4877bfe129Sjoerg
4977bfe129Sjoerg static void
once_cleanup(void * closure)5077bfe129Sjoerg once_cleanup(void *closure)
5177bfe129Sjoerg {
5277bfe129Sjoerg
5377bfe129Sjoerg pthread_mutex_unlock((pthread_mutex_t *)closure);
5477bfe129Sjoerg }
5577bfe129Sjoerg
5677bfe129Sjoerg int
pthread_once(pthread_once_t * once_control,void (* routine)(void))5777bfe129Sjoerg pthread_once(pthread_once_t *once_control, void (*routine)(void))
5877bfe129Sjoerg {
5971d484f9Schristos if (__predict_false(__uselibcstub))
6071d484f9Schristos return __libc_thr_once_stub(once_control, routine);
6177bfe129Sjoerg
6277bfe129Sjoerg if (once_control->pto_done == 0) {
6377bfe129Sjoerg pthread_mutex_lock(&once_control->pto_mutex);
6477bfe129Sjoerg pthread_cleanup_push(&once_cleanup, &once_control->pto_mutex);
6577bfe129Sjoerg if (once_control->pto_done == 0) {
6677bfe129Sjoerg routine();
6777bfe129Sjoerg once_control->pto_done = 1;
6877bfe129Sjoerg }
6977bfe129Sjoerg pthread_cleanup_pop(1);
7077bfe129Sjoerg }
7177bfe129Sjoerg
7277bfe129Sjoerg return 0;
7377bfe129Sjoerg }
74e865ac28Sjoerg
75e865ac28Sjoerg __strong_alias(__libc_thr_once,pthread_once)
76