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_cond_timedwait PORTABLE C */
35 /* 6.1.7 */
36 /* AUTHOR */
37 /* */
38 /* William E. Lamie, Microsoft Corporation */
39 /* */
40 /* DESCRIPTION */
41 /* */
42 /* This function shall block on a condition variable. They shall be */
43 /* called with mutex locked by the calling thread or undefined behavior*/
44 /* results.These functions atomically release the mutex and cause the */
45 /* calling thread to block on the condition variable cond; atomically */
46 /* here means ��atomically with respect to access by another */
47 /* thread to the mutex and then the request for semaphore. */
48 /* */
49 /* Upon successful return, the mutex shall have been locked and shall */
50 /* be owned by the calling thread. */
51 /* */
52 /* Upon successful return, the mutex shall have been locked and shall */
53 /* be owned by the calling thread. */
54 /* When using condition variables there is always a Boolean predicate */
55 /* involving shared variables associated with each condition wait that */
56 /* is true if the thread should proceed. Spurious wakeups from the */
57 /* pthread_cond_timedwait or pthread_cond_wait functions may occur. */
58 /* Since the return from pthread_cond_timedwait or pthread_cond_wait */
59 /* does not imply anything about the value of this predicate, the */
60 /* predicate should be re-evaluated upon such return. The effect of */
61 /* using more than one mutex for concurrent pthread_cond_timedwait or */
62 /* pthread_cond_wait operations on the same condition variable is */
63 /* undefined; that is, a condition variable becomes bound to a unique */
64 /* mutex when a thread waits on the condition variable, and this */
65 /* (dynamic) binding shall end when the wait returns. */
66 /* */
67 /* INPUT */
68 /* */
69 /* cond condition variable */
70 /* mutex mutex to be associated with condition */
71 /* variable */
72 /* abstime time interval for wait */
73 /* */
74 /* OUTPUT */
75 /* */
76 /* OK if succesfull */
77 /* ERROR in case of any error */
78 /* */
79 /* CALLS */
80 /* */
81 /* pthread_mutex_unlock unlocks the mutex held by the caller */
82 /* tx_semaphore_get try to get sempaphore internal to cond */
83 /* tx_semaphore_prioritize prioritize all suspended pthreads */
84 /* pthread_mutex_lock lock the mutex */
85 /* */
86 /* CALLED BY */
87 /* */
88 /* Application Code */
89 /* */
90 /* RELEASE HISTORY */
91 /* */
92 /* DATE NAME DESCRIPTION */
93 /* */
94 /* 06-02-2021 William Lamie Initial Version 6.1.7 */
95 /* */
96 /**************************************************************************/
pthread_cond_timedwait(pthread_cond_t * cond,pthread_mutex_t * mutex,struct timespec * abstime)97 INT pthread_cond_timedwait(pthread_cond_t *cond,pthread_mutex_t *mutex,
98 struct timespec *abstime)
99 {
100
101 TX_SEMAPHORE *semaphore_ptr;
102 ULONG wait_option;
103 UINT status;
104 UINT old_threshold,dummy;
105 TX_THREAD *thread;
106
107 /* Find the current thread. */
108 thread = tx_thread_identify();
109
110 /* Raise its preemption threshold so it does not get descheduled. */
111 tx_thread_preemption_change(thread,0,&old_threshold);
112
113 pthread_mutex_unlock(mutex);
114 semaphore_ptr = (&( cond->cond_semaphore ));
115
116 wait_option = posix_abs_time_to_rel_ticks(abstime);
117
118 status = tx_semaphore_get(semaphore_ptr, wait_option);
119
120 /* Restore original preemption threshold */
121 tx_thread_preemption_change(thread, old_threshold, &dummy);
122
123 if( status == TX_NO_INSTANCE)
124 {
125 posix_errno = ETIMEDOUT;
126 posix_set_pthread_errno(ETIMEDOUT);
127 return(ETIMEDOUT);
128 }
129 else if ( status != TX_SUCCESS)
130 {
131 posix_errno = EINVAL;
132 posix_set_pthread_errno(EINVAL);
133 return(EINVAL);
134 }
135 status = tx_semaphore_prioritize(semaphore_ptr);
136 if (status != TX_SUCCESS)
137 {
138 posix_errno = EINVAL;
139 posix_set_pthread_errno(EINVAL);
140 return(EINVAL);
141 }
142 pthread_mutex_lock(mutex);
143
144 return(OK);
145 }
146