17ec681f3Smrg/**************************************************************************
27ec681f3Smrg *
37ec681f3Smrg * Copyright 2020 Lag Free Games, LLC
47ec681f3Smrg * All Rights Reserved.
57ec681f3Smrg *
67ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
77ec681f3Smrg * copy of this software and associated documentation files (the
87ec681f3Smrg * "Software"), to deal in the Software without restriction, including
97ec681f3Smrg * without limitation the rights to use, copy, modify, merge, publish,
107ec681f3Smrg * distribute, sub license, and/or sell copies of the Software, and to
117ec681f3Smrg * permit persons to whom the Software is furnished to do so, subject to
127ec681f3Smrg * the following conditions:
137ec681f3Smrg *
147ec681f3Smrg * The above copyright notice and this permission notice (including the
157ec681f3Smrg * next paragraph) shall be included in all copies or substantial portions
167ec681f3Smrg * of the Software.
177ec681f3Smrg *
187ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
197ec681f3Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
207ec681f3Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
217ec681f3Smrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
227ec681f3Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
237ec681f3Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
247ec681f3Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
257ec681f3Smrg *
267ec681f3Smrg **************************************************************************/
277ec681f3Smrg
287ec681f3Smrg#ifndef CND_MONOTONIC_H
297ec681f3Smrg#define CND_MONOTONIC_H
307ec681f3Smrg
317ec681f3Smrg#include "c11/threads.h"
327ec681f3Smrg#include "util/os_time.h"
337ec681f3Smrg
347ec681f3Smrg#ifdef __cplusplus
357ec681f3Smrgextern "C" {
367ec681f3Smrg#endif
377ec681f3Smrg
387ec681f3Smrgstruct u_cnd_monotonic
397ec681f3Smrg{
407ec681f3Smrg#ifdef _WIN32
417ec681f3Smrg   CONDITION_VARIABLE condvar;
427ec681f3Smrg#else
437ec681f3Smrg   pthread_cond_t cond;
447ec681f3Smrg#endif
457ec681f3Smrg};
467ec681f3Smrg
477ec681f3Smrgstatic inline int
487ec681f3Smrgu_cnd_monotonic_init(struct u_cnd_monotonic *cond)
497ec681f3Smrg{
507ec681f3Smrg   assert(cond != NULL);
517ec681f3Smrg
527ec681f3Smrg#ifdef _WIN32
537ec681f3Smrg   InitializeConditionVariable(&cond->condvar);
547ec681f3Smrg   return thrd_success;
557ec681f3Smrg#else
567ec681f3Smrg   int ret = thrd_error;
577ec681f3Smrg   pthread_condattr_t condattr;
587ec681f3Smrg   if (pthread_condattr_init(&condattr) == 0) {
597ec681f3Smrg      if ((pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC) == 0) &&
607ec681f3Smrg         (pthread_cond_init(&cond->cond, &condattr) == 0)) {
617ec681f3Smrg         ret = thrd_success;
627ec681f3Smrg      }
637ec681f3Smrg
647ec681f3Smrg      pthread_condattr_destroy(&condattr);
657ec681f3Smrg   }
667ec681f3Smrg
677ec681f3Smrg   return ret;
687ec681f3Smrg#endif
697ec681f3Smrg}
707ec681f3Smrg
717ec681f3Smrgstatic inline void
727ec681f3Smrgu_cnd_monotonic_destroy(struct u_cnd_monotonic *cond)
737ec681f3Smrg{
747ec681f3Smrg   assert(cond != NULL);
757ec681f3Smrg
767ec681f3Smrg#ifdef _WIN32
777ec681f3Smrg   // Do nothing
787ec681f3Smrg#else
797ec681f3Smrg   pthread_cond_destroy(&cond->cond);
807ec681f3Smrg#endif
817ec681f3Smrg}
827ec681f3Smrg
837ec681f3Smrgstatic inline int
847ec681f3Smrgu_cnd_monotonic_broadcast(struct u_cnd_monotonic *cond)
857ec681f3Smrg{
867ec681f3Smrg   assert(cond != NULL);
877ec681f3Smrg
887ec681f3Smrg#ifdef _WIN32
897ec681f3Smrg   WakeAllConditionVariable(&cond->condvar);
907ec681f3Smrg   return thrd_success;
917ec681f3Smrg#else
927ec681f3Smrg   return (pthread_cond_broadcast(&cond->cond) == 0) ? thrd_success : thrd_error;
937ec681f3Smrg#endif
947ec681f3Smrg}
957ec681f3Smrg
967ec681f3Smrgstatic inline int
977ec681f3Smrgu_cnd_monotonic_signal(struct u_cnd_monotonic *cond)
987ec681f3Smrg{
997ec681f3Smrg   assert(cond != NULL);
1007ec681f3Smrg
1017ec681f3Smrg#ifdef _WIN32
1027ec681f3Smrg   WakeConditionVariable(&cond->condvar);
1037ec681f3Smrg   return thrd_success;
1047ec681f3Smrg#else
1057ec681f3Smrg   return (pthread_cond_signal(&cond->cond) == 0) ? thrd_success : thrd_error;
1067ec681f3Smrg#endif
1077ec681f3Smrg}
1087ec681f3Smrg
1097ec681f3Smrgstatic inline int
1107ec681f3Smrgu_cnd_monotonic_timedwait(struct u_cnd_monotonic *cond, mtx_t *mtx, const struct timespec *abs_time)
1117ec681f3Smrg{
1127ec681f3Smrg   assert(cond != NULL);
1137ec681f3Smrg   assert(mtx != NULL);
1147ec681f3Smrg   assert(abs_time != NULL);
1157ec681f3Smrg
1167ec681f3Smrg#ifdef _WIN32
1177ec681f3Smrg   const uint64_t future = (abs_time->tv_sec * 1000) + (abs_time->tv_nsec / 1000000);
1187ec681f3Smrg   const uint64_t now = os_time_get_nano() / 1000000;
1197ec681f3Smrg   const DWORD timeout = (future > now) ? (DWORD)(future - now) : 0;
1207ec681f3Smrg   if (SleepConditionVariableCS(&cond->condvar, mtx, timeout))
1217ec681f3Smrg      return thrd_success;
1227ec681f3Smrg   return (GetLastError() == ERROR_TIMEOUT) ? thrd_busy : thrd_error;
1237ec681f3Smrg#else
1247ec681f3Smrg   int rt = pthread_cond_timedwait(&cond->cond, mtx, abs_time);
1257ec681f3Smrg   if (rt == ETIMEDOUT)
1267ec681f3Smrg      return thrd_busy;
1277ec681f3Smrg   return (rt == 0) ? thrd_success : thrd_error;
1287ec681f3Smrg#endif
1297ec681f3Smrg}
1307ec681f3Smrg
1317ec681f3Smrgstatic inline int
1327ec681f3Smrgu_cnd_monotonic_wait(struct u_cnd_monotonic *cond, mtx_t *mtx)
1337ec681f3Smrg{
1347ec681f3Smrg   assert(cond != NULL);
1357ec681f3Smrg   assert(mtx != NULL);
1367ec681f3Smrg
1377ec681f3Smrg#ifdef _WIN32
1387ec681f3Smrg   SleepConditionVariableCS(&cond->condvar, mtx, INFINITE);
1397ec681f3Smrg   return thrd_success;
1407ec681f3Smrg#else
1417ec681f3Smrg   return (pthread_cond_wait(&cond->cond, mtx) == 0) ? thrd_success : thrd_error;
1427ec681f3Smrg#endif
1437ec681f3Smrg}
1447ec681f3Smrg
1457ec681f3Smrg#ifdef __cplusplus
1467ec681f3Smrg}
1477ec681f3Smrg#endif
1487ec681f3Smrg
1497ec681f3Smrg#endif
150