Add possibility to wait several times on a semaphore

This commit is contained in:
Johan Wikman
2017-04-24 15:38:08 +03:00
parent 19cf8c489e
commit 1eb409a666
3 changed files with 155 additions and 12 deletions

View File

@ -17,13 +17,14 @@
namespace maxscale
{
bool Semaphore::timedwait(time_t seconds,
long nseconds,
signal_approach_t signal_approach) const
//static
void Semaphore::get_current_timespec(time_t seconds,
long nseconds,
timespec* pTs)
{
ss_dassert(nseconds <= 999999999);
timespec ts;
timespec& ts = *pTs;
ss_debug(int rc=) clock_gettime(CLOCK_REALTIME, &ts);
ss_dassert(rc == 0);
@ -39,8 +40,6 @@ bool Semaphore::timedwait(time_t seconds,
}
ts.tv_nsec = nseconds_sum;
return timedwait(ts, signal_approach);
}
}

View File

@ -65,6 +65,14 @@ void test_simple()
ss_dassert(rv);
cout << "Waited" << endl;
sem2.post();
sem2.post();
sem2.post();
cout << "Waiting 3 times for semaphore with a count of 3." << endl;
rv = sem2.wait_n(3);
cout << "Waited" << endl;
Semaphore sem3;
time_t started;
@ -118,10 +126,7 @@ void test_threads()
cout << "Waiting for threads." << endl;
for (int i = 0; i < n_threads; ++i)
{
sem.wait();
}
sem.wait_n(n_threads);
cout << "Joining threads." << endl;