Fix RCU Unit-test on macOS/X

Apple's pthread does not implement barriers
This commit is contained in:
Robin Gareus 2020-11-05 19:55:39 +01:00
parent 1fce0c6513
commit 01f9332458
Signed by: rgareus
GPG Key ID: A090BCE02CF57F04
2 changed files with 40 additions and 0 deletions

View File

@ -39,7 +39,13 @@ launch_writer(void* self)
void
RCUTest::race ()
{
#ifdef __APPLE__
pthread_mutex_init (&_mutex, NULL);
pthread_cond_init (&_cond, NULL);
_cnt = 0;
#else
pthread_barrier_init (&_barrier, NULL, 2);
#endif
pthread_t reader_thread;
pthread_t writer_thread;
@ -51,7 +57,12 @@ RCUTest::race ()
CPPUNIT_ASSERT (pthread_join (writer_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);
#endif
}
/* ****************************************************************************/
@ -59,7 +70,18 @@ RCUTest::race ()
void
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);
#endif
for (int i = 0; i < 15000; ++i) {
boost::shared_ptr<Values> reader = _values.reader ();
@ -72,7 +94,18 @@ RCUTest::read_thread ()
void
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);
#endif
for (int i = 0; i < 10000; ++i) {
RCUWriter<Values> writer (_values);

View File

@ -31,5 +31,12 @@ private:
typedef std::map<std::string, boost::shared_ptr<Value> > Values;
SerializedRCUManager<Values> _values;
#ifdef __APPLE__
pthread_mutex_t _mutex;
pthread_cond_t _cond;
size_t _cnt;
#else
pthread_barrier_t _barrier;
#endif
};