RTC auf true
rtos/Mutex.cpp@2:7aab896b1a3b, 2019-03-13 (annotated)
- Committer:
- kevman
- Date:
- Wed Mar 13 11:03:24 2019 +0000
- Revision:
- 2:7aab896b1a3b
- Parent:
- 0:38ceb79fef03
2019-03-13
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kevman | 0:38ceb79fef03 | 1 | /* mbed Microcontroller Library |
kevman | 0:38ceb79fef03 | 2 | * Copyright (c) 2006-2012 ARM Limited |
kevman | 0:38ceb79fef03 | 3 | * |
kevman | 0:38ceb79fef03 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
kevman | 0:38ceb79fef03 | 5 | * of this software and associated documentation files (the "Software"), to deal |
kevman | 0:38ceb79fef03 | 6 | * in the Software without restriction, including without limitation the rights |
kevman | 0:38ceb79fef03 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
kevman | 0:38ceb79fef03 | 8 | * copies of the Software, and to permit persons to whom the Software is |
kevman | 0:38ceb79fef03 | 9 | * furnished to do so, subject to the following conditions: |
kevman | 0:38ceb79fef03 | 10 | * |
kevman | 0:38ceb79fef03 | 11 | * The above copyright notice and this permission notice shall be included in |
kevman | 0:38ceb79fef03 | 12 | * all copies or substantial portions of the Software. |
kevman | 0:38ceb79fef03 | 13 | * |
kevman | 0:38ceb79fef03 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
kevman | 0:38ceb79fef03 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
kevman | 0:38ceb79fef03 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
kevman | 0:38ceb79fef03 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
kevman | 0:38ceb79fef03 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kevman | 0:38ceb79fef03 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
kevman | 0:38ceb79fef03 | 20 | * SOFTWARE. |
kevman | 0:38ceb79fef03 | 21 | */ |
kevman | 0:38ceb79fef03 | 22 | #include "rtos/Mutex.h" |
kevman | 0:38ceb79fef03 | 23 | #include "rtos/Kernel.h" |
kevman | 0:38ceb79fef03 | 24 | |
kevman | 0:38ceb79fef03 | 25 | #include <string.h> |
kevman | 0:38ceb79fef03 | 26 | #include "mbed_error.h" |
kevman | 0:38ceb79fef03 | 27 | #include "mbed_assert.h" |
kevman | 0:38ceb79fef03 | 28 | |
kevman | 0:38ceb79fef03 | 29 | namespace rtos { |
kevman | 0:38ceb79fef03 | 30 | |
kevman | 0:38ceb79fef03 | 31 | Mutex::Mutex(): _count(0) |
kevman | 0:38ceb79fef03 | 32 | { |
kevman | 0:38ceb79fef03 | 33 | constructor(); |
kevman | 0:38ceb79fef03 | 34 | } |
kevman | 0:38ceb79fef03 | 35 | |
kevman | 0:38ceb79fef03 | 36 | Mutex::Mutex(const char *name) |
kevman | 0:38ceb79fef03 | 37 | { |
kevman | 0:38ceb79fef03 | 38 | constructor(name); |
kevman | 0:38ceb79fef03 | 39 | } |
kevman | 0:38ceb79fef03 | 40 | |
kevman | 0:38ceb79fef03 | 41 | void Mutex::constructor(const char *name) |
kevman | 0:38ceb79fef03 | 42 | { |
kevman | 0:38ceb79fef03 | 43 | memset(&_obj_mem, 0, sizeof(_obj_mem)); |
kevman | 0:38ceb79fef03 | 44 | osMutexAttr_t attr = |
kevman | 0:38ceb79fef03 | 45 | { 0 }; |
kevman | 0:38ceb79fef03 | 46 | attr.name = name ? name : "aplication_unnamed_mutex"; |
kevman | 0:38ceb79fef03 | 47 | attr.cb_mem = &_obj_mem; |
kevman | 0:38ceb79fef03 | 48 | attr.cb_size = sizeof(_obj_mem); |
kevman | 0:38ceb79fef03 | 49 | attr.attr_bits = osMutexRecursive | osMutexPrioInherit | osMutexRobust; |
kevman | 0:38ceb79fef03 | 50 | _id = osMutexNew(&attr); |
kevman | 0:38ceb79fef03 | 51 | MBED_ASSERT(_id); |
kevman | 0:38ceb79fef03 | 52 | } |
kevman | 0:38ceb79fef03 | 53 | |
kevman | 0:38ceb79fef03 | 54 | osStatus Mutex::lock(void) |
kevman | 0:38ceb79fef03 | 55 | { |
kevman | 0:38ceb79fef03 | 56 | osStatus status = osMutexAcquire(_id, osWaitForever); |
kevman | 0:38ceb79fef03 | 57 | if (osOK == status) { |
kevman | 0:38ceb79fef03 | 58 | _count++; |
kevman | 0:38ceb79fef03 | 59 | } |
kevman | 0:38ceb79fef03 | 60 | |
kevman | 0:38ceb79fef03 | 61 | if (status != osOK) { |
kevman | 0:38ceb79fef03 | 62 | MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_MUTEX_LOCK_FAILED), "Mutex lock failed", status); |
kevman | 0:38ceb79fef03 | 63 | } |
kevman | 0:38ceb79fef03 | 64 | |
kevman | 0:38ceb79fef03 | 65 | return osOK; |
kevman | 0:38ceb79fef03 | 66 | } |
kevman | 0:38ceb79fef03 | 67 | |
kevman | 0:38ceb79fef03 | 68 | osStatus Mutex::lock(uint32_t millisec) |
kevman | 0:38ceb79fef03 | 69 | { |
kevman | 0:38ceb79fef03 | 70 | osStatus status = osMutexAcquire(_id, millisec); |
kevman | 0:38ceb79fef03 | 71 | if (osOK == status) { |
kevman | 0:38ceb79fef03 | 72 | _count++; |
kevman | 0:38ceb79fef03 | 73 | } |
kevman | 0:38ceb79fef03 | 74 | |
kevman | 0:38ceb79fef03 | 75 | bool success = (status == osOK || |
kevman | 0:38ceb79fef03 | 76 | (status == osErrorResource && millisec == 0) || |
kevman | 0:38ceb79fef03 | 77 | (status == osErrorTimeout && millisec != osWaitForever)); |
kevman | 0:38ceb79fef03 | 78 | |
kevman | 0:38ceb79fef03 | 79 | if (!success) { |
kevman | 0:38ceb79fef03 | 80 | MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_MUTEX_LOCK_FAILED), "Mutex lock failed", status); |
kevman | 0:38ceb79fef03 | 81 | } |
kevman | 0:38ceb79fef03 | 82 | |
kevman | 0:38ceb79fef03 | 83 | return status; |
kevman | 0:38ceb79fef03 | 84 | } |
kevman | 0:38ceb79fef03 | 85 | |
kevman | 0:38ceb79fef03 | 86 | bool Mutex::trylock() |
kevman | 0:38ceb79fef03 | 87 | { |
kevman | 0:38ceb79fef03 | 88 | return trylock_for(0); |
kevman | 0:38ceb79fef03 | 89 | } |
kevman | 0:38ceb79fef03 | 90 | |
kevman | 0:38ceb79fef03 | 91 | bool Mutex::trylock_for(uint32_t millisec) |
kevman | 0:38ceb79fef03 | 92 | { |
kevman | 0:38ceb79fef03 | 93 | osStatus status = osMutexAcquire(_id, millisec); |
kevman | 0:38ceb79fef03 | 94 | if (status == osOK) { |
kevman | 0:38ceb79fef03 | 95 | return true; |
kevman | 0:38ceb79fef03 | 96 | } |
kevman | 0:38ceb79fef03 | 97 | |
kevman | 0:38ceb79fef03 | 98 | bool success = (status == osOK || |
kevman | 0:38ceb79fef03 | 99 | (status == osErrorResource && millisec == 0) || |
kevman | 0:38ceb79fef03 | 100 | (status == osErrorTimeout && millisec != osWaitForever)); |
kevman | 0:38ceb79fef03 | 101 | |
kevman | 0:38ceb79fef03 | 102 | if (!success) { |
kevman | 0:38ceb79fef03 | 103 | MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_MUTEX_LOCK_FAILED), "Mutex lock failed", status); |
kevman | 0:38ceb79fef03 | 104 | } |
kevman | 0:38ceb79fef03 | 105 | |
kevman | 0:38ceb79fef03 | 106 | return false; |
kevman | 0:38ceb79fef03 | 107 | } |
kevman | 0:38ceb79fef03 | 108 | |
kevman | 0:38ceb79fef03 | 109 | bool Mutex::trylock_until(uint64_t millisec) |
kevman | 0:38ceb79fef03 | 110 | { |
kevman | 0:38ceb79fef03 | 111 | uint64_t now = Kernel::get_ms_count(); |
kevman | 0:38ceb79fef03 | 112 | |
kevman | 0:38ceb79fef03 | 113 | if (now >= millisec) { |
kevman | 0:38ceb79fef03 | 114 | return trylock(); |
kevman | 0:38ceb79fef03 | 115 | } else if (millisec - now >= osWaitForever) { |
kevman | 0:38ceb79fef03 | 116 | // API permits early return |
kevman | 0:38ceb79fef03 | 117 | return trylock_for(osWaitForever - 1); |
kevman | 0:38ceb79fef03 | 118 | } else { |
kevman | 0:38ceb79fef03 | 119 | return trylock_for(millisec - now); |
kevman | 0:38ceb79fef03 | 120 | } |
kevman | 0:38ceb79fef03 | 121 | } |
kevman | 0:38ceb79fef03 | 122 | |
kevman | 0:38ceb79fef03 | 123 | osStatus Mutex::unlock() |
kevman | 0:38ceb79fef03 | 124 | { |
kevman | 0:38ceb79fef03 | 125 | _count--; |
kevman | 0:38ceb79fef03 | 126 | |
kevman | 0:38ceb79fef03 | 127 | osStatus status = osMutexRelease(_id); |
kevman | 0:38ceb79fef03 | 128 | |
kevman | 0:38ceb79fef03 | 129 | if (status != osOK) { |
kevman | 0:38ceb79fef03 | 130 | MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_MUTEX_UNLOCK_FAILED), "Mutex unlock failed", status); |
kevman | 0:38ceb79fef03 | 131 | } |
kevman | 0:38ceb79fef03 | 132 | |
kevman | 0:38ceb79fef03 | 133 | return osOK; |
kevman | 0:38ceb79fef03 | 134 | } |
kevman | 0:38ceb79fef03 | 135 | |
kevman | 0:38ceb79fef03 | 136 | osThreadId Mutex::get_owner() |
kevman | 0:38ceb79fef03 | 137 | { |
kevman | 0:38ceb79fef03 | 138 | return osMutexGetOwner(_id); |
kevman | 0:38ceb79fef03 | 139 | } |
kevman | 0:38ceb79fef03 | 140 | |
kevman | 0:38ceb79fef03 | 141 | Mutex::~Mutex() |
kevman | 0:38ceb79fef03 | 142 | { |
kevman | 0:38ceb79fef03 | 143 | osMutexDelete(_id); |
kevman | 0:38ceb79fef03 | 144 | } |
kevman | 0:38ceb79fef03 | 145 | |
kevman | 0:38ceb79fef03 | 146 | } |