thr_once.c revision 155739
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: head/lib/libthr/thread/thr_once.c 155739 2006-02-15 23:05:03Z davidxu $
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
41144518Sdavidxu#define	ONCE_MASK		0x03
42144518Sdavidxu
43155739Sdavidxustatic pthread_mutex_t		_thr_once_lock = PTHREAD_MUTEX_INITIALIZER;
44155739Sdavidxustatic pthread_cond_t		_thr_once_cv = PTHREAD_COND_INITIALIZER;
45144518Sdavidxu
46144518Sdavidxu/*
47144518Sdavidxu * POSIX:
48144518Sdavidxu * The pthread_once() function is not a cancellation point. However,
49144518Sdavidxu * if init_routine is a cancellation point and is canceled, the effect
50144518Sdavidxu * on once_control shall be as if pthread_once() was never called.
51144518Sdavidxu */
52144518Sdavidxu
53144518Sdavidxustatic void
54144518Sdavidxuonce_cancel_handler(void *arg)
55144518Sdavidxu{
56144518Sdavidxu	pthread_once_t *once_control = arg;
57144518Sdavidxu
58155714Sdavidxu	_pthread_mutex_lock(&_thr_once_lock);
59144518Sdavidxu	once_control->state = ONCE_NEVER_DONE;
60155714Sdavidxu	_pthread_mutex_unlock(&_thr_once_lock);
61155714Sdavidxu	_pthread_cond_broadcast(&_thr_once_cv);
62144518Sdavidxu}
63144518Sdavidxu
64112918Sjeffint
65144518Sdavidxu_pthread_once(pthread_once_t *once_control, void (*init_routine) (void))
66112918Sjeff{
67144518Sdavidxu	int wakeup = 0;
68144518Sdavidxu
69144518Sdavidxu	if (once_control->state == ONCE_DONE)
70144518Sdavidxu		return (0);
71155714Sdavidxu	_pthread_mutex_lock(&_thr_once_lock);
72144518Sdavidxu	while (*(volatile int *)&(once_control->state) == ONCE_IN_PROGRESS)
73155714Sdavidxu		_pthread_cond_wait(&_thr_once_cv, &_thr_once_lock);
74144518Sdavidxu	/*
75144518Sdavidxu	 * If previous thread was canceled, then the state still
76144518Sdavidxu	 * could be ONCE_NEVER_DONE, we need to check it again.
77144518Sdavidxu	 */
78144518Sdavidxu	if (*(volatile int *)&(once_control->state) == ONCE_NEVER_DONE) {
79144518Sdavidxu		once_control->state = ONCE_IN_PROGRESS;
80155714Sdavidxu		_pthread_mutex_unlock(&_thr_once_lock);
81144518Sdavidxu		_pthread_cleanup_push(once_cancel_handler, once_control);
82144518Sdavidxu		init_routine();
83144518Sdavidxu		_pthread_cleanup_pop(0);
84155714Sdavidxu		_pthread_mutex_lock(&_thr_once_lock);
85144518Sdavidxu		once_control->state = ONCE_DONE;
86144518Sdavidxu		wakeup = 1;
87112918Sjeff	}
88155714Sdavidxu	_pthread_mutex_unlock(&_thr_once_lock);
89144518Sdavidxu	if (wakeup)
90155714Sdavidxu		_pthread_cond_broadcast(&_thr_once_cv);
91112918Sjeff	return (0);
92112918Sjeff}
93144518Sdavidxu
94155739Sdavidxuvoid
95155739Sdavidxu_thr_once_init()
96155739Sdavidxu{
97155739Sdavidxu	_thr_once_lock = PTHREAD_MUTEX_INITIALIZER;
98155739Sdavidxu	_thr_once_cv = PTHREAD_COND_INITIALIZER;
99155739Sdavidxu}
100