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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
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
|
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
+
-
+
-
+
|
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#import "runtime.h"
#import "runtime-private.h"
#import "threading.h"
static objc_mutex_t global_mutex;
static of_rmutex_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;
mutex->count = 0;
return YES;
}
BOOL
objc_mutex_lock(objc_mutex_t *mutex)
{
if (mutex->count > 0 && of_thread_is_current(mutex->owner)) {
mutex->count++;
return YES;
}
if (!of_mutex_lock(&mutex->mutex))
return NO;
mutex->owner = of_thread_current();
mutex->count++;
return YES;
}
BOOL
objc_mutex_unlock(objc_mutex_t *mutex)
{
if (--mutex->count == 0)
return of_mutex_unlock(&mutex->mutex);
return YES;
}
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))
if (!of_rmutex_new(&global_mutex))
OBJC_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))
if (!of_rmutex_lock(&global_mutex))
OBJC_ERROR("Failed to lock global mutex!");
}
void
objc_global_mutex_unlock(void)
{
if (!objc_mutex_unlock(&global_mutex))
if (!of_rmutex_unlock(&global_mutex))
OBJC_ERROR("Failed to unlock global mutex!");
}
void
objc_global_mutex_free(void)
{
if (!objc_mutex_free(&global_mutex))
if (!of_rmutex_free(&global_mutex))
OBJC_ERROR("Failed to free global mutex!");
}
|