Skip to content

Commit

Permalink
Use Lock instead of re-throwing exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
chschulte committed Nov 12, 2018
1 parent 0b6f236 commit 8711c9b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
2 changes: 1 addition & 1 deletion changelog.in
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Added example for magic squares with pre-filled instances.
Module: kernel
What: bug
Rank: minor
Author: Conrad Drescher
Author: Conrad Drescher, Patrick Zimmer
[DESCRIPTION]
Fixed potential deadlock when memory has been exhausted.

Expand Down
14 changes: 4 additions & 10 deletions gecode/kernel/memory/manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ namespace Gecode { namespace Kernel {

forceinline HeapChunk*
SharedMemory::alloc(size_t s, size_t l) {
m().acquire();
// To protect from exceptions from heap.ralloc()
Support::Lock guard(m());
while ((heap.hc != NULL) && (heap.hc->size < l)) {
heap.n_hc--;
HeapChunk* hc = heap.hc;
Expand All @@ -206,31 +207,24 @@ namespace Gecode { namespace Kernel {
HeapChunk* hc;
if (heap.hc == NULL) {
assert(heap.n_hc == 0);
try {
hc = static_cast<HeapChunk*>(Gecode::heap.ralloc(s));
} catch (Exception& e) {
m().release();
throw e;
}
hc = static_cast<HeapChunk*>(Gecode::heap.ralloc(s));
hc->size = s;
} else {
heap.n_hc--;
hc = heap.hc;
heap.hc = static_cast<HeapChunk*>(hc->next);
}
m().release();
return hc;
}
forceinline void
SharedMemory::free(HeapChunk* hc) {
m().acquire();
Support::Lock guard(m());
if (heap.n_hc == MemoryConfig::n_hc_cache) {
Gecode::heap.rfree(hc);
} else {
heap.n_hc++;
hc->next = heap.hc; heap.hc = hc;
}
m().release();
}


Expand Down

0 comments on commit 8711c9b

Please sign in to comment.