Fix RCU Unit-test on macOS/X
Apple's pthread does not implement barriers
This commit is contained in:
parent
1fce0c6513
commit
01f9332458
|
@ -39,7 +39,13 @@ launch_writer(void* self)
|
||||||
void
|
void
|
||||||
RCUTest::race ()
|
RCUTest::race ()
|
||||||
{
|
{
|
||||||
|
#ifdef __APPLE__
|
||||||
|
pthread_mutex_init (&_mutex, NULL);
|
||||||
|
pthread_cond_init (&_cond, NULL);
|
||||||
|
_cnt = 0;
|
||||||
|
#else
|
||||||
pthread_barrier_init (&_barrier, NULL, 2);
|
pthread_barrier_init (&_barrier, NULL, 2);
|
||||||
|
#endif
|
||||||
|
|
||||||
pthread_t reader_thread;
|
pthread_t reader_thread;
|
||||||
pthread_t writer_thread;
|
pthread_t writer_thread;
|
||||||
|
@ -51,7 +57,12 @@ RCUTest::race ()
|
||||||
CPPUNIT_ASSERT (pthread_join (writer_thread, &return_value) == 0);
|
CPPUNIT_ASSERT (pthread_join (writer_thread, &return_value) == 0);
|
||||||
CPPUNIT_ASSERT (pthread_join (reader_thread, &return_value) == 0);
|
CPPUNIT_ASSERT (pthread_join (reader_thread, &return_value) == 0);
|
||||||
|
|
||||||
|
#ifdef __APPLE__
|
||||||
|
pthread_mutex_destroy (&_mutex);
|
||||||
|
pthread_cond_destroy (&_cond);
|
||||||
|
#else
|
||||||
pthread_barrier_destroy (&_barrier);
|
pthread_barrier_destroy (&_barrier);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ****************************************************************************/
|
/* ****************************************************************************/
|
||||||
|
@ -59,7 +70,18 @@ RCUTest::race ()
|
||||||
void
|
void
|
||||||
RCUTest::read_thread ()
|
RCUTest::read_thread ()
|
||||||
{
|
{
|
||||||
|
#ifdef __APPLE__
|
||||||
|
pthread_mutex_lock (&_mutex);
|
||||||
|
if (++_cnt == 2) {
|
||||||
|
pthread_cond_broadcast (&_cond);
|
||||||
|
pthread_mutex_unlock (&_mutex);
|
||||||
|
} else {
|
||||||
|
pthread_cond_wait (&_cond, &_mutex);
|
||||||
|
}
|
||||||
|
pthread_mutex_unlock (&_mutex);
|
||||||
|
#else
|
||||||
pthread_barrier_wait (&_barrier);
|
pthread_barrier_wait (&_barrier);
|
||||||
|
#endif
|
||||||
|
|
||||||
for (int i = 0; i < 15000; ++i) {
|
for (int i = 0; i < 15000; ++i) {
|
||||||
boost::shared_ptr<Values> reader = _values.reader ();
|
boost::shared_ptr<Values> reader = _values.reader ();
|
||||||
|
@ -72,7 +94,18 @@ RCUTest::read_thread ()
|
||||||
void
|
void
|
||||||
RCUTest::write_thread ()
|
RCUTest::write_thread ()
|
||||||
{
|
{
|
||||||
|
#ifdef __APPLE__
|
||||||
|
pthread_mutex_lock (&_mutex);
|
||||||
|
if (++_cnt == 2) {
|
||||||
|
pthread_cond_broadcast (&_cond);
|
||||||
|
pthread_mutex_unlock (&_mutex);
|
||||||
|
} else {
|
||||||
|
pthread_cond_wait (&_cond, &_mutex);
|
||||||
|
}
|
||||||
|
pthread_mutex_unlock (&_mutex);
|
||||||
|
#else
|
||||||
pthread_barrier_wait (&_barrier);
|
pthread_barrier_wait (&_barrier);
|
||||||
|
#endif
|
||||||
|
|
||||||
for (int i = 0; i < 10000; ++i) {
|
for (int i = 0; i < 10000; ++i) {
|
||||||
RCUWriter<Values> writer (_values);
|
RCUWriter<Values> writer (_values);
|
||||||
|
|
|
@ -31,5 +31,12 @@ private:
|
||||||
typedef std::map<std::string, boost::shared_ptr<Value> > Values;
|
typedef std::map<std::string, boost::shared_ptr<Value> > Values;
|
||||||
|
|
||||||
SerializedRCUManager<Values> _values;
|
SerializedRCUManager<Values> _values;
|
||||||
|
|
||||||
|
#ifdef __APPLE__
|
||||||
|
pthread_mutex_t _mutex;
|
||||||
|
pthread_cond_t _cond;
|
||||||
|
size_t _cnt;
|
||||||
|
#else
|
||||||
pthread_barrier_t _barrier;
|
pthread_barrier_t _barrier;
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user