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.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
equeue_posix.c
00001 /* 00002 * Implementation for Posix compliant platforms 00003 * 00004 * Copyright (c) 2016-2019 ARM Limited 00005 * 00006 * Licensed under the Apache License, Version 2.0 (the "License"); 00007 * you may not use this file except in compliance with the License. 00008 * You may obtain a copy of the License at 00009 * 00010 * http://www.apache.org/licenses/LICENSE-2.0 00011 * 00012 * Unless required by applicable law or agreed to in writing, software 00013 * distributed under the License is distributed on an "AS IS" BASIS, 00014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 * See the License for the specific language governing permissions and 00016 * limitations under the License. 00017 */ 00018 #include "events/internal/equeue_platform.h" 00019 00020 #if defined(EQUEUE_PLATFORM_POSIX) 00021 00022 #include <time.h> 00023 #include <sys/time.h> 00024 #include <errno.h> 00025 00026 00027 // Tick operations 00028 void equeue_tick_init(void) 00029 { 00030 } 00031 00032 unsigned equeue_tick(void) 00033 { 00034 struct timeval tv; 00035 gettimeofday(&tv, 0); 00036 return (unsigned)(tv.tv_sec * 1000 + tv.tv_usec / 1000); 00037 } 00038 00039 00040 // Mutex operations 00041 int equeue_mutex_create(equeue_mutex_t *m) 00042 { 00043 pthread_mutexattr_t attr; 00044 pthread_mutexattr_init(&attr); 00045 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); 00046 return pthread_mutex_init(m, &attr); 00047 } 00048 00049 void equeue_mutex_destroy(equeue_mutex_t *m) 00050 { 00051 pthread_mutex_destroy(m); 00052 } 00053 00054 void equeue_mutex_lock(equeue_mutex_t *m) 00055 { 00056 pthread_mutex_lock(m); 00057 } 00058 00059 void equeue_mutex_unlock(equeue_mutex_t *m) 00060 { 00061 pthread_mutex_unlock(m); 00062 } 00063 00064 00065 // Semaphore operations 00066 int equeue_sema_create(equeue_sema_t *s) 00067 { 00068 int err = pthread_mutex_init(&s->mutex, 0); 00069 if (err) { 00070 return err; 00071 } 00072 00073 err = pthread_cond_init(&s->cond, 0); 00074 if (err) { 00075 return err; 00076 } 00077 00078 s->signal = false; 00079 return 0; 00080 } 00081 00082 void equeue_sema_destroy(equeue_sema_t *s) 00083 { 00084 pthread_cond_destroy(&s->cond); 00085 pthread_mutex_destroy(&s->mutex); 00086 } 00087 00088 void equeue_sema_signal(equeue_sema_t *s) 00089 { 00090 pthread_mutex_lock(&s->mutex); 00091 s->signal = true; 00092 pthread_cond_signal(&s->cond); 00093 pthread_mutex_unlock(&s->mutex); 00094 } 00095 00096 bool equeue_sema_wait(equeue_sema_t *s, int ms) 00097 { 00098 pthread_mutex_lock(&s->mutex); 00099 if (!s->signal) { 00100 if (ms < 0) { 00101 pthread_cond_wait(&s->cond, &s->mutex); 00102 } else { 00103 struct timeval tv; 00104 gettimeofday(&tv, 0); 00105 00106 struct timespec ts = { 00107 .tv_sec = ms / 1000 + tv.tv_sec, 00108 .tv_nsec = ms * 1000000 + tv.tv_usec * 1000, 00109 }; 00110 00111 pthread_cond_timedwait(&s->cond, &s->mutex, &ts); 00112 } 00113 } 00114 00115 bool signal = s->signal; 00116 s->signal = false; 00117 pthread_mutex_unlock(&s->mutex); 00118 00119 return signal; 00120 } 00121 00122 #endif
Generated on Tue Jul 12 2022 13:54:18 by
