1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #include "cpp_semaphore.h"
6 
7 using namespace AOS;
8 
9 /**
10  * This function will create a semaphore
11  * @param[in]  name   name of the semaphore
12  * @param[in]  count  the init count of the semaphore
13  * @return  the operation status, RHINO_SUCCESS is OK, others is error
14  */
create(const name_t * name,sem_count_t count)15 kstat_t Semaphore::create(const name_t *name, sem_count_t count)
16 {
17     return krhino_sem_create(&_sem_def, name, count);
18 }
19 
20 /**
21  * This function will delete a semaphore
22  * @param[in]  NULL
23  * @return  the operation status, RHINO_SUCCESS is OK, others is error
24  */
destroy(void)25 kstat_t Semaphore::destroy(void)
26 {
27     return krhino_sem_del(&_sem_def);
28 }
29 
30 /**
31  * This function will wait a semaphore
32  * @param[in]  millisec  millisec to wait before take
33  * @return  the operation status, RHINO_SUCCESS is OK, others is error
34  */
wait(uint32_t millisec)35 kstat_t Semaphore::wait(uint32_t millisec)
36 {
37     tick_t ticks = 0;
38 
39     if (millisec == 0) {
40         ticks = RHINO_NO_WAIT;
41     } else if (millisec == Semaphore_WAIT_FOREVER){
42         ticks = RHINO_WAIT_FOREVER;
43     } else {
44         ticks = krhino_ms_to_ticks(millisec);
45     }
46 
47     return krhino_sem_take(&_sem_def, ticks);
48 }
49 
50 /**
51  * This function will release a semaphore
52  * @param[in]  NULL
53  * @return  the operation status, RHINO_SUCCESS is OK, others is error
54  */
release(void)55 kstat_t Semaphore::release(void)
56 {
57     return krhino_sem_give(&_sem_def);
58 }
59 
60 /**
61  * This function will release all semaphore
62  * @param[in]  NULL
63  * @return  the operation status, RHINO_SUCCESS is OK, others is error
64  */
release_all(void)65 kstat_t Semaphore::release_all(void)
66 {
67     return krhino_sem_give_all(&_sem_def);
68 }
69 
70 /**
71  * This function will set the count of a semaphore
72  * @param[in]  sem_count  count of the semaphore
73  * @return  the operation status, RHINO_SUCCESS is OK, others is error
74  */
count_set(sem_count_t count)75 kstat_t Semaphore::count_set(sem_count_t  count)
76 {
77     return krhino_sem_count_set(&_sem_def, count);
78 }
79 
80 /**
81  * This function will get count of a semaphore
82  * @param[out]  count  count of the semaphore
83  * @return  the operation status, RHINO_SUCCESS is OK, others is error
84  */
count_get(sem_count_t * count)85 kstat_t Semaphore::count_get(sem_count_t *count)
86 {
87     return krhino_sem_count_get(&_sem_def, count);
88 }
89 
90 /**
91  * This function will get a Semaphore struct pointer
92  * @param[in]  none
93  * @return  Semaphore struct pointer
94  */
self(void)95 ksem_t *Semaphore::self(void)
96 {
97     return &_sem_def;
98 }
99