1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** POSIX wrapper for THREADX                                             */
17 /**                                                                       */
18 /**                                                                       */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 /* Include necessary system files.  */
24 
25 #include "tx_api.h"    /* Threadx API */
26 #include "pthread.h"  /* Posix API */
27 #include "px_int.h"    /* Posix helper functions */
28 
29 
30 /**************************************************************************/
31 /*                                                                        */
32 /*  FUNCTION                                               RELEASE        */
33 /*                                                                        */
34 /*    sem_wait                                            PORTABLE C      */
35 /*                                                           6.1.7        */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    William E. Lamie, Microsoft Corporation                             */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This function locks (takes) a semaphore.                            */
43 /*                                                                        */
44 /*  INPUT                                                                 */
45 /*                                                                        */
46 /*    *sem                      Pointer to Semaphore                      */
47 /*                                                                        */
48 /*  OUTPUT                                                                */
49 /*                                                                        */
50 /*    OK                        If successful                             */
51 /*    ERROR                     If error occurs                           */
52 /*                                                                        */
53 /*  CALLS                                                                 */
54 /*                                                                        */
55 /*    tx_thread_identify        To check whether calling from a thread    */
56 /*    tx_semaphore_get          ThreadX Semaphore get                     */
57 /*    posix_internal_error      Returns a generic error                   */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    Application Code                                                    */
62 /*                                                                        */
63 /*  RELEASE HISTORY                                                       */
64 /*                                                                        */
65 /*    DATE              NAME                      DESCRIPTION             */
66 /*                                                                        */
67 /*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
68 /*                                                                        */
69 /**************************************************************************/
sem_wait(sem_t * sem)70 INT  sem_wait( sem_t * sem )
71 {
72 
73 TX_SEMAPHORE         * TheSem;
74 
75 
76     /* Make sure we're calling this routine from a thread context.  */
77     if (! tx_thread_identify())
78     {
79         /* No wait when called from ISR.  */
80         posix_internal_error(242);
81 
82         /* Return Error.  */
83         return (ERROR);
84     }
85 
86     /* get ThreadX semaphore.  */
87     TheSem = (TX_SEMAPHORE *)sem;
88 
89     /* Check for an invalid semaphore pointer.  */
90     if ((!TheSem) || (TheSem -> tx_semaphore_id != TX_SEMAPHORE_ID))
91     {
92         /* error in POSIX.  */
93         posix_errno = EINVAL;
94 	    posix_set_pthread_errno(EINVAL);
95 
96         /* Return error.  */
97         return (EINVAL);
98     }
99     else
100     {
101         /* Takes the semaphore.  */
102         if(tx_semaphore_get(TheSem,TX_WAIT_FOREVER))
103         {
104             /* Return general error.  */
105             posix_internal_error(246);
106 
107             /* Return error.  */
108             return(ERROR);
109         }
110 
111         return (OK);
112     }
113 }
114