+static const struct timespec time_delta = {
+ .tv_sec = DEFAULT_HEALTH_CHECK_DELTA_S,
+ .tv_nsec = DEFAULT_HEALTH_CHECK_DELTA_NS,
+};
+
+/* Define TLS health state. */
+DEFINE_URCU_TLS(struct health_state, health_state);
+
+/*
+ * It ensures that TLS memory used for the node and its container structure
+ * don't get reclaimed after the TLS owner thread exits until we have finished
+ * using it.
+ */
+static pthread_mutex_t health_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static struct health_tls_state_list health_state_list = {
+ .head = CDS_LIST_HEAD_INIT(health_state_list.head),
+};
+
+/*
+ * This keeps track of the error state for unregistered thread. A thread
+ * reporting a health error, normally unregisters and quits. This makes the TLS
+ * health state not available to the health_check_state() call so on unregister
+ * we update this global error array so we can keep track of which thread was
+ * on error if the TLS health state has been removed.
+ */
+static enum health_flags global_error_state[HEALTH_NUM_TYPE];
+
+/*
+ * Lock health state global list mutex.
+ */
+static void state_lock(void)
+{
+ pthread_mutex_lock(&health_mutex);
+}
+
+/*
+ * Unlock health state global list mutex.
+ */
+static void state_unlock(void)
+{
+ pthread_mutex_unlock(&health_mutex);
+}
+
+/*
+ * Set time difference in res from time_a and time_b.
+ */
+static void time_diff(const struct timespec *time_a,
+ const struct timespec *time_b, struct timespec *res)
+{
+ if (time_a->tv_nsec - time_b->tv_nsec < 0) {
+ res->tv_sec = time_a->tv_sec - time_b->tv_sec - 1;
+ res->tv_nsec = 1000000000L + time_a->tv_sec - time_b->tv_sec;
+ } else {
+ res->tv_sec = time_a->tv_sec - time_b->tv_sec;
+ res->tv_nsec = time_a->tv_nsec - time_b->tv_nsec;
+ }
+}
+
+/*
+ * Return true if time_a - time_b > diff, else false.
+ */
+static int time_diff_gt(const struct timespec *time_a,
+ const struct timespec *time_b, const struct timespec *diff)
+{
+ struct timespec res;
+
+ time_diff(time_a, time_b, &res);
+ time_diff(&res, diff, &res);
+
+ if (res.tv_sec > 0) {
+ return 1;
+ } else if (res.tv_sec == 0 && res.tv_nsec > 0) {
+ return 1;
+ }
+
+ return 0;
+}
+