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 /** 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_wait                                     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 thread to   */
46 /*    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 /*    When using condition variables there is always a Boolean predicate  */
52 /*    involving shared variables associated with each condition wait that */
53 /*    is true if the thread should proceed. Spurious wakeups from the     */
54 /*    pthread_cond_timedwait or pthread_cond_wait functions may occur.    */
55 /*    Since the return from pthread_cond_timedwait or pthread_cond_wait   */
56 /*    does not imply anything about the value of this predicate, the      */
57 /*    predicate should be re-evaluated upon such return. The effect of    */
58 /*    using more than one mutex for concurrent pthread_cond_timedwait or  */
59 /*    pthread_cond_wait operations on the same condition variable is      */
60 /*    undefined; that is, a condition variable becomes bound to a unique  */
61 /*    mutex when a thread waits on the condition variable, and this       */
62 /*    (dynamic) binding shall end when the wait returns.                  */
63 /*                                                                        */
64 /*  INPUT                                                                 */
65 /*                                                                        */
66 /*     cond                         condition variable                    */
67 /*     mutex                        mutex to be associated with condition */
68 /*                                  variable                              */
69 /*                                                                        */
70 /*  OUTPUT                                                                */
71 /*                                                                        */
72 /*     OK                           if succesfull                         */
73 /*     ERROR                        in case of any error                  */
74 /*                                                                        */
75 /*  CALLS                                                                 */
76 /*                                                                        */
77 /*   pthread_mutex_unlock          unlocks the mutex held by the caller   */
78 /*   tx_semaphore_get              try to get sempaphore internal to cond */
79 /*   tx_semaphore_prioritize       prioritize all suspended pthreads      */
80 /*   pthread_mutex_lock            lock the mutex                         */
81 /*                                                                        */
82 /*  CALLED BY                                                             */
83 /*                                                                        */
84 /*    Application Code                                                    */
85 /*                                                                        */
86 /*  RELEASE HISTORY                                                       */
87 /*                                                                        */
88 /*    DATE              NAME                      DESCRIPTION             */
89 /*                                                                        */
90 /*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
91 /*                                                                        */
92 /**************************************************************************/
pthread_cond_wait(pthread_cond_t * cond,pthread_mutex_t * mutex)93 INT pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
94 {
95 
96 TX_SEMAPHORE   *semaphore_ptr;
97 UINT            status;
98 UINT            old_threshold,dummy;
99 TX_THREAD       *thread;
100 
101     /* Find the current thread. */
102     thread = tx_thread_identify();
103 
104     /* Raise its preemption threshold so it does not get descheduled. */
105     tx_thread_preemption_change(thread,0,&old_threshold);
106 
107     pthread_mutex_unlock(mutex);
108 
109     semaphore_ptr = (&( cond->cond_semaphore ));
110 
111     status = tx_semaphore_get(semaphore_ptr, TX_WAIT_FOREVER);
112 
113     /* Restore original preemption threshold. */
114     tx_thread_preemption_change(thread, old_threshold, &dummy);
115 
116 
117     if ( status != TX_SUCCESS)
118     {
119         posix_errno = EINVAL;
120         posix_set_pthread_errno(EINVAL);
121         return(EINVAL);
122     }
123 
124     status = tx_semaphore_prioritize(semaphore_ptr);
125 
126     if ( status != TX_SUCCESS)
127     {
128         posix_errno = EINVAL;
129         posix_set_pthread_errno(EINVAL);
130         return(EINVAL);
131     }
132 
133     pthread_mutex_lock(mutex);
134     return(OK);
135 }
136