sdm: Cancel display event thread on exit.

- Send a cancellation request to display event thread before waiting
  for it join. This thread waits for poll to return indefinitely
  which may not return if all of the events (vsync etc) are in
  disabled state.
- Drop this call for Android for now as this api is not supported in
  bionic.
- Fix GetVSyncState return value.
- Fix pwrite return value in case of set on idle time out.

Change-Id: I7da5be4c8a77fa986450dedb914fa2dba2751da9
diff --git a/sdm/include/utils/sys.h b/sdm/include/utils/sys.h
index 1c7e415..0a90fc1 100644
--- a/sdm/include/utils/sys.h
+++ b/sdm/include/utils/sys.h
@@ -28,6 +28,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <poll.h>
+#include <pthread.h>
 
 namespace sdm {
 
@@ -43,6 +44,7 @@
   typedef FILE* (*fopen)( const char *fname, const char *mode);
   typedef int (*fclose)(FILE* fileptr);
   typedef ssize_t (*getline)(char **lineptr, size_t *linelen, FILE *stream);
+  typedef int (*pthread_cancel)(pthread_t thread);
 
   static ioctl ioctl_;
   static open open_;
@@ -53,6 +55,7 @@
   static fopen fopen_;
   static fclose fclose_;
   static getline getline_;
+  static pthread_cancel pthread_cancel_;
 };
 
 }  // namespace sdm
diff --git a/sdm/libs/core/display_base.cpp b/sdm/libs/core/display_base.cpp
index 30aac4e..c42b89f 100644
--- a/sdm/libs/core/display_base.cpp
+++ b/sdm/libs/core/display_base.cpp
@@ -302,6 +302,8 @@
     return kErrorParameters;
   }
 
+  *enabled = vsync_enable_;
+
   return kErrorNone;
 }
 
diff --git a/sdm/libs/core/fb/hw_primary.cpp b/sdm/libs/core/fb/hw_primary.cpp
index e5d0d12..bd3b3e8 100644
--- a/sdm/libs/core/fb/hw_primary.cpp
+++ b/sdm/libs/core/fb/hw_primary.cpp
@@ -239,6 +239,7 @@
 
 DisplayError HWPrimary::Deinit() {
   exit_threads_ = true;
+  Sys::pthread_cancel_(event_thread_);
   pthread_join(event_thread_, NULL);
 
   for (int event = 0; event < kNumDisplayEvents; event++) {
@@ -566,7 +567,7 @@
 
   // Notify driver about the timeout value
   ssize_t length = Sys::pwrite_(fd, timeout_string, strlen(timeout_string), 0);
-  if (length < -1) {
+  if (length <= 0) {
     DLOGE("Unable to write into %s, node %s", node_path, strerror(errno));
   }
 
diff --git a/sdm/libs/utils/sys.cpp b/sdm/libs/utils/sys.cpp
index dd62d6e..03310ff 100644
--- a/sdm/libs/utils/sys.cpp
+++ b/sdm/libs/utils/sys.cpp
@@ -38,6 +38,10 @@
 
 #ifndef SDM_VIRTUAL_DRIVER
 
+int PthreadCancel(pthread_t /* thread */) {
+  return 0;
+}
+
 // Pointer to actual driver interfaces.
 Sys::ioctl Sys::ioctl_ = ::ioctl;
 Sys::open Sys::open_ = ::open;
@@ -48,6 +52,7 @@
 Sys::fopen Sys::fopen_ = ::fopen;
 Sys::fclose Sys::fclose_ = ::fclose;
 Sys::getline Sys::getline_ = ::getline;
+Sys::pthread_cancel Sys::pthread_cancel_ = PthreadCancel;
 
 #else
 
@@ -71,6 +76,7 @@
 Sys::fopen Sys::fopen_ = virtual_fopen;
 Sys::fclose Sys::fclose_ = virtual_fclose;
 Sys::getline Sys::getline_ = virtual_getline;
+Sys::pthread_cancel Sys::pthread_cancel_ = ::pthread_cancel;
 
 #endif  // SDM_VIRTUAL_DRIVER