IDState* ids = &dss->idStates[queryId];
int64_t usageIndicator = ids->usageIndicator;
- if(usageIndicator < 0) {
+ if(!IDState::isInUse(usageIndicator)) {
/* the corresponding state is marked as not in use, meaning that:
- it was already cleaned up by another thread and the state is gone ;
- we already got a response for this query and this one is a duplicate.
bool isDoH = du != nullptr;
/* atomically mark the state as available, but only if it has not been altered
in the meantime */
- if (ids->usageIndicator.compare_exchange_strong(usageIndicator, -1)) {
+ if (ids->tryMarkUnused(usageIndicator)) {
/* clear the potential DOHUnit asap, it's ours now
and since we just marked the state as unused,
someone could overwrite it. */
/* that means that the state was in use, possibly with an allocated
DOHUnit that we will need to handle, but we can't touch it before
confirming that we now own this state */
- if (ids->usageIndicator != -1) {
+ if (ids->isInUse()) {
du = ids->du;
}
/* we atomically replace the value, we now own this state */
- auto generation = ids->generation++;
- int64_t oldUsage = ids->usageIndicator.exchange(generation);
- if(oldUsage < 0) {
- /* the value was -1, meaning that the state was not in use.
+ if (!ids->markAsUsed()) {
+ /* the state was not in use.
we reset 'du' because it might have still been in use when we read it. */
du = nullptr;
++ss->outstanding;
for(IDState& ids : dss->idStates) { // timeouts
int64_t usageIndicator = ids.usageIndicator;
- if(usageIndicator >=0 && ids.age++ > g_udpTimeout) {
- /* We set usageIndicator to -1 as soon as possible
+ if(IDState::isInUse(usageIndicator) && ids.age++ > g_udpTimeout) {
+ /* We mark the state as unused as soon as possible
to limit the risk of racing with the
responder thread.
*/
auto oldDU = ids.du;
- if (!ids.usageIndicator.compare_exchange_strong(usageIndicator, -1)) {
+ if (!ids.tryMarkUnused(usageIndicator)) {
/* this state has been altered in the meantime,
don't go anywhere near it */
continue;
tempFailureTTL = orig.tempFailureTTL;
}
+ static const int64_t unusedIndicator = -1;
+
+ static bool isInUse(int64_t usageIndicator)
+ {
+ return usageIndicator != unusedIndicator;
+ }
+
+ bool isInUse() const
+ {
+ return usageIndicator != unusedIndicator;
+ }
+
+ /* return true if the value has been successfully replaced meaning that
+ no-one updated the usage indicator in the meantime */
+ bool tryMarkUnused(int64_t expectedUsageIndicator)
+ {
+ return usageIndicator.compare_exchange_strong(expectedUsageIndicator, unusedIndicator);
+ }
+
+ /* mark as unused no matter what, return true if the state was in use before */
+ bool markAsUsed()
+ {
+ auto currentGeneration = generation++;
+ return markAsUsed(currentGeneration);
+ }
+
+ /* mark as unused no matter what, return true if the state was in use before */
+ bool markAsUsed(int64_t currentGeneration)
+ {
+ int64_t oldUsage = usageIndicator.exchange(currentGeneration);
+ return oldUsage != unusedIndicator;
+ }
+
/* We use this value to detect whether this state is in use.
For performance reasons we don't want to use a lock here, but that means
we need to be very careful when modifying this value. Modifications happen
wrapping around if necessary, and we set an atomic signed 64-bit value, so that we still have -1
when the state is unused and the value of our counter otherwise.
*/
- std::atomic<int64_t> usageIndicator{-1}; // set to <0 to indicate this state is empty // 4
- std::atomic<uint32_t> generation{0}; // increased every time a state is used, to be able to detect an ABA issue
+ std::atomic<int64_t> usageIndicator{unusedIndicator}; // set to unusedIndicator to indicate this state is empty // 8
+ std::atomic<uint32_t> generation{0}; // increased every time a state is used, to be able to detect an ABA issue // 4
ComboAddress origRemote; // 28
ComboAddress origDest; // 28
StopWatch sentTime; // 16
IDState* ids = &ss->idStates[idOffset];
ids->age = 0;
DOHUnit* oldDU = nullptr;
- if (ids->usageIndicator != -1) {
+ if (ids->isInUse()) {
/* that means that the state was in use, possibly with an allocated
DOHUnit that we will need to handle, but we can't touch it before
confirming that we now own this state */
/* we atomically replace the value, we now own this state */
int64_t generation = ids->generation++;
- int64_t oldUsage = ids->usageIndicator.exchange(generation);
- if (oldUsage < 0) {
- /* the value was -1, meaning that the state was not in use.
+ if (!ids->markAsUsed(generation)) {
+ /* the state was not in use.
we reset 'oldDU' because it might have still been in use when we read it. */
oldDU = nullptr;
++ss->outstanding;
}
else {
+ ids->du = nullptr;
+ /* we are reusing a state, no change in outstanding but if there was an existing DOHUnit we need
+ to handle it because it's about to be overwritten. */
++ss->reuseds;
++g_stats.downstreamTimeouts;
- /* we are reusing a state, if there was an existing DOHUnit we need
- to handle it because it's about to be overwritten. */
- ids->du = nullptr;
handleDOHTimeout(oldDU);
}
/* we are about to handle the error, make sure that
this pointer is not accessed when the state is cleaned,
but first check that it still belongs to us */
- if (ids->usageIndicator.compare_exchange_strong(generation, -1)) {
+ if (ids->tryMarkUnused(generation)) {
ids->du = nullptr;
--ss->outstanding;
}