mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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