1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #ifndef K_SEM_H
6 #define K_SEM_H
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 /** @addtogroup aos_rhino sem
13  *  Semaphore can be used for mutual exclusion between tasks, or tasks synchronize.
14  *
15  *  @{
16  */
17 
18 #define WAKE_ONE_SEM 0u
19 #define WAKE_ALL_SEM 1u
20 
21 /**
22  * Semaphore object
23  */
24 typedef struct sem_s {
25     /**<
26      *  Manage blocked tasks
27      *  List head is this semaphore, list node is task, which are blocked in this semaphore
28      */
29     blk_obj_t   blk_obj;
30     sem_count_t count;
31     sem_count_t peak_count;
32 #if (RHINO_CONFIG_KOBJ_LIST > 0)
33     klist_t     sem_item;   /**< kobj list for statistics */
34 #endif
35     uint8_t     mm_alloc_flag;  /**< buffer from internal malloc or caller input */
36 } ksem_t;
37 
38 /**
39  * Create a semaphore.
40  *
41  * @param[in]  sem      pointer to the semaphore (the space is provided outside, by user)
42  * @param[in]  name     name of the semaphore
43  * @param[in]  count    the init count of the semaphore
44  *
45  * @return  the operation status, RHINO_SUCCESS is OK, others is error
46  */
47 kstat_t krhino_sem_create(ksem_t *sem, const name_t *name, sem_count_t count);
48 
49 /**
50  * Delete a semaphore.
51  *
52  * @param[in]  sem      pointer to the semaphore
53  *
54  * @return  the operation status, RHINO_SUCCESS is OK, others is error
55  */
56 kstat_t krhino_sem_del(ksem_t *sem);
57 
58 #if (RHINO_CONFIG_KOBJ_DYN_ALLOC > 0)
59 /**
60  * Create and malloc a semaphore.
61  *
62  * @param[out]  sem     pointer to the semaphore (the space is provided inside, from heap)
63  * @param[in]   name    name of the semaphore
64  * @param[in]   count   the init count of the semaphore
65  *
66  * @return  the operation status, RHINO_SUCCESS is OK, others is error
67  */
68 kstat_t krhino_sem_dyn_create(ksem_t **sem, const name_t *name, sem_count_t count);
69 
70 /**
71  * Delete and free a semaphore.
72  *
73  * @param[in]  sem      pointer to the semaphore
74  *
75  * @return  the operation status, RHINO_SUCCESS is OK, others is error
76  */
77 kstat_t krhino_sem_dyn_del(ksem_t *sem);
78 #endif
79 
80 /**
81  * Give a semaphore.
82  *
83  * @param[in]  sem      pointer to the semphore
84  *
85  * @return  the operation status, RHINO_SUCCESS is OK, others is error
86  */
87 kstat_t krhino_sem_give(ksem_t *sem);
88 
89 /**
90  * Give a semaphore and wakeup all pending task.
91  *
92  * @param[in]  sem  pointer to the semaphore
93  *
94  * @return  the operation status, RHINO_SUCCESS is OK, others is error
95  */
96 kstat_t krhino_sem_give_all(ksem_t *sem);
97 
98 /**
99  * Take a semaphore, task may be blocked.
100  *
101  * @param[in]  sem    pointer to the semaphore
102  * @param[in]  ticks  ticks to wait before take
103  *
104  * @return  the operation status, RHINO_SUCCESS is OK, others is error
105  */
106 kstat_t krhino_sem_take(ksem_t *sem, tick_t ticks);
107 
108 /**
109  * Set the count of a semaphore.
110  *
111  * @param[in]  sem        pointer to the semaphore
112  * @param[in]  sem_count  count of the semaphore
113  *
114  * @return  the operation status, RHINO_SUCCESS is OK, others is error
115  */
116 kstat_t krhino_sem_count_set(ksem_t *sem, sem_count_t  count);
117 
118 /**
119  * Get count of a semaphore.
120  *
121  * @param[in]   sem    pointer to the semaphore
122  * @param[out]  count  count of the semaphore
123  *
124  * @return  the operation status, RHINO_SUCCESS is OK, others is error
125  */
126 kstat_t krhino_sem_count_get(ksem_t *sem, sem_count_t *count);
127 
128 /** @} */
129 
130 #ifdef __cplusplus
131 }
132 #endif
133 
134 #endif /* K_SEM_H */
135 
136