ObjFW  Check-in [e58b9661a5]

Overview
Comment:Rename ERROR to SYNC_ERR in objc_sync.m to prevent conflicts.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: e58b9661a52b18514587ce0d9fc0a93e4e6a891a1d42b0e4da5853791619cb3d
User & Date: js on 2009-06-09 17:19:13
Other Links: manifest | tags
Context
2009-06-09
20:10
Move documentation of mutating methods.
Mutating methods are now documented in the mutable class.
check-in: 41974cf38d user: js tags: trunk
17:19
Rename ERROR to SYNC_ERR in objc_sync.m to prevent conflicts. check-in: e58b9661a5 user: js tags: trunk
13:21
Handle reads with a returned size of 0 safely in -[readLine]. check-in: b464ddcda5 user: js tags: trunk
Changes

Modified src/objc_sync.m from [5b0b24464d] to [8a15b08565].

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
	return YES;
}

#define thread_is_current(t) (t == GetCurrentThreadId())
#define thread_current() GetCurrentThreadId()
#endif

#define ERROR(f)							\
	{								\
		fprintf(stderr, "WARNING: %s failed in line %d!\n"	\
		    "WARNING: This might result in a race "		\
		    "condition!\n", f, __LINE__);			\
		return 1;						\
	}








|







104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
	return YES;
}

#define thread_is_current(t) (t == GetCurrentThreadId())
#define thread_current() GetCurrentThreadId()
#endif

#define SYNC_ERR(f)							\
	{								\
		fprintf(stderr, "WARNING: %s failed in line %d!\n"	\
		    "WARNING: This might result in a race "		\
		    "condition!\n", f, __LINE__);			\
		return 1;						\
	}

127
128
129
130
131
132
133
134
135
136
137
138
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
{
	int i;

	if (obj == nil)
		return 0;

	if (!mutex_lock(&mutex))
		ERROR("mutex_lock(&mutex)");

	for (i = num_locks - 1; i >= 0; 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))
					ERROR("mutex_unlock(&mutex)");

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

				if (!mutex_lock(&mutex))
					ERROR("mutex_lock(&mutex)");

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

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

			if (!mutex_unlock(&mutex))
				ERROR("mutex_unlock(&mutex)");

			return 0;
		}
	}

	if (locks == NULL) {
		if ((locks = malloc(sizeof(struct locks_s))) == NULL) {
			mutex_unlock(&mutex);
			ERROR("malloc(...)");
		}
	} else {
		struct locks_s *new_locks;

		if ((new_locks = realloc(locks, (num_locks + 1) *
		    sizeof(struct locks_s))) == NULL) {
			mutex_unlock(&mutex);
			ERROR("realloc(...)");
		}

		locks = new_locks;
	}

	locks[num_locks].obj = obj;
	locks[num_locks].count = 1;
	locks[num_locks].recursion = 0;
	locks[num_locks].thread = thread_current();

	if (!mutex_new(&locks[num_locks].mutex)) {
		mutex_unlock(&mutex);
		ERROR("mutex_new(&locks[num_locks].mutex");
	}

	if (!mutex_lock(&locks[num_locks].mutex)) {
		mutex_unlock(&mutex);
		ERROR("mutex_lock(&locks[num_locks].mutex");
	}

	num_locks++;

	if (!mutex_unlock(&mutex))
		ERROR("mutex_unlock(&mutex)");

	return 0;
}

int
objc_sync_exit(id obj)
{
	int i;

	if (obj == nil)
		return 0;

	if (!mutex_lock(&mutex))
		ERROR("mutex_lock(&mutex)");

	for (i = num_locks - 1; i >= 0; i--) {
		if (locks[i].obj == obj) {
			if (locks[i].recursion > 0 &&
			    thread_is_current(locks[i].thread)) {
				locks[i].recursion--;

				if (!mutex_unlock(&mutex))
					ERROR("mutex_unlock(&mutex)");

				return 0;
			}

			if (!mutex_unlock(&locks[i].mutex)) {
				mutex_unlock(&mutex);
				ERROR("mutex_unlock(&locks[i].mutex)");
			}

			locks[i].count--;

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

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

				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);
					ERROR("realloc(...)");
				}

				locks = new_locks;
			}

			if (!mutex_unlock(&mutex))
				ERROR("mutex_unlock(&mutex)");

			return 0;
		}
	}

	mutex_unlock(&mutex);
	ERROR("objc_sync_exit()");
}







|











|



|



|








|








|







|












|




|





|













|








|






|









|












|






|






|

127
128
129
130
131
132
133
134
135
136
137
138
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
{
	int i;

	if (obj == nil)
		return 0;

	if (!mutex_lock(&mutex))
		SYNC_ERR("mutex_lock(&mutex)");

	for (i = num_locks - 1; i >= 0; 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))
					SYNC_ERR("mutex_unlock(&mutex)");

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

				if (!mutex_lock(&mutex))
					SYNC_ERR("mutex_lock(&mutex)");

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

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

			if (!mutex_unlock(&mutex))
				SYNC_ERR("mutex_unlock(&mutex)");

			return 0;
		}
	}

	if (locks == NULL) {
		if ((locks = malloc(sizeof(struct locks_s))) == NULL) {
			mutex_unlock(&mutex);
			SYNC_ERR("malloc(...)");
		}
	} else {
		struct locks_s *new_locks;

		if ((new_locks = realloc(locks, (num_locks + 1) *
		    sizeof(struct locks_s))) == NULL) {
			mutex_unlock(&mutex);
			SYNC_ERR("realloc(...)");
		}

		locks = new_locks;
	}

	locks[num_locks].obj = obj;
	locks[num_locks].count = 1;
	locks[num_locks].recursion = 0;
	locks[num_locks].thread = thread_current();

	if (!mutex_new(&locks[num_locks].mutex)) {
		mutex_unlock(&mutex);
		SYNC_ERR("mutex_new(&locks[num_locks].mutex");
	}

	if (!mutex_lock(&locks[num_locks].mutex)) {
		mutex_unlock(&mutex);
		SYNC_ERR("mutex_lock(&locks[num_locks].mutex");
	}

	num_locks++;

	if (!mutex_unlock(&mutex))
		SYNC_ERR("mutex_unlock(&mutex)");

	return 0;
}

int
objc_sync_exit(id obj)
{
	int i;

	if (obj == nil)
		return 0;

	if (!mutex_lock(&mutex))
		SYNC_ERR("mutex_lock(&mutex)");

	for (i = num_locks - 1; i >= 0; i--) {
		if (locks[i].obj == obj) {
			if (locks[i].recursion > 0 &&
			    thread_is_current(locks[i].thread)) {
				locks[i].recursion--;

				if (!mutex_unlock(&mutex))
					SYNC_ERR("mutex_unlock(&mutex)");

				return 0;
			}

			if (!mutex_unlock(&locks[i].mutex)) {
				mutex_unlock(&mutex);
				SYNC_ERR("mutex_unlock(&locks[i].mutex)");
			}

			locks[i].count--;

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

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

				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);
					SYNC_ERR("realloc(...)");
				}

				locks = new_locks;
			}

			if (!mutex_unlock(&mutex))
				SYNC_ERR("mutex_unlock(&mutex)");

			return 0;
		}
	}

	mutex_unlock(&mutex);
	SYNC_ERR("objc_sync_exit()");
}