ObjFW  Check-in [24796a2dc1]

Overview
Comment:Add of_memory_barrier_{producer,consumer}()

Also changes the naming slightly (leave -> exit). This is more similar
to the membar_*() API NetBSD and OpenBSD provides.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 24796a2dc1a00886490ba88d40a27cf2cd342c9bca29178a3bfe6765679b80e1
User & Date: js on 2016-07-31 21:54:30
Other Links: manifest | tags
Context
2016-07-31
23:04
Split atomic.h into multiple files check-in: 7115c55ef0 user: js tags: trunk
21:54
Add of_memory_barrier_{producer,consumer}() check-in: 24796a2dc1 user: js tags: trunk
2016-07-30
23:29
atomic.h: Use the OSAtomic variant without barrier check-in: 0c3e3da576 user: js tags: trunk
Changes

Modified src/atomic.h from [50acc02363] to [0145f59683].

964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
	return OSAtomicCompareAndSwapPtr(o, n, p);
#else
# error of_atomic_ptr_cmpswap not implemented!
#endif
}

static OF_INLINE void
of_memory_barrier(void)
{
#if !defined(OF_HAVE_THREADS)
	/* nop */
#elif defined(OF_X86_64_ASM)
	__asm__ __volatile__ (
	    "mfence" ::: "memory"
	);







|







964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
	return OSAtomicCompareAndSwapPtr(o, n, p);
#else
# error of_atomic_ptr_cmpswap not implemented!
#endif
}

static OF_INLINE void
of_memory_barrier_sync(void)
{
#if !defined(OF_HAVE_THREADS)
	/* nop */
#elif defined(OF_X86_64_ASM)
	__asm__ __volatile__ (
	    "mfence" ::: "memory"
	);
990
991
992
993
994
995
996


997

998

999












1000
1001
1002
1003
1004



1005

1006
1007
1008
	OSMemoryBarrier();
#else
# error of_memory_barrier not implemented!
#endif
}

static OF_INLINE void


of_memory_enter_barrier(void)

{

	of_memory_barrier();












}

static OF_INLINE void
of_memory_leave_barrier(void)
{



	of_memory_barrier();

}

OF_ASSUME_NONNULL_END







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



|

>
>
>
|
>



990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
	OSMemoryBarrier();
#else
# error of_memory_barrier not implemented!
#endif
}

static OF_INLINE void
of_memory_barrier_enter(void)
{
	of_memory_barrier_sync();
}

static OF_INLINE void
of_memory_barrier_exit(void)
{
	of_memory_barrier_sync();
}

static OF_INLINE void
of_memory_barrier_producer(void)
{
#if defined(OF_X86_64_ASM)
	__asm__ __volatile__ ("sfence" ::: "memory");
#else
	of_memory_barrier_sync();
#endif
}

static OF_INLINE void
of_memory_barrier_consumer(void)
{
#if defined(OF_X86_64_ASM)
	__asm__ __volatile__ ("lfence" ::: "memory");
#else
	of_memory_barrier_sync();
#endif
}

OF_ASSUME_NONNULL_END

Modified src/threading.h from [8449195015] to [91e8883d41].

180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
}

static OF_INLINE bool
of_spinlock_trylock(of_spinlock_t *spinlock)
{
#if defined(OF_HAVE_ATOMIC_OPS)
	if (of_atomic_int_cmpswap(spinlock, 0, 1)) {
		of_memory_enter_barrier();
		return true;
	}

	return false;
#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
	return !pthread_spin_trylock(spinlock);
#else







|







180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
}

static OF_INLINE bool
of_spinlock_trylock(of_spinlock_t *spinlock)
{
#if defined(OF_HAVE_ATOMIC_OPS)
	if (of_atomic_int_cmpswap(spinlock, 0, 1)) {
		of_memory_barrier_enter();
		return true;
	}

	return false;
#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
	return !pthread_spin_trylock(spinlock);
#else
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233

static OF_INLINE bool
of_spinlock_unlock(of_spinlock_t *spinlock)
{
#if defined(OF_HAVE_ATOMIC_OPS)
	bool ret = of_atomic_int_cmpswap(spinlock, 1, 0);

	of_memory_leave_barrier();

	return ret;
#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
	return !pthread_spin_unlock(spinlock);
#else
	return of_mutex_unlock(spinlock);
#endif







|







219
220
221
222
223
224
225
226
227
228
229
230
231
232
233

static OF_INLINE bool
of_spinlock_unlock(of_spinlock_t *spinlock)
{
#if defined(OF_HAVE_ATOMIC_OPS)
	bool ret = of_atomic_int_cmpswap(spinlock, 1, 0);

	of_memory_barrier_exit();

	return ret;
#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
	return !pthread_spin_unlock(spinlock);
#else
	return of_mutex_unlock(spinlock);
#endif