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
// Copyright The Pit Project Owners. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Please see https://github.com/openpitkit and the OWNERS file for details.
//! Locking policy that synchronizes both index and values.
use ;
use crate;
/// Locking policy factory that synchronizes **both** the index and the
/// values domain.
///
/// A [`Storage`](crate::storage::Storage) configured with `FullLocking` is
/// fully thread-safe:
///
/// * Many threads may concurrently look up existing keys.
/// * A thread inserting a new key or removing an existing one blocks
/// every other index access until it completes.
/// * Many threads may concurrently read values.
/// * A thread mutating any value blocks every other value access (read or
/// write) until it completes.
///
/// Note that the values lock is a single coarse reader-writer lock shared
/// by all keys, not a per-key lock. A writer to one value blocks readers
/// of every other value. The trade-off keeps the policy independent of
/// the key type and avoids the per-entry allocation overhead of per-key
/// locks.
///
/// The storage type is `Send + Sync` provided `Key: Send + Sync` and
/// `Value: Send + Sync`.
;
/// Concrete full-locking policy installed in storages built from [`FullLocking`].
///
/// Not re-exported from the crate; name it via the associated type if
/// needed: `<FullLocking as LockingPolicyFactory>::Policy`.
// SAFETY: both domains are protected by independent `parking_lot::RwLock`s.
// Each lock upholds the standard reader-writer invariants, which is
// exactly what the `LockingPolicy` contract requires. The storage takes
// the index lock before the values lock for every operation, eliminating
// deadlock risk between domains. Shared access uses `read_recursive()` to
// support the storage-level read-from-read re-entry contract even when a
// writer is queued.
unsafe
// SAFETY: `FullLockingPolicy` holds an independent `RwLock` for the
// values domain whose guards block every other read or write across the
// whole storage for the guard's lifetime, satisfying the
// `FullySynchronized` contract.
unsafe