Add semaphore tests, fix sem_destroy.

Bug: https://code.google.com/p/android/issues/detail?id=76088
Change-Id: I4a0561b23e90312384d40a1c804ca64ee98f4066
diff --git a/libc/bionic/bionic_time_conversions.cpp b/libc/bionic/bionic_time_conversions.cpp
index 7f3c026..75e8d49 100644
--- a/libc/bionic/bionic_time_conversions.cpp
+++ b/libc/bionic/bionic_time_conversions.cpp
@@ -28,6 +28,8 @@
 
 #include "private/bionic_time_conversions.h"
 
+#include "private/bionic_constants.h"
+
 bool timespec_from_timeval(timespec& ts, const timeval& tv) {
   // Whole seconds can just be copied.
   ts.tv_sec = tv.tv_sec;
@@ -49,3 +51,19 @@
   tv.tv_sec = ts.tv_sec;
   tv.tv_usec = ts.tv_nsec / 1000;
 }
+
+// Initializes 'ts' with the difference between 'abs_ts' and the current time
+// according to 'clock'. Returns false if abstime already expired, true otherwise.
+bool timespec_from_absolute_timespec(timespec& ts, const timespec& abs_ts, clockid_t clock) {
+  clock_gettime(clock, &ts);
+  ts.tv_sec = abs_ts.tv_sec - ts.tv_sec;
+  ts.tv_nsec = abs_ts.tv_nsec - ts.tv_nsec;
+  if (ts.tv_nsec < 0) {
+    ts.tv_sec--;
+    ts.tv_nsec += NS_PER_S;
+  }
+  if (ts.tv_nsec < 0 || ts.tv_sec < 0) {
+    return false;
+  }
+  return true;
+}