ObjFW  Check-in [be99da0c09]

Overview
Comment:threading: WinAPI's CriticalSection is recursive

No need to manually implement recursiveness for WinAPI.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: be99da0c0941b2820fba717d06c679923c6ca009b2748f4dee3198f67fdde995
User & Date: js on 2014-10-04 19:24:47
Other Links: manifest | tags
Context
2014-10-04
21:59
Add +[OFSystemInfo native8BitEncoding] check-in: ec66e49dca user: js tags: trunk
19:24
threading: WinAPI's CriticalSection is recursive check-in: be99da0c09 user: js tags: trunk
2014-09-10
22:09
Fix OFBigDataArray's initWithStringRepresentation: check-in: ab270674db user: js tags: trunk
Changes

Modified src/threading.h from [7cf05d0077] to [a8b345125c].

67
68
69
70
71
72
73
74

75
76
77
78
79
80
81
67
68
69
70
71
72
73

74
75
76
77
78
79
80
81







-
+







typedef of_mutex_t of_spinlock_t;
#endif

#ifdef OF_HAVE_SCHED_YIELD
# include <sched.h>
#endif

#ifdef OF_HAVE_RECURSIVE_PTHREAD_MUTEXES
#if defined(OF_HAVE_RECURSIVE_PTHREAD_MUTEXES) || defined(_WIN32)
# define of_rmutex_t of_mutex_t
#else
typedef struct {
	of_mutex_t mutex;
	of_tlskey_t count;
} of_rmutex_t;
#endif

Modified src/threading.m from [ac9dc3c880] to [139fd47bbf].

29
30
31
32
33
34
35
36

37
38
39
40
41
42
43
29
30
31
32
33
34
35

36
37
38
39
40
41
42
43







-
+







#ifdef __HAIKU__
# include <kernel/OS.h>
#endif

bool
of_rmutex_new(of_rmutex_t *rmutex)
{
#ifdef OF_HAVE_RECURSIVE_PTHREAD_MUTEXES
#if defined(OF_HAVE_RECURSIVE_PTHREAD_MUTEXES) || defined(_WIN32)
	pthread_mutexattr_t attr;

	if (pthread_mutexattr_init(&attr) != 0)
		return false;

	if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
		return false;
59
60
61
62
63
64
65
66

67
68
69
70
71
72
73
59
60
61
62
63
64
65

66
67
68
69
70
71
72
73







-
+







	return true;
#endif
}

bool
of_rmutex_lock(of_rmutex_t *rmutex)
{
#ifdef OF_HAVE_RECURSIVE_PTHREAD_MUTEXES
#if defined(OF_HAVE_RECURSIVE_PTHREAD_MUTEXES) || defined(_WIN32)
	return of_mutex_lock(rmutex);
#else
	uintptr_t count = (uintptr_t)of_tlskey_get(rmutex->count);

	if (count > 0) {
		if (!of_tlskey_set(rmutex->count, (void*)(count + 1)))
			return false;
86
87
88
89
90
91
92
93

94
95
96
97
98
99
100
86
87
88
89
90
91
92

93
94
95
96
97
98
99
100







-
+







	return true;
#endif
}

bool
of_rmutex_trylock(of_rmutex_t *rmutex)
{
#ifdef OF_HAVE_RECURSIVE_PTHREAD_MUTEXES
#if defined(OF_HAVE_RECURSIVE_PTHREAD_MUTEXES) || defined(_WIN32)
	return of_mutex_trylock(rmutex);
#else
	uintptr_t count = (uintptr_t)of_tlskey_get(rmutex->count);

	if (count > 0) {
		if (!of_tlskey_set(rmutex->count, (void*)(count + 1)))
			return false;
113
114
115
116
117
118
119
120

121
122
123
124
125
126
127
113
114
115
116
117
118
119

120
121
122
123
124
125
126
127







-
+







	return true;
#endif
}

bool
of_rmutex_unlock(of_rmutex_t *rmutex)
{
#ifdef OF_HAVE_RECURSIVE_PTHREAD_MUTEXES
#if defined(OF_HAVE_RECURSIVE_PTHREAD_MUTEXES) || defined(_WIN32)
	return of_mutex_unlock(rmutex);
#else
	uintptr_t count = (uintptr_t)of_tlskey_get(rmutex->count);

	if (count > 1) {
		if (!of_tlskey_set(rmutex->count, (void*)(count - 1)))
			return false;
138
139
140
141
142
143
144
145

146
147
148
149
150
151
152
153
154
155
156
138
139
140
141
142
143
144

145
146
147
148
149
150
151
152
153
154
155
156







-
+











	return true;
#endif
}

bool
of_rmutex_free(of_rmutex_t *rmutex)
{
#ifdef OF_HAVE_RECURSIVE_PTHREAD_MUTEXES
#if defined(OF_HAVE_RECURSIVE_PTHREAD_MUTEXES) || defined(_WIN32)
	return of_mutex_free(rmutex);
#else
	if (!of_mutex_free(&rmutex->mutex))
		return false;

	if (!of_tlskey_free(rmutex->count))
		return false;

	return true;
#endif
}