]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/pdk.git/blob - packages/ti/osal/src/linux/SemaphoreP_linux.c
[OSAL]: Added testcase for task sub module
[processor-sdk/pdk.git] / packages / ti / osal / src / linux / SemaphoreP_linux.c
1 /*
2  * Copyright (c) 2016, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * *  Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
33 /*
34  *  ======== SemaphoreP_linux.c ========
35  */
36 #include <ti/osal/SemaphoreP.h>
37 #include <ti/osal/osal.h>
39 #include <stdint.h>
40 #include <stdbool.h>
41 #include <stdlib.h>
43 #include <semaphore.h>
44 #include <fcntl.h>
45 #include <time.h>
46 #include <errno.h>
48 /*
49  *  ======== SemaphoreP_create ========
50  */
51 SemaphoreP_Handle SemaphoreP_create(uint32_t count,
52                                     const SemaphoreP_Params *params)
53 {
54     sem_t *handle;
56     /* Creates a COUNTING semaphore */
57     handle = sem_open(params->name, O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, count);
58     if (SEM_FAILED == handle)
59     {
60         return NULL;
61     }
62     return ((SemaphoreP_Handle)handle);
63 }
66 /*
67  *  ======== SemaphoreP_delete ========
68  */
69 SemaphoreP_Status SemaphoreP_delete(SemaphoreP_Handle handle)
70 {
71     OSAL_Assert(NULL == handle);
73     int ret;
75     ret = sem_close((sem_t *)handle);
76     if (ret < 0) {
77         return SemaphoreP_FAILURE;
78     }
80     return (SemaphoreP_OK);
81 }
83 /*
84  *  ======== SemaphoreP_Params_init ========
85  */
86 void SemaphoreP_Params_init(SemaphoreP_Params *params)
87 {
88     OSAL_Assert(NULL == params);
90     params->mode = SemaphoreP_Mode_COUNTING;
91     params->name = NULL;
92 }
94 /*
95  *  ======== SemaphoreP_pend ========
96  */
97 SemaphoreP_Status SemaphoreP_pend(SemaphoreP_Handle handle, uint32_t timeout)
98 {
99     OSAL_Assert(NULL == handle);
101     int ret;
102     struct timespec ts;
103     int timeout_ns = timeout*1000;
105     if (SemaphoreP_WAIT_FOREVER == timeout) {
106         ret = sem_wait((sem_t *)handle);
107     } else {
108         clock_gettime(CLOCK_REALTIME, &ts);
109         ts.tv_sec += (timeout_ns)/1000000000;
110         ts.tv_nsec += timeout_ns%1000000000;
112         ret = sem_timedwait((sem_t *)handle, &ts);
113     }
114     if (ret < 0) {
115         if (ETIMEDOUT == errno) {
116                 return (SemaphoreP_TIMEOUT);
117         } else {
118                 return (SemaphoreP_FAILURE);
119         }
120     }
122     return (SemaphoreP_OK);
125 /*
126  *  ======== SemaphoreP_post ========
127  */
128 SemaphoreP_Status SemaphoreP_post(SemaphoreP_Handle handle)
130     OSAL_Assert(NULL == handle);
132     int ret;
134     ret = sem_post((sem_t *)handle);
135     if (ret < 0) {
136         return (SemaphoreP_FAILURE);
137     }
139     return (SemaphoreP_OK);
142 /*
143  *  ======== SemaphoreP_postFromClock ========
144  */
145 SemaphoreP_Status SemaphoreP_postFromClock(SemaphoreP_Handle handle)
147     return (SemaphoreP_post(handle));
150 /*
151  *  ======== SemaphoreP_postFromISR ========
152  */
153 SemaphoreP_Status SemaphoreP_postFromISR(SemaphoreP_Handle handle)
155     return (SemaphoreP_post(handle));
158 /* Nothing past this point */