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_trylock PORTABLE C */
35 /* 6.1.7 */
36 /* AUTHOR */
37 /* */
38 /* William E. Lamie, Microsoft Corporation */
39 /* */
40 /* DESCRIPTION */
41 /* */
42 /* This function shall be equivalent to pthread_mutex_lock(), except */
43 /* that if the mutex object referenced by mutex is currently locked */
44 /* (by any thread,including the current thread), the call shall return */
45 /* immediately. If the mutex type is PTHREAD_MUTEX_RECURSIVE and the */
46 /* mutex is currently owned by the calling thread,the mutex lock count */
47 /* shall be incremented by one and the pthread_mutex_trylock()function */
48 /* shall immediately return success. */
49 /* */
50 /* INPUT */
51 /* */
52 /* mutex Pointer to the mutex object */
53 /* */
54 /* OUTPUT */
55 /* */
56 /* 0 If successful */
57 /* Value In case of any error */
58 /* */
59 /* CALLS */
60 /* */
61 /* tx_mutex_get ThreadX Mutex get service. */
62 /* */
63 /* CALLED BY */
64 /* */
65 /* Application Code */
66 /* */
67 /* RELEASE HISTORY */
68 /* */
69 /* DATE NAME DESCRIPTION */
70 /* */
71 /* 06-02-2021 William E. Lamie Initial Version 6.1.7 */
72 /* */
73 /**************************************************************************/
pthread_mutex_trylock(pthread_mutex_t * mutex)74 INT pthread_mutex_trylock(pthread_mutex_t *mutex)
75 {
76
77 TX_MUTEX *mutex_ptr;
78 INT retval,status;
79
80 /* convert pthread mutex object to ThreadX mutex */
81 mutex_ptr = (TX_MUTEX *)mutex;
82
83 /* Try to get the mutex */
84 status = tx_mutex_get( mutex_ptr, TX_NO_WAIT);
85
86 switch ( status)
87 {
88 case TX_SUCCESS:
89 retval = OK;
90 break;
91
92 case TX_DELETED:
93 posix_errno = EINVAL;
94 posix_set_pthread_errno(EINVAL);
95 retval = EINVAL;
96 break;
97
98 case TX_NOT_AVAILABLE:
99 posix_errno = EBUSY;
100 posix_set_pthread_errno(EBUSY);
101 retval = EBUSY;
102 break;
103
104 default:
105 posix_errno = EINVAL;
106 posix_set_pthread_errno(EINVAL);
107 retval = EINVAL;
108 break;
109 }
110 return (retval);
111 }
112