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_trywait                                         PORTABLE C      */
35 /*                                                           6.1.7        */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    William E. Lamie, Microsoft Corporation                             */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This routine locks(takes) a semaphore if the semaphore is not       */
43 /*    currently locked.                                                   */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    *sem                      Pointer to Semaphore                      */
48 /*                                                                        */
49 /*  OUTPUT                                                                */
50 /*                                                                        */
51 /*    OK                        If successful                             */
52 /*    ERROR                     If error occurs                           */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    posix_internal_error      Returns generic error                     */
57 /*    tx_semaphore_get          ThreadX Semaphore get                     */
58 /*    tx_thread_identify        Returns currently running thread          */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    Application Code                                                    */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
69 /*                                                                        */
70 /**************************************************************************/
sem_trywait(sem_t * sem)71 INT sem_trywait(sem_t * sem)
72 {
73 
74 TX_SEMAPHORE          * TheSem;
75 INT                    retval;
76 INT                    pxerror;
77 
78      /* Make sure we're calling this routine from a thread context.  */
79     if (!tx_thread_identify() )
80     {
81         /* No error in POSIX.  */
82         posix_internal_error(242);
83 
84         /* Return error.  */
85         return (ERROR);
86     }
87 
88      /* Get ThreadX semaphore.  */
89     TheSem = (TX_SEMAPHORE *)sem;
90 
91     /* Check for an invalid semaphore pointer.  */
92     if ((!TheSem) || (TheSem -> tx_semaphore_id != TX_SEMAPHORE_ID))
93     {
94         /* error in POSIX.  */
95         posix_errno = EINVAL;
96 	    posix_set_pthread_errno(EINVAL);
97         return (EINVAL);
98     }
99     else
100         retval = tx_semaphore_get(TheSem,TX_NO_WAIT);
101 
102     switch(retval)
103     {
104 
105         case TX_SUCCESS :
106 
107             sem->refCnt -= 1;
108             pxerror = OK;
109             break;
110 
111         case TX_NO_INSTANCE:
112 
113             posix_errno = EAGAIN;
114 	        posix_set_pthread_errno(EAGAIN);
115             pxerror = ERROR;
116             break;
117 
118         case  TX_DELETED :
119         case  TX_WAIT_ABORTED :
120         case  TX_SEMAPHORE_ERROR :
121         case  TX_WAIT_ERROR :
122 
123             /* No error in POSIX, give default.  */
124             posix_errno = EINVAL;
125 	        posix_set_pthread_errno(EINVAL);
126             pxerror = ERROR;
127             break;
128     }
129     return(pxerror);
130 }
131