Skip to content

Commit 0823c61

Browse files
committed
Don't throw from ctor when allocation fails and exceptions are disabled (abort instead)
1 parent b70c303 commit 0823c61

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

readerwriterqueue.h

+23-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ©2013-2015 Cameron Desrochers.
1+
// ©2013-2016 Cameron Desrochers.
22
// Distributed under the simplified BSD license (see the license file that
33
// should have come with this header).
44

@@ -10,7 +10,7 @@
1010
#include <cassert>
1111
#include <stdexcept>
1212
#include <cstdint>
13-
#include <cstdlib> // For malloc/free & size_t
13+
#include <cstdlib> // For malloc/free/abort & size_t
1414

1515

1616
// A lock-free queue for a single-consumer, single-producer architecture.
@@ -26,7 +26,15 @@
2626
// one role, is not safe unless properly synchronized.
2727
// Using the queue exclusively from one thread is fine, though a bit silly.
2828

29-
#define CACHE_LINE_SIZE 64
29+
#ifndef MOODYCAMEL_CACHE_LINE_SIZE
30+
#define MOODYCAMEL_CACHE_LINE_SIZE 64
31+
#endif
32+
33+
#ifndef MOODYCAMEL_EXCEPTIONS_ENABLED
34+
#if (defined(_MSC_VER) && defined(_CPPUNWIND)) || (defined(__GNUC__) && defined(__EXCEPTIONS)) || (!defined(_MSC_VER) && !defined(__GNUC__))
35+
#define MOODYCAMEL_EXCEPTIONS_ENABLED
36+
#endif
37+
#endif
3038

3139
#ifdef AE_VCPP
3240
#pragma warning(push)
@@ -90,7 +98,11 @@ class ReaderWriterQueue
9098
for (size_t i = 0; i != initialBlockCount; ++i) {
9199
auto block = make_block(largestBlockSize);
92100
if (block == nullptr) {
101+
#ifdef MOODYCAMEL_EXCEPTIONS_ENABLED
93102
throw std::bad_alloc();
103+
#else
104+
abort();
105+
#endif
94106
}
95107
if (firstBlock == nullptr) {
96108
firstBlock = block;
@@ -105,7 +117,11 @@ class ReaderWriterQueue
105117
else {
106118
firstBlock = make_block(largestBlockSize);
107119
if (firstBlock == nullptr) {
120+
#ifdef MOODYCAMEL_EXCEPTIONS_ENABLED
108121
throw std::bad_alloc();
122+
#else
123+
abort();
124+
#endif
109125
}
110126
firstBlock->next = firstBlock;
111127
}
@@ -543,11 +559,7 @@ class ReaderWriterQueue
543559
ReentrantGuard(bool& _inSection)
544560
: inSection(_inSection)
545561
{
546-
assert(!inSection);
547-
if (inSection) {
548-
throw std::runtime_error("ReaderWriterQueue does not support enqueuing or dequeuing elements from other elements' ctors and dtors");
549-
}
550-
562+
assert(!inSection && "ReaderWriterQueue does not support enqueuing or dequeuing elements from other elements' ctors and dtors");
551563
inSection = true;
552564
}
553565

@@ -567,11 +579,11 @@ class ReaderWriterQueue
567579
weak_atomic<size_t> front; // (Atomic) Elements are read from here
568580
size_t localTail; // An uncontended shadow copy of tail, owned by the consumer
569581

570-
char cachelineFiller0[CACHE_LINE_SIZE - sizeof(weak_atomic<size_t>) - sizeof(size_t)];
582+
char cachelineFiller0[MOODYCAMEL_CACHE_LINE_SIZE - sizeof(weak_atomic<size_t>) - sizeof(size_t)];
571583
weak_atomic<size_t> tail; // (Atomic) Elements are enqueued here
572584
size_t localFront;
573585

574-
char cachelineFiller1[CACHE_LINE_SIZE - sizeof(weak_atomic<size_t>) - sizeof(size_t)]; // next isn't very contended, but we don't want it on the same cache line as tail (which is)
586+
char cachelineFiller1[MOODYCAMEL_CACHE_LINE_SIZE - sizeof(weak_atomic<size_t>) - sizeof(size_t)]; // next isn't very contended, but we don't want it on the same cache line as tail (which is)
575587
weak_atomic<Block*> next; // (Atomic)
576588

577589
char* data; // Contents (on heap) are aligned to T's alignment
@@ -612,7 +624,7 @@ class ReaderWriterQueue
612624
private:
613625
weak_atomic<Block*> frontBlock; // (Atomic) Elements are enqueued to this block
614626

615-
char cachelineFiller[CACHE_LINE_SIZE - sizeof(weak_atomic<Block*>)];
627+
char cachelineFiller[MOODYCAMEL_CACHE_LINE_SIZE - sizeof(weak_atomic<Block*>)];
616628
weak_atomic<Block*> tailBlock; // (Atomic) Elements are dequeued from this block
617629

618630
size_t largestBlockSize;

0 commit comments

Comments
 (0)