]> git.proxmox.com Git - ceph.git/blob - ceph/src/zstd/contrib/pzstd/ErrorHolder.h
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / zstd / contrib / pzstd / ErrorHolder.h
1 /**
2 * Copyright (c) 2016-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
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 }