Explorar el Código

core: open(/dev/urandom) at initialization only and close() at shutdown

Daniele Bartolini hace 6 años
padre
commit
f88f5dc11f

+ 25 - 4
src/core/guid.cpp

@@ -18,18 +18,39 @@
 
 namespace crown
 {
+namespace guid_globals
+{
+#if CROWN_PLATFORM_POSIX
+	static int _fd = -1;
+#endif
+
+	void init()
+	{
+#if CROWN_PLATFORM_POSIX
+		_fd = ::open("/dev/urandom", O_RDONLY);
+		CE_ASSERT(_fd != -1, "open: errno = %d", errno);
+#endif // CROWN_PLATFORM_POSIX
+	}
+
+	void shutdown()
+	{
+#if CROWN_PLATFORM_POSIX
+		::close(_fd);
+#endif // CROWN_PLATFORM_POSIX
+	}
+
+} // namespace guid_globals
+
 namespace guid
 {
 	Guid new_guid()
 	{
 		Guid guid;
 #if CROWN_PLATFORM_POSIX
-		int fd = open("/dev/urandom", O_RDONLY);
-		CE_ASSERT(fd != -1, "open: errno = %d", errno);
-		ssize_t rb = read(fd, &guid, sizeof(guid));
+		CE_ASSERT(guid_globals::_fd != -1, "new_guid: library uninitialized");
+		ssize_t rb = read(guid_globals::_fd, &guid, sizeof(guid));
 		CE_ENSURE(rb == sizeof(guid));
 		CE_UNUSED(rb);
-		close(fd);
 		guid.data3 = (guid.data3 & 0x4fffu) | 0x4000u;
 		guid.data4 = (guid.data4 & 0x3fffffffffffffffu) | 0x8000000000000000u;
 #elif CROWN_PLATFORM_WINDOWS

+ 11 - 0
src/core/guid.h

@@ -21,12 +21,23 @@ struct Guid
 	u64 data4;
 };
 
+namespace guid_globals
+{
+	///
+	void init();
+
+	///
+	void shutdown();
+
+} // namespace guid_globals
+
 /// Functions to manipulate Guid.
 ///
 /// @ingroup Core
 namespace guid
 {
 	/// Returns a new randomly generated Guid.
+	/// @note User must call guid_globals::init() first.
 	Guid new_guid();
 
 	/// Parses the guid from @a str.

+ 2 - 0
src/core/unit_tests.cpp

@@ -997,6 +997,7 @@ static void test_dynamic_string()
 static void test_guid()
 {
 	memory_globals::init();
+	guid_globals::init();
 	{
 		Guid guid = guid::new_guid();
 		char str[37];
@@ -1009,6 +1010,7 @@ static void test_guid()
 		ENSURE(guid::try_parse(guid, "961f8005-6a7e-4371-9272-8454dd786884"));
 		ENSURE(!guid::try_parse(guid, "961f80056a7e-4371-9272-8454dd786884"));
 	}
+	guid_globals::shutdown();
 	memory_globals::shutdown();
 }
 

+ 3 - 0
src/device/main_android.cpp

@@ -7,6 +7,7 @@
 
 #if CROWN_PLATFORM_ANDROID
 
+#include "core/guid.h"
 #include "core/thread/thread.h"
 #include "device/device.h"
 #include "device/device_event_queue.h"
@@ -337,11 +338,13 @@ void android_main(struct android_app* app)
 	app_dummy();
 
 	memory_globals::init();
+	guid_globals::init();
 
 	DeviceOptions opts(default_allocator(), 0, NULL);
 	opts._asset_manager = app->activity->assetManager;
 
 	crown::s_advc.run(app, opts);
+	guid_globals::shutdown();
 	memory_globals::shutdown();
 }
 

+ 7 - 4
src/device/main_linux.cpp

@@ -9,6 +9,7 @@
 
 #include "core/command_line.h"
 #include "core/containers/array.h"
+#include "core/guid.h"
 #include "core/os.h"
 #include "core/thread/thread.h"
 #include "core/unit_tests.h"
@@ -830,15 +831,17 @@ bool next_event(OsEvent& ev)
 
 } // namespace crown
 
-struct InitMemoryGlobals
+struct InitGlobals
 {
-	InitMemoryGlobals()
+	InitGlobals()
 	{
 		crown::memory_globals::init();
+		crown::guid_globals::init();
 	}
 
-	~InitMemoryGlobals()
+	~InitGlobals()
 	{
+		crown::guid_globals::shutdown();
 		crown::memory_globals::shutdown();
 	}
 };
@@ -854,7 +857,7 @@ int main(int argc, char** argv)
 	}
 #endif // CROWN_BUILD_UNIT_TESTS
 
-	InitMemoryGlobals m;
+	InitGlobals m;
 	CE_UNUSED(m);
 
 	DeviceOptions opts(default_allocator(), argc, (const char**)argv);

+ 6 - 4
src/device/main_windows.cpp

@@ -726,15 +726,17 @@ bool next_event(OsEvent& ev)
 
 } // namespace crown
 
-struct InitMemoryGlobals
+struct InitGlobals
 {
-	InitMemoryGlobals()
+	InitGlobals()
 	{
 		crown::memory_globals::init();
+		crown::guid::init();
 	}
 
-	~InitMemoryGlobals()
+	~InitGlobals()
 	{
+		crown::guid::shutdown();
 		crown::memory_globals::shutdown();
 	}
 };
@@ -757,7 +759,7 @@ int main(int argc, char** argv)
 	}
 #endif // CROWN_BUILD_UNIT_TESTS
 
-	InitMemoryGlobals m;
+	InitGlobals m;
 	CE_UNUSED(m);
 
 	DeviceOptions opts(default_allocator(), argc, (const char**)argv);