1 | /******************************************************************************* |
2 | * Copyright 2021 Intel Corporation |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | *******************************************************************************/ |
16 | |
17 | #ifndef COMMON_CACHE_BLOB_ID_HPP |
18 | #define COMMON_CACHE_BLOB_ID_HPP |
19 | |
20 | #include <atomic> |
21 | #include <cstdint> |
22 | #include <mutex> |
23 | #include <vector> |
24 | #include <type_traits> |
25 | |
26 | #include "common/serialization_stream.hpp" |
27 | |
28 | namespace dnnl { |
29 | namespace impl { |
30 | |
31 | struct primitive_desc_t; |
32 | struct cache_blob_id_t { |
33 | cache_blob_id_t() : is_initialized_ {false} {} |
34 | cache_blob_id_t(const cache_blob_id_t &other) |
35 | : sstream_(other.is_initialized_ ? other.sstream_ |
36 | : serialization_stream_t {}) |
37 | , is_initialized_(!sstream_.empty()) {} |
38 | |
39 | cache_blob_id_t(cache_blob_id_t &&other) = delete; |
40 | cache_blob_id_t &operator=(const cache_blob_id_t &other) = delete; |
41 | cache_blob_id_t &operator=(cache_blob_id_t &&other) = delete; |
42 | |
43 | const std::vector<uint8_t> &get( |
44 | const engine_t *engine, const primitive_desc_t *pd); |
45 | |
46 | private: |
47 | serialization_stream_t sstream_; |
48 | std::once_flag flag_; |
49 | |
50 | // The `std::once_flag` is neither copyable nor movable therefore we |
51 | // define a copy constructor that skips copying the `flag_`. To be able |
52 | // to carry over the `flag_`'s state from the `other` object we introduce |
53 | // an atomic `is_initialized_` flag. |
54 | std::atomic<bool> is_initialized_; |
55 | }; |
56 | |
57 | } // namespace impl |
58 | } // namespace dnnl |
59 | |
60 | #endif |
61 | |