pivotal-jbarrett commented on a change in pull request #731:
URL: https://github.com/apache/geode-native/pull/731#discussion_r565524041
##########
File path: cppcache/src/TXId.cpp
##########
@@ -27,15 +27,27 @@ namespace apache {
namespace geode {
namespace client {
-std::atomic<int32_t> TXId::m_transactionId(1);
+std::atomic<int32_t> TXId::m_transactionId(0);
-TXId::TXId() : m_TXId(m_transactionId++) {}
+TXId::TXId() {
+ // If m_transactionId has reached maximum value, then set
+ // it to zero in order to avoid overflow to negative value.
+ auto maxValueTXId = INT32_MAX;
Review comment:
```c++
auto maxValueTXId = std::numeric_limits<int>::max();
```
https://en.cppreference.com/w/cpp/types/numeric_limits
##########
File path: cppcache/src/TXId.cpp
##########
@@ -27,15 +27,27 @@ namespace apache {
namespace geode {
namespace client {
-std::atomic<int32_t> TXId::m_transactionId(1);
+std::atomic<int32_t> TXId::m_transactionId(0);
-TXId::TXId() : m_TXId(m_transactionId++) {}
+TXId::TXId() {
+ // If m_transactionId has reached maximum value, then set
+ // it to zero in order to avoid overflow to negative value.
+ auto maxValueTXId = INT32_MAX;
+ m_transactionId.compare_exchange_strong(maxValueTXId, 0);
+ m_TXId = ++m_transactionId;
Review comment:
While highly unlikely it is possible for `m_transactionId` to be at max
value again between lines 36 and 37. This whole operation should be done in a
loop where you fetch the current, increment if less than max or reset to 0,
then compare and set the new value. If failure to set, then try again.
```c++
auto current = m_transactionId.load();
decltype(current) next;
do {
next = std::numeric_limits<decltype(current)>::max() == current ? 1 :
current + 1;
} while(!m_transactionId.compare_exchange_strong(current, next));
m_TXId = next;
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]