Nicolas Borla
/
BBR_1Ebene
BBR 1 Ebene
mbed-os/rtos/Semaphore.cpp@0:fbdae7e6d805, 2018-05-14 (annotated)
- Committer:
- borlanic
- Date:
- Mon May 14 11:29:06 2018 +0000
- Revision:
- 0:fbdae7e6d805
BBR
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
borlanic | 0:fbdae7e6d805 | 1 | /* mbed Microcontroller Library |
borlanic | 0:fbdae7e6d805 | 2 | * Copyright (c) 2006-2012 ARM Limited |
borlanic | 0:fbdae7e6d805 | 3 | * |
borlanic | 0:fbdae7e6d805 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
borlanic | 0:fbdae7e6d805 | 5 | * of this software and associated documentation files (the "Software"), to deal |
borlanic | 0:fbdae7e6d805 | 6 | * in the Software without restriction, including without limitation the rights |
borlanic | 0:fbdae7e6d805 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
borlanic | 0:fbdae7e6d805 | 8 | * copies of the Software, and to permit persons to whom the Software is |
borlanic | 0:fbdae7e6d805 | 9 | * furnished to do so, subject to the following conditions: |
borlanic | 0:fbdae7e6d805 | 10 | * |
borlanic | 0:fbdae7e6d805 | 11 | * The above copyright notice and this permission notice shall be included in |
borlanic | 0:fbdae7e6d805 | 12 | * all copies or substantial portions of the Software. |
borlanic | 0:fbdae7e6d805 | 13 | * |
borlanic | 0:fbdae7e6d805 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
borlanic | 0:fbdae7e6d805 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
borlanic | 0:fbdae7e6d805 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
borlanic | 0:fbdae7e6d805 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
borlanic | 0:fbdae7e6d805 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
borlanic | 0:fbdae7e6d805 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
borlanic | 0:fbdae7e6d805 | 20 | * SOFTWARE. |
borlanic | 0:fbdae7e6d805 | 21 | */ |
borlanic | 0:fbdae7e6d805 | 22 | #include "rtos/Semaphore.h" |
borlanic | 0:fbdae7e6d805 | 23 | #include "rtos/Kernel.h" |
borlanic | 0:fbdae7e6d805 | 24 | #include "platform/mbed_assert.h" |
borlanic | 0:fbdae7e6d805 | 25 | |
borlanic | 0:fbdae7e6d805 | 26 | #include <string.h> |
borlanic | 0:fbdae7e6d805 | 27 | |
borlanic | 0:fbdae7e6d805 | 28 | namespace rtos { |
borlanic | 0:fbdae7e6d805 | 29 | |
borlanic | 0:fbdae7e6d805 | 30 | Semaphore::Semaphore(int32_t count) { |
borlanic | 0:fbdae7e6d805 | 31 | constructor(count, 0xffff); |
borlanic | 0:fbdae7e6d805 | 32 | } |
borlanic | 0:fbdae7e6d805 | 33 | |
borlanic | 0:fbdae7e6d805 | 34 | Semaphore::Semaphore(int32_t count, uint16_t max_count) { |
borlanic | 0:fbdae7e6d805 | 35 | constructor(count, max_count); |
borlanic | 0:fbdae7e6d805 | 36 | } |
borlanic | 0:fbdae7e6d805 | 37 | |
borlanic | 0:fbdae7e6d805 | 38 | void Semaphore::constructor(int32_t count, uint16_t max_count) { |
borlanic | 0:fbdae7e6d805 | 39 | memset(&_obj_mem, 0, sizeof(_obj_mem)); |
borlanic | 0:fbdae7e6d805 | 40 | osSemaphoreAttr_t attr = { 0 }; |
borlanic | 0:fbdae7e6d805 | 41 | attr.cb_mem = &_obj_mem; |
borlanic | 0:fbdae7e6d805 | 42 | attr.cb_size = sizeof(_obj_mem); |
borlanic | 0:fbdae7e6d805 | 43 | _id = osSemaphoreNew(max_count, count, &attr); |
borlanic | 0:fbdae7e6d805 | 44 | MBED_ASSERT(_id != NULL); |
borlanic | 0:fbdae7e6d805 | 45 | } |
borlanic | 0:fbdae7e6d805 | 46 | |
borlanic | 0:fbdae7e6d805 | 47 | int32_t Semaphore::wait(uint32_t millisec) { |
borlanic | 0:fbdae7e6d805 | 48 | osStatus_t stat = osSemaphoreAcquire(_id, millisec); |
borlanic | 0:fbdae7e6d805 | 49 | switch (stat) { |
borlanic | 0:fbdae7e6d805 | 50 | case osOK: |
borlanic | 0:fbdae7e6d805 | 51 | return osSemaphoreGetCount(_id) + 1; |
borlanic | 0:fbdae7e6d805 | 52 | case osErrorTimeout: |
borlanic | 0:fbdae7e6d805 | 53 | case osErrorResource: |
borlanic | 0:fbdae7e6d805 | 54 | return 0; |
borlanic | 0:fbdae7e6d805 | 55 | case osErrorParameter: |
borlanic | 0:fbdae7e6d805 | 56 | default: |
borlanic | 0:fbdae7e6d805 | 57 | return -1; |
borlanic | 0:fbdae7e6d805 | 58 | } |
borlanic | 0:fbdae7e6d805 | 59 | } |
borlanic | 0:fbdae7e6d805 | 60 | |
borlanic | 0:fbdae7e6d805 | 61 | int32_t Semaphore::wait_until(uint64_t millisec) { |
borlanic | 0:fbdae7e6d805 | 62 | uint64_t now = Kernel::get_ms_count(); |
borlanic | 0:fbdae7e6d805 | 63 | |
borlanic | 0:fbdae7e6d805 | 64 | if (now >= millisec) { |
borlanic | 0:fbdae7e6d805 | 65 | return wait(0); |
borlanic | 0:fbdae7e6d805 | 66 | } else if (millisec - now >= osWaitForever) { |
borlanic | 0:fbdae7e6d805 | 67 | // API permits early return |
borlanic | 0:fbdae7e6d805 | 68 | return wait(osWaitForever - 1); |
borlanic | 0:fbdae7e6d805 | 69 | } else { |
borlanic | 0:fbdae7e6d805 | 70 | return wait(millisec - now); |
borlanic | 0:fbdae7e6d805 | 71 | } |
borlanic | 0:fbdae7e6d805 | 72 | } |
borlanic | 0:fbdae7e6d805 | 73 | |
borlanic | 0:fbdae7e6d805 | 74 | osStatus Semaphore::release(void) { |
borlanic | 0:fbdae7e6d805 | 75 | return osSemaphoreRelease(_id); |
borlanic | 0:fbdae7e6d805 | 76 | } |
borlanic | 0:fbdae7e6d805 | 77 | |
borlanic | 0:fbdae7e6d805 | 78 | Semaphore::~Semaphore() { |
borlanic | 0:fbdae7e6d805 | 79 | osSemaphoreDelete(_id); |
borlanic | 0:fbdae7e6d805 | 80 | } |
borlanic | 0:fbdae7e6d805 | 81 | |
borlanic | 0:fbdae7e6d805 | 82 | } |