13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
* Public License, either version 2 or 3, which can be found in the file
* LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
* file.
*/
#include "config.h"
#import "once.h"
#ifdef OF_AMIGAOS
# include <proto/exec.h>
#endif
#if defined(OF_HAVE_THREADS) && defined(OF_HAVE_ATOMIC_OPS)
# import "atomic.h"
# import "mutex.h"
#endif
void
of_once(of_once_t *control, void (*func)(void))
{
#if !defined(OF_HAVE_THREADS)
if (*control == 0) {
*control = 1;
func();
}
#elif defined(OF_HAVE_PTHREADS)
pthread_once(control, func);
#elif defined(OF_HAVE_ATOMIC_OPS)
if (of_atomic_int_cmpswap(control, 0, 1)) {
func();
of_memory_barrier();
of_atomic_int_inc(control);
} else
while (*control == 1)
of_thread_yield();
#elif defined(OF_AMIGAOS)
Forbid();
@try {
if (*control == 0) {
*control = 1;
func();
}
} @finally {
Permit();
}
#else
# error No of_once available
#endif
}
|
>
>
<
>
>
>
>
>
>
>
>
>
>
>
|
|
>
|
>
>
>
>
|
>
>
|
|
>
>
>
>
|
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
* Public License, either version 2 or 3, which can be found in the file
* LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
* file.
*/
#include "config.h"
#include <stdbool.h>
#import "once.h"
#ifdef OF_AMIGAOS
# include <proto/exec.h>
#endif
#if defined(OF_HAVE_THREADS) && defined(OF_HAVE_ATOMIC_OPS)
# import "atomic.h"
# import "mutex.h"
#endif
void
of_once(of_once_t *control, void (*func)(void))
{
#if !defined(OF_HAVE_THREADS)
if (*control == 0) {
func();
*control = 1;
}
#elif defined(OF_HAVE_PTHREADS)
pthread_once(control, func);
#elif defined(OF_HAVE_ATOMIC_OPS)
/* Avoid atomic operations in case it's already done. */
if (*control == 2)
return;
if (of_atomic_int_cmpswap(control, 0, 1)) {
func();
of_memory_barrier();
of_atomic_int_inc(control);
} else
while (*control == 1)
of_thread_yield();
#elif defined(OF_AMIGAOS)
bool run = false;
/* Avoid Forbid() in case it's already done. */
if (*control == 2)
return;
Forbid();
switch (*control) {
case 0:
*control = 1;
run = true;
break;
case 1:
while (*control == 1) {
Permit();
Forbid();
}
}
Permit();
if (run) {
func();
*control = 2;
}
#else
# error No of_once available
#endif
}
|