summaryrefslogtreecommitdiff
path: root/storage/tokudb/PerconaFT/util/frwlock.cc
blob: 1f821fe5f546aa60e6742c24c866e45c858e1c43 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
#ident "$Id$"
/*======
This file is part of PerconaFT.


Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.

    PerconaFT is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2,
    as published by the Free Software Foundation.

    PerconaFT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.

----------------------------------------

    PerconaFT is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License, version 3,
    as published by the Free Software Foundation.

    PerconaFT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
======= */

#ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."

#include <toku_assert.h>

#include <util/context.h>
#include <util/frwlock.h>

toku_instr_key *frwlock_m_wait_read_key;

namespace toku {

    static __thread int thread_local_tid = -1;
    static int get_local_tid() {
        if (thread_local_tid == -1) {
            thread_local_tid = toku_os_gettid();
        }
        return thread_local_tid;
    }

    void frwlock::init(toku_mutex_t *const mutex
#if defined(TOKU_MYSQL_WITH_PFS)
                       ,
                       const toku_instr_key &rwlock_instr_key
#endif
                       ) {
        m_mutex = mutex;

        m_num_readers = 0;
        m_num_writers = 0;
        m_num_want_write = 0;
        m_num_want_read = 0;
        m_num_signaled_readers = 0;
        m_num_expensive_want_write = 0;
#if defined(TOKU_MYSQL_WITH_PFS)
        toku_pthread_rwlock_init(rwlock_instr_key, &m_rwlock, nullptr);
#endif
        toku_cond_init(toku_uninstrumented, &m_wait_read, nullptr);
        m_queue_item_read.cond = &m_wait_read;
        m_queue_item_read.next = nullptr;
        m_wait_read_is_in_queue = false;
        m_current_writer_expensive = false;
        m_read_wait_expensive = false;
        m_current_writer_tid = -1;
        m_blocking_writer_context_id = CTX_INVALID;

        m_wait_head = nullptr;
        m_wait_tail = nullptr;
    }

    void frwlock::deinit(void) {
        toku_cond_destroy(&m_wait_read);
#if defined(TOKU_MYSQL_WITH_PFS)
        toku_pthread_rwlock_destroy(&m_rwlock);
#endif
    }

    bool frwlock::queue_is_empty(void) const { return m_wait_head == nullptr; }

    void frwlock::enq_item(queue_item *const item) {
        paranoid_invariant_null(item->next);
        if (m_wait_tail != nullptr) {
            m_wait_tail->next = item;
        } else {
            paranoid_invariant_null(m_wait_head);
            m_wait_head = item;
        }
        m_wait_tail = item;
    }

    toku_cond_t *frwlock::deq_item(void) {
        paranoid_invariant_notnull(m_wait_head);
        paranoid_invariant_notnull(m_wait_tail);
        queue_item *item = m_wait_head;
        m_wait_head = m_wait_head->next;
        if (m_wait_tail == item) {
            m_wait_tail = nullptr;
        }
        return item->cond;
    }

