Committer:
borlanic
Date:
Thu Mar 29 07:02:09 2018 +0000
Revision:
0:380207fcb5c1
Encoder, IMU --> OK; Controller --> in bearbeitung

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:380207fcb5c1 1 /* mbed Microcontroller Library
borlanic 0:380207fcb5c1 2 * Copyright (c) 2006-2012 ARM Limited
borlanic 0:380207fcb5c1 3 *
borlanic 0:380207fcb5c1 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
borlanic 0:380207fcb5c1 5 * of this software and associated documentation files (the "Software"), to deal
borlanic 0:380207fcb5c1 6 * in the Software without restriction, including without limitation the rights
borlanic 0:380207fcb5c1 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
borlanic 0:380207fcb5c1 8 * copies of the Software, and to permit persons to whom the Software is
borlanic 0:380207fcb5c1 9 * furnished to do so, subject to the following conditions:
borlanic 0:380207fcb5c1 10 *
borlanic 0:380207fcb5c1 11 * The above copyright notice and this permission notice shall be included in
borlanic 0:380207fcb5c1 12 * all copies or substantial portions of the Software.
borlanic 0:380207fcb5c1 13 *
borlanic 0:380207fcb5c1 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
borlanic 0:380207fcb5c1 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
borlanic 0:380207fcb5c1 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
borlanic 0:380207fcb5c1 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
borlanic 0:380207fcb5c1 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
borlanic 0:380207fcb5c1 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
borlanic 0:380207fcb5c1 20 * SOFTWARE.
borlanic 0:380207fcb5c1 21 */
borlanic 0:380207fcb5c1 22 #include "rtos/Mutex.h"
borlanic 0:380207fcb5c1 23 #include "rtos/Kernel.h"
borlanic 0:380207fcb5c1 24
borlanic 0:380207fcb5c1 25 #include <string.h>
borlanic 0:380207fcb5c1 26 #include "mbed_error.h"
borlanic 0:380207fcb5c1 27 #include "mbed_assert.h"
borlanic 0:380207fcb5c1 28
borlanic 0:380207fcb5c1 29 namespace rtos {
borlanic 0:380207fcb5c1 30
borlanic 0:380207fcb5c1 31 Mutex::Mutex(): _count(0)
borlanic 0:380207fcb5c1 32 {
borlanic 0:380207fcb5c1 33 constructor();
borlanic 0:380207fcb5c1 34 }
borlanic 0:380207fcb5c1 35
borlanic 0:380207fcb5c1 36 Mutex::Mutex(const char *name)
borlanic 0:380207fcb5c1 37 {
borlanic 0:380207fcb5c1 38 constructor(name);
borlanic 0:380207fcb5c1 39 }
borlanic 0:380207fcb5c1 40
borlanic 0:380207fcb5c1 41 void Mutex::constructor(const char *name)
borlanic 0:380207fcb5c1 42 {
borlanic 0:380207fcb5c1 43 memset(&_obj_mem, 0, sizeof(_obj_mem));
borlanic 0:380207fcb5c1 44 osMutexAttr_t attr = { 0 };
borlanic 0:380207fcb5c1 45 attr.name = name ? name : "aplication_unnamed_mutex";
borlanic 0:380207fcb5c1 46 attr.cb_mem = &_obj_mem;
borlanic 0:380207fcb5c1 47 attr.cb_size = sizeof(_obj_mem);
borlanic 0:380207fcb5c1 48 attr.attr_bits = osMutexRecursive | osMutexPrioInherit | osMutexRobust;
borlanic 0:380207fcb5c1 49 _id = osMutexNew(&attr);
borlanic 0:380207fcb5c1 50 MBED_ASSERT(_id);
borlanic 0:380207fcb5c1 51 }
borlanic 0:380207fcb5c1 52
borlanic 0:380207fcb5c1 53 osStatus Mutex::lock(uint32_t millisec) {
borlanic 0:380207fcb5c1 54 osStatus status = osMutexAcquire(_id, millisec);
borlanic 0:380207fcb5c1 55 if (osOK == status) {
borlanic 0:380207fcb5c1 56 _count++;
borlanic 0:380207fcb5c1 57 }
borlanic 0:380207fcb5c1 58 return status;
borlanic 0:380207fcb5c1 59 }
borlanic 0:380207fcb5c1 60
borlanic 0:380207fcb5c1 61 bool Mutex::trylock() {
borlanic 0:380207fcb5c1 62 return trylock_for(0);
borlanic 0:380207fcb5c1 63 }
borlanic 0:380207fcb5c1 64
borlanic 0:380207fcb5c1 65 bool Mutex::trylock_for(uint32_t millisec) {
borlanic 0:380207fcb5c1 66 osStatus status = lock(millisec);
borlanic 0:380207fcb5c1 67 if (status == osOK) {
borlanic 0:380207fcb5c1 68 return true;
borlanic 0:380207fcb5c1 69 }
borlanic 0:380207fcb5c1 70
borlanic 0:380207fcb5c1 71 MBED_ASSERT(status == osErrorTimeout || status == osErrorResource);
borlanic 0:380207fcb5c1 72
borlanic 0:380207fcb5c1 73 return false;
borlanic 0:380207fcb5c1 74 }
borlanic 0:380207fcb5c1 75
borlanic 0:380207fcb5c1 76 bool Mutex::trylock_until(uint64_t millisec) {
borlanic 0:380207fcb5c1 77 uint64_t now = Kernel::get_ms_count();
borlanic 0:380207fcb5c1 78
borlanic 0:380207fcb5c1 79 if (now >= millisec) {
borlanic 0:380207fcb5c1 80 return trylock();
borlanic 0:380207fcb5c1 81 } else if (millisec - now >= osWaitForever) {
borlanic 0:380207fcb5c1 82 // API permits early return
borlanic 0:380207fcb5c1 83 return trylock_for(osWaitForever - 1);
borlanic 0:380207fcb5c1 84 } else {
borlanic 0:380207fcb5c1 85 return trylock_for(millisec - now);
borlanic 0:380207fcb5c1 86 }
borlanic 0:380207fcb5c1 87 }
borlanic 0:380207fcb5c1 88
borlanic 0:380207fcb5c1 89 osStatus Mutex::unlock() {
borlanic 0:380207fcb5c1 90 _count--;
borlanic 0:380207fcb5c1 91 return osMutexRelease(_id);
borlanic 0:380207fcb5c1 92 }
borlanic 0:380207fcb5c1 93
borlanic 0:380207fcb5c1 94 osThreadId Mutex::get_owner() {
borlanic 0:380207fcb5c1 95 return osMutexGetOwner(_id);
borlanic 0:380207fcb5c1 96 }
borlanic 0:380207fcb5c1 97
borlanic 0:380207fcb5c1 98 Mutex::~Mutex() {
borlanic 0:380207fcb5c1 99 osMutexDelete(_id);
borlanic 0:380207fcb5c1 100 }
borlanic 0:380207fcb5c1 101
borlanic 0:380207fcb5c1 102 }