condition_variable.c 5.91 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
 *
 * 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) {
36 37 38
  if (condition_var == NULL || mutex == NULL) {
    return EMBB_ERROR;
  }
39 40 41 42 43 44 45 46
  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);
}

47
#ifdef EMBB_PLATFORM_THREADING_WINTHREADS
48 49

int embb_condition_init(embb_condition_t* condition_var) {
50 51 52
  if (condition_var == NULL) {
    return EMBB_ERROR;
  }
53 54 55 56 57
  InitializeConditionVariable(condition_var);
  return EMBB_SUCCESS;
}

int embb_condition_notify_one(embb_condition_t* condition_var) {
58 59 60
  if (condition_var == NULL) {
    return EMBB_ERROR;
  }
61 62 63 64 65
  WakeConditionVariable(condition_var);
  return EMBB_SUCCESS;
}

int embb_condition_notify_all(embb_condition_t* condition_var) {
66 67 68
  if (condition_var == NULL) {
    return EMBB_ERROR;
  }
69 70 71 72 73 74
  WakeAllConditionVariable(condition_var);
  return EMBB_SUCCESS;
}

int embb_condition_wait(embb_condition_t* condition_var,
                        embb_mutex_t* mutex) {
75 76 77
  if (condition_var == NULL || mutex == NULL) {
    return EMBB_ERROR;
  }
78 79 80 81 82 83 84 85
  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) {
86 87 88
  if (condition_var == NULL || mutex == NULL || time == NULL) {
    return EMBB_ERROR;
  }
89 90 91 92 93
  /* 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 */
94 95
  if ((time->seconds * 1000 + time->nanoseconds / 1000000)
      > (now.seconds * 1000 + now.nanoseconds / 1000000)) {
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    /* 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) {
114 115 116
  if (condition_var == NULL) {
    return EMBB_ERROR;
  }
117 118 119 120
  EMBB_UNUSED_IN_RELEASE(condition_var);
  return EMBB_SUCCESS;
}

121
#endif /* EMBB_PLATFORM_THREADING_WINTHREADS */
122

123
#ifdef EMBB_PLATFORM_THREADING_POSIXTHREADS
124 125

int embb_condition_init(embb_condition_t* condition_var) {
126 127 128
  if (condition_var == NULL) {
    return EMBB_ERROR;
  }
129 130 131 132 133
  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) {
134 135 136
  if (condition_var == NULL) {
    return EMBB_ERROR;
  }
137 138 139 140 141
  int result = pthread_cond_signal(condition_var);
  return result == 0 ? EMBB_SUCCESS : EMBB_ERROR;
}

int embb_condition_notify_all(embb_condition_t* condition_var) {
142 143 144
  if (condition_var == NULL) {
    return EMBB_ERROR;
  }
145 146 147 148 149
  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) {
150 151 152
  if (condition_var == NULL || mutex == NULL) {
    return EMBB_ERROR;
  }
153 154 155 156 157 158
  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) {
159 160 161
  if (condition_var == NULL || mutex == NULL || time == NULL) {
    return EMBB_ERROR;
  }
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
  /* 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) {
177 178 179
  if (condition_var == NULL) {
    return EMBB_ERROR;
  }
180 181 182 183 184 185 186
  int status = pthread_cond_destroy(condition_var);
  if (status != 0) {
    return EMBB_ERROR;
  }
  return EMBB_SUCCESS;
}

187
#endif /* EMBB_PLATFORM_THREADING_POSIXTHREADS */