Fix AAudioLoader
Fixed the following issues:
-- using a static member variable for a singleton is not thread-safe
without explicit synchronization. A better approach is to use
a static variable in 'getInstance' function, which is guaranteed
to be synchronized by the standard;
-- the constructor has to be private;
-- the destructor doesn't need to be virtual;
-- inconsistend header guard name.
Change-Id: Ia926ce7601a2166d29d2558ceea51e122ce89232
diff --git a/CONTRIBUTORS b/CONTRIBUTORS
index 6e982a1..0b77924 100755
--- a/CONTRIBUTORS
+++ b/CONTRIBUTORS
@@ -11,3 +11,4 @@
Phil Burk <philburk@google.com>
Don Turner <donturner@google.com>
+Mikhail Naganov <mnaganov@google.com>
diff --git a/src/aaudio/AAudioLoader.cpp b/src/aaudio/AAudioLoader.cpp
index 7a8c8a3..4f1d0e2 100644
--- a/src/aaudio/AAudioLoader.cpp
+++ b/src/aaudio/AAudioLoader.cpp
@@ -20,17 +20,13 @@
#define LIB_AAUDIO_NAME "libaaudio.so"
-AAudioLoader *AAudioLoader::mInstance = nullptr;
-
AAudioLoader::~AAudioLoader() {
close(); // TODO dangerous from a destructor, require caller to close()
}
-AAudioLoader *AAudioLoader::getInstance() {
- if (mInstance == nullptr) {
- mInstance = new AAudioLoader();
- }
- return mInstance;
+AAudioLoader* AAudioLoader::getInstance() {
+ static AAudioLoader instance;
+ return &instance;
}
int AAudioLoader::open() {
diff --git a/src/aaudio/AAudioLoader.h b/src/aaudio/AAudioLoader.h
index b8f3dd3..389959f 100644
--- a/src/aaudio/AAudioLoader.h
+++ b/src/aaudio/AAudioLoader.h
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-#ifndef NATIVEMIDISYNTH_AAUDIOLOADER_H
-#define NATIVEMIDISYNTH_AAUDIOLOADER_H
+#ifndef OBOE_AAUDIO_LOADER_H_
+#define OBOE_AAUDIO_LOADER_H_
#include <unistd.h>
#include "oboe/OboeDefinitions.h"
@@ -42,10 +42,9 @@
*/
class AAudioLoader {
public:
- AAudioLoader() {}
- virtual ~AAudioLoader();
+ ~AAudioLoader();
- static AAudioLoader *getInstance(); // singleton
+ static AAudioLoader* getInstance(); // singleton
/**
* Open the AAudio shared library and load the function pointers.
@@ -142,7 +141,7 @@
// TODO add any missing AAudio functions.
private:
- static AAudioLoader *mInstance;
+ AAudioLoader() {}
// Load function pointers for specific signatures.
signature_PC_I load_PC_I(const char *name);
@@ -158,4 +157,4 @@
};
-#endif //NATIVEMIDISYNTH_AAUDIOLOADER_H
+#endif //OBOE_AAUDIO_LOADER_H_