ObjFW  Diff

Differences From Artifact [af444c86d5]:

To Artifact [ac90a9eaf1]:


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
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







-
+
+
+
+
+





-
-
-
-
-

-
+













-
+


-
+

-
+


-
+







 * file.
 */

#include "config.h"

#include <stdbool.h>

#import "once.h"
#import "OFOnce.h"
#if defined(OF_HAVE_THREADS) && defined(OF_HAVE_ATOMIC_OPS)
# import "OFAtomic.h"
# import "OFPlainMutex.h"
#endif

#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))
OFOnce(OFOnceControl *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)) {
	if (OFAtomicIntCompareAndSwap(control, 0, 1)) {
		func();

		of_memory_barrier();
		OFMemoryBarrier();

		of_atomic_int_inc(control);
		OFAtomicIntIncrease(control);
	} else
		while (*control == 1)
			of_thread_yield();
			OFYieldThread();
#elif defined(OF_AMIGAOS)
	bool run = false;

	/* Avoid Forbid() in case it's already done. */
	if (*control == 2)
		return;

76
77
78
79
80
81
82
83

84
85
75
76
77
78
79
80
81

82
83
84







-
+


	Permit();

	if (run) {
		func();
		*control = 2;
	}
#else
# error No of_once available
# error No OFOnce available
#endif
}