ObjFW  Check-in [1edd5313ae]

Overview
Comment:Improve spinlock implementation.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 1edd5313aee03e4be93bb21a44970889c00ff549fd6aea64633bdcc18d53b17c
User & Date: js on 2010-11-06 11:40:24
Other Links: manifest | tags
Context
2010-11-06
11:44
More reliable cleanup on failure in OFMutex and OFTLSKey. check-in: f1d813ef6a user: js tags: trunk
11:40
Improve spinlock implementation. check-in: 1edd5313ae user: js tags: trunk
11:27
Add -[OFArray objectsInRange:]. check-in: 009bf787e3 user: js tags: trunk
Changes

Modified src/threading.h from [1fc17ada35] to [707f139f43].

25
26
27
28
29
30
31
32

33
34
35
36
37
38
39
typedef HANDLE of_thread_t;
typedef CRITICAL_SECTION of_mutex_t;
typedef DWORD of_tlskey_t;
#endif

#if defined(OF_ATOMIC_OPS)
# import "atomic.h"
typedef int32_t of_spinlock_t;

#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
typedef pthread_spinlock_t of_spinlock_t;
#else
typedef of_mutex_t of_spinlock_t;
#endif

#if defined(OF_HAVE_PTHREADS)







|
>







25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
typedef HANDLE of_thread_t;
typedef CRITICAL_SECTION of_mutex_t;
typedef DWORD of_tlskey_t;
#endif

#if defined(OF_ATOMIC_OPS)
# import "atomic.h"
typedef volatile int of_spinlock_t;
# define OF_SPINCOUNT 10
#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
typedef pthread_spinlock_t of_spinlock_t;
#else
typedef of_mutex_t of_spinlock_t;
#endif

#if defined(OF_HAVE_PTHREADS)
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
#endif
}

static OF_INLINE BOOL
of_spinlock_trylock(of_spinlock_t *s)
{
#if defined(OF_ATOMIC_OPS)
	return (of_atomic_cmpswap_32(s, 0, 1) ? YES : NO);
#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
	return (pthread_spin_trylock(s) ? NO : YES);
#else
	return of_mutex_trylock(s);
#endif
}

static OF_INLINE BOOL
of_spinlock_lock(of_spinlock_t *s)
{
#if defined(OF_ATOMIC_OPS)




	while (!of_spinlock_trylock(s)) {

# ifdef OF_HAVE_SCHED_YIELD

		sched_yield();


# endif
	}

	return YES;
#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
	return (pthread_spin_lock(s) ? NO : YES);
#else
	return of_mutex_lock(s);
#endif
}

static OF_INLINE BOOL
of_spinlock_unlock(of_spinlock_t *s)
{
#if defined(OF_ATOMIC_OPS)
	of_atomic_and_32((uint32_t*)s, 0);
	return YES;
#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
	return (pthread_spin_unlock(s) ? NO : YES);
#else
	return of_mutex_unlock(s);
#endif
}







|











>
>
>
>
|
>
|
>

>
>

<













|







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
#endif
}

static OF_INLINE BOOL
of_spinlock_trylock(of_spinlock_t *s)
{
#if defined(OF_ATOMIC_OPS)
	return (of_atomic_cmpswap_int(s, 0, 1) ? YES : NO);
#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
	return (pthread_spin_trylock(s) ? NO : YES);
#else
	return of_mutex_trylock(s);
#endif
}

static OF_INLINE BOOL
of_spinlock_lock(of_spinlock_t *s)
{
#if defined(OF_ATOMIC_OPS)
# ifdef OF_HAVE_SCHED_YIELD
	int i;

	for (i = 0; i < OF_SPINCOUNT; i++)
		if (of_spinlock_trylock(s))
			return YES;

	while (!of_spinlock_trylock(s))
		sched_yield();
# else
	while (!of_spinlock_trylock(s));
# endif


	return YES;
#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
	return (pthread_spin_lock(s) ? NO : YES);
#else
	return of_mutex_lock(s);
#endif
}

static OF_INLINE BOOL
of_spinlock_unlock(of_spinlock_t *s)
{
#if defined(OF_ATOMIC_OPS)
	*s = 0;
	return YES;
#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
	return (pthread_spin_unlock(s) ? NO : YES);
#else
	return of_mutex_unlock(s);
#endif
}