Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcozecchini 0:9fca2b23d0ba 1 /*
marcozecchini 0:9fca2b23d0ba 2 * Implementation for Posix compliant platforms
marcozecchini 0:9fca2b23d0ba 3 *
marcozecchini 0:9fca2b23d0ba 4 * Copyright (c) 2016 Christopher Haster
marcozecchini 0:9fca2b23d0ba 5 *
marcozecchini 0:9fca2b23d0ba 6 * Licensed under the Apache License, Version 2.0 (the "License");
marcozecchini 0:9fca2b23d0ba 7 * you may not use this file except in compliance with the License.
marcozecchini 0:9fca2b23d0ba 8 * You may obtain a copy of the License at
marcozecchini 0:9fca2b23d0ba 9 *
marcozecchini 0:9fca2b23d0ba 10 * http://www.apache.org/licenses/LICENSE-2.0
marcozecchini 0:9fca2b23d0ba 11 *
marcozecchini 0:9fca2b23d0ba 12 * Unless required by applicable law or agreed to in writing, software
marcozecchini 0:9fca2b23d0ba 13 * distributed under the License is distributed on an "AS IS" BASIS,
marcozecchini 0:9fca2b23d0ba 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
marcozecchini 0:9fca2b23d0ba 15 * See the License for the specific language governing permissions and
marcozecchini 0:9fca2b23d0ba 16 * limitations under the License.
marcozecchini 0:9fca2b23d0ba 17 */
marcozecchini 0:9fca2b23d0ba 18 #include "equeue/equeue_platform.h"
marcozecchini 0:9fca2b23d0ba 19
marcozecchini 0:9fca2b23d0ba 20 #if defined(EQUEUE_PLATFORM_POSIX)
marcozecchini 0:9fca2b23d0ba 21
marcozecchini 0:9fca2b23d0ba 22 #include <time.h>
marcozecchini 0:9fca2b23d0ba 23 #include <sys/time.h>
marcozecchini 0:9fca2b23d0ba 24 #include <errno.h>
marcozecchini 0:9fca2b23d0ba 25
marcozecchini 0:9fca2b23d0ba 26
marcozecchini 0:9fca2b23d0ba 27 // Tick operations
marcozecchini 0:9fca2b23d0ba 28 unsigned equeue_tick(void) {
marcozecchini 0:9fca2b23d0ba 29 struct timeval tv;
marcozecchini 0:9fca2b23d0ba 30 gettimeofday(&tv, 0);
marcozecchini 0:9fca2b23d0ba 31 return (unsigned)(tv.tv_sec*1000 + tv.tv_usec/1000);
marcozecchini 0:9fca2b23d0ba 32 }
marcozecchini 0:9fca2b23d0ba 33
marcozecchini 0:9fca2b23d0ba 34
marcozecchini 0:9fca2b23d0ba 35 // Mutex operations
marcozecchini 0:9fca2b23d0ba 36 int equeue_mutex_create(equeue_mutex_t *m) {
marcozecchini 0:9fca2b23d0ba 37 return pthread_mutex_init(m, 0);
marcozecchini 0:9fca2b23d0ba 38 }
marcozecchini 0:9fca2b23d0ba 39
marcozecchini 0:9fca2b23d0ba 40 void equeue_mutex_destroy(equeue_mutex_t *m) {
marcozecchini 0:9fca2b23d0ba 41 pthread_mutex_destroy(m);
marcozecchini 0:9fca2b23d0ba 42 }
marcozecchini 0:9fca2b23d0ba 43
marcozecchini 0:9fca2b23d0ba 44 void equeue_mutex_lock(equeue_mutex_t *m) {
marcozecchini 0:9fca2b23d0ba 45 pthread_mutex_lock(m);
marcozecchini 0:9fca2b23d0ba 46 }
marcozecchini 0:9fca2b23d0ba 47
marcozecchini 0:9fca2b23d0ba 48 void equeue_mutex_unlock(equeue_mutex_t *m) {
marcozecchini 0:9fca2b23d0ba 49 pthread_mutex_unlock(m);
marcozecchini 0:9fca2b23d0ba 50 }
marcozecchini 0:9fca2b23d0ba 51
marcozecchini 0:9fca2b23d0ba 52
marcozecchini 0:9fca2b23d0ba 53 // Semaphore operations
marcozecchini 0:9fca2b23d0ba 54 int equeue_sema_create(equeue_sema_t *s) {
marcozecchini 0:9fca2b23d0ba 55 int err = pthread_mutex_init(&s->mutex, 0);
marcozecchini 0:9fca2b23d0ba 56 if (err) {
marcozecchini 0:9fca2b23d0ba 57 return err;
marcozecchini 0:9fca2b23d0ba 58 }
marcozecchini 0:9fca2b23d0ba 59
marcozecchini 0:9fca2b23d0ba 60 err = pthread_cond_init(&s->cond, 0);
marcozecchini 0:9fca2b23d0ba 61 if (err) {
marcozecchini 0:9fca2b23d0ba 62 return err;
marcozecchini 0:9fca2b23d0ba 63 }
marcozecchini 0:9fca2b23d0ba 64
marcozecchini 0:9fca2b23d0ba 65 s->signal = false;
marcozecchini 0:9fca2b23d0ba 66 return 0;
marcozecchini 0:9fca2b23d0ba 67 }
marcozecchini 0:9fca2b23d0ba 68
marcozecchini 0:9fca2b23d0ba 69 void equeue_sema_destroy(equeue_sema_t *s) {
marcozecchini 0:9fca2b23d0ba 70 pthread_cond_destroy(&s->cond);
marcozecchini 0:9fca2b23d0ba 71 pthread_mutex_destroy(&s->mutex);
marcozecchini 0:9fca2b23d0ba 72 }
marcozecchini 0:9fca2b23d0ba 73
marcozecchini 0:9fca2b23d0ba 74 void equeue_sema_signal(equeue_sema_t *s) {
marcozecchini 0:9fca2b23d0ba 75 pthread_mutex_lock(&s->mutex);
marcozecchini 0:9fca2b23d0ba 76 s->signal = true;
marcozecchini 0:9fca2b23d0ba 77 pthread_cond_signal(&s->cond);
marcozecchini 0:9fca2b23d0ba 78 pthread_mutex_unlock(&s->mutex);
marcozecchini 0:9fca2b23d0ba 79 }
marcozecchini 0:9fca2b23d0ba 80
marcozecchini 0:9fca2b23d0ba 81 bool equeue_sema_wait(equeue_sema_t *s, int ms) {
marcozecchini 0:9fca2b23d0ba 82 pthread_mutex_lock(&s->mutex);
marcozecchini 0:9fca2b23d0ba 83 if (!s->signal) {
marcozecchini 0:9fca2b23d0ba 84 if (ms < 0) {
marcozecchini 0:9fca2b23d0ba 85 pthread_cond_wait(&s->cond, &s->mutex);
marcozecchini 0:9fca2b23d0ba 86 } else {
marcozecchini 0:9fca2b23d0ba 87 struct timeval tv;
marcozecchini 0:9fca2b23d0ba 88 gettimeofday(&tv, 0);
marcozecchini 0:9fca2b23d0ba 89
marcozecchini 0:9fca2b23d0ba 90 struct timespec ts = {
marcozecchini 0:9fca2b23d0ba 91 .tv_sec = ms/1000 + tv.tv_sec,
marcozecchini 0:9fca2b23d0ba 92 .tv_nsec = ms*1000000 + tv.tv_usec*1000,
marcozecchini 0:9fca2b23d0ba 93 };
marcozecchini 0:9fca2b23d0ba 94
marcozecchini 0:9fca2b23d0ba 95 pthread_cond_timedwait(&s->cond, &s->mutex, &ts);
marcozecchini 0:9fca2b23d0ba 96 }
marcozecchini 0:9fca2b23d0ba 97 }
marcozecchini 0:9fca2b23d0ba 98
marcozecchini 0:9fca2b23d0ba 99 bool signal = s->signal;
marcozecchini 0:9fca2b23d0ba 100 s->signal = false;
marcozecchini 0:9fca2b23d0ba 101 pthread_mutex_unlock(&s->mutex);
marcozecchini 0:9fca2b23d0ba 102
marcozecchini 0:9fca2b23d0ba 103 return signal;
marcozecchini 0:9fca2b23d0ba 104 }
marcozecchini 0:9fca2b23d0ba 105
marcozecchini 0:9fca2b23d0ba 106 #endif