    // Prerequisite: Holds m_mutex.
    void frwlock::write_lock(bool expensive) {
#if defined(TOKU_MYSQL_WITH_PFS)
        /* Instrumentation start */
        toku_rwlock_instrumentation rwlock_instr;
        toku_instr_rwlock_wrlock_wait_start(
            rwlock_instr, m_rwlock, __FILE__, __LINE__);
#endif

        toku_mutex_assert_locked(m_mutex);
        if (this->try_write_lock(expensive)) {
#if defined(TOKU_MYSQL_WITH_PFS)
            /* Instrumentation end */
            toku_instr_rwlock_wrlock_wait_end(rwlock_instr, 0);
#endif
            return;
        }

        toku_cond_t cond = TOKU_COND_INITIALIZER;
        queue_item item = {.cond = &cond, .next = nullptr};
        this->enq_item(&item);

        // Wait for our turn.
        ++m_num_want_write;
        if (expensive) {
            ++m_num_expensive_want_write;
        }
        if (m_num_writers == 0 && m_num_want_write == 1) {
            // We are the first to want a write lock. No new readers can get the
            // lock.
            // Set our thread id and context for proper instrumentation.
            // see: toku_context_note_frwlock_contention()
            m_current_writer_tid = get_local_tid();
            m_blocking_writer_context_id = toku_thread_get_context()->get_id();
        }
        toku_cond_wait(&cond, m_mutex);
        toku_cond_destroy(&cond);

        // Now it's our turn.
        paranoid_invariant(m_num_want_write > 0);
        paranoid_invariant_zero(m_num_readers);
        paranoid_invariant_zero(m_num_writers);
        paranoid_invariant_zero(m_num_signaled_readers);

        // Not waiting anymore; grab the lock.
        --m_num_want_write;
        if (expensive) {
            --m_num_expensive_want_write;
        }
        m_num_writers = 1;
        m_current_writer_expensive = expensive;
        m_current_writer_tid = get_local_tid();
        m_blocking_writer_context_id = toku_thread_get_context()->get_id();

#if defined(TOKU_MYSQL_WITH_PFS)
        /* Instrumentation end */
        toku_instr_rwlock_wrlock_wait_end(rwlock_instr, 0);
#endif
    }

    bool frwlock::try_write_lock(bool expensive) {
        toku_mutex_assert_locked(m_mutex);
        if (m_num_readers > 0 || m_num_writers > 0 ||
            m_num_signaled_readers > 0 || m_num_want_write > 0) {
            return false;
        }
        // No one holds the lock.  Grant the write lock.
        paranoid_invariant_zero(m_num_want_write);
        paranoid_invariant_zero(m_num_want_read);
        m_num_writers = 1;
        m_current_writer_expensive = expensive;
        m_current_writer_tid = get_local_tid();
        m_blocking_writer_context_id = toku_thread_get_context()->get_id();
        return true;
    }

    void frwlock::read_lock(void) {
#if defined(TOKU_MYSQL_WITH_PFS)
        /* Instrumentation start */
        toku_rwlock_instrumentation rwlock_instr;
        toku_instr_rwlock_rdlock_wait_start(
            rwlock_instr, m_rwlock, __FILE__, __LINE__);
#endif
        toku_mutex_assert_locked(m_mutex);
        if (m_num_writers > 0 || m_num_want_write > 0) {
            if (!m_wait_read_is_in_queue) {
                // Throw the read cond_t onto the queue.
                paranoid_invariant(m_num_signaled_readers == m_num_want_read);
                m_queue_item_read.next = nullptr;
                this->enq_item(&m_queue_item_read);
                m_wait_read_is_in_queue = true;
                paranoid_invariant(!m_read_wait_expensive);
                m_read_wait_expensive = (m_current_writer_expensive ||
                                         (m_num_expensive_want_write > 0));
            }

            // Note this contention event in engine status.
            toku_context_note_frwlock_contention(
                toku_thread_get_context()->get_id(),
                m_blocking_writer_context_id);

            // Wait for our turn.
            ++m_num_want_read;
            toku_cond_wait(&m_wait_read, m_mutex);

            // Now it's our turn.
            paranoid_invariant_zero(m_num_writers);
            paranoid_invariant(m_num_want_read > 0);
            paranoid_invariant(m_num_signaled_readers > 0);

            // Not waiting anymore; grab the lock.
            --m_num_want_read;
            --m_num_signaled_readers;
        }
        ++m_num_readers;
#if defined(TOKU_MYSQL_WITH_PFS)
        /* Instrumentation end */
        toku_instr_rwlock_rdlock_wait_end(rwlock_instr, 0);
#endif
    }

    bool frwlock::try_read_lock(void) {
        toku_mutex_assert_locked(m_mutex);
        if (m_num_writers > 0 || m_num_want_write > 0) {
            return false;
        }
        // No writer holds the lock.
        // No writers are waiting.
        // Grant the read lock.
        ++m_num_readers;
        return true;
    }

