Skip to content

Commit 0b29145

Browse files
Remove unreachable code (#535)
* Remove unreachable code This was noticed by MSVC in a debug build which produced the following warning: warning C4702: unreachable code * Update slot outside of the loop in MPMCQueue As suggested by @absurdfarce this change keeps finding a slot in the loop but moves the use of the slot past the loop. That way there is a return after the loop which should make clear to any code analysis that this function will always return something.
1 parent 1e8c520 commit 0b29145

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

src/execution_profile.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ CassError cass_execution_profile_set_latency_aware_routing_settings(
108108
CassError cass_execution_profile_set_whitelist_filtering(CassExecProfile* profile,
109109
const char* hosts) {
110110
return cass_execution_profile_set_whitelist_filtering_n(profile, hosts, SAFE_STRLEN(hosts));
111-
return CASS_OK;
112111
}
113112

114113
CassError cass_execution_profile_set_whitelist_filtering_n(CassExecProfile* profile,

src/mpmc_queue.hpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ class MPMCQueue : public Allocated {
5656
// buffer must be a size which is a power of 2. this also allows
5757
// the sequence to double as a ticket/lock.
5858
size_t pos = tail_.load(MEMORY_ORDER_RELAXED);
59+
Node* node;
5960

6061
for (;;) {
61-
Node* node = &buffer_[pos & mask_];
62+
node = &buffer_[pos & mask_];
6263
size_t node_seq = node->seq.load(MEMORY_ORDER_ACQUIRE);
6364
intptr_t dif = (intptr_t)node_seq - (intptr_t)pos;
6465

@@ -69,11 +70,7 @@ class MPMCQueue : public Allocated {
6970
// weak compare is faster, but can return spurious results
7071
// which in this instance is OK, because it's in the loop
7172
if (tail_.compare_exchange_weak(pos, pos + 1, MEMORY_ORDER_RELAXED)) {
72-
// set the data
73-
node->data = data;
74-
// increment the sequence so that the tail knows it's accessible
75-
node->seq.store(pos + 1, MEMORY_ORDER_RELEASE);
76-
return true;
73+
break;
7774
}
7875
} else if (dif < 0) {
7976
// if seq is less than head seq then it means this slot is
@@ -85,15 +82,19 @@ class MPMCQueue : public Allocated {
8582
}
8683
}
8784

88-
// never taken
89-
return false;
85+
// set the data
86+
node->data = data;
87+
// increment the sequence so that the tail knows it's accessible
88+
node->seq.store(pos + 1, MEMORY_ORDER_RELEASE);
89+
return true;
9090
}
9191

9292
bool dequeue(T& data) {
9393
size_t pos = head_.load(MEMORY_ORDER_RELAXED);
94+
Node* node;
9495

9596
for (;;) {
96-
Node* node = &buffer_[pos & mask_];
97+
node = &buffer_[pos & mask_];
9798
size_t node_seq = node->seq.load(MEMORY_ORDER_ACQUIRE);
9899
intptr_t dif = (intptr_t)node_seq - (intptr_t)(pos + 1);
99100

@@ -104,12 +105,7 @@ class MPMCQueue : public Allocated {
104105
// weak compare is faster, but can return spurious results
105106
// which in this instance is OK, because it's in the loop
106107
if (head_.compare_exchange_weak(pos, pos + 1, MEMORY_ORDER_RELAXED)) {
107-
// set the output
108-
data = node->data;
109-
// set the sequence to what the head sequence should be next
110-
// time around
111-
node->seq.store(pos + mask_ + 1, MEMORY_ORDER_RELEASE);
112-
return true;
108+
break;
113109
}
114110
} else if (dif < 0) {
115111
// if seq is less than head seq then it means this slot is
@@ -121,8 +117,12 @@ class MPMCQueue : public Allocated {
121117
}
122118
}
123119

124-
// never taken
125-
return false;
120+
// set the output
121+
data = node->data;
122+
// set the sequence to what the head sequence should be next
123+
// time around
124+
node->seq.store(pos + mask_ + 1, MEMORY_ORDER_RELEASE);
125+
return true;
126126
}
127127

128128
bool is_empty() const {

0 commit comments

Comments
 (0)