Skip to content

Commit cded187

Browse files
Jeffrey MendelsohnGitHub Enterprise
Jeffrey Mendelsohn
authored and
GitHub Enterprise
committed
add watchdog to case 7 (#5178)
1 parent e39ba50 commit cded187

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

groups/bdl/bdlcc/bdlcc_fixedqueue.t.cpp

+50-1
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,52 @@ static int veryVeryVerbose;
151151
typedef void Element;
152152
typedef bdlcc::FixedQueue<Element*> Obj;
153153

154+
const int k_DECISECOND = 100000; // microseconds in 0.1 seconds
155+
154156
// ============================================================================
155157
// HELPER CLASSES AND FUNCTIONS FOR TESTING
156158
// ----------------------------------------------------------------------------
157159

158160
namespace {
159161
namespace u {
160162

163+
static bsls::AtomicInt s_continue;
164+
static char s_watchdogText[128];
165+
166+
/// Assign the specified `value` to be displayed if the watchdog expires.
167+
void setWatchdogText(const char *value)
168+
{
169+
memcpy(s_watchdogText, value, strlen(value) + 1);
170+
}
171+
172+
/// Watchdog function used to determine when a timeout should occur. This
173+
/// function returns without expiration if `0 == s_continue` before one
174+
/// second elapses. Upon expiration, `s_watchdogText` is displayed and the
175+
/// program is aborted.
176+
extern "C" void *watchdog(void *arg)
177+
{
178+
if (arg) {
179+
setWatchdogText(static_cast<const char *>(arg));
180+
}
181+
182+
const int MAX = 300; // one iteration is a deci-second
183+
184+
int count = 0;
185+
186+
while (s_continue) {
187+
bslmt::ThreadUtil::microSleep(k_DECISECOND);
188+
++count;
189+
190+
ASSERTV(s_watchdogText, count < MAX);
191+
192+
if (MAX == count && s_continue) {
193+
abort();
194+
}
195+
}
196+
197+
return 0;
198+
}
199+
161200
/// Return the current time, as a `TimeInterval`.
162201
bsls::TimeInterval now()
163202
{
@@ -2348,6 +2387,12 @@ int main(int argc, char *argv[])
23482387
<< "ABA \"empty\" value test" << endl
23492388
<< "======================" << endl;
23502389

2390+
bslmt::ThreadUtil::Handle watchdogHandle;
2391+
2392+
::u::s_continue = 1;
2393+
::u::setWatchdogText("ABA 'empty' test");
2394+
bslmt::ThreadUtil::create(&watchdogHandle, ::u::watchdog, 0);
2395+
23512396
enum {
23522397
k_NUM_PUSHER_THREADS = 40,
23532398
k_NUM_VALUES = 6,
@@ -2398,8 +2443,12 @@ int main(int argc, char *argv[])
23982443
}
23992444
ASSERT(0 < ta.numAllocations());
24002445
ASSERT(0 == ta.numBytesInUse());
2401-
} break;
24022446

2447+
::u::s_continue = 0;
2448+
2449+
::u::setWatchdogText("ABA 'empty' test: join watchdog");
2450+
bslmt::ThreadUtil::join(watchdogHandle);
2451+
} break;
24032452
case 6: {
24042453
// --------------------------------------------------------------------
24052454
// CONCERN: REMOVE_ALL

0 commit comments

Comments
 (0)