summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/src/support/cond_auto.c
blob: c5a43e6d67cca2e3d5a6ab73c2e934b31cc8cc4b (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
84
85
86
87
88
89
/*-
 * Copyright (c) 2014-2020 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, saved_prev_wait;

    /*
     * 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);
        /*
         * Try to update the previous wait value for the condition variable. There can be multiple
         * threads doing this concurrently, so use atomic operations to make sure the value remains
         * within the bounds of the maximum configured. Don't retry if our update didn't make it in
         * - it's not necessary for the previous wait time to be updated every time.
         */
        WT_ORDERED_READ(saved_prev_wait, cond->prev_wait);
        __wt_atomic_cas64(
          &cond->prev_wait, saved_prev_wait, WT_MIN(cond->max_wait, saved_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);
}