xref: /nrf52832-nimble/rt-thread/components/cplusplus/Semaphore.cpp (revision 167494296f0543431a51b6b1b83e957045294e05)
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 "Semaphore.h"
10 
11 using namespace rtthread;
12 
13 Semaphore::Semaphore(const char *name, int32_t count)
14 {
15     rt_sem_init(&mID, name, count, RT_IPC_FLAG_FIFO);
16 }
17 
18 bool Semaphore::wait(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_sem_take(&mID, tick) == RT_EOK;
28 }
29 
30 void Semaphore::release(void)
31 {
32     rt_sem_release(&mID);
33 }
34 
35 Semaphore::~Semaphore()
36 {
37     rt_sem_detach(&mID);
38 }
39