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