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