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 "tx_posix.h"  /* Posix API */
28 #include "px_int.h"    /* Posix helper functions */
29 
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    sched_yield                                         PORTABLE C      */
37 /*                                                           6.1.7        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    William E. Lamie, Microsoft Corporation                             */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This routine forces the running thread to relinquish the CPU        */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    void                                 .                              */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    OK                         If successful                            */
53 /*    ERROR                      IF fails                                 */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    posix_in_thread_context    Checks thread is called from its context.*/
58 /*    tx_thread_relinquish       Relinquishes processor control           */
59 /*    posix_internal_error       Returns generic error                    */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Application Code                                                    */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
70 /*                                                                        */
71 /**************************************************************************/
sched_yield(VOID)72 INT sched_yield(VOID)
73 {
74 
75     /* Make sure we're calling this routine from a thread context.  */
76     if (!posix_in_thread_context())
77     {
78         /* return POSIX error.  */
79         posix_internal_error(444);
80 
81         /* return error.  */
82         return (ERROR);
83     }
84 
85     /* This relinquishes the CPU.  */
86     tx_thread_relinquish();
87     return(OK);
88 }
89