1331722Seadler/*
2153496Sdavidxu * Copyright (c) 2005, David Xu <davidxu@freebsd.org>
3112918Sjeff * All rights reserved.
4112918Sjeff *
5112918Sjeff * Redistribution and use in source and binary forms, with or without
6112918Sjeff * modification, are permitted provided that the following conditions
7112918Sjeff * are met:
8112918Sjeff * 1. Redistributions of source code must retain the above copyright
9153496Sdavidxu *    notice unmodified, this list of conditions, and the following
10153496Sdavidxu *    disclaimer.
11112918Sjeff * 2. Redistributions in binary form must reproduce the above copyright
12112918Sjeff *    notice, this list of conditions and the following disclaimer in the
13112918Sjeff *    documentation and/or other materials provided with the distribution.
14112918Sjeff *
15153496Sdavidxu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16153496Sdavidxu * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17153496Sdavidxu * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18153496Sdavidxu * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19153496Sdavidxu * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20153496Sdavidxu * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21153496Sdavidxu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22153496Sdavidxu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23153496Sdavidxu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24153496Sdavidxu * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25112918Sjeff */
26144518Sdavidxu
27297706Skib#include <sys/cdefs.h>
28297706Skib__FBSDID("$FreeBSD$");
29297706Skib
30144518Sdavidxu#include "namespace.h"
31112918Sjeff#include <pthread.h>
32144518Sdavidxu#include "un-namespace.h"
33144518Sdavidxu
34112918Sjeff#include "thr_private.h"
35112918Sjeff
36112918Sjeff__weak_reference(_pthread_once, pthread_once);
37112918Sjeff
38144518Sdavidxu#define ONCE_NEVER_DONE		PTHREAD_NEEDS_INIT
39144518Sdavidxu#define ONCE_DONE		PTHREAD_DONE_INIT
40144518Sdavidxu#define	ONCE_IN_PROGRESS	0x02
41179417Sdavidxu#define ONCE_WAIT		0x03
42144518Sdavidxu
43144518Sdavidxu/*
44144518Sdavidxu * POSIX:
45144518Sdavidxu * The pthread_once() function is not a cancellation point. However,
46144518Sdavidxu * if init_routine is a cancellation point and is canceled, the effect
47144518Sdavidxu * on once_control shall be as if pthread_once() was never called.
48144518Sdavidxu */
49144518Sdavidxu
50144518Sdavidxustatic void
51144518Sdavidxuonce_cancel_handler(void *arg)
52144518Sdavidxu{
53287557Skib	pthread_once_t *once_control;
54144518Sdavidxu
55287557Skib	once_control = arg;
56287557Skib	if (atomic_cmpset_rel_int(&once_control->state, ONCE_IN_PROGRESS,
57287557Skib	    ONCE_NEVER_DONE))
58179417Sdavidxu		return;
59179417Sdavidxu	atomic_store_rel_int(&once_control->state, ONCE_NEVER_DONE);
60179417Sdavidxu	_thr_umtx_wake(&once_control->state, INT_MAX, 0);
61144518Sdavidxu}
62144518Sdavidxu
63112918Sjeffint
64144518Sdavidxu_pthread_once(pthread_once_t *once_control, void (*init_routine) (void))
65112918Sjeff{
66172695Sdavidxu	struct pthread *curthread;
67179417Sdavidxu	int state;
68144518Sdavidxu
69220888Srstone	_thr_check_init();
70220888Srstone
71179417Sdavidxu	for (;;) {
72179417Sdavidxu		state = once_control->state;
73287556Skib		if (state == ONCE_DONE) {
74287556Skib			atomic_thread_fence_acq();
75179417Sdavidxu			return (0);
76287556Skib		}
77179417Sdavidxu		if (state == ONCE_NEVER_DONE) {
78287557Skib			if (atomic_cmpset_int(&once_control->state, state,
79287557Skib			    ONCE_IN_PROGRESS))
80179417Sdavidxu				break;
81179417Sdavidxu		} else if (state == ONCE_IN_PROGRESS) {
82287557Skib			if (atomic_cmpset_int(&once_control->state, state,
83287557Skib			    ONCE_WAIT))
84287557Skib				_thr_umtx_wait_uint(&once_control->state,
85287557Skib				    ONCE_WAIT, NULL, 0);
86179417Sdavidxu		} else if (state == ONCE_WAIT) {
87287557Skib			_thr_umtx_wait_uint(&once_control->state, state,
88287557Skib			    NULL, 0);
89179417Sdavidxu		} else
90179417Sdavidxu			return (EINVAL);
91179417Sdavidxu        }
92179417Sdavidxu
93179417Sdavidxu	curthread = _get_curthread();
94179417Sdavidxu	THR_CLEANUP_PUSH(curthread, once_cancel_handler, once_control);
95179417Sdavidxu	init_routine();
96179417Sdavidxu	THR_CLEANUP_POP(curthread, 0);
97287557Skib	if (atomic_cmpset_rel_int(&once_control->state, ONCE_IN_PROGRESS,
98287557Skib	    ONCE_DONE))
99144518Sdavidxu		return (0);
100179417Sdavidxu	atomic_store_rel_int(&once_control->state, ONCE_DONE);
101179417Sdavidxu	_thr_umtx_wake(&once_control->state, INT_MAX, 0);
102112918Sjeff	return (0);
103112918Sjeff}
104