]>
Commit | Line | Data |
---|---|---|
11fdf7f2 | 1 | /* |
7c673cae FG |
2 | * Copyright (c) 2016-present, Facebook, Inc. |
3 | * All rights reserved. | |
4 | * | |
11fdf7f2 TL |
5 | * This source code is licensed under both the BSD-style license (found in the |
6 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found | |
7 | * in the COPYING file in the root directory of this source tree). | |
7c673cae FG |
8 | */ |
9 | #pragma once | |
10 | ||
11 | #include <atomic> | |
12 | #include <cassert> | |
13 | #include <stdexcept> | |
14 | #include <string> | |
15 | ||
16 | namespace pzstd { | |
17 | ||
18 | // Coordinates graceful shutdown of the pzstd pipeline | |
19 | class ErrorHolder { | |
20 | std::atomic<bool> error_; | |
21 | std::string message_; | |
22 | ||
23 | public: | |
24 | ErrorHolder() : error_(false) {} | |
25 | ||
26 | bool hasError() noexcept { | |
27 | return error_.load(); | |
28 | } | |
29 | ||
30 | void setError(std::string message) noexcept { | |
31 | // Given multiple possibly concurrent calls, exactly one will ever succeed. | |
32 | bool expected = false; | |
33 | if (error_.compare_exchange_strong(expected, true)) { | |
34 | message_ = std::move(message); | |
35 | } | |
36 | } | |
37 | ||
38 | bool check(bool predicate, std::string message) noexcept { | |
39 | if (!predicate) { | |
40 | setError(std::move(message)); | |
41 | } | |
42 | return !hasError(); | |
43 | } | |
44 | ||
45 | std::string getError() noexcept { | |
46 | error_.store(false); | |
47 | return std::move(message_); | |
48 | } | |
49 | ||
50 | ~ErrorHolder() { | |
51 | assert(!hasError()); | |
52 | } | |
53 | }; | |
54 | } |