Skip to content

Commit 4e99334

Browse files
committed
Use os_unfair_lock instead of (deprecated) OSSpinLock on mac os
git-svn-id: svn+ssh://svn.gecode.org/srv/gecode/svn/gecode/trunk@16767 64335634-5103-0410-b293-fc3d331e086d
1 parent 020695b commit 4e99334

File tree

7 files changed

+70
-15
lines changed

7 files changed

+70
-15
lines changed

changelog.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ by an array of variables), a new much faster implementation of
8181
extensional constraints using tuple sets, and support for tracing
8282
search engines.
8383

84+
[ENTRY]
85+
Module: support
86+
What: change
87+
Rank: minor
88+
[DESCRIPTION]
89+
Replace deprecated OSSpinLock with os_unfair_lock on newer versions
90+
of macOS.
91+
8492
[ENTRY]
8593
Module: search
8694
What: bug

configure

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6487,15 +6487,21 @@ $as_echo "#define GECODE_THREADS_PTHREADS 1" >>confdefs.h
64876487
CFLAGS="-pthread${CFLAGS:+ }${CFLAGS}"
64886488
CXXFLAGS="-pthread${CXXFLAGS:+ }${CXXFLAGS}"
64896489
DLLFLAGS="-pthread${DLLFLAGS:+ }${DLLFLAGS}"
6490-
ac_fn_cxx_check_header_mongrel "$LINENO" "libkern/OSAtomic.h" "ac_cv_header_libkern_OSAtomic_h" "$ac_includes_default"
6490+
ac_fn_cxx_check_header_mongrel "$LINENO" "os/lock.h" "ac_cv_header_os_lock_h" "$ac_includes_default"
6491+
if test "x$ac_cv_header_os_lock_h" = xyes; then :
6492+
6493+
$as_echo "#define GECODE_THREADS_OSX_UNFAIR 1" >>confdefs.h
6494+
6495+
else
6496+
ac_fn_cxx_check_header_mongrel "$LINENO" "libkern/OSAtomic.h" "ac_cv_header_libkern_OSAtomic_h" "$ac_includes_default"
64916497
if test "x$ac_cv_header_libkern_OSAtomic_h" = xyes; then :
64926498

64936499
$as_echo "#define GECODE_THREADS_OSX 1" >>confdefs.h
64946500

64956501
else
64966502
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for spin locks" >&5
64976503
$as_echo_n "checking for spin locks... " >&6; }
6498-
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
6504+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
64996505
/* end confdefs.h. */
65006506
#include <pthread.h>
65016507
int
@@ -6523,6 +6529,9 @@ fi
65236529

65246530

65256531

6532+
fi
6533+
6534+
65266535
else
65276536
ac_fn_cxx_check_header_mongrel "$LINENO" "windows.h" "ac_cv_header_windows_h" "$ac_includes_default"
65286537
if test "x$ac_cv_header_windows_h" = xyes; then :

