/* * This file is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This file is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see . * * Code by Andrew Tridgell and Siddharth Bharat Purohit */ #include #if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS #include "Semaphores.h" extern const AP_HAL::HAL& hal; using namespace ChibiOS; bool Semaphore::give() { chMtxUnlock(&_lock); return true; } bool Semaphore::take(uint32_t timeout_ms) { if (timeout_ms == HAL_SEMAPHORE_BLOCK_FOREVER) { chMtxLock(&_lock); return true; } if (take_nonblocking()) { return true; } uint64_t start = AP_HAL::micros64(); do { hal.scheduler->delay_microseconds(200); if (take_nonblocking()) { return true; } } while ((AP_HAL::micros64() - start) < timeout_ms*1000); return false; } bool Semaphore::take_nonblocking() { return chMtxTryLock(&_lock); } #endif // CONFIG_HAL_BOARD