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 "tx_posix.h"   /* Posix API */
28 #include "px_int.h"     /* Posix helper functions */
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    sem_init                                             PORTABLE C     */
36 /*                                                           6.1.7        */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    William E. Lamie, Microsoft Corporation                             */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function initializes a nameless sempaphore.                    */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    sem                           Semaphore pointer                     */
48 /*    pshared                       Shared semphore (TRUE/FALSE)          */
49 /*    value                         Initial value                         */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    EINVAL                        if error occurs                       */
54 /*    NO_ERROR                      successful                            */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    tx_semaphore_create           creates actual THreadx semaphore      */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    POSIX internal 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_init(sem_t * sem,INT pshared,UINT value)71 INT sem_init(sem_t *sem , INT pshared, UINT value)
72 {
73     INT result;
74 
75     if((sem==NULL)||(value > SEM_VALUE_MAX)||(sem->in_use==TX_TRUE))
76     {
77         result = EINVAL; /* general error */
78 
79     }
80     else
81     {
82         if(tx_semaphore_create(&(sem->sem),"",value)) result = EINVAL;
83         else
84         {            /* initialize semaphore fields */
85             sem->count = 0;
86             sem->in_use = TX_TRUE;
87             sem->psemId = 0;
88             sem->refCnt = value;
89             sem->sem_name = "";
90             sem->unlink_flag =TX_FALSE;
91 
92             result = NO_ERROR;
93         }
94     }
95 
96 
97     return result;
98 }
99