Implement threads

Use std::thread

Change-Id: I912c8ebddd4da6e31dc131b18c275c67792b76cc
diff --git a/services/server/env/fuchsia/osfunc.cc b/services/server/env/fuchsia/osfunc.cc
index b0dd0b8..967ed53 100644
--- a/services/server/env/fuchsia/osfunc.cc
+++ b/services/server/env/fuchsia/osfunc.cc
@@ -29,6 +29,7 @@
 */ /**************************************************************************/
 
 #include <shared_mutex>
+#include <thread>
 
 #include <lib/zx/event.h>
 #include <zircon/syscalls.h>
@@ -66,6 +67,22 @@
 	}
 };
 
+struct Thread
+{
+	static constexpr uint32_t kMagic = 'hThr';
+	uint32_t magic = kMagic;
+	std::thread thread;
+
+	static Thread *cast(IMG_HANDLE thr)
+	{
+		auto thread_ptr = static_cast<Thread *>(thr);
+		assert(thread_ptr->magic == kMagic);
+
+		return thread_ptr;
+	}
+};
+
+
 } // namespace msd_img
 
 static PVRSRV_ERROR
@@ -432,14 +449,6 @@
 	return PVRSRV_ERROR_NOT_SUPPORTED;
 }
 
-
-static int
-OSThreadRun(void *data)
-{
-	NOT_IMPLEMENTED();
-	return 0;
-}
-
 PVRSRV_ERROR
 OSThreadCreate(IMG_HANDLE *phThread,
 	       IMG_CHAR *pszThreadName,
@@ -448,8 +457,10 @@
 	       IMG_BOOL bIsSupportingThread,
 	       void *hData)
 {
-	NOT_IMPLEMENTED();
-	return PVRSRV_ERROR_NOT_SUPPORTED;
+	auto handle = std::make_unique<msd_img::Thread>();
+	handle->thread = std::thread([](PFN_THREAD thread, void *data) { thread(data); }, pfnThread, hData);
+	*phThread = handle.release();
+	return PVRSRV_OK;
 }
 
 PVRSRV_ERROR
@@ -461,15 +472,18 @@
 		       void *hData,
 		       OS_THREAD_LEVEL eThreadPriority)
 {
-	NOT_IMPLEMENTED();
-	return PVRSRV_ERROR_NOT_SUPPORTED;
+	// The thread is created, but priority is ignored.
+	// TODO(MA-579): Implement.
+	return OSThreadCreate(phThread, pszThreadName, pfnThread, pfnDebugDumpCB, bIsSupportingThread, hData);
 }
 
 PVRSRV_ERROR
 OSThreadDestroy(IMG_HANDLE hThread)
 {
-	NOT_IMPLEMENTED();
-	return PVRSRV_ERROR_NOT_SUPPORTED;
+	msd_img::Thread *handle = msd_img::Thread::cast(hThread);
+	handle->thread.join();
+	delete handle;
+	return PVRSRV_OK;
 }
 
 void
@@ -481,8 +495,9 @@
 PVRSRV_ERROR
 OSSetThreadPriority(IMG_HANDLE hThread, IMG_UINT32 nThreadPriority, IMG_UINT32 nThreadWeight)
 {
+	// TODO(MA-579): Implement.
 	NOT_IMPLEMENTED();
-	return PVRSRV_ERROR_NOT_SUPPORTED;
+	return PVRSRV_OK;
 }
 
 void *