gecode.m4

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,17 +1467,19 @@ AC_DEFUN([AC_GECODE_THREADS],[
14671467
[AC_DEFINE(GECODE_THREADS_PTHREADS,1,[Whether we have posix threads])
14681468
AC_GECODE_ADD_TO_COMPILERFLAGS([-pthread])
14691469
AC_GECODE_ADD_TO_DLLFLAGS([-pthread])
1470-
AC_CHECK_HEADER([libkern/OSAtomic.h],
1471-
[AC_DEFINE(GECODE_THREADS_OSX,1,[Whether we have Mac OS threads])],
1472-
AC_MSG_CHECKING([for spin locks])
1473-
AC_TRY_COMPILE([#include <pthread.h>],
1474-
[pthread_spinlock_t t;],
1475-
[AC_MSG_RESULT(yes)
1476-
AC_DEFINE(GECODE_THREADS_PTHREADS_SPINLOCK,1,Whether we have posix spinlocks)],
1477-
[AC_MSG_RESULT(no)]
1478-
)
1479-
)
1480-
],
1470+
AC_CHECK_HEADER([os/lock.h],
1471+
[AC_DEFINE(GECODE_THREADS_OSX_UNFAIR,1,[Whether we have Mac OS threads (new version)])],
1472+
AC_CHECK_HEADER([libkern/OSAtomic.h],
1473+
[AC_DEFINE(GECODE_THREADS_OSX,1,[Whether we have Mac OS threads])],
1474+
AC_MSG_CHECKING([for spin locks])
1475+
AC_TRY_COMPILE([#include <pthread.h>],
1476+
[pthread_spinlock_t t;],
1477+
[AC_MSG_RESULT(yes)
1478+
AC_DEFINE(GECODE_THREADS_PTHREADS_SPINLOCK,1,Whether we have posix spinlocks)],
1479+
[AC_MSG_RESULT(no)]
1480+
)
1481+
)
1482+
)],
14811483
[AC_CHECK_HEADER(windows.h,
14821484
[AC_DEFINE(GECODE_THREADS_WINDOWS,1,[Whether we have windows threads])])]
14831485
)

gecode/support/config.hpp.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@
115115
/* Whether we have Mac OS threads */
116116
#undef GECODE_THREADS_OSX
117117

118+
/* Whether we have Mac OS threads (new version) */
119+
#undef GECODE_THREADS_OSX_UNFAIR
120+
118121
/* Whether we have posix threads */
119122
#undef GECODE_THREADS_PTHREADS
120123

gecode/support/thread.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@
6868

6969
#endif
7070

71+
#ifdef GECODE_THREADS_OSX_UNFAIR
72+
73+
#include <os/lock.h>
74+
75+
#endif
76+
7177
#endif
7278

7379
/**
@@ -136,7 +142,7 @@ namespace Gecode { namespace Support {
136142

137143
#ifdef GECODE_THREADS_PTHREADS
138144

139-
#if defined(GECODE_THREADS_OSX) || defined(GECODE_THREADS_PTHREADS_SPINLOCK)
145+
#if defined(GECODE_THREADS_OSX) || defined(GECODE_THREADS_OSX_UNFAIR) || defined(GECODE_THREADS_PTHREADS_SPINLOCK)
140146

141147
/**
142148
* \brief A fast mutex for mutual exclausion among several threads
@@ -157,6 +163,9 @@ namespace Gecode { namespace Support {
157163
#ifdef GECODE_THREADS_OSX
158164
/// The OSX spin lock
159165
OSSpinLock lck;
166+
#elif defined(GECODE_THREADS_OSX_UNFAIR)
167+
/// The OSX spin lock
168+
os_unfair_lock lck;
160169
#else
161170
/// The Pthread spinlock
162171
pthread_spinlock_t p_s;

gecode/support/thread/pthreads.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,30 @@ namespace Gecode { namespace Support {
9898

9999
#endif
100100

101+
#ifdef GECODE_THREADS_OSX_UNFAIR
102+
103+
/*
104+
* FastMutex
105+
*/
106+
forceinline
107+
FastMutex::FastMutex(void) {}
108+
forceinline void
109+
FastMutex::acquire(void) {
110+
os_unfair_lock_lock(&lck);
111+
}
112+
forceinline bool
113+
FastMutex::tryacquire(void) {
114+
return os_unfair_lock_trylock(&lck);
115+
}
116+
forceinline void
117+
FastMutex::release(void) {
118+
os_unfair_lock_unlock(&lck);
119+
}
120+
forceinline
121+
FastMutex::~FastMutex(void) {}
122+
123+
#endif
124+
101125
#ifdef GECODE_THREADS_PTHREADS_SPINLOCK
102126

103127
/*

gecode/support/thread/thread.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ namespace Gecode { namespace Support {
7575
Gecode::heap.rfree(p);
7676
}
7777

78-
#if defined(GECODE_THREADS_OSX) || defined(GECODE_THREADS_PTHREADS_SPINLOCK)
78+
#if defined(GECODE_THREADS_OSX) || defined(GECODE_THREADS_OSX_UNFAIR) || defined(GECODE_THREADS_PTHREADS_SPINLOCK)
7979

8080
/*
8181
* Fast mutexes

0 commit comments

Comments
 (0)