Skip to content

Commit 78e23cb

Browse files
committed
Replace platform specific thread code with C++ threads
1 parent fc22c40 commit 78e23cb

16 files changed

+526
-1670
lines changed

CMakeLists.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,10 @@ if (HAVE_VISIBILITY_HIDDEN_FLAG)
221221
set(GECODE_GCC_HAS_CLASS_VISIBILITY "/**/")
222222
endif ()
223223

224-
if (WIN32)
225-
set(GECODE_THREADS_WINDOWS 1)
226-
else ()
227-
set(GECODE_THREADS_PTHREADS 1)
228-
endif ()
224+
option(ENABLE_THREADS "Enable thread support" ON)
225+
if (ENABLE_THREADS)
226+
set(GECODE_HAS_THREADS 1)
227+
endif()
229228

230229
option(ENABLE_GIST "Enable gist" OFF)
231230
if (ENABLE_GIST)

Makefile.dep

Lines changed: 423 additions & 868 deletions
Large diffs are not rendered by default.

Makefile.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,14 @@ endif
146146
#
147147
SUPPORTSRC0 = \
148148
exception allocator heap \
149-
thread/thread thread/windows thread/pthreads \
149+
thread/thread \
150150
hw-rnd
151151
SUPPORTHDR0 = \
152152
block-allocator cast hash dynamic-array \
153153
dynamic-stack exception allocator heap \
154154
macros random sort static-stack \
155155
marked-pointer int-type auto-link \
156-
thread thread/thread thread/windows thread/pthreads thread/none timer \
156+
thread thread/thread timer \
157157
dynamic-queue bitset-base bitset bitset-offset \
158158
hw-rnd run-jobs ref-count
159159

changelog.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ Date: 2020-??-??
6969
[DESCRIPTION]
7070
Let's see.
7171

72+
[ENTRY]
73+
Module: support
74+
What: change
75+
Rank: major
76+
[DESCRIPTION]
77+
Replace platform-specific multithreading code with standard C++ threads.
78+
7279
[ENTRY]
7380
Module: test
7481
What: new

configure

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -6502,80 +6502,8 @@ $as_echo_n "checking whether to build with multi-threading support... " >&6; }
65026502
if test "${enable_thread:-yes}" = "yes"; then
65036503
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
65046504
$as_echo "yes" >&6; }
6505-
ac_fn_cxx_check_header_mongrel "$LINENO" "unistd.h" "ac_cv_header_unistd_h" "$ac_includes_default"
6506-
if test "x$ac_cv_header_unistd_h" = xyes; then :
6507-
6508-
$as_echo "#define GECODE_HAS_UNISTD_H 1" >>confdefs.h
6509-
6510-
6511-
fi
6512-
6513-
6514-
ac_fn_cxx_check_header_mongrel "$LINENO" "pthread.h" "ac_cv_header_pthread_h" "$ac_includes_default"
6515-
if test "x$ac_cv_header_pthread_h" = xyes; then :
6516-
6517-
$as_echo "#define GECODE_THREADS_PTHREADS 1" >>confdefs.h
6518-
6519-
CFLAGS="-pthread${CFLAGS:+ }${CFLAGS}"
6520-
CXXFLAGS="-pthread${CXXFLAGS:+ }${CXXFLAGS}"
6521-
DLLFLAGS="-pthread${DLLFLAGS:+ }${DLLFLAGS}"
6522-
ac_fn_cxx_check_header_mongrel "$LINENO" "os/lock.h" "ac_cv_header_os_lock_h" "$ac_includes_default"
6523-
if test "x$ac_cv_header_os_lock_h" = xyes; then :
6524-
6525-
$as_echo "#define GECODE_THREADS_OSX_UNFAIR 1" >>confdefs.h
6526-
6527-
else
6528-
ac_fn_cxx_check_header_mongrel "$LINENO" "libkern/OSAtomic.h" "ac_cv_header_libkern_OSAtomic_h" "$ac_includes_default"
6529-
if test "x$ac_cv_header_libkern_OSAtomic_h" = xyes; then :
6530-
6531-
$as_echo "#define GECODE_THREADS_OSX 1" >>confdefs.h
6532-
6533-
else
6534-
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for spin locks" >&5
6535-
$as_echo_n "checking for spin locks... " >&6; }
6536-
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
6537-
/* end confdefs.h. */
6538-
#include <pthread.h>
6539-
int
6540-
main ()
6541-
{
6542-
pthread_spinlock_t t;
6543-
;
6544-
return 0;
6545-
}
6546-
_ACEOF
6547-
if ac_fn_cxx_try_compile "$LINENO"; then :
6548-
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
6549-
$as_echo "yes" >&6; }
6550-
6551-
$as_echo "#define GECODE_THREADS_PTHREADS_SPINLOCK 1" >>confdefs.h
6552-
6553-
else
6554-
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
6555-
$as_echo "no" >&6; }
6556-
6557-
fi
6558-
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
6559-
6560-
fi
6561-
6562-
6563-
6564-
fi
6565-
6566-
6567-
else
6568-
ac_fn_cxx_check_header_mongrel "$LINENO" "windows.h" "ac_cv_header_windows_h" "$ac_includes_default"
6569-
if test "x$ac_cv_header_windows_h" = xyes; then :
6570-
6571-
$as_echo "#define GECODE_THREADS_WINDOWS 1" >>confdefs.h
6572-
6573-
fi
6574-
6575-
6576-
6577-
fi
65786505

