ObjFW  Check-in [ba5e756264]

Overview
Comment:threading.h: Add of_once()
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: ba5e75626485a4de7b21693dd9790871db41807b2b619c19b29d711e61d3a1d3
User & Date: js on 2014-02-28 00:38:15
Other Links: manifest | tags
Context
2014-02-28
00:41
Make MinGW32 and MinGW-w64 happy at the same time check-in: 5ca844dd3b user: js tags: trunk
00:38
threading.h: Add of_once() check-in: ba5e756264 user: js tags: trunk
2014-02-27
22:40
Allow thread-unsafe getaddrinfo() with locks check-in: fae85e954f user: js tags: trunk
Changes

Modified src/runtime/threading.m from [ec09e7071d] to [0bd9199ab6].

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
#include <stdlib.h>

#import "runtime.h"
#import "runtime-private.h"
#import "threading.h"

static of_rmutex_t global_mutex;
static bool global_mutex_init = false;

static void
objc_global_mutex_new(void)
{
	if (!of_rmutex_new(&global_mutex))
		OBJC_ERROR("Failed to create global mutex!");

	global_mutex_init = true;
}

void
objc_global_mutex_lock(void)
{
	if (!global_mutex_init)
		objc_global_mutex_new();

	if (!of_rmutex_lock(&global_mutex))
		OBJC_ERROR("Failed to lock global mutex!");
}

void
objc_global_mutex_unlock(void)







|


|



<
<





|
<







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
#include <stdlib.h>

#import "runtime.h"
#import "runtime-private.h"
#import "threading.h"

static of_rmutex_t global_mutex;
static of_once_t once_control = OF_ONCE_INIT;

static void
init(void)
{
	if (!of_rmutex_new(&global_mutex))
		OBJC_ERROR("Failed to create global mutex!");


}

void
objc_global_mutex_lock(void)
{
	of_once(&once_control, init);


	if (!of_rmutex_lock(&global_mutex))
		OBJC_ERROR("Failed to lock global mutex!");
}

void
objc_global_mutex_unlock(void)

Modified src/socket.m from [75d8ae44e0] to [e7e7f50976].

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
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#import "socket.h"





static bool initialized = false;

bool
of_init_sockets()
{
	if (initialized)
		return true;

#ifdef _WIN32
	WSADATA wsa;

	if (WSAStartup(MAKEWORD(2, 0), &wsa))
		return false;
#elif defined(__wii__)
	if (net_init() < 0)
		return false;
#endif

	initialized = true;












	return true;
}







>
>

>
>


|
|

<
<
<
|



|


|



>
>
>
>
>
>
>
>
>
>
>
>
|

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
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#import "socket.h"
#ifdef OF_HAVE_THREADS
# include "threading.h"

static of_once_t onceControl = OF_ONCE_INIT;
#endif
static bool initialized = false;

static void
init(void)
{



#if defined(_WIN32)
	WSADATA wsa;

	if (WSAStartup(MAKEWORD(2, 0), &wsa))
		return;
#elif defined(__wii__)
	if (net_init() < 0)
		return;
#endif

	initialized = true;
}

bool
of_init_sockets()
{
#ifdef OF_HAVE_THREADS
	of_once(&onceControl, init);
#else
	if (!initialized)
		init();
#endif

	return initialized;
}

Modified src/threading.h from [682651858d] to [1628bdd768].

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

#if defined(OF_HAVE_PTHREADS)
# include <pthread.h>
typedef pthread_t of_thread_t;
typedef pthread_key_t of_tlskey_t;
typedef pthread_mutex_t of_mutex_t;
typedef pthread_cond_t of_condition_t;


#elif defined(_WIN32)
/*
 * winsock2.h needs to be included before windows.h. Not including it here
 * would make it impossible to use sockets after threading.h has been
 * imported.
 */
# ifdef OF_HAVE_SOCKETS
#  include <winsock2.h>
# endif
# include <windows.h>
typedef HANDLE of_thread_t;
typedef DWORD of_tlskey_t;
typedef CRITICAL_SECTION of_mutex_t;
typedef struct {
	HANDLE event;
	int count;
} of_condition_t;


#else
# error No threads available!
#endif

#if defined(OF_HAVE_ATOMIC_OPS)
# import "atomic.h"
typedef volatile int of_spinlock_t;







>
>

















>
>







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

#if defined(OF_HAVE_PTHREADS)
# include <pthread.h>
typedef pthread_t of_thread_t;
typedef pthread_key_t of_tlskey_t;
typedef pthread_mutex_t of_mutex_t;
typedef pthread_cond_t of_condition_t;
typedef pthread_once_t of_once_t;
# define OF_ONCE_INIT PTHREAD_ONCE_INIT
#elif defined(_WIN32)
/*
 * winsock2.h needs to be included before windows.h. Not including it here
 * would make it impossible to use sockets after threading.h has been
 * imported.
 */
# ifdef OF_HAVE_SOCKETS
#  include <winsock2.h>
# endif
# include <windows.h>
typedef HANDLE of_thread_t;
typedef DWORD of_tlskey_t;
typedef CRITICAL_SECTION of_mutex_t;
typedef struct {
	HANDLE event;
	int count;
} of_condition_t;
typedef volatile LONG of_once_t;
# define OF_ONCE_INIT 0
#else
# error No threads available!
#endif

#if defined(OF_HAVE_ATOMIC_OPS)
# import "atomic.h"
typedef volatile int of_spinlock_t;
146
147
148
149
150
151
152





















153
154
155
156
157
158
159
	pthread_exit(NULL);
#elif defined(_WIN32)
	ExitThread(0);
#else
# error of_thread_exit not implemented!
#endif
}






















static OF_INLINE bool
of_mutex_new(of_mutex_t *mutex)
{
#if defined(OF_HAVE_PTHREADS)
	return !pthread_mutex_init(mutex, NULL);
#elif defined(_WIN32)







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
	pthread_exit(NULL);
#elif defined(_WIN32)
	ExitThread(0);
#else
# error of_thread_exit not implemented!
#endif
}

static OF_INLINE void
of_once(of_once_t *control, void (*func)(void))
{
#if defined(OF_HAVE_PTHREADS)
	pthread_once(control, func);
#elif defined(_WIN32)
	switch (InterlockedCompareExchange(control, 1, 0)) {
	case 0:
		func();
		InterlockedIncrement(control);
		break;
	case 1:
		while (*control == 1)
			Sleep(0);
		break;
	}
#else
# error of_once not implemented!
#endif
}

static OF_INLINE bool
of_mutex_new(of_mutex_t *mutex)
{
#if defined(OF_HAVE_PTHREADS)
	return !pthread_mutex_init(mutex, NULL);
#elif defined(_WIN32)