1 /* 2 * Copyright (c) 2006-2018, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 */ 9 #include "Mutex.h" 10 11 using namespace rtthread; 12 13 Mutex::Mutex(const char *name) 14 { 15 rt_mutex_init(&mID, name, RT_IPC_FLAG_FIFO); 16 } 17 18 bool Mutex::lock(int32_t millisec) 19 { 20 rt_int32_t tick; 21 22 if (millisec < 0) 23 tick = -1; 24 else 25 tick = rt_tick_from_millisecond(millisec); 26 27 return rt_mutex_take(&mID, tick) == RT_EOK; 28 } 29 30 bool Mutex::trylock() 31 { 32 return lock(0); 33 } 34 35 void Mutex::unlock() 36 { 37 rt_mutex_release(&mID); 38 } 39 40 Mutex::~Mutex() 41 { 42 rt_mutex_detach(&mID); 43 } 44