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 /*  sleep                                                  PORTABLE C     */
35 /*                                                           6.1.7        */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    William E. Lamie, Microsoft Corporation                             */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This function shall cause the calling thread to be suspended from   */
43 /*    execution until either the number of realtime seconds specified by  */
44 /*    the argument seconds has elapsed                                    */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*   seconds                      Is the number of real-time (as opposed  */
49 /*                                to CPU-time) seconds to suspend the     */
50 /*                                calling thread.                         */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*   number of seconds remaining to sleep                                 */
55 /*                                A nonzero value indicates sleep was     */
56 /*                                interrupted. Zero is successful         */
57 /*                                completion                              */
58 /*                                                                        */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    tx_thread_sleep             ThreadX thread sleep service            */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Application Code                                                    */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
73 /*                                                                        */
74 /**************************************************************************/
sleep(ULONG seconds)75 UINT sleep(ULONG seconds)
76 {
77 
78 UINT temp1, temp2, diff, result;
79 
80     temp1 = tx_time_get();
81     tx_thread_sleep(seconds * CPU_TICKS_PER_SECOND);
82     temp2 = tx_time_get();
83     diff = temp2 - temp1;
84 
85     if (diff > (seconds * CPU_TICKS_PER_SECOND))
86     {
87         result = (diff - (seconds * CPU_TICKS_PER_SECOND));
88     }
89     else
90     {
91         result = ((seconds * CPU_TICKS_PER_SECOND) - diff);
92     }
93 
94     return (result/CPU_TICKS_PER_SECOND);
95 
96 }
97 
98