1112918Sjeff/*
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 *
26112918Sjeff * $FreeBSD$
27153496Sdavidxu *
28112918Sjeff */
29144518Sdavidxu
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{
53144518Sdavidxu	pthread_once_t *once_control = arg;
54144518Sdavidxu
55179417Sdavidxu	if (atomic_cmpset_rel_int(&once_control->state, ONCE_IN_PROGRESS, ONCE_NEVER_DONE))
56179417Sdavidxu		return;
57179417Sdavidxu	atomic_store_rel_int(&once_control->state, ONCE_NEVER_DONE);
58179417Sdavidxu	_thr_umtx_wake(&once_control->state, INT_MAX, 0);
59144518Sdavidxu}
60144518Sdavidxu
61112918Sjeffint
62144518Sdavidxu_pthread_once(pthread_once_t *once_control, void (*init_routine) (void))
63112918Sjeff{
64172695Sdavidxu	struct pthread *curthread;
65179417Sdavidxu	int state;
66144518Sdavidxu
67220888Srstone	_thr_check_init();
68220888Srstone
69179417Sdavidxu	for (;;) {
70179417Sdavidxu		state = once_control->state;
71179417Sdavidxu		if (state == ONCE_DONE)
72179417Sdavidxu			return (0);
73179417Sdavidxu		if (state == ONCE_NEVER_DONE) {
74179417Sdavidxu			if (atomic_cmpset_acq_int(&once_control->state, state, ONCE_IN_PROGRESS))
75179417Sdavidxu				break;
76179417Sdavidxu		} else if (state == ONCE_IN_PROGRESS) {
77179417Sdavidxu			if (atomic_cmpset_acq_int(&once_control->state, state, ONCE_WAIT))
78179417Sdavidxu				_thr_umtx_wait_uint(&once_control->state, ONCE_WAIT, NULL, 0);
79179417Sdavidxu		} else if (state == ONCE_WAIT) {
80179417Sdavidxu			_thr_umtx_wait_uint(&once_control->state, state, NULL, 0);
81179417Sdavidxu		} else
82179417Sdavidxu			return (EINVAL);
83179417Sdavidxu        }
84179417Sdavidxu
85179417Sdavidxu	curthread = _get_curthread();
86179417Sdavidxu	THR_CLEANUP_PUSH(curthread, once_cancel_handler, once_control);
87179417Sdavidxu	init_routine();
88179417Sdavidxu	THR_CLEANUP_POP(curthread, 0);
89179417Sdavidxu	if (atomic_cmpset_rel_int(&once_control->state, ONCE_IN_PROGRESS, ONCE_DONE))
90144518Sdavidxu		return (0);
91179417Sdavidxu	atomic_store_rel_int(&once_control->state, ONCE_DONE);
92179417Sdavidxu	_thr_umtx_wake(&once_control->state, INT_MAX, 0);
93112918Sjeff	return (0);
94112918Sjeff}
95