|
| 1 | +/* |
| 2 | + Copyright (c) 2025 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 | +#if __cplusplus >= 201703L |
| 18 | + |
| 19 | +/*begin_custom_mutex_chmap_example*/ |
| 20 | +#define TBB_PREVIEW_CONCURRENT_HASH_MAP_EXTENSIONS 1 |
| 21 | +#include "oneapi/tbb/concurrent_hash_map.h" |
| 22 | +#include <shared_mutex> |
| 23 | + |
| 24 | +class SharedMutexWrapper { |
| 25 | +public: |
| 26 | + // ReaderWriterMutex requirements |
| 27 | + |
| 28 | + static constexpr bool is_rw_mutex = true; |
| 29 | + static constexpr bool is_recursive_mutex = false; |
| 30 | + static constexpr bool is_fair_mutex = false; |
| 31 | + |
| 32 | + class scoped_lock { |
| 33 | + public: |
| 34 | + scoped_lock() : my_mutex_ptr(nullptr), my_writer_flag(false) {} |
| 35 | + scoped_lock(SharedMutexWrapper& mutex, bool write = true) |
| 36 | + : my_mutex_ptr(&mutex), my_writer_flag(write) |
| 37 | + { |
| 38 | + if (my_writer_flag) { |
| 39 | + my_mutex_ptr->my_mutex.lock(); |
| 40 | + } else { |
| 41 | + my_mutex_ptr->my_mutex.lock_shared(); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + ~scoped_lock() { |
| 46 | + if (my_mutex_ptr) release(); |
| 47 | + } |
| 48 | + |
| 49 | + void acquire(SharedMutexWrapper& mutex, bool write = true) { |
| 50 | + if (my_mutex_ptr) release(); |
| 51 | + |
| 52 | + my_mutex_ptr = &mutex; |
| 53 | + my_writer_flag = write; |
| 54 | + |
| 55 | + if (my_writer_flag) { |
| 56 | + my_mutex_ptr->my_mutex.lock(); |
| 57 | + } else { |
| 58 | + my_mutex_ptr->my_mutex.lock_shared(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + bool try_acquire(SharedMutexWrapper& mutex, bool write = true) { |
| 63 | + if (my_mutex_ptr) release(); |
| 64 | + |
| 65 | + my_mutex_ptr = &mutex; |
| 66 | + |
| 67 | + bool result = false; |
| 68 | + |
| 69 | + if (my_writer_flag) { |
| 70 | + result = my_mutex_ptr->my_mutex.try_lock(); |
| 71 | + } else { |
| 72 | + result = my_mutex_ptr->my_mutex.try_lock_shared(); |
| 73 | + } |
| 74 | + |
| 75 | + if (result) my_writer_flag = write; |
| 76 | + return result; |
| 77 | + } |
| 78 | + |
| 79 | + void release() { |
| 80 | + if (my_writer_flag) { |
| 81 | + my_mutex_ptr->my_mutex.unlock(); |
| 82 | + } else { |
| 83 | + my_mutex_ptr->my_mutex.unlock_shared(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + bool upgrade_to_writer() { |
| 88 | + // std::shared_mutex does not have the upgrade/downgrade semantics |
| 89 | + if (my_writer_flag) return true; // Already a writer |
| 90 | + |
| 91 | + my_mutex_ptr->my_mutex.unlock_shared(); |
| 92 | + my_mutex_ptr->my_mutex.lock(); |
| 93 | + return false; // The lock was reacquired |
| 94 | + } |
| 95 | + |
| 96 | + bool downgrade_to_reader() { |
| 97 | + if (!my_writer_flag) return true; // Already a reader |
| 98 | + |
| 99 | + my_mutex_ptr->my_mutex.unlock(); |
| 100 | + my_mutex_ptr->my_mutex.lock_shared(); |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + bool is_writer() const { |
| 105 | + return my_writer_flag; |
| 106 | + } |
| 107 | + |
| 108 | + private: |
| 109 | + SharedMutexWrapper* my_mutex_ptr; |
| 110 | + bool my_writer_flag; |
| 111 | + }; |
| 112 | +private: |
| 113 | + std::shared_mutex my_mutex; |
| 114 | +}; // struct SharedMutexWrapper |
| 115 | + |
| 116 | +int main() { |
| 117 | + using map_type = oneapi::tbb::concurrent_hash_map<int, int, |
| 118 | + oneapi::tbb::tbb_hash_compare<int>, |
| 119 | + oneapi::tbb::tbb_allocator<std::pair<const int, int>>, |
| 120 | + SharedMutexWrapper>; |
| 121 | + |
| 122 | + map_type map; // This object will use SharedMutexWrapper for thread safety of insert/find/erase operations |
| 123 | +} |
| 124 | +/*end_custom_mutex_chmap_example*/ |
| 125 | + |
| 126 | +#else // C++17 |
| 127 | +// Skip |
| 128 | +int main() {} |
| 129 | +#endif |
0 commit comments