/src/trafficserver/include/tsutil/DenseThreadId.h
Line | Count | Source |
1 | | /** @file |
2 | | |
3 | | A replacement for std::shared_mutex with guarantees against writer starvation. |
4 | | Cache contention between CPU cores is avoided except when a write lock is taken. |
5 | | Assumes no thread will exit while holding mutex. |
6 | | |
7 | | @section license License |
8 | | |
9 | | Licensed to the Apache Software Foundation (ASF) under one |
10 | | or more contributor license agreements. See the NOTICE file |
11 | | distributed with this work for additional information |
12 | | regarding copyright ownership. The ASF licenses this file |
13 | | to you under the Apache License, Version 2.0 (the |
14 | | "License"); you may not use this file except in compliance |
15 | | with the License. You may obtain a copy of the License at |
16 | | |
17 | | http://www.apache.org/licenses/LICENSE-2.0 |
18 | | |
19 | | Unless required by applicable law or agreed to in writing, software |
20 | | distributed under the License is distributed on an "AS IS" BASIS, |
21 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
22 | | See the License for the specific language governing permissions and |
23 | | limitations under the License. |
24 | | */ |
25 | | |
26 | | #pragma once |
27 | | |
28 | | #include "tsutil/Assert.h" |
29 | | |
30 | | #include <array> |
31 | | #include <cstddef> |
32 | | #include <mutex> |
33 | | |
34 | | // Provide an alternate thread id, suitable for use as an array index. |
35 | | // |
36 | | class DenseThreadId |
37 | | { |
38 | | public: |
39 | | static constexpr std::size_t |
40 | | num_possible_values() |
41 | 0 | { |
42 | 0 | return _num_possible_values; |
43 | 0 | } |
44 | | |
45 | | static std::size_t |
46 | | self() |
47 | 0 | { |
48 | 0 | return _id.val; |
49 | 0 | } |
50 | | |
51 | | private: |
52 | | static constexpr std::size_t _num_possible_values{256}; |
53 | | inline static std::mutex _mtx; |
54 | | inline static std::array<std::size_t, _num_possible_values> _id_stack; |
55 | | inline static std::size_t _stack_top_idx; |
56 | | |
57 | | static void |
58 | | _init() |
59 | 0 | { |
60 | 0 | _stack_top_idx = 0; |
61 | 0 | for (std::size_t i{0}; i < _num_possible_values; ++i) { |
62 | 0 | _id_stack[i] = i + 1; |
63 | 0 | } |
64 | 0 | } |
65 | | |
66 | | struct _Id { |
67 | | _Id() |
68 | 0 | { |
69 | 0 | std::unique_lock<std::mutex> ul{_mtx}; |
70 | |
|
71 | 0 | if (!_inited) { |
72 | 0 | _init(); |
73 | 0 | _inited = true; |
74 | 0 | } |
75 | 0 | if (_num_possible_values == _stack_top_idx) { |
76 | 0 | fatal_error("DenseThreadId: number of threads exceeded maximum {}", unsigned(_num_possible_values)); |
77 | 0 | } |
78 | 0 | val = _stack_top_idx; |
79 | 0 | _stack_top_idx = _id_stack[_stack_top_idx]; |
80 | 0 | } |
81 | | |
82 | | ~_Id() |
83 | 0 | { |
84 | 0 | std::unique_lock<std::mutex> ul{_mtx}; |
85 | |
|
86 | 0 | _id_stack[val] = _stack_top_idx; |
87 | 0 | _stack_top_idx = val; |
88 | 0 | } |
89 | | |
90 | | std::size_t val; |
91 | | }; |
92 | | |
93 | | inline static thread_local _Id _id; |
94 | | inline static bool _inited{false}; |
95 | | }; |