1 /***************************************************************************
2 * Copyright (c) 2024 Microsoft Corporation
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the MIT License which is available at
6 * https://opensource.org/licenses/MIT.
7 *
8 * SPDX-License-Identifier: MIT
9 **************************************************************************/
10
11
12 /**************************************************************************/
13 /**************************************************************************/
14 /** */
15 /** POSIX wrapper for THREADX */
16 /** */
17 /** */
18 /** */
19 /**************************************************************************/
20 /**************************************************************************/
21
22 /* Include necessary system files. */
23
24 #include "tx_api.h" /* Threadx API */
25 #include "pthread.h" /* Posix API */
26 #include "px_int.h" /* Posix helper functions */
27
28
29 /**************************************************************************/
30 /* */
31 /* FUNCTION RELEASE */
32 /* */
33 /* pthread_mutex_init PORTABLE C */
34 /* 6.1.7 */
35 /* AUTHOR */
36 /* */
37 /* William E. Lamie, Microsoft Corporation */
38 /* */
39 /* DESCRIPTION */
40 /* */
41 /* This function shall init the mutex object referenced by mutex with */
42 /* attributes specified by attr. */
43 /* If attr is NULL, the default mutex attributes are used; the effect */
44 /* shall be the same as passing the address of a default mutex */
45 /* attributes object. Upon successful initialization,the state of the */
46 /* mutex becomes initialized and unlocked. */
47 /* */
48 /* INPUT */
49 /* */
50 /* mutex Pointer to a pthread mutex object */
51 /* attr Pointer to mutex attributes */
52 /* */
53 /* OUTPUT */
54 /* */
55 /* 0 If successful */
56 /* Value In case of any error */
57 /* */
58 /* CALLS */
59 /* */
60 /* posix_internal_error In case of some special errors */
61 /* posix_in_thread_context Check whether called from a thread */
62 /* tx_mutex_create Create a ThreadX Mutex object */
63 /* */
64 /* CALLED BY */
65 /* */
66 /* Application Code */
67 /* */
68 /* RELEASE HISTORY */
69 /* */
70 /* DATE NAME DESCRIPTION */
71 /* */
72 /* 06-02-2021 William E. Lamie Initial Version 6.1.7 */
73 /* */
74 /**************************************************************************/
pthread_mutex_init(pthread_mutex_t * mutex,pthread_mutexattr_t * attr)75 INT pthread_mutex_init(pthread_mutex_t *mutex ,pthread_mutexattr_t *attr)
76 {
77
78 TX_INTERRUPT_SAVE_AREA
79
80 TX_MUTEX *mutex_ptr;
81 ULONG status,retval;
82
83
84
85 /* Make sure we're calling this routine from a thread context. */
86 if (!posix_in_thread_context())
87 {
88 /* return POSIX error. */
89 posix_internal_error(444);
90 }
91
92 /* Disable interrupts. */
93 TX_DISABLE
94
95 /* Check for any pthread_mutexattr_t suggested */
96 if (!attr)
97 {
98 /* no attributes passed so assume default attributes */
99 attr = &(posix_default_mutex_attr);
100 }
101 else
102 {
103 /* attributes passed , check for validity */
104 if (( (attr->in_use) == TX_FALSE)|| (attr->type!=PTHREAD_MUTEX_RECURSIVE))
105 {
106 /* attributes passed is not valid, return with an error */
107 /* Restore interrupts. */
108 TX_RESTORE
109 posix_errno = EINVAL;
110 posix_set_pthread_errno(EINVAL);
111 return(EINVAL);
112 }
113 }
114
115 mutex_ptr = (&(mutex->mutex_info)) ;
116
117 /* Now actually create the mutex */
118 status = tx_mutex_create(mutex_ptr, "PMTX", TX_INHERIT);
119
120 if ( status == TX_SUCCESS)
121 {
122 mutex->in_use = TX_TRUE;
123 retval = OK;
124 }
125 else
126 {
127 mutex->in_use = TX_FALSE;
128 posix_errno = EINVAL;
129 posix_set_pthread_errno(EINVAL);
130 retval = EINVAL;
131 }
132
133 TX_RESTORE
134 return(retval);
135 }
136