ObjFW  Diff

Differences From Artifact [9f958f8552]:

To Artifact [469f559547]:


9
10
11
12
13
14
15

16
17
18
19
20
21
22
 * the packaging of this file.
 */

#include "config.h"

#include <stddef.h>
#include <stdlib.h>


#ifndef _WIN32
#include <pthread.h>
#endif

#import <objc/objc.h>








>







9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * the packaging of this file.
 */

#include "config.h"

#include <stddef.h>
#include <stdlib.h>
#include <assert.h>

#ifndef _WIN32
#include <pthread.h>
#endif

#import <objc/objc.h>

138
139
140
141
142
143
144
145







146
147
148
149
150






151

152
153
154
155
156
157
158
	if (!mutex_lock(&mutex))
		return 1;

	for (i = 0; i < num_locks; i++) {
		if (locks[i].obj == obj) {
			if (thread_is_current(locks[i].thread))
				locks[i].recursion++;
			else







				if (!mutex_lock(&locks[i].mutex)) {
					mutex_unlock(&mutex);
					return 1;
				}







			locks[i].count++;


			if (!mutex_unlock(&mutex))
				return 1;

			return 0;
		}
	}







|
>
>
>
>
>
>
>





>
>
>
>
>
>
|
>







139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
	if (!mutex_lock(&mutex))
		return 1;

	for (i = 0; i < num_locks; i++) {
		if (locks[i].obj == obj) {
			if (thread_is_current(locks[i].thread))
				locks[i].recursion++;
			else {
				/* Make sure objc_sync_exit doesn't free it */
				locks[i].count++;

				/* Unlock so objc_sync_exit can return */
				if (!mutex_unlock(&mutex))
					return 1;

				if (!mutex_lock(&locks[i].mutex)) {
					mutex_unlock(&mutex);
					return 1;
				}

				if (!mutex_lock(&mutex))
					return 1;

				assert(locks[i].recursion == 0);

				/* Update lock's active thread */
				locks[i].thread = thread_current();
			}

			if (!mutex_unlock(&mutex))
				return 1;

			return 0;
		}
	}
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240



241
242

243
244
245
246
247
248
249
				mutex_unlock(&mutex);
				return 1;
			}

			locks[i].count--;

			if (locks[i].count == 0) {
				struct locks_s *new_locks;

				if (!mutex_free(&locks[i].mutex)) {
					mutex_unlock(&mutex);
					return 1;
				}

				num_locks--;
				locks[i] = locks[num_locks];




				if ((new_locks = realloc(locks, (num_locks) *
				    sizeof(struct locks_s))) == NULL) {

					mutex_unlock(&mutex);
					return 1;
				}

				locks = new_locks;
			}








|









>
>
>
|
|
>







239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
				mutex_unlock(&mutex);
				return 1;
			}

			locks[i].count--;

			if (locks[i].count == 0) {
				struct locks_s *new_locks = NULL;

				if (!mutex_free(&locks[i].mutex)) {
					mutex_unlock(&mutex);
					return 1;
				}

				num_locks--;
				locks[i] = locks[num_locks];

				if (num_locks == 0) {
					free(locks);
					new_locks = NULL;
				} else if ((new_locks = realloc(locks,
				    num_locks * sizeof(struct locks_s))) ==
				    NULL) {
					mutex_unlock(&mutex);
					return 1;
				}

				locks = new_locks;
			}