yamc_rwlock_sched.hpp 4.24 KB
Newer Older
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
/*
 * yamc_rwlock_sched.hpp
 *
 * MIT License
 *
 * Copyright (c) 2017 yohhoy
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#ifndef YAMC_RWLOCK_SCHED_HPP_
#define YAMC_RWLOCK_SCHED_HPP_

#include <cassert>
#include <cstddef>

/// default shared_mutex rwlock policy
#ifndef YAMC_RWLOCK_SCHED_DEFAULT
#define YAMC_RWLOCK_SCHED_DEFAULT yamc::rwlock::ReaderPrefer
#endif

namespace yamc {

/*
 * readers-writer locking policy for basic_shared_(timed)_mutex<RwLockPolicy>
 *
 * - yamc::rwlock::ReaderPrefer
 * - yamc::rwlock::WriterPrefer
 */
namespace rwlock {

/// Reader prefer scheduling
///
/// NOTE:
//    This policy might introduce "Writer Starvation" if readers continuously
//    hold shared lock. PThreads rwlock implementation in Linux use this
//    scheduling policy as default. (see also PTHREAD_RWLOCK_PREFER_READER_NP)
//
struct ReaderPrefer {
  static const std::size_t writer_mask = ~(~std::size_t(0u) >> 1);  // MSB 1bit
  static const std::size_t reader_mask = ~std::size_t(0u) >> 1;

  struct state {
    std::size_t rwcount = 0;
  };

  static void before_wait_wlock(const state&) {}
  static void after_wait_wlock(const state&) {}

  static bool wait_wlock(const state& s) { return (s.rwcount != 0); }

  static void acquire_wlock(state* s) {
    assert(!(s->rwcount & writer_mask));
    s->rwcount |= writer_mask;
  }

  static void release_wlock(state* s) {
    assert(s->rwcount & writer_mask);
    s->rwcount &= ~writer_mask;
  }

  static bool wait_rlock(const state& s) { return (s.rwcount & writer_mask) != 0; }

  static void acquire_rlock(state* s) {
    assert((s->rwcount & reader_mask) < reader_mask);
    ++(s->rwcount);
  }

  static bool release_rlock(state* s) {
    assert(0 < (s->rwcount & reader_mask));
    return (--(s->rwcount) == 0);
  }
};

/// Writer prefer scheduling
///
/// NOTE:
///   If there are waiting writer, new readers are blocked until all shared lock
///   are released,
//    and the writer thread can get exclusive lock in preference to blocked
//    reader threads. This policy might introduce "Reader Starvation" if writers
//    continuously request exclusive lock.
///   (see also PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP)
///
struct WriterPrefer {
  static const std::size_t locked = ~(~std::size_t(0u) >> 1);  // MSB 1bit
  static const std::size_t wait_mask = ~std::size_t(0u) >> 1;

  struct state {
    std::size_t nwriter = 0;
    std::size_t nreader = 0;
  };

  static void before_wait_wlock(state* s) {
    assert((s->nwriter & wait_mask) < wait_mask);
    ++(s->nwriter);
  }

  static bool wait_wlock(const state& s) {
    return ((s.nwriter & locked) || 0 < s.nreader);
  }

  static void after_wait_wlock(state* s) {
    assert(0 < (s->nwriter & wait_mask));
    --(s->nwriter);
  }

  static void acquire_wlock(state* s) {
    assert(!(s->nwriter & locked));
    s->nwriter |= locked;
  }

  static void release_wlock(state* s) {
    assert(s->nwriter & locked);
    s->nwriter &= ~locked;
  }

  static bool wait_rlock(const state& s) { return (s.nwriter != 0); }

  static void acquire_rlock(state* s) {
    assert(!(s->nwriter & locked));
    ++(s->nreader);
  }

  static bool release_rlock(state* s) {
    assert(0 < s->nreader);
    return (--(s->nreader) == 0);
  }
};

}  // namespace rwlock
}  // namespace yamc

#endif