diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-08-31 13:57:25 -0600 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-08-31 13:57:25 -0600 |
commit | 4a1bcc63cd2919f74f29b41c7b000f80da7449f4 (patch) | |
tree | cb213c45c610bff106761bbc0d4117ba6c0f8bd1 | |
parent | bbf14304d1112882c20f4ad3818712cdfecdedaa (diff) | |
download | sink-4a1bcc63cd2919f74f29b41c7b000f80da7449f4.tar.gz sink-4a1bcc63cd2919f74f29b41c7b000f80da7449f4.zip |
Avoid non threadsafe initialization.
local static initialization is only threadsafe if initialized on
construction. The other codepath is not threadsafe, but is only used in
testcode.
-rw-r--r-- | common/definitions.cpp | 18 | ||||
-rw-r--r-- | common/definitions.h | 8 |
2 files changed, 20 insertions, 6 deletions
diff --git a/common/definitions.cpp b/common/definitions.cpp index 17977bc..ee18d52 100644 --- a/common/definitions.cpp +++ b/common/definitions.cpp | |||
@@ -39,11 +39,17 @@ QString Sink::storageLocation() | |||
39 | return dataLocation() + "/storage"; | 39 | return dataLocation() + "/storage"; |
40 | } | 40 | } |
41 | 41 | ||
42 | static QString sinkLocation(QStandardPaths::StandardLocation location) | ||
43 | { | ||
44 | return QStandardPaths::writableLocation(location) + "/sink"; | ||
45 | } | ||
46 | |||
42 | QString Sink::dataLocation() | 47 | QString Sink::dataLocation() |
43 | { | 48 | { |
44 | static QString location; | 49 | static QString location = sinkLocation(QStandardPaths::GenericDataLocation); |
50 | //Warning: This is not threadsafe, but clearLocationCache is only ever used in testcode. The initialization above is required to make at least the initialization threadsafe (relies on C++11 threadsafe initialization). | ||
45 | if (rereadDataLocation) { | 51 | if (rereadDataLocation) { |
46 | location = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/sink"; | 52 | location = sinkLocation(QStandardPaths::GenericDataLocation); |
47 | rereadDataLocation = false; | 53 | rereadDataLocation = false; |
48 | } | 54 | } |
49 | return location; | 55 | return location; |
@@ -51,9 +57,10 @@ QString Sink::dataLocation() | |||
51 | 57 | ||
52 | QString Sink::configLocation() | 58 | QString Sink::configLocation() |
53 | { | 59 | { |
54 | static QString location; | 60 | static QString location = sinkLocation(QStandardPaths::GenericConfigLocation); |
61 | //Warning: This is not threadsafe, but clearLocationCache is only ever used in testcode. The initialization above is required to make at least the initialization threadsafe (relies on C++11 threadsafe initialization). | ||
55 | if (rereadConfigLocation) { | 62 | if (rereadConfigLocation) { |
56 | location = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + "/sink"; | 63 | location = sinkLocation(QStandardPaths::GenericConfigLocation); |
57 | rereadConfigLocation = false; | 64 | rereadConfigLocation = false; |
58 | } | 65 | } |
59 | return location; | 66 | return location; |
@@ -61,8 +68,9 @@ QString Sink::configLocation() | |||
61 | 68 | ||
62 | QString Sink::temporaryFileLocation() | 69 | QString Sink::temporaryFileLocation() |
63 | { | 70 | { |
64 | static QString location; | 71 | static QString location = dataLocation() + "/temporaryFiles"; |
65 | static bool dirCreated = false; | 72 | static bool dirCreated = false; |
73 | //Warning: This is not threadsafe, but clearLocationCache is only ever used in testcode. The initialization above is required to make at least the initialization threadsafe (relies on C++11 threadsafe initialization). | ||
66 | if (rereadTemporaryFileLocation) { | 74 | if (rereadTemporaryFileLocation) { |
67 | location = dataLocation() + "/temporaryFiles"; | 75 | location = dataLocation() + "/temporaryFiles"; |
68 | dirCreated = QDir{}.mkpath(location); | 76 | dirCreated = QDir{}.mkpath(location); |
diff --git a/common/definitions.h b/common/definitions.h index ce9e794..7ef215b 100644 --- a/common/definitions.h +++ b/common/definitions.h | |||
@@ -25,10 +25,16 @@ | |||
25 | #include <QByteArray> | 25 | #include <QByteArray> |
26 | 26 | ||
27 | namespace Sink { | 27 | namespace Sink { |
28 | void SINK_EXPORT clearLocationCache(); | ||
29 | QString SINK_EXPORT storageLocation(); | 28 | QString SINK_EXPORT storageLocation(); |
30 | QString SINK_EXPORT dataLocation(); | 29 | QString SINK_EXPORT dataLocation(); |
31 | QString SINK_EXPORT configLocation(); | 30 | QString SINK_EXPORT configLocation(); |
32 | QString SINK_EXPORT temporaryFileLocation(); | 31 | QString SINK_EXPORT temporaryFileLocation(); |
33 | QString SINK_EXPORT resourceStorageLocation(const QByteArray &resourceInstanceIdentifier); | 32 | QString SINK_EXPORT resourceStorageLocation(const QByteArray &resourceInstanceIdentifier); |
33 | |||
34 | /** | ||
35 | * Clear the location cache and lookup locations again. | ||
36 | * | ||
37 | * Warning: Calling this results in non-threadsafe initialization, only use it in test-code. | ||
38 | */ | ||
39 | void SINK_EXPORT clearLocationCache(); | ||
34 | } | 40 | } |