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