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_close                                           PORTABLE C      */
35 /*                                                           6.1.7        */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    William E. Lamie, Microsoft Corporation                             */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This routine is called to indicate that the calling thread is       */
43 /*    finished with the specified named semaphore, sem.                   */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    sem                           type of semaphore                     */
48 /*                                                                        */
49 /*  OUTPUT                                                                */
50 /*                                                                        */
51 /*    OK                            If successful                         */
52 /*    ERROR                         If error occurs                       */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    posix_internal_error          Generic error handler                 */
57 /*    posix_sem_reset               Resets the semaphore structure        */
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_close(sem_t * sem)71 INT  sem_close(sem_t  * sem)
72 {
73 
74 TX_SEMAPHORE    * TheSem;
75 TX_THREAD       * Thethread;
76 
77     /* Make sure we're calling this routine from a thread context.  */
78     Thethread = tx_thread_identify();
79 
80     if (!Thethread)
81     {
82         /* Return appropriate error.  */
83         posix_internal_error(240);
84 
85         /* there is no error defined for this in POSIX, hence give default.  */
86         return(ERROR);
87     }
88 
89     /* Get ThreadX semaphore.  */
90     TheSem = (TX_SEMAPHORE * )sem;
91 
92     /* Check for an invalid semaphore pointer.  */
93     if ((!TheSem) || (TheSem -> tx_semaphore_id != TX_SEMAPHORE_ID))
94     {
95 
96         /* Return appropriate error.  */
97         posix_errno = EINVAL;
98 	    posix_set_pthread_errno(EINVAL);
99 
100         /* return error.  */
101         return(ERROR);
102     }
103     if(sem)
104         sem->count -= 1;
105 
106     if(! (sem->count) )
107     {
108 
109         if(sem->unlink_flag == TX_TRUE)
110         {
111             posix_sem_reset(sem );
112             sem = NULL;
113         }
114     }
115     return(OK);
116 }
117