summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/src/support/cond_auto.c
blob: 61db334de248f3341a5eae6f40eb4500c5db5826 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*-
 * Copyright (c) 2014-2018 MongoDB, Inc.
 * Copyright (c) 2008-2014 WiredTiger, Inc.
 *	All rights reserved.
 *
 * See the file LICENSE for redistribution information.
 */

#include "wt_internal.h"

/*
 * This is an implementation of condition variables that automatically adjust
 * the wait time depending on whether the wake is resulting in useful work.
 */

/*
 * __wt_cond_auto_alloc --
 *	Allocate and initialize an automatically adjusting condition variable.
 */
int
__wt_cond_auto_alloc(WT_SESSION_IMPL *session,
    const char *name, uint64_t min, uint64_t max, WT_CONDVAR **condp)
{
	WT_CONDVAR *cond;

	WT_RET(__wt_cond_alloc(session, name, condp));
	cond = *condp;

	cond->min_wait = min;
	cond->max_wait = max;
	cond->prev_wait = min;

	return (0);
}

/*
 * __wt_cond_auto_wait_signal --
 *	Wait on a mutex, optionally timing out.  If we get it before the time
 *	out period expires, let the caller know.
 */
void
__wt_cond_auto_wait_signal(WT_SESSION_IMPL *session, WT_CONDVAR *cond,
    bool progress, bool (*run_func)(WT_SESSION_IMPL *), bool *signalled)
{
	uint64_t delta;

	/*
	 * Catch cases where this function is called with a condition variable
	 * that wasn't initialized to do automatic adjustments.
	 */
	WT_ASSERT(session, cond->min_wait != 0);

	WT_STAT_CONN_INCR(session, cond_auto_wait);
	if (progress)
		cond->prev_wait = cond->min_wait;
	else {
		delta = WT_MAX(1, (cond->max_wait - cond->min_wait) / 10);
		cond->prev_wait = WT_MIN(
		    cond->max_wait, cond->prev_wait + delta);
	}

	__wt_cond_wait_signal(
	    session, cond, cond->prev_wait, run_func, signalled);

	if (progress || *signalled)
		WT_STAT_CONN_INCR(session, cond_auto_wait_reset);
	if (*signalled)
		cond->prev_wait = cond->min_wait;
}

/*
 * __wt_cond_auto_wait --
 *	Wait on a mutex, optionally timing out.  If we get it before the time
 *	out period expires, let the caller know.
 */
void
__wt_cond_auto_wait(WT_SESSION_IMPL *session,
    WT_CONDVAR *cond, bool progress, bool (*run_func)(WT_SESSION_IMPL *))
{
	bool notused;

	__wt_cond_auto_wait_signal(session, cond, progress, run_func, &notused);
}