summaryrefslogtreecommitdiff
path: root/components/libc/cplusplus/os/cxx_Semaphore.cpp
blob: e0dbeb158a8c41e545922341296ff2d2c2899876 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 */

#include "cxx_semaphore.h"

using namespace rtthread;

/** 
 * @brief Semaphore class constructor.
 * @param name Semaphore name
 * @param count Initial semaphore count
 */
Semaphore::Semaphore(const char *name, int32_t count)
{
    rt_sem_init(&mID, name, count, RT_IPC_FLAG_FIFO);
}

/** 
 * @brief Wait on the semaphore.
 * @param millisec Timeout in milliseconds (-1 for infinite wait).
 * @return true if the semaphore was successfully taken, false on timeout.
 */
bool Semaphore::wait(int32_t millisec)
{
    rt_int32_t tick;

    if (millisec < 0)
        tick = -1;
    else
        tick = rt_tick_from_millisecond(millisec);

    return rt_sem_take(&mID, tick) == RT_EOK;
}

/** 
 * @brief Release the semaphore.
 */
void Semaphore::release(void)
{
    rt_sem_release(&mID);
}

/** 
 * @brief Detach the semaphore when the object is destroyed.
 */
Semaphore::~Semaphore()
{
    rt_sem_detach(&mID);
}