Skip to content

Commit

Permalink
Removed undefined behavior on concurrently operating on set - global …
Browse files Browse the repository at this point in the history
…locks on set
  • Loading branch information
Michał Ołtarzewski committed Dec 29, 2017
1 parent 1f5409b commit baa1c5d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project (MIMUW-adorate CXX)
find_package ( Threads REQUIRED )

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "-O3")
set(CMAKE_CXX_FLAGS "-O2")

# http://stackoverflow.com/questions/10555706/
macro (add_executable _name)
Expand Down
31 changes: 11 additions & 20 deletions adorate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,10 @@ void readGraphAndPrepare(char* fileName) {
infile.close();
}

atomic<bool> *removeSpinLock;
atomic<bool> *spinLock;

inline pair<int, int> sLast(int x, int method) {
pair<int, int> result;
bool expected;
do {
expected = true;
removeSpinLock[x].compare_exchange_weak(expected, false);
} while (expected == false);

if (bvalue(method, mapping[x]) < S[x].size())
result = *(++(S[x].begin()));
Expand All @@ -80,29 +75,35 @@ inline pair<int, int> sLast(int x, int method) {
else
result = {-1, -1};

removeSpinLock[x] = true;
return result;
}

auto findMax(int curr, int method) {
auto i = lastProcessed[curr];
while (i != N[curr].rend()) {
bool expected;
do {
expected = true;
spinLock[i->second].compare_exchange_weak(expected, false);
} while (expected == false);
if (S[i->second].find({i->first, curr}) == S[i->second].end() && bvalue(method, mapping[i->second]) != 0) { // TODO usunac != 0
auto last = sLast(i->second, method);
if (i->first > last.first ||
(last.first == i->first && mapping[curr] > mapping[last.second])) {
lastProcessed[curr] = ++i;
return --i;
--i;
spinLock[i->second] = true;
return i;
}
}
spinLock[i->second] = true;
i++;
}
lastProcessed[curr] = N[curr].rend();
return N[curr].rend();
}

atomic<bool> lockR;
atomic<bool> *spinLock;
atomic<int> nodesQueue;
bool *inR;

Expand Down Expand Up @@ -146,14 +147,8 @@ void processNode(int method, bool isFirstRound) {
if (y.second != -1)
T[y.second]--;
S[x->second].insert({x->first, curr});
if (y.second != -1) {
do {
expected = true;
removeSpinLock[x->second].compare_exchange_weak(expected, false);
} while (expected == false);
if (y.second != -1)
S[x->second].erase(S[x->second].begin());
removeSpinLock[x->second] = true;
}
spinLock[x->second] = true;

if (y.second != -1) {
Expand Down Expand Up @@ -219,11 +214,7 @@ int main(int argc, char** argv) {
lockR = true;
lastProcessed = new set<pair<int, int>>::reverse_iterator[N.size()];
T = new atomic<unsigned int>[N.size()];

spinLock = new atomic<bool>[N.size()];
removeSpinLock = new atomic<bool>[N.size()];
for (unsigned int i = 0; i < N.size(); i++)
spinLock[i] = removeSpinLock[i] = true;

for (int method = 0; method <= blimit; method++) {
S = new set<pair<int, int>, setComp>[N.size()];
Expand Down

0 comments on commit baa1c5d

Please sign in to comment.