]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libthr/thread/thr_once.c
MFV r354378,r354379,r354386: 10499 Multi-modifier protection (MMP)
[FreeBSD/FreeBSD.git] / lib / libthr / thread / thr_once.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2005, David Xu <davidxu@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "namespace.h"
33 #include <pthread.h>
34 #include "un-namespace.h"
35
36 #include "thr_private.h"
37
38 __weak_reference(_thr_once, pthread_once);
39 __weak_reference(_thr_once, _pthread_once);
40
41 #define ONCE_NEVER_DONE         PTHREAD_NEEDS_INIT
42 #define ONCE_DONE               PTHREAD_DONE_INIT
43 #define ONCE_IN_PROGRESS        0x02
44 #define ONCE_WAIT               0x03
45
46 /*
47  * POSIX:
48  * The pthread_once() function is not a cancellation point. However,
49  * if init_routine is a cancellation point and is canceled, the effect
50  * on once_control shall be as if pthread_once() was never called.
51  */
52  
53 static void
54 once_cancel_handler(void *arg)
55 {
56         pthread_once_t *once_control;
57
58         once_control = arg;
59         if (atomic_cmpset_rel_int(&once_control->state, ONCE_IN_PROGRESS,
60             ONCE_NEVER_DONE))
61                 return;
62         atomic_store_rel_int(&once_control->state, ONCE_NEVER_DONE);
63         _thr_umtx_wake(&once_control->state, INT_MAX, 0);
64 }
65
66 int
67 _thr_once(pthread_once_t *once_control, void (*init_routine)(void))
68 {
69         struct pthread *curthread;
70         int state;
71
72         _thr_check_init();
73
74         for (;;) {
75                 state = once_control->state;
76                 if (state == ONCE_DONE) {
77                         atomic_thread_fence_acq();
78                         return (0);
79                 }
80                 if (state == ONCE_NEVER_DONE) {
81                         if (atomic_cmpset_int(&once_control->state, state,
82                             ONCE_IN_PROGRESS))
83                                 break;
84                 } else if (state == ONCE_IN_PROGRESS) {
85                         if (atomic_cmpset_int(&once_control->state, state,
86                             ONCE_WAIT))
87                                 _thr_umtx_wait_uint(&once_control->state,
88                                     ONCE_WAIT, NULL, 0);
89                 } else if (state == ONCE_WAIT) {
90                         _thr_umtx_wait_uint(&once_control->state, state,
91                             NULL, 0);
92                 } else
93                         return (EINVAL);
94         }
95
96         curthread = _get_curthread();
97         THR_CLEANUP_PUSH(curthread, once_cancel_handler, once_control);
98         init_routine();
99         THR_CLEANUP_POP(curthread, 0);
100         if (atomic_cmpset_rel_int(&once_control->state, ONCE_IN_PROGRESS,
101             ONCE_DONE))
102                 return (0);
103         atomic_store_rel_int(&once_control->state, ONCE_DONE);
104         _thr_umtx_wake(&once_control->state, INT_MAX, 0);
105         return (0);
106 }