6506+
$as_echo "#define GECODE_HAS_THREADS 1" >>confdefs.h
65796507

65806508
else
65816509
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5

gecode.m4

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,29 +1499,7 @@ AC_DEFUN([AC_GECODE_THREADS],[
14991499
AC_MSG_CHECKING(whether to build with multi-threading support)
15001500
if test "${enable_thread:-yes}" = "yes"; then
15011501
AC_MSG_RESULT(yes)
1502-
AC_CHECK_HEADER(unistd.h,
1503-
[AC_DEFINE(GECODE_HAS_UNISTD_H,1,[Whether unistd.h is available])]
1504-
)
1505-
AC_CHECK_HEADER(pthread.h,
1506-
[AC_DEFINE(GECODE_THREADS_PTHREADS,1,[Whether we have posix threads])
1507-
AC_GECODE_ADD_TO_COMPILERFLAGS([-pthread])
1508-
AC_GECODE_ADD_TO_DLLFLAGS([-pthread])
1509-
AC_CHECK_HEADER([os/lock.h],
1510-
[AC_DEFINE(GECODE_THREADS_OSX_UNFAIR,1,[Whether we have Mac OS threads (new version)])],
1511-
AC_CHECK_HEADER([libkern/OSAtomic.h],
1512-
[AC_DEFINE(GECODE_THREADS_OSX,1,[Whether we have Mac OS threads])],
1513-
AC_MSG_CHECKING([for spin locks])
1514-
AC_TRY_COMPILE([#include <pthread.h>],
1515-
[pthread_spinlock_t t;],
1516-
[AC_MSG_RESULT(yes)
1517-
AC_DEFINE(GECODE_THREADS_PTHREADS_SPINLOCK,1,Whether we have posix spinlocks)],
1518-
[AC_MSG_RESULT(no)]
1519-
)
1520-
)
1521-
)],
1522-
[AC_CHECK_HEADER(windows.h,
1523-
[AC_DEFINE(GECODE_THREADS_WINDOWS,1,[Whether we have windows threads])])]
1524-
)
1502+
AC_DEFINE(GECODE_HAS_THREADS,1,[Whether we have threads])
15251503
else
15261504
AC_MSG_RESULT(no)
15271505
fi

gecode/support.hh

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,6 @@
8080
#include <gecode/support/auto-link.hpp>
8181
#endif
8282

83-
// Configure threads
84-
#ifdef GECODE_THREADS_WINDOWS
85-
#define GECODE_HAS_THREADS
86-
#endif
87-
88-
#ifdef GECODE_THREADS_PTHREADS
89-
#define GECODE_HAS_THREADS
90-
#endif
91-
92-
9383
/*
9484
* Basic support needed everywhere
9585
*
@@ -127,16 +117,6 @@
127117
*
128118
*/
129119

130-
#ifdef GECODE_THREADS_WINDOWS
131-
#include <gecode/support/thread/windows.hpp>
132-
#endif
133-
#ifdef GECODE_THREADS_PTHREADS
134-
#include <gecode/support/thread/pthreads.hpp>
135-
#endif
136-
#ifndef GECODE_HAS_THREADS
137-
#include <gecode/support/thread/none.hpp>
138-
#endif
139-
140120
#include <gecode/support/thread/thread.hpp>
141121

142122
#include <gecode/support/ref-count.hpp>

gecode/support/config.hpp.in

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,8 @@
117117
/* Whether we are compiling static libraries */
118118
#undef GECODE_STATIC_LIBS
119119

120-
/* Whether we have Mac OS threads */
121-
#undef GECODE_THREADS_OSX
122-
123-
/* Whether we have Mac OS threads (new version) */
124-
#undef GECODE_THREADS_OSX_UNFAIR
125-
126-
/* Whether we have posix threads */
127-
#undef GECODE_THREADS_PTHREADS
128-
129-
/* Whether we have posix spinlocks */
130-
#undef GECODE_THREADS_PTHREADS_SPINLOCK
131-
132-
/* Whether we have windows threads */
133-
#undef GECODE_THREADS_WINDOWS
120+
/* Whether we are compiling with thread support */
121+
#undef GECODE_HAS_THREADS
134122

135123
/* Use clock() for time-measurement */
136124
#undef GECODE_USE_CLOCK

gecode/support/thread.hpp

Lines changed: 11 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,8 @@
3636

3737
#include <cstddef>
3838

39-
#ifdef GECODE_THREADS_WINDOWS
40-
41-
#ifndef NOMINMAX
42-
# define NOMINMAX
43-
#endif
44-
45-
#ifndef _WIN32_WINNT
46-
# define _WIN32_WINNT 0x400
47-
#endif
48-
49-
#ifndef WIN32_LEAN_AND_MEAN
50-
# define WIN32_LEAN_AND_MEAN
51-
#endif
52-
53-
#include <windows.h>
54-
55-
#endif
56-
57-
#ifdef GECODE_THREADS_PTHREADS
58-
59-
#include <pthread.h>
60-
61-
#ifdef GECODE_THREADS_OSX_UNFAIR
62-
63-
#include <os/lock.h>
64-
#include <libkern/OSAtomic.h>
65-
66-
#endif
67-
39+
#ifdef GECODE_HAS_THREADS
40+
#include <thread>
6841
#endif
6942

7043
/**
@@ -95,25 +68,9 @@ namespace Gecode { namespace Support {
9568
*/
9669
class Mutex {
9770
private:
98-
#if defined(GECODE_THREADS_WINDOWS)
99-
/// Use a simple but more efficient critical section on Windows
100-
CRITICAL_SECTION w_cs;
101-
#elif defined(GECODE_THREADS_OSX_UNFAIR)
102-
/// Use unfair lock on macOS if available, OSSpinLock on older macOS
103-
union {
104-
os_unfair_lock unfair_lck;
105-
#pragma clang diagnostic push
106-
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
107-
OSSpinLock spin_lck;
108-
#pragma clang diagnostic pop
109-
} u;
110-
#elif defined(GECODE_THREADS_PTHREADS)
111-
/// The Pthread mutex
112-
pthread_mutex_t p_m;
113-
#elif defined(GECODE_HAS_THREADS)
114-
#error No suitable mutex implementation found
115-
#else
116-
// When no threads are used, Mutex is a no-op
71+
#ifdef GECODE_HAS_THREADS
72+
/// The mutex
73+
std::mutex m;
11774
#endif
11875
public:
11976
/// Initialize mutex
@@ -124,8 +81,6 @@ namespace Gecode { namespace Support {
12481
bool tryacquire(void);
12582
/// Release the mutex
12683
void release(void);
127-
/// Delete mutex
128-
~Mutex(void);
12984
/// Allocate memory from heap
13085
static void* operator new(size_t s);
13186
/// Free memory allocated from heap
@@ -137,53 +92,8 @@ namespace Gecode { namespace Support {
13792
void operator=(const Mutex&) {}
13893
};
13994

140-
#ifndef GECODE_THREADS_PTHREADS_SPINLOCK
141-
14295
typedef Mutex FastMutex;
14396

144-
#else
145-
146-
/**
147-
* \brief A fast mutex for mutual exclausion among several threads
148-
*
149-
* This mutex is implemeneted using spin locks on some platforms
150-
* and is not guaranteed to be compatible with events. It should be used
151-
* for low-contention locks that are only acquired for short periods of
152-
* time.
153-
*
154-
* It is not specified whether the mutex is recursive or not.
155-
* Likewise, there is no guarantee of fairness among the
156-
* blocking threads.
157-
*
158-
* \ingroup FuncSupportThread
159-
*/
160-
class FastMutex {
161-
private:
162-
/// The Pthread spinlock
163-
pthread_spinlock_t p_s;
164-
public:
165-
/// Initialize mutex
166-
FastMutex(void);
167-
/// Acquire the mutex and possibly block
168-
void acquire(void);
169-
/// Try to acquire the mutex, return true if succesful
170-
bool tryacquire(void);
171-
/// Release the mutex
172-
void release(void);
173-
/// Delete mutex
174-
~FastMutex(void);
175-
/// Allocate memory from heap
176-
static void* operator new(size_t s);
177-
/// Free memory allocated from heap
178-
static void operator delete(void* p);
179-
private:
180-
/// A mutex cannot be copied
181-
FastMutex(const FastMutex&) {}
182-
/// A mutex cannot be assigned
183-
void operator=(const FastMutex&) {}
184-
};
185-
186-
#endif
18797

18898
/**
18999
* \brief A lock as a scoped frontend for a mutex
@@ -216,27 +126,21 @@ namespace Gecode { namespace Support {
216126
*/
217127
class Event {
218128
private:
219-
#ifdef GECODE_THREADS_WINDOWS
220-
/// The Windows specific handle to an event
221-
HANDLE w_h;
129+
#ifdef GECODE_HAS_THREADS
130+
/// The mutex
131+
std::mutex m;
132+
/// The condition variable
133+
std::condition_variable c;
222134
#endif
223-
#ifdef GECODE_THREADS_PTHREADS
224-
/// The Pthread mutex
225-
pthread_mutex_t p_m;
226-
/// The Pthread condition variable
227-
pthread_cond_t p_c;
228135
/// Whether the event is signalled
229-
bool p_s;
230-
#endif
136+
bool s;
231137
public:
232138
/// Initialize event
233139
Event(void);
234140
/// Signal the event
235141
void signal(void);
236142
/// Wait until the event becomes signalled
237143
void wait(void);
238-
/// Delete event
239-
~Event(void);
240144
private:
241145
/// An event cannot be copied
242146
Event(const Event&) {}

0 commit comments

Comments
 (0)