ObjFW  Check-in [51b73c821c]

Overview
Comment:Safer way to create the global mutex.

__attribute__((constructor)) might not have been called when the runtime
is first used. However, the first usage of the runtime is always
single-threaded, so it is safe to do it this way.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | runtime
Files: files | file ages | folders
SHA3-256: 51b73c821c8f1f2e93067376fb0cf2190e6558000715172796f066502cc174e9
User & Date: js on 2012-04-22 16:05:21
Other Links: branch diff | manifest | tags
Context
2012-04-22
16:12
Don't reexport libobjc when using ObjFW runtime. check-in: e05fff3653 user: js tags: runtime
16:05
Safer way to create the global mutex. check-in: 51b73c821c user: js tags: runtime
16:02
Add --enable-runtime. check-in: 5ca3d6302a user: js tags: runtime
Changes

Modified src/runtime/threading.m from [2d30e1cc90] to [92014702e9].

19
20
21
22
23
24
25

26
27
28
29
30
31
32
#include <stdio.h>
#include <stdlib.h>

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

static objc_mutex_t global_mutex;


BOOL
objc_mutex_new(objc_mutex_t *mutex)
{
	if (!of_mutex_new(&mutex->mutex ))
		return NO;








>







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <stdlib.h>

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

static objc_mutex_t global_mutex;
static BOOL global_mutex_init = NO;

BOOL
objc_mutex_new(objc_mutex_t *mutex)
{
	if (!of_mutex_new(&mutex->mutex ))
		return NO;

63
64
65
66
67
68
69
70
71
72
73
74


75
76
77
78
79



80
81
82
83
84
85
86

BOOL
objc_mutex_free(objc_mutex_t *mutex)
{
	return of_mutex_free(&mutex->mutex);
}

static void __attribute__((constructor))
objc_global_mutex_new(void)
{
	if (!objc_mutex_new(&global_mutex))
		ERROR("Failed to create global mutex!");


}

void
objc_global_mutex_lock(void)
{



	if (!objc_mutex_lock(&global_mutex))
		ERROR("Failed to lock global mutex!");
}

void
objc_global_mutex_unlock(void)
{







|




>
>





>
>
>







64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

BOOL
objc_mutex_free(objc_mutex_t *mutex)
{
	return of_mutex_free(&mutex->mutex);
}

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

	global_mutex_init = YES;
}

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

	if (!objc_mutex_lock(&global_mutex))
		ERROR("Failed to lock global mutex!");
}

void
objc_global_mutex_unlock(void)
{