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_broadcast                                PORTABLE C      */
35 /*                                                           6.1.7        */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    William E. Lamie, Microsoft Corporation                             */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    These functions shall unblock all threads currently blocked on a    */
43 /*    specified condition variable cond.                                  */
44 /*    If more than one thread is blocked on a condition variable,         */
45 /*    the scheduling policy shall determine the order in which threads are*/
46 /*    unblocked. When each thread unblocked as a result of this function  */
47 /*    call returns from its call to pthread_cond_wait or                  */
48 /*    pthread_cond_timedwait, the thread shall own the mutex with which it*/
49 /*    called pthread_cond_wait or pthread_cond_timedwait. The thread(s)   */
50 /*    that are unblocked shall contend for the mutex according to the     */
51 /*    scheduling policy (if applicable), and as if each had called        */
52 /*    pthread_mutex_lock.The pthread_cond_broadcast function may be called*/
53 /*    by a thread whether or not it currently owns the mutex that threads */
54 /*    calling pthread_cond_wait or pthread_cond_timedwait have associated */
55 /*    with the condition variable during their waits; however,            */
56 /*    if predictable scheduling behavior is required, then that mutex     */
57 /*    shall be locked by the thread calling pthread_cond_broadcast.       */
58 /*    The pthread_cond_broadcast function shall have no effect if there   */
59 /*    are no threads currently blocked on cond.                           */
60 /*                                                                        */
61 /*  INPUT                                                                 */
62 /*                                                                        */
63 /*     cond                       condition variable                      */
64 /*                                                                        */
65 /*  OUTPUT                                                                */
66 /*                                                                        */
67 /*     Ok                        if successful                            */
68 /*     Error                     in case of any errors                    */
69 /*                                                                        */
70 /*  CALLS                                                                 */
71 /*                                                                        */
72 /*     tx_semaphore_prioritize       line up pthreads waiting at semaphore*/
73 /*     tx_thread_identify            to check which pthread?              */
74 /*     tx_thread_preemption_change   to disable thread preemption         */
75 /*     tx_semaphore_put              ThreadX semaphore put service        */
76 /*     tx_thread_preemption_change   to enable thread preemption          */
77 /*                                                                        */
78 /*  CALLED BY                                                             */
79 /*                                                                        */
80 /*    Application Code                                                    */
81 /*                                                                        */
82 /*  RELEASE HISTORY                                                       */
83 /*                                                                        */
84 /*    DATE              NAME                      DESCRIPTION             */
85 /*                                                                        */
86 /*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
87 /*                                                                        */
88 /**************************************************************************/
pthread_cond_broadcast(pthread_cond_t * cond)89 INT pthread_cond_broadcast(pthread_cond_t *cond)
90 {
91 
92 TX_SEMAPHORE        *semaphore_ptr;
93 TX_THREAD           *thread;
94 UINT                 status;
95 ULONG                sem_count;
96 UINT                 old_threshold,dummy;
97 
98 
99     /* Get the condition variable's internal semaphore.  */
100     /* Simply convert the condition variable control block into a semaphore  a cast */
101     semaphore_ptr = (&( cond->cond_semaphore ));
102     sem_count = semaphore_ptr->tx_semaphore_suspended_count;
103 
104     if (!sem_count)
105         return(OK);
106 
107     status = tx_semaphore_prioritize(semaphore_ptr);
108 
109     if ( status != TX_SUCCESS)
110     {
111         posix_errno = EINVAL;
112         posix_set_pthread_errno(EINVAL);
113         return(EINVAL);
114     }
115 
116     /* get to know about current thread */
117     thread = tx_thread_identify();
118 
119     /* Got the current thread , now raise its preemption threshold */
120     /* that way the current thread does not get descheduled when   */
121     /* threads with higher priority are activated */
122     tx_thread_preemption_change(thread,0,&old_threshold);
123 
124     while( sem_count)
125     {
126 
127         status = tx_semaphore_put(semaphore_ptr);
128         if ( status != TX_SUCCESS)
129         {
130 
131             /* restore changed preemption threshold */
132             tx_thread_preemption_change(thread,old_threshold,&dummy);
133 
134             posix_errno = EINVAL;
135 
136             posix_set_pthread_errno(EINVAL);
137 
138             return(EINVAL);
139         }
140 
141         sem_count--;
142     }
143 
144     /* restore changed preemption threshold */
145     tx_thread_preemption_change(thread,old_threshold,&dummy);
146     return(OK);
147 }
148