Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of OmniWheels by
mbed-os/events/equeue/equeue_posix.c@1:9c5af431a1f1, 2018-05-01 (annotated)
- Committer:
- gustavatmel
- Date:
- Tue May 01 15:47:08 2018 +0000
- Revision:
- 1:9c5af431a1f1
sdf
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gustavatmel | 1:9c5af431a1f1 | 1 | /* |
gustavatmel | 1:9c5af431a1f1 | 2 | * Implementation for Posix compliant platforms |
gustavatmel | 1:9c5af431a1f1 | 3 | * |
gustavatmel | 1:9c5af431a1f1 | 4 | * Copyright (c) 2016 Christopher Haster |
gustavatmel | 1:9c5af431a1f1 | 5 | * |
gustavatmel | 1:9c5af431a1f1 | 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
gustavatmel | 1:9c5af431a1f1 | 7 | * you may not use this file except in compliance with the License. |
gustavatmel | 1:9c5af431a1f1 | 8 | * You may obtain a copy of the License at |
gustavatmel | 1:9c5af431a1f1 | 9 | * |
gustavatmel | 1:9c5af431a1f1 | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
gustavatmel | 1:9c5af431a1f1 | 11 | * |
gustavatmel | 1:9c5af431a1f1 | 12 | * Unless required by applicable law or agreed to in writing, software |
gustavatmel | 1:9c5af431a1f1 | 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
gustavatmel | 1:9c5af431a1f1 | 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
gustavatmel | 1:9c5af431a1f1 | 15 | * See the License for the specific language governing permissions and |
gustavatmel | 1:9c5af431a1f1 | 16 | * limitations under the License. |
gustavatmel | 1:9c5af431a1f1 | 17 | */ |
gustavatmel | 1:9c5af431a1f1 | 18 | #include "equeue/equeue_platform.h" |
gustavatmel | 1:9c5af431a1f1 | 19 | |
gustavatmel | 1:9c5af431a1f1 | 20 | #if defined(EQUEUE_PLATFORM_POSIX) |
gustavatmel | 1:9c5af431a1f1 | 21 | |
gustavatmel | 1:9c5af431a1f1 | 22 | #include <time.h> |
gustavatmel | 1:9c5af431a1f1 | 23 | #include <sys/time.h> |
gustavatmel | 1:9c5af431a1f1 | 24 | #include <errno.h> |
gustavatmel | 1:9c5af431a1f1 | 25 | |
gustavatmel | 1:9c5af431a1f1 | 26 | |
gustavatmel | 1:9c5af431a1f1 | 27 | // Tick operations |
gustavatmel | 1:9c5af431a1f1 | 28 | unsigned equeue_tick(void) { |
gustavatmel | 1:9c5af431a1f1 | 29 | struct timeval tv; |
gustavatmel | 1:9c5af431a1f1 | 30 | gettimeofday(&tv, 0); |
gustavatmel | 1:9c5af431a1f1 | 31 | return (unsigned)(tv.tv_sec*1000 + tv.tv_usec/1000); |
gustavatmel | 1:9c5af431a1f1 | 32 | } |
gustavatmel | 1:9c5af431a1f1 | 33 | |
gustavatmel | 1:9c5af431a1f1 | 34 | |
gustavatmel | 1:9c5af431a1f1 | 35 | // Mutex operations |
gustavatmel | 1:9c5af431a1f1 | 36 | int equeue_mutex_create(equeue_mutex_t *m) { |
gustavatmel | 1:9c5af431a1f1 | 37 | return pthread_mutex_init(m, 0); |
gustavatmel | 1:9c5af431a1f1 | 38 | } |
gustavatmel | 1:9c5af431a1f1 | 39 | |
gustavatmel | 1:9c5af431a1f1 | 40 | void equeue_mutex_destroy(equeue_mutex_t *m) { |
gustavatmel | 1:9c5af431a1f1 | 41 | pthread_mutex_destroy(m); |
gustavatmel | 1:9c5af431a1f1 | 42 | } |
gustavatmel | 1:9c5af431a1f1 | 43 | |
gustavatmel | 1:9c5af431a1f1 | 44 | void equeue_mutex_lock(equeue_mutex_t *m) { |
gustavatmel | 1:9c5af431a1f1 | 45 | pthread_mutex_lock(m); |
gustavatmel | 1:9c5af431a1f1 | 46 | } |
gustavatmel | 1:9c5af431a1f1 | 47 | |
gustavatmel | 1:9c5af431a1f1 | 48 | void equeue_mutex_unlock(equeue_mutex_t *m) { |
gustavatmel | 1:9c5af431a1f1 | 49 | pthread_mutex_unlock(m); |
gustavatmel | 1:9c5af431a1f1 | 50 | } |
gustavatmel | 1:9c5af431a1f1 | 51 | |
gustavatmel | 1:9c5af431a1f1 | 52 | |
gustavatmel | 1:9c5af431a1f1 | 53 | // Semaphore operations |
gustavatmel | 1:9c5af431a1f1 | 54 | int equeue_sema_create(equeue_sema_t *s) { |
gustavatmel | 1:9c5af431a1f1 | 55 | int err = pthread_mutex_init(&s->mutex, 0); |
gustavatmel | 1:9c5af431a1f1 | 56 | if (err) { |
gustavatmel | 1:9c5af431a1f1 | 57 | return err; |
gustavatmel | 1:9c5af431a1f1 | 58 | } |
gustavatmel | 1:9c5af431a1f1 | 59 | |
gustavatmel | 1:9c5af431a1f1 | 60 | err = pthread_cond_init(&s->cond, 0); |
gustavatmel | 1:9c5af431a1f1 | 61 | if (err) { |
gustavatmel | 1:9c5af431a1f1 | 62 | return err; |
gustavatmel | 1:9c5af431a1f1 | 63 | } |
gustavatmel | 1:9c5af431a1f1 | 64 | |
gustavatmel | 1:9c5af431a1f1 | 65 | s->signal = false; |
gustavatmel | 1:9c5af431a1f1 | 66 | return 0; |
gustavatmel | 1:9c5af431a1f1 | 67 | } |
gustavatmel | 1:9c5af431a1f1 | 68 | |
gustavatmel | 1:9c5af431a1f1 | 69 | void equeue_sema_destroy(equeue_sema_t *s) { |
gustavatmel | 1:9c5af431a1f1 | 70 | pthread_cond_destroy(&s->cond); |
gustavatmel | 1:9c5af431a1f1 | 71 | pthread_mutex_destroy(&s->mutex); |
gustavatmel | 1:9c5af431a1f1 | 72 | } |
gustavatmel | 1:9c5af431a1f1 | 73 | |
gustavatmel | 1:9c5af431a1f1 | 74 | void equeue_sema_signal(equeue_sema_t *s) { |
gustavatmel | 1:9c5af431a1f1 | 75 | pthread_mutex_lock(&s->mutex); |
gustavatmel | 1:9c5af431a1f1 | 76 | s->signal = true; |
gustavatmel | 1:9c5af431a1f1 | 77 | pthread_cond_signal(&s->cond); |
gustavatmel | 1:9c5af431a1f1 | 78 | pthread_mutex_unlock(&s->mutex); |
gustavatmel | 1:9c5af431a1f1 | 79 | } |
gustavatmel | 1:9c5af431a1f1 | 80 | |
gustavatmel | 1:9c5af431a1f1 | 81 | bool equeue_sema_wait(equeue_sema_t *s, int ms) { |
gustavatmel | 1:9c5af431a1f1 | 82 | pthread_mutex_lock(&s->mutex); |
gustavatmel | 1:9c5af431a1f1 | 83 | if (!s->signal) { |
gustavatmel | 1:9c5af431a1f1 | 84 | if (ms < 0) { |
gustavatmel | 1:9c5af431a1f1 | 85 | pthread_cond_wait(&s->cond, &s->mutex); |
gustavatmel | 1:9c5af431a1f1 | 86 | } else { |
gustavatmel | 1:9c5af431a1f1 | 87 | struct timeval tv; |
gustavatmel | 1:9c5af431a1f1 | 88 | gettimeofday(&tv, 0); |
gustavatmel | 1:9c5af431a1f1 | 89 | |
gustavatmel | 1:9c5af431a1f1 | 90 | struct timespec ts = { |
gustavatmel | 1:9c5af431a1f1 | 91 | .tv_sec = ms/1000 + tv.tv_sec, |
gustavatmel | 1:9c5af431a1f1 | 92 | .tv_nsec = ms*1000000 + tv.tv_usec*1000, |
gustavatmel | 1:9c5af431a1f1 | 93 | }; |
gustavatmel | 1:9c5af431a1f1 | 94 | |
gustavatmel | 1:9c5af431a1f1 | 95 | pthread_cond_timedwait(&s->cond, &s->mutex, &ts); |
gustavatmel | 1:9c5af431a1f1 | 96 | } |
gustavatmel | 1:9c5af431a1f1 | 97 | } |
gustavatmel | 1:9c5af431a1f1 | 98 | |
gustavatmel | 1:9c5af431a1f1 | 99 | bool signal = s->signal; |
gustavatmel | 1:9c5af431a1f1 | 100 | s->signal = false; |
gustavatmel | 1:9c5af431a1f1 | 101 | pthread_mutex_unlock(&s->mutex); |
gustavatmel | 1:9c5af431a1f1 | 102 | |
gustavatmel | 1:9c5af431a1f1 | 103 | return signal; |
gustavatmel | 1:9c5af431a1f1 | 104 | } |
gustavatmel | 1:9c5af431a1f1 | 105 | |
gustavatmel | 1:9c5af431a1f1 | 106 | #endif |