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