ObjFW
Loading...
Searching...
No Matches
OFOnce.m
1/*
2 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
3 *
4 * All rights reserved.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License version 3.0 only,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * version 3.0 for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * version 3.0 along with this program. If not, see
17 * <https://www.gnu.org/licenses/>.
18 */
19
20#include "config.h"
21
22#include <stdbool.h>
23
24#import "OFOnce.h"
25#if defined(OF_HAVE_THREADS) && defined(OF_HAVE_ATOMIC_OPS)
26# import "OFAtomic.h"
27# import "OFPlainMutex.h"
28#endif
29
30#ifdef OF_AMIGAOS
31# define Class IntuitionClass
32# include <proto/exec.h>
33# undef Class
34#endif
35
36void
37OFOnce(OFOnceControl *control, void (*function)(void))
38{
39#if !defined(OF_HAVE_THREADS)
40 if (*control == 0) {
41 function();
42 *control = 1;
43 }
44#elif defined(OF_HAVE_PTHREADS)
45 pthread_once(control, function);
46#elif defined(OF_HAVE_ATOMIC_OPS)
47 /* Avoid atomic operations in case it's already done. */
48 if (*control == 2)
49 return;
50
51 if (OFAtomicIntCompareAndSwap(control, 0, 1)) {
52 function();
53
54 OFMemoryBarrier();
55
56 OFAtomicIntIncrease(control);
57 } else
58 while (*control == 1)
59 OFYieldThread();
60#elif defined(OF_AMIGAOS)
61 bool run = false;
62
63 /* Avoid Forbid() in case it's already done. */
64 if (*control == 2)
65 return;
66
67 Forbid();
68
69 switch (*control) {
70 case 0:
71 *control = 1;
72 run = true;
73 break;
74 case 1:
75 while (*control == 1) {
76 Permit();
77 Forbid();
78 }
79 }
80
81 Permit();
82
83 if (run) {
84 function();
85 *control = 2;
86 }
87#else
88# error No OFOnce available
89#endif
90}
void OFOnce(OFOnceControl *control, OFOnceFunction function)
Executes the specified function exactly once in the application's lifetime, even in a multi-threaded ...
Definition OFOnce.m:37