Skip to content

Commit be41174

Browse files
committed
Detect is os_unfair_lock is available at runtime, in order to be able to support older versions of macOS in the same binaries.
1 parent f31a4bd commit be41174

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

changelog.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ Date: TBD
7272
[DESCRIPTION]
7373
Changes
7474

75+
[ENTRY]
76+
Module: support
77+
What: change
78+
Rank: minor
79+
[DESCRIPTION]
80+
Detect is os_unfair_lock is available at runtime, in order to be able
81+
to support older versions of macOS in the same binaries.
82+
7583
[ENTRY]
7684
Module: flatzinc
7785
What: change

gecode/support/thread.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,14 @@ namespace Gecode { namespace Support {
108108
/// Use a simple but more efficient critical section on Windows
109109
CRITICAL_SECTION w_cs;
110110
#elif defined(GECODE_THREADS_OSX_UNFAIR)
111-
/// Use unfair lock on macOS
112-
os_unfair_lock lck;
111+
/// Use unfair lock on macOS if available, OSSpinLock on older macOS
112+
union {
113+
os_unfair_lock unfair_lck;
114+
#pragma clang diagnostic push
115+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
116+
OSSpinLock spin_lck;
117+
#pragma clang diagnostic pop
118+
} u;
113119
#elif defined(GECODE_THREADS_PTHREADS)
114120
/// The Pthread mutex
115121
pthread_mutex_t p_m;

gecode/support/thread/pthreads.hpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,45 @@ namespace Gecode { namespace Support {
4545

4646
#ifdef GECODE_THREADS_OSX_UNFAIR
4747

48+
#pragma clang diagnostic push
49+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
4850
/*
4951
* Mutex
5052
*/
5153
forceinline
52-
Mutex::Mutex(void) : lck(OS_UNFAIR_LOCK_INIT) {}
54+
Mutex::Mutex(void) {
55+
if (&os_unfair_lock_lock != NULL)
56+
u.unfair_lck = OS_UNFAIR_LOCK_INIT;
57+
else
58+
u.spin_lck = OS_SPINLOCK_INIT;
59+
}
5360
forceinline void
5461
Mutex::acquire(void) {
55-
os_unfair_lock_lock(&lck);
62+
if (&os_unfair_lock_lock != NULL) {
63+
os_unfair_lock_lock(&u.unfair_lck);
64+
} else {
65+
OSSpinLockLock(&u.spin_lck);
66+
}
5667
}
5768
forceinline bool
5869
Mutex::tryacquire(void) {
59-
return os_unfair_lock_trylock(&lck);
70+
if (&os_unfair_lock_trylock != NULL)
71+
return os_unfair_lock_trylock(&u.unfair_lck);
72+
else
73+
return OSSpinLockTry(&u.spin_lck);
6074
}
6175
forceinline void
6276
Mutex::release(void) {
63-
os_unfair_lock_unlock(&lck);
77+
if (&os_unfair_lock_unlock != NULL)
78+
os_unfair_lock_unlock(&u.unfair_lck);
79+
else
80+
OSSpinLockUnlock(&u.spin_lck);
6481
}
6582
forceinline
6683
Mutex::~Mutex(void) {}
6784

85+
#pragma clang diagnostic pop
86+
6887
#else
6988

7089
/*

0 commit comments

Comments
 (0)