Add error check when init RS thread in sync mode.

RS should return false and stop further waiting when it failed to init RS thread,
otherwise it will cause RS caller ANR errors.
Update destructor function for the exception handling accordingly.

Change-Id: I80a946e6be6b23cc93388b5f56d58ad6dc809d8b
Signed-off-by: Xiaofei Wan <xiaofei.wan@intel.com>
diff --git a/cpp/RenderScript.cpp b/cpp/RenderScript.cpp
index 5709f79..d05e4cd 100644
--- a/cpp/RenderScript.cpp
+++ b/cpp/RenderScript.cpp
@@ -58,15 +58,19 @@
     if (mInit == true) {
         mMessageRun = false;
 
-        RS::dispatch->ContextDeinitToClient(mContext);
+        if (mContext) {
+            RS::dispatch->ContextDeinitToClient(mContext);
 
-        void *res = NULL;
-        int status = pthread_join(mMessageThreadId, &res);
+            void *res = NULL;
+            int status = pthread_join(mMessageThreadId, &res);
 
-        RS::dispatch->ContextDestroy(mContext);
-        mContext = NULL;
-        RS::dispatch->DeviceDestroy(mDev);
-        mDev = NULL;
+            RS::dispatch->ContextDestroy(mContext);
+            mContext = NULL;
+        }
+        if (mDev) {
+            RS::dispatch->DeviceDestroy(mDev);
+            mDev = NULL;
+        }
     }
 }
 
diff --git a/rsContext.cpp b/rsContext.cpp
index 1bcea6b..ef77685 100644
--- a/rsContext.cpp
+++ b/rsContext.cpp
@@ -580,6 +580,11 @@
     timerSet(RS_TIMER_INTERNAL);
     if (mSynchronous) {
         threadProc(this);
+
+        if (mError != RS_ERROR_NONE) {
+            ALOGE("Errors during thread init (sync mode)");
+            return false;
+        }
     } else {
         status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
         if (status) {
@@ -611,7 +616,7 @@
         int status = pthread_join(mThreadId, &res);
         rsAssert(mExit);
 
-        if (mHal.funcs.shutdownDriver) {
+        if (mHal.funcs.shutdownDriver && mHal.drv) {
             mHal.funcs.shutdownDriver(this);
         }