condition_variable.c 5.65 KB
Newer Older
1
/*
Marcus Winter committed
2
 * Copyright (c) 2014-2016, Siemens AG. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <embb/base/c/condition_variable.h>
#include <embb/base/c/time.h>
#include <embb/base/c/internal/unused.h>
#include <assert.h>
#include <stdio.h>

int embb_condition_wait_for(embb_condition_t* condition_var,
                            embb_mutex_t* mutex,
                            const embb_duration_t* duration) {
  assert(condition_var != NULL);
  assert(mutex != NULL);
  embb_time_t time;
  int status = embb_time_in(&time, duration);
  if (status != EMBB_SUCCESS) {
    return status;
  }
  return embb_condition_wait_until(condition_var, mutex, &time);
}

46
#ifdef EMBB_PLATFORM_THREADING_WINTHREADS
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

int embb_condition_init(embb_condition_t* condition_var) {
  assert(condition_var != NULL);
  InitializeConditionVariable(condition_var);
  return EMBB_SUCCESS;
}

int embb_condition_notify_one(embb_condition_t* condition_var) {
  assert(condition_var != NULL);
  WakeConditionVariable(condition_var);
  return EMBB_SUCCESS;
}

int embb_condition_notify_all(embb_condition_t* condition_var) {
  assert(condition_var != NULL);
  WakeAllConditionVariable(condition_var);
  return EMBB_SUCCESS;
}

int embb_condition_wait(embb_condition_t* condition_var,
                        embb_mutex_t* mutex) {
  assert(condition_var != NULL);
  assert(mutex != NULL);
  if (SleepConditionVariableCS(condition_var, mutex, INFINITE)) {
    return EMBB_SUCCESS;
  }
  return EMBB_ERROR;
}

int embb_condition_wait_until(embb_condition_t* condition_var,
                              embb_mutex_t* mutex, const embb_time_t* time) {
  assert(condition_var != NULL);
  assert(mutex != NULL);
  assert(time != NULL);
  /* The Windows API needs a time duration, so we need to convert the given time
     by using the time now. */
  embb_time_t now;
  embb_time_now(&now);
  /* Check if absolute timepoint (in milliseconds) still is in the future */
86 87
  if ((time->seconds * 1000 + time->nanoseconds / 1000000)
      > (now.seconds * 1000 + now.nanoseconds / 1000000)) {
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    /* Convert to (unsigned type) milliseconds and round up */
    DWORD time_diff = (DWORD) (
        time->seconds * 1000 + time->nanoseconds / 1000000
        - now.seconds * 1000 - now.nanoseconds / 1000000);
    if (SleepConditionVariableCS(condition_var, mutex, time_diff) == 0) {
      if (GetLastError() == ERROR_TIMEOUT) {
        return EMBB_TIMEDOUT;
      } else {
        return EMBB_ERROR;
      }
    }
  } else {
    return EMBB_TIMEDOUT;
  }
  return EMBB_SUCCESS;
}

int embb_condition_destroy(embb_condition_t* condition_var) {
  assert(condition_var != NULL);
  EMBB_UNUSED_IN_RELEASE(condition_var);
  return EMBB_SUCCESS;
}

111
#endif /* EMBB_PLATFORM_THREADING_WINTHREADS */
112

113
#ifdef EMBB_PLATFORM_THREADING_POSIXTHREADS
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

int embb_condition_init(embb_condition_t* condition_var) {
  assert(condition_var != NULL);
  int result = pthread_cond_init(condition_var, NULL);
  return result == 0 ? EMBB_SUCCESS : EMBB_ERROR;
}

int embb_condition_notify_one(embb_condition_t* condition_var) {
  assert(condition_var != NULL);
  int result = pthread_cond_signal(condition_var);
  return result == 0 ? EMBB_SUCCESS : EMBB_ERROR;
}

int embb_condition_notify_all(embb_condition_t* condition_var) {
  assert(condition_var != NULL);
  int result = pthread_cond_broadcast(condition_var);
  return result == 0 ? EMBB_SUCCESS : EMBB_ERROR;
}

int embb_condition_wait(embb_condition_t* condition_var, embb_mutex_t* mutex) {
  assert(condition_var != NULL);
  assert(mutex != NULL);
  int result = pthread_cond_wait(condition_var, mutex);
  return result == 0 ? EMBB_SUCCESS : EMBB_ERROR;
}

int embb_condition_wait_until(embb_condition_t* condition_var,
                              embb_mutex_t* mutex, const embb_time_t* time) {
  assert(condition_var != NULL);
  assert(mutex != NULL);
  assert(time != NULL);
  /* Convert EMBB time to Unix time format */
  struct timespec unix_time;
  unix_time.tv_sec = time->seconds;
  unix_time.tv_nsec = time->nanoseconds;
  int result = pthread_cond_timedwait(condition_var, mutex, &unix_time);
  if (result == ETIMEDOUT) {
    return EMBB_TIMEDOUT;
  }
  if (result != 0) {
    return EMBB_ERROR;
  }
  return EMBB_SUCCESS;
}

int embb_condition_destroy(embb_condition_t* condition_var) {
  assert(condition_var != NULL);
  int status = pthread_cond_destroy(condition_var);
  if (status != 0) {
    return EMBB_ERROR;
  }
  return EMBB_SUCCESS;
}

168
#endif /* EMBB_PLATFORM_THREADING_POSIXTHREADS */