Fix a race condition on taking the mutex

The workqueue worker can take the mutex before the tester tries to take it after
calling workqueue_enqueue. If it happens, the worker calls cv_broadcast before
the tester calls cv_timedwait and the tester will wait until the cv timed out

Take the mutex before calling workqueue_enqueue so that the tester surely calls
cv_timedwait before the worker calls cv_broadcast.

The fix stabilizes the test, t_workqueue/workqueue1.
This commit is contained in:
ozaki-r 2017-12-28 04:38:02 +00:00
parent 1315c7eeb1
commit bf33e35aca
1 changed files with 3 additions and 3 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: workqueue.c,v 1.2 2017/12/28 04:36:15 ozaki-r Exp $ */
/* $NetBSD: workqueue.c,v 1.3 2017/12/28 04:38:02 ozaki-r Exp $ */
/*-
* Copyright (c) 2017 The NetBSD Foundation, Inc.
@ -29,7 +29,7 @@
#include <sys/cdefs.h>
#if !defined(lint)
__RCSID("$NetBSD: workqueue.c,v 1.2 2017/12/28 04:36:15 ozaki-r Exp $");
__RCSID("$NetBSD: workqueue.c,v 1.3 2017/12/28 04:38:02 ozaki-r Exp $");
#endif /* !lint */
#include <sys/param.h>
@ -84,8 +84,8 @@ rumptest_workqueue1()
#define ITERATIONS 12435
for (size_t i = 0; i < ITERATIONS; ++i) {
int e;
workqueue_enqueue(sc->wq, &sc->wk, NULL);
mutex_enter(&sc->mtx);
workqueue_enqueue(sc->wq, &sc->wk, NULL);
e = cv_timedwait(&sc->cv, &sc->mtx, hz * 2);
if (e != 0)
panic("cv_timedwait timed out (i=%lu)", i);