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