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_signal                                   PORTABLE C      */
34 /*                                                           6.1.7        */
35 /*  AUTHOR                                                                */
36 /*                                                                        */
37 /*    William E. Lamie, Microsoft Corporation                             */
38 /*                                                                        */
39 /*  DESCRIPTION                                                           */
40 /*                                                                        */
41 /*    This function shall unblock at least one of the threads that are    */
42 /*    blocked on the specified condition variable cond                    */
43 /*    (if any threads are blocked on cond).If more than one thread is     */
44 /*    blocked on a condition variable, the scheduling policy shall        */
45 /*    determine the order in which threads are unblocked.When each thread */
46 /*    unblocked as a result of a pthread_cond_signal returns from its call*/
47 /*    to pthread_cond_wait or pthread_cond_timedwait, the thread shall own*/
48 /*    the mutex with which it called pthread_cond_wait or                 */
49 /*    pthread_cond_timedwait.The thread(s) that are unblocked shall       */
50 /*    contend for the mutex according to the scheduling policy            */
51 /*    (if applicable), and as if each had called pthread_mutex_lock.      */
52 /*    The pthread_cond_broadcast or pthread_cond_signal functions may be  */
53 /*    called by a thread whether or not it currently owns the mutex that  */
54 /*    threads calling pthread_cond_wait or pthread_cond_timedwait have    */
55 /*    associated with the condition variable during their waits; however, */
56 /*    if predictable scheduling behavior is required,then that mutex shall*/
57 /*    be locked by the thread calling pthread_cond_signal.                */
58 /*    This function shall have no effect if there are no threads currently*/
59 /*    blocked on cond.                                                    */
60 /*                                                                        */
61 /*  INPUT                                                                 */
62 /*                                                                        */
63 /*     Nothing                                                            */
64 /*                                                                        */
65 /*  OUTPUT                                                                */
66 /*                                                                        */
67 /*     Nothing                                                            */
68 /*                                                                        */
69 /*  CALLS                                                                 */
70 /*                                                                        */
71 /*     tx_semaphore_prioritize       line up pthreads waiting at semaphore*/
72 /*     tx_semaphore_put              ThreadX semaphore put service        */
73 /*                                                                        */
74 /*  CALLED BY                                                             */
75 /*                                                                        */
76 /*    Application Code                                                    */
77 /*                                                                        */
78 /*  RELEASE HISTORY                                                       */
79 /*                                                                        */
80 /*    DATE              NAME                      DESCRIPTION             */
81 /*                                                                        */
82 /*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
83 /*                                                                        */
84 /**************************************************************************/
pthread_cond_signal(pthread_cond_t * cond)85 INT  pthread_cond_signal(pthread_cond_t *cond)
86 {
87 
88 TX_SEMAPHORE        *semaphore_ptr;
89 UINT                 status;
90 
91     /* Get the condition variable's internal semaphore.  */
92     /* Simply convert the COndition variable control block into a semaphore  a cast */
93     semaphore_ptr = (&( cond->cond_semaphore ));
94     if ( semaphore_ptr->tx_semaphore_suspended_count)
95     {
96     status = tx_semaphore_prioritize(semaphore_ptr);
97         if ( status != TX_SUCCESS)
98         {
99             posix_errno = EINVAL;
100 	        posix_set_pthread_errno(EINVAL);
101             return(EINVAL);
102         }
103         status = tx_semaphore_put(semaphore_ptr);
104         if ( status != TX_SUCCESS)
105         {
106             posix_errno = EINVAL;
107 	        posix_set_pthread_errno(EINVAL);
108             return(EINVAL);
109         }
110     }
111     return(OK);
112 }
113