ObjFW  Check-in [47f08207d7]

Overview
Comment:Small optimization for of_once()
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 47f08207d740a230884e4a9309544dec8a786a32800d2aae5471f4bfd2136fdb
User & Date: js on 2019-08-18 20:04:45
Other Links: manifest | tags
Context
2019-08-18
21:32
configure: Enable threads on AmigaOS and MorphOS check-in: beb77aaea1 user: js tags: trunk
20:04
Small optimization for of_once() check-in: 47f08207d7 user: js tags: trunk
2019-08-11
18:56
Support sockets in multiple threads on AmigaOS check-in: a40f3fc829 user: js tags: trunk
Changes

Modified src/OFFile.m from [eb447a10e1] to [e53f8eae67].

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef O_EXLOCK
# define O_EXLOCK 0
#endif

#ifndef OF_AMIGAOS
# define closeHandle(h) close(h)
#else
struct of_file_handle {
	of_file_handle_t previous, next;
	BPTR handle;
	bool append;
} *firstHandle = NULL;

static void
closeHandle(of_file_handle_t handle)







|







74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef O_EXLOCK
# define O_EXLOCK 0
#endif

#ifndef OF_AMIGAOS
# define closeHandle(h) close(h)
#else
static struct of_file_handle {
	of_file_handle_t previous, next;
	BPTR handle;
	bool append;
} *firstHandle = NULL;

static void
closeHandle(of_file_handle_t handle)

Modified src/once.m from [4811cc42bf] to [6a54846aa0].

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
}