File indexing completed on 2024-05-19 05:42:10

0001 // ct_lvtmdb_lockable.cpp                                             -*-C++-*-
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #include <ct_lvtmdb_lockable.h>
0021 
0022 #include <cassert>
0023 #include <iostream>
0024 
0025 namespace Codethink::lvtmdb {
0026 
0027 std::pair<Lockable::ROLock, Lockable::ROLock> Lockable::roLockTwo(Lockable *a, Lockable *b)
0028 {
0029     assert(a);
0030     assert(b);
0031     if (a == b) {
0032         std::cerr << "lvtmdb::Lockable::lockTwo with identical arguments" << std::endl;
0033         // we can't return two distinct locks to one object: that would be a
0034         // deadlock
0035         std::abort();
0036     }
0037 
0038     // order by memory address
0039     if (a < b) {
0040         return {a->readOnlyLock(), b->readOnlyLock()};
0041     }
0042     return {b->readOnlyLock(), a->readOnlyLock()};
0043 }
0044 
0045 std::pair<Lockable::RWLock, Lockable::RWLock> Lockable::rwLockTwo(Lockable *a, Lockable *b)
0046 {
0047     assert(a);
0048     assert(b);
0049     if (a == b) {
0050         std::cerr << "lvtmdb::Lockable::lockTwo with identical arguments" << std::endl;
0051         // we can't return two distinct locks to one object: that would be a
0052         // deadlock
0053         std::abort();
0054     }
0055 
0056     // order by memory address
0057     if (a < b) {
0058         return {a->rwLock(), b->rwLock()};
0059     }
0060     return {b->rwLock(), a->rwLock()};
0061 }
0062 
0063 } // namespace Codethink::lvtmdb