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 /* pthread_mutex_init PORTABLE C */
35 /* 6.1.7 */
36 /* AUTHOR */
37 /* */
38 /* William E. Lamie, Microsoft Corporation */
39 /* */
40 /* DESCRIPTION */
41 /* */
42 /* This function shall init the mutex object referenced by mutex with */
43 /* attributes specified by attr. */
44 /* If attr is NULL, the default mutex attributes are used; the effect */
45 /* shall be the same as passing the address of a default mutex */
46 /* attributes object. Upon successful initialization,the state of the */
47 /* mutex becomes initialized and unlocked. */
48 /* */
49 /* INPUT */
50 /* */
51 /* mutex Pointer to a pthread mutex object */
52 /* attr Pointer to mutex attributes */
53 /* */
54 /* OUTPUT */
55 /* */
56 /* 0 If successful */
57 /* Value In case of any error */
58 /* */
59 /* CALLS */
60 /* */
61 /* posix_internal_error In case of some special errors */
62 /* posix_in_thread_context Check whether called from a thread */
63 /* tx_mutex_create Create a ThreadX Mutex object */
64 /* */
65 /* CALLED BY */
66 /* */
67 /* Application Code */
68 /* */
69 /* RELEASE HISTORY */
70 /* */
71 /* DATE NAME DESCRIPTION */
72 /* */
73 /* 06-02-2021 William E. Lamie Initial Version 6.1.7 */
74 /* */
75 /**************************************************************************/
pthread_mutex_init(pthread_mutex_t * mutex,pthread_mutexattr_t * attr)76 INT pthread_mutex_init(pthread_mutex_t *mutex ,pthread_mutexattr_t *attr)
77 {
78
79 TX_INTERRUPT_SAVE_AREA
80
81 TX_MUTEX *mutex_ptr;
82 ULONG status,retval;
83
84
85
86 /* Make sure we're calling this routine from a thread context. */
87 if (!posix_in_thread_context())
88 {
89 /* return POSIX error. */
90 posix_internal_error(444);
91 }
92
93 /* Disable interrupts. */
94 TX_DISABLE
95
96 /* Check for any pthread_mutexattr_t suggested */
97 if (!attr)
98 {
99 /* no attributes passed so assume default attributes */
100 attr = &(posix_default_mutex_attr);
101 }
102 else
103 {
104 /* attributes passed , check for validity */
105 if (( (attr->in_use) == TX_FALSE)|| (attr->type!=PTHREAD_MUTEX_RECURSIVE))
106 {
107 /* attributes passed is not valid, return with an error */
108 /* Restore interrupts. */
109 TX_RESTORE
110 posix_errno = EINVAL;
111 posix_set_pthread_errno(EINVAL);
112 return(EINVAL);
113 }
114 }
115
116 mutex_ptr = (&(mutex->mutex_info)) ;
117
118 /* Now actually create the mutex */
119 status = tx_mutex_create(mutex_ptr, "PMTX", TX_INHERIT);
120
121 if ( status == TX_SUCCESS)
122 {
123 mutex->in_use = TX_TRUE;
124 retval = OK;
125 }
126 else
127 {
128 mutex->in_use = TX_FALSE;
129 posix_errno = EINVAL;
130 posix_set_pthread_errno(EINVAL);
131 retval = EINVAL;
132 }
133
134 TX_RESTORE
135 return(retval);
136 }
137