ObjFW  Check-in [839f45a293]

Overview
Comment:Replace some macros with OF_INLINE functions.
This way, there won't be a warning about unused results.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 839f45a2930718de6f3bdaabc620c4dceea1579cf6cecf203b007ca0dd5e144c
User & Date: js on 2010-01-30 02:09:07
Other Links: manifest | tags
Context
2010-01-30
10:47
Check return value of of_spinlock_*. check-in: 8a97fac06f user: js tags: trunk
02:09
Replace some macros with OF_INLINE functions.
This way, there won't be a warning about unused results.
check-in: 839f45a293 user: js tags: trunk
01:50
Fall back to spinlocks if atomic ops are unavailable. check-in: bd6a71aad3 user: js tags: trunk
Changes

Modified src/atomic.h from [a1c4c931ea] to [243643e5ed].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20










21
22
23
24
25
26
27
/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "objfw-defs.h"

#if !defined(OF_THREADS)
# define of_atomic_add32(p, i) (*p += i)
# define of_atomic_sub32(p, i) (*p -= i)
# define of_atomic_or32(p, i) (*p |= i)
# define of_atomic_and32(p, i) (*p &= i)
# define of_atomic_xor32(p, i) (*p ^= i)
# define of_atomic_cmpswap32(p, o, n) (*p == o ? ((*p = n) ? 1 : 1) : 0)










#elif defined(OF_HAVE_GCC_ATOMIC_OPS)
# define of_atomic_add32(p, i) __sync_add_and_fetch(p, i)
# define of_atomic_sub32(p, i) __sync_sub_and_fetch(p, i)
# define of_atomic_or32(p, i) __sync_or_and_fetch(p, i)
# define of_atomic_and32(p, i) __sync_and_and_fetch(p, i)
# define of_atomic_xor32(p, i) __sync_xor_and_fetch(p, i)
# define of_atomic_cmpswap32(p, o, n) __sync_bool_compare_and_swap(p, o, n)











|







|
>
>
>
>
>
>
>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "OFMacros.h"

#if !defined(OF_THREADS)
# define of_atomic_add32(p, i) (*p += i)
# define of_atomic_sub32(p, i) (*p -= i)
# define of_atomic_or32(p, i) (*p |= i)
# define of_atomic_and32(p, i) (*p &= i)
# define of_atomic_xor32(p, i) (*p ^= i)

static OF_INLINE BOOL
of_atomic_cmpswap32(int32_t *p, int32_t o, int32_t n)
{
	if (*p == o) {
		*p = n;
		return YES;
	}

	return NO;
}
#elif defined(OF_HAVE_GCC_ATOMIC_OPS)
# define of_atomic_add32(p, i) __sync_add_and_fetch(p, i)
# define of_atomic_sub32(p, i) __sync_sub_and_fetch(p, i)
# define of_atomic_or32(p, i) __sync_or_and_fetch(p, i)
# define of_atomic_and32(p, i) __sync_and_and_fetch(p, i)
# define of_atomic_xor32(p, i) __sync_xor_and_fetch(p, i)
# define of_atomic_cmpswap32(p, o, n) __sync_bool_compare_and_swap(p, o, n)

Modified src/threading.h from [f8a41f877f] to [27e884cfec].

22
23
24
25
26
27
28









29
30
31
32
33
34
35
typedef pthread_key_t of_tlskey_t;
#elif defined(_WIN32)
#include <windows.h>
typedef HANDLE of_thread_t;
typedef CRITICAL_SECTION of_mutex_t;
typedef DWORD of_tlskey_t;
#endif










#if defined(OF_HAVE_PTHREADS)
# define of_thread_is_current(t) pthread_equal(t, pthread_self())
# define of_thread_current() pthread_self()
#elif defined(_WIN32)
# define of_thread_is_current(t) (t == GetCurrentThread())
# define of_thread_current() GetCurrentThread()







>
>
>
>
>
>
>
>
>







22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
typedef pthread_key_t of_tlskey_t;
#elif defined(_WIN32)
#include <windows.h>
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 pthread_mutex_t of_spinlock_t;
#endif

#if defined(OF_HAVE_PTHREADS)
# define of_thread_is_current(t) pthread_equal(t, pthread_self())
# define of_thread_current() pthread_self()
#elif defined(_WIN32)
# define of_thread_is_current(t) (t == GetCurrentThread())
# define of_thread_current() GetCurrentThread()
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

#if defined(OF_HAVE_PTHREADS)
	return (pthread_key_delete(key) ? NO : YES);
#elif defined(_WIN32)
	return (TlsFree(key) ? YES : NO);
#endif
}




#if defined(OF_ATOMIC_OPS)








# import "atomic.h"

typedef int32_t of_spinlock_t;

# define of_spinlock_new(s) ((*(s) = 0) + YES)
# define of_spinlock_trylock(s) (of_atomic_cmpswap32(s, 0, 1) ? YES : NO)






# ifdef OF_HAVE_SCHED_YIELD

#  define of_spinlock_lock(s) \


	while (!of_spinlock_trylock(s)) \

		sched_yield()






# else
#  define of_spinlock_lock(s) while (!of_spinlock_trylock(s));
# endif



# define of_spinlock_unlock(s) *(s) = 0












# define of_spinlock_free(s) YES



#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
typedef pthread_spinlock_t of_spinlock_t;
# define of_spinlock_new(s) (pthread_spin_init(s, 0) ? NO : YES)
# define of_spinlock_trylock(s) (pthread_spin_trylock(s) ? NO : YES)
# define of_spinlock_lock(s) pthread_spin_lock(s)
# define of_spinlock_unlock(s) pthread_spin_unlock(s)
# define of_spinlock_free(s) (pthread_spin_destroy(s) ? NO : YES)
#else
typedef of_mutex_t of_spinlock_t;
# define of_spinlock_new(s) of_mutex_new(s)
# define of_spinlock_trylock(s) of_mutex_trylock(s)
# define of_spinlock_lock(s) of_mutex_lock(s)
# define of_spinlock_unlock(s) of_mutex_unlock(s)
# define of_spinlock_free(s) of_mutex_free(s)
#endif








>
>
>

>
>
>
>
>
>
>
>
|
>
|
>
|
|
>
>
>
>
>
>
|
>
|
>
>
|
>
|
>
>
>
>
>
>
|
|
|
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>

<
<
<
<
<
|

<
<
<
<
<
|

>
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
#if defined(OF_HAVE_PTHREADS)
	return (pthread_key_delete(key) ? NO : YES);
#elif defined(_WIN32)
	return (TlsFree(key) ? YES : NO);
#endif
}

static OF_INLINE BOOL
of_spinlock_new(of_spinlock_t *s)
{
#if defined(OF_ATOMIC_OPS)
	*s = 0;
	return YES;
#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
	return (pthread_spin_init(s, 0) ? NO : YES);
#else
	return of_mutex_new(s);
#endif
}

static OF_INLINE BOOL
of_spinlock_trylock(of_spinlock_t *s)
{
#if defined(OF_ATOMIC_OPS)
	return (of_atomic_cmpswap32(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)
	*s = 0;
	return YES;
#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
	return (pthread_spin_unlock(s) ? NO : YES);
#else
	return of_mutex_unlock(s);
#endif
}

static OF_INLINE BOOL
of_spinlock_free(of_spinlock_t *s)
{
#if defined(OF_ATOMIC_OPS)
	return YES;
#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)





	return (pthread_spin_destroy(s) ? NO : YES);
#else





	return of_mutex_free(s);
#endif
}