forked from eclipse-iceoryx/iceoryx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05f686b
commit b86f22e
Showing
5 changed files
with
297 additions
and
10 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
iceoryx_hoofs/posix/sync/include/iox/spin_lock_mutex.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright (c) 2024 by ekxide IO GmbH. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#ifndef IOX_HOOFS_POXIX_SYNC_SPIN_LOCK_MUTEX_HPP | ||
#define IOX_HOOFS_POXIX_SYNC_SPIN_LOCK_MUTEX_HPP | ||
|
||
#include "iox/atomic.hpp" | ||
#include "iox/detail/adaptive_wait.hpp" | ||
#include "iox/mutex.hpp" | ||
|
||
namespace iox | ||
{ | ||
class SpinlockMutex | ||
{ | ||
public: | ||
SpinlockMutex() | ||
: m_flag(ATOMIC_FLAG_INIT) | ||
, m_recursive{Recursive{0, 0}} | ||
{ | ||
} | ||
|
||
expected<void, MutexLockError> lock() noexcept | ||
{ | ||
pid_t tid = gettid(); | ||
|
||
auto recursive = m_recursive.load(); | ||
|
||
if (recursive.tid == tid) | ||
{ | ||
recursive.count += 1; | ||
m_recursive.store(recursive); | ||
|
||
return ok(); | ||
} | ||
|
||
detail::adaptive_wait spinner; | ||
spinner.wait_loop([this] { return this->m_flag.test_and_set(std::memory_order_acquire); }); | ||
|
||
m_recursive.store(Recursive{tid, 1}); | ||
|
||
return ok(); | ||
} | ||
|
||
expected<void, MutexUnlockError> unlock() noexcept | ||
{ | ||
pid_t tid = gettid(); | ||
|
||
auto recursive = m_recursive.load(); | ||
|
||
if (recursive.tid == tid) | ||
{ | ||
recursive.count -= 1; | ||
|
||
if (recursive.count == 0) | ||
{ | ||
recursive.tid = 0; | ||
m_recursive.store(recursive); | ||
m_flag.clear(std::memory_order_release); | ||
} | ||
else | ||
{ | ||
m_recursive.store(recursive); | ||
} | ||
|
||
return ok(); | ||
} | ||
|
||
|
||
return err(MutexUnlockError::UNKNOWN_ERROR); | ||
} | ||
|
||
expected<MutexTryLock, MutexTryLockError> try_lock() noexcept | ||
{ | ||
pid_t tid = gettid(); | ||
|
||
auto recursive = m_recursive.load(); | ||
|
||
if (recursive.tid == tid) | ||
{ | ||
recursive.count += 1; | ||
m_recursive.store(recursive); | ||
return ok(MutexTryLock::LOCK_SUCCEEDED); | ||
} | ||
|
||
if (!m_flag.test_and_set(std::memory_order_acquire)) | ||
{ | ||
m_recursive.store(Recursive{tid, 1}); | ||
|
||
return ok(MutexTryLock::LOCK_SUCCEEDED); | ||
} | ||
return ok(MutexTryLock::FAILED_TO_ACQUIRE_LOCK); | ||
} | ||
|
||
struct Recursive | ||
{ | ||
pid_t tid; | ||
uint32_t count; | ||
}; | ||
|
||
private: | ||
concurrent::AtomicFlag m_flag; | ||
concurrent::Atomic<Recursive> m_recursive; | ||
}; | ||
|
||
} // namespace iox | ||
|
||
#endif // IOX_HOOFS_POXIX_SYNC_SPIN_LOCK_MUTEX_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters