File: kill_waiter.cc

package info (click to toggle)
mariadb-10.0 10.0.32-0%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 476,064 kB
  • sloc: cpp: 1,400,131; ansic: 832,140; perl: 54,391; sh: 41,304; pascal: 32,365; yacc: 14,921; xml: 5,257; sql: 4,667; cs: 4,647; makefile: 4,555; ruby: 4,465; python: 2,292; lex: 1,427; java: 941; asm: 295; awk: 54; php: 22; sed: 16
file content (100 lines) | stat: -rw-r--r-- 2,455 bytes parent folder | download | duplicates (4)
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
90
91
92
93
94
95
96
97
98
99
100
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:

// test the lock manager kill waiter function

#include "locktree.h"
#include "lock_request.h"
#include "test.h"
#include "locktree_unit_test.h"
#include <thread>
#include <atomic>

namespace toku {

const uint64_t my_lock_wait_time = 1000 * 1000;
const uint64_t my_killed_time = 500 * 1000;
const int n_locks = 4;

static int my_killed_callback(void) {
    if (1) fprintf(stderr, "%s:%u %s\n", __FILE__, __LINE__, __FUNCTION__);
    return 0;
}

static void locktree_release_lock(locktree *lt, TXNID txn_id, const DBT *left, const DBT *right) {
    range_buffer buffer;
    buffer.create();
    buffer.append(left, right);
    lt->release_locks(txn_id, &buffer);
    buffer.destroy();
}

static void wait_lock(lock_request *lr, std::atomic_int *done) {
    int r = lr->wait(my_lock_wait_time, my_killed_time, my_killed_callback);
    assert(r == DB_LOCK_NOTGRANTED);
    *done = 1;
}

static void test_kill_waiter(void) {
    int r;

    locktree_manager mgr;
    mgr.create(nullptr, nullptr, nullptr, nullptr);

    DICTIONARY_ID dict_id = { 1 };
    locktree *lt = mgr.get_lt(dict_id, dbt_comparator, nullptr);

    const DBT *one = get_dbt(1);

    lock_request locks[n_locks];
    std::thread waiters[n_locks-1];
    for (int i = 0; i < n_locks; i++) {
        locks[i].create();
        locks[i].set(lt, i+1, one, one, lock_request::type::WRITE, false, &waiters[i]);
    }

    // txn 'n_locks' grabs the lock
    r = locks[n_locks-1].start();
    assert_zero(r);

    for (int i = 0; i < n_locks-1; i++) {
        r = locks[i].start();
        assert(r == DB_LOCK_NOTGRANTED);
    }

    std::atomic_int done[n_locks-1];
    for (int i = 0; i < n_locks-1; i++) {
        done[i] = 0;
        waiters[i] = std::thread(wait_lock, &locks[i], &done[i]);
    }

    for (int i = 0; i < n_locks-1; i++) {
        assert(!done[i]);
    }

    sleep(1);
    for (int i = 0; i < n_locks-1; i++) {
        mgr.kill_waiter(&waiters[i]);
        while (!done[i]) sleep(1);
        waiters[i].join();
        for (int j = i+1; j < n_locks-1; j++)
            assert(!done[j]);
    }

    locktree_release_lock(lt, n_locks, one, one);

    for (int i = 0; i < n_locks; i++) {
        locks[i].destroy();
    }

    mgr.release_lt(lt);
    mgr.destroy();
}

} /* namespace toku */

int main(void) {
    toku::test_kill_waiter();
    return 0;
}