c3ebc028f4a716f6cf706d7149c6f55197e90b9b
1 /******************************************************************************
2 @file ti_semaphore.c
4 @brief TIMAC 2.0 API Implimentation of the semaphore abstraction
6 Group: WCS LPC
7 $Target Devices: Linux: AM335x, Embedded Devices: CC1310, CC1350$
9 ******************************************************************************
10 $License: BSD3 2016 $
12 Copyright (c) 2015, Texas Instruments Incorporated
13 All rights reserved.
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions
17 are met:
19 * Redistributions of source code must retain the above copyright
20 notice, this list of conditions and the following disclaimer.
22 * Redistributions in binary form must reproduce the above copyright
23 notice, this list of conditions and the following disclaimer in the
24 documentation and/or other materials provided with the distribution.
26 * Neither the name of Texas Instruments Incorporated nor the names of
27 its contributors may be used to endorse or promote products derived
28 from this software without specific prior written permission.
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
31 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
32 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
36 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
37 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
38 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
39 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
40 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 ******************************************************************************
42 $Release Name: TI-15.4Stack Linux x64 SDK$
43 $Release Date: July 14, 2016 (2.00.00.30)$
44 *****************************************************************************/
46 #include "compiler.h"
47 #include "ti_semaphore.h"
48 #include "log.h"
49 #include "hlos_specific.h"
51 #include <malloc.h>
52 #include <string.h>
53 #include <errno.h>
54 #include <stdlib.h>
56 /*
57 * @var sem_check
58 * @brief [private] Used to validate a semaphore pointer.
59 */
60 static const int sem_check = 'S';
61 /*
62 * @var _sem_no_name
63 * @brief default name for a semaphore with no name
64 */
65 static const char _sem_no_name[] = "sem-no-name";
67 /* @struct semaphore_details
68 * @brief abstract semaphore
69 */
70 struct semaphore_details {
71 const int *check_ptr;
72 const char *dbg_name;
73 intptr_t s;
74 };
76 /*
77 * @brief convert a handle to a semaphore pointer
78 * @param h - handle
79 * @returns valid handle or null
80 */
81 static struct semaphore_details *h2pS(intptr_t h)
82 {
83 struct semaphore_details *pS;
85 pS = (struct semaphore_details *)(h);
86 if(pS)
87 {
88 if(pS->check_ptr != &sem_check)
89 {
90 pS = NULL;
91 BUG_HERE("not a semaphore: %p\n", (void *)(pS));
92 }
93 }
94 return (pS);
95 }
97 /*
98 * Destroy a semaphore
99 *
100 * Public function defined in ti_semaphore.h
101 */
102 void SEMAPHORE_destroy(intptr_t h)
103 {
104 struct semaphore_details *pS;
106 pS = h2pS(h);
107 if(pS == NULL)
108 {
109 return;
110 }
112 if(pS->s)
113 {
114 _ATOMIC_sem_destroy(pS->s);
115 pS->s = 0;
116 }
118 if(pS->dbg_name)
119 {
120 free_const((const void *)(pS->dbg_name));
121 pS->dbg_name = NULL;
122 }
123 memset((void *)(pS), 0, sizeof(*pS));
124 free((void *)(pS));
125 }
127 /*
128 * Create a semaphore
129 *
130 * Public function defined in ti_semaphore.h
131 */
132 intptr_t SEMAPHORE_create(const char *dbg_name, unsigned initial_value)
133 {
134 struct semaphore_details *pS;
136 /* get space */
137 pS = calloc(1, sizeof(*pS));
138 if(pS == NULL)
139 {
140 LOG_printf(LOG_ERROR, "sem: no memory\n");
141 return (0);
142 }
144 /* get a default name */
145 if(dbg_name == NULL)
146 {
147 dbg_name = _sem_no_name;
148 }
150 /* mark struct as valid. */
151 pS->check_ptr = &sem_check;
152 pS->dbg_name = strdup(dbg_name);
153 if(pS->dbg_name == NULL)
154 {
155 BUG_HERE("no memory\n");
156 }
158 pS->s = _ATOMIC_sem_create();
159 if(pS->s == 0)
160 {
161 BUG_HERE("cannot create semaphore\n");
162 }
163 while(initial_value > 0)
164 {
165 initial_value--;
166 _ATOMIC_sem_put(pS->s);
167 }
168 return (intptr_t)(pS);
169 }
171 /*
172 * Put to a semaphore N times
173 *
174 * Public function defined in ti_semaphore.h
175 */
176 int SEMAPHORE_putN(intptr_t h, unsigned n)
177 {
178 struct semaphore_details *pS;
180 pS = h2pS(h);
181 if(pS == NULL)
182 {
183 return (-1);
184 }
185 else
186 {
187 while(n > 0)
188 {
189 n--;
190 _ATOMIC_sem_put(pS->s);
191 }
192 return (0);
193 }
194 }
196 /*
197 * Wait on a seamphore N times
198 *
199 * Public function defined in ti_semaphore.h
200 */
201 int SEMAPHORE_waitNWithTimeout(intptr_t h, unsigned n, int timeout_mSec)
202 {
203 struct semaphore_details *pS;
204 int r;
206 pS = h2pS(h);
207 if(pS == NULL)
208 {
209 return (-1);
210 }
212 if(((timeout_mSec == 0) && (n != 1)))
213 {
214 BUG_HERE("sem operation not supported\n");
215 }
217 r = 1;
218 while(n > 0)
219 {
220 n--;
221 r = _ATOMIC_sem_get(pS->s, timeout_mSec);
222 }
223 return (r);
224 }
226 /*
227 * Inspect (return current count) a semaphore
228 *
229 * Public function defined in ti_semaphore.h
230 */
231 int SEMAPHORE_inspect(intptr_t h)
232 {
233 struct semaphore_details *pS;
234 int r;
236 pS = h2pS(h);
237 if(pS == NULL)
238 {
239 return (-1);
240 }
242 r = _ATOMIC_sem_cnt(pS->s);
243 return (r);
244 }
246 /*
247 * ========================================
248 * Texas Instruments Micro Controller Style
249 * ========================================
250 * Local Variables:
251 * mode: c
252 * c-file-style: "bsd"
253 * tab-width: 4
254 * c-basic-offset: 4
255 * indent-tabs-mode: nil
256 * End:
257 * vim:set filetype=c tabstop=4 shiftwidth=4 expandtab=true
258 */