    void frwlock::maybe_signal_next_writer(void) {
        if (m_num_want_write > 0 && m_num_signaled_readers == 0 &&
            m_num_readers == 0) {
            toku_cond_t *cond = this->deq_item();
            paranoid_invariant(cond != &m_wait_read);
            // Grant write lock to waiting writer.
            paranoid_invariant(m_num_want_write > 0);
            toku_cond_signal(cond);
        }
    }

    void frwlock::read_unlock(void) {
#ifdef TOKU_MYSQL_WITH_PFS
        toku_instr_rwlock_unlock(m_rwlock);
#endif
        toku_mutex_assert_locked(m_mutex);
        paranoid_invariant(m_num_writers == 0);
        paranoid_invariant(m_num_readers > 0);
        --m_num_readers;
        this->maybe_signal_next_writer();
    }

    bool frwlock::read_lock_is_expensive(void) {
        toku_mutex_assert_locked(m_mutex);
        if (m_wait_read_is_in_queue) {
            return m_read_wait_expensive;
        } else {
            return m_current_writer_expensive ||
                   (m_num_expensive_want_write > 0);
        }
    }

    void frwlock::maybe_signal_or_broadcast_next(void) {
        paranoid_invariant(m_num_signaled_readers == 0);

        if (this->queue_is_empty()) {
            paranoid_invariant(m_num_want_write == 0);
            paranoid_invariant(m_num_want_read == 0);
            return;
        }
        toku_cond_t *cond = this->deq_item();
        if (cond == &m_wait_read) {
            // Grant read locks to all waiting readers
            paranoid_invariant(m_wait_read_is_in_queue);
            paranoid_invariant(m_num_want_read > 0);
            m_num_signaled_readers = m_num_want_read;
            m_wait_read_is_in_queue = false;
            m_read_wait_expensive = false;
            toku_cond_broadcast(cond);
        } else {
            // Grant write lock to waiting writer.
            paranoid_invariant(m_num_want_write > 0);
            toku_cond_signal(cond);
        }
    }

    void frwlock::write_unlock(void) {
#if defined(TOKU_MYSQL_WITH_PFS)
        toku_instr_rwlock_unlock(m_rwlock);
#endif
        toku_mutex_assert_locked(m_mutex);
        paranoid_invariant(m_num_writers == 1);
        m_num_writers = 0;
        m_current_writer_expensive = false;
        m_current_writer_tid = -1;
        m_blocking_writer_context_id = CTX_INVALID;
        this->maybe_signal_or_broadcast_next();
    }
    bool frwlock::write_lock_is_expensive(void) {
        toku_mutex_assert_locked(m_mutex);
        return (m_num_expensive_want_write > 0) || (m_current_writer_expensive);
    }

    uint32_t frwlock::users(void) const {
        toku_mutex_assert_locked(m_mutex);
        return m_num_readers + m_num_writers + m_num_want_read +
               m_num_want_write;
    }
    uint32_t frwlock::blocked_users(void) const {
        toku_mutex_assert_locked(m_mutex);
        return m_num_want_read + m_num_want_write;
    }
    uint32_t frwlock::writers(void) const {
        // this is sometimes called as "assert(lock->writers())" when we
        // assume we have the write lock.  if that's the assumption, we may
        // not own the mutex, so we don't assert_locked here
        return m_num_writers;
    }
    uint32_t frwlock::blocked_writers(void) const {
        toku_mutex_assert_locked(m_mutex);
        return m_num_want_write;
    }
    uint32_t frwlock::readers(void) const {
        toku_mutex_assert_locked(m_mutex);
        return m_num_readers;
    }
    uint32_t frwlock::blocked_readers(void) const {
        toku_mutex_assert_locked(m_mutex);
        return m_num_want_read;
    }

} // namespace toku