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