mirror of
https://github.com/matrix-construct/construct
synced 2024-09-26 18:38:52 +02:00
ircd::util: Add a non-atomic compare_exchange().
This commit is contained in:
parent
d884757155
commit
770088d677
2 changed files with 38 additions and 0 deletions
37
include/ircd/util/compare_exchange.h
Normal file
37
include/ircd/util/compare_exchange.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
// The Construct
|
||||
//
|
||||
// Copyright (C) The Construct Developers, Authors & Contributors
|
||||
// Copyright (C) 2016-2020 Jason Volk <jason@zemos.net>
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice is present in all copies. The
|
||||
// full license for this software is available in the LICENSE file.
|
||||
|
||||
#pragma once
|
||||
#define HAVE_IRCD_UTIL_COMPARE_EXCHANGE_H
|
||||
|
||||
namespace ircd {
|
||||
inline namespace util
|
||||
{
|
||||
template<class T>
|
||||
bool compare_exchange(T &value, T &expect, T desire) noexcept;
|
||||
}}
|
||||
|
||||
/// Non-atomic compare_exchange().
|
||||
template<class T>
|
||||
inline bool
|
||||
ircd::util::compare_exchange(T &value,
|
||||
T &expect,
|
||||
T desire)
|
||||
noexcept
|
||||
{
|
||||
const bool ret
|
||||
{
|
||||
value == expect
|
||||
};
|
||||
|
||||
value = ret? desire: value;
|
||||
expect = ret? expect: value;
|
||||
return ret;
|
||||
}
|
|
@ -64,6 +64,7 @@ namespace ircd
|
|||
#include "boolean.h"
|
||||
#include "maybe.h"
|
||||
#include "all.h"
|
||||
#include "compare_exchange.h"
|
||||
|
||||
// Unsorted section
|
||||
namespace ircd {
|
||||
|
|
Loading…
Reference in a new issue