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