]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/language/c/libc/time/v2_0/src/clock.cxx
Initial revision
[karo-tx-redboot.git] / packages / language / c / libc / time / v2_0 / src / clock.cxx
1 //===========================================================================
2 //
3 //      clock.cxx
4 //
5 //      ISO C date and time implementation for clock()
6 //
7 //===========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12 //
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
16 //
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 //
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
32 //
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
35 //
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //===========================================================================
41 //#####DESCRIPTIONBEGIN####
42 //
43 // Author(s):    jlarmour
44 // Contributors: jlarmour
45 // Date:         1999-03-05
46 // Purpose:      Provides an implementation of the ISO C function clock()
47 //               from ISO C section 7.12.2.1
48 // Description:  This file uses the kernel real time clock to determine
49 //               the complete running time of the system - since we only
50 //               have one task, even though there are perhaps multiple threads
51 //               that is still the running time of the "program"
52 // Usage:       
53 //
54 //####DESCRIPTIONEND####
55 //
56 //===========================================================================
57
58 // CONFIGURATION
59
60 #include <pkgconf/libc_time.h>    // Configuration header
61
62 #ifdef CYGSEM_LIBC_TIME_CLOCK_WORKING
63 # include <pkgconf/kernel.h> // Kernel config header
64 #endif
65
66 // INCLUDES
67
68 #include <cyg/infra/cyg_type.h>    // Common type definitions and support
69 #include <cyg/infra/cyg_ass.h>     // Assertion infrastructure
70 #include <cyg/infra/cyg_trac.h>    // Tracing infrastructure
71
72 #include <time.h>                  // Header for all time-related functions
73
74 #ifdef CYGSEM_LIBC_TIME_CLOCK_WORKING
75 # include <cyg/kernel/clock.hxx>   // Kernel clock definitions
76 # include <cyg/kernel/clock.inl>   // Kernel clock inline functions
77 #endif
78
79
80 // TRACE
81
82 # if defined(CYGDBG_USE_TRACING) && defined(CYGNUM_LIBC_TIME_CLOCK_TRACE_LEVEL)
83 static int clock_trace = CYGNUM_LIBC_TIME_CLOCK_TRACE_LEVEL;
84 #  define TL1 (0 < clock_trace)
85 # else
86 #  define TL1 (0)
87 # endif
88
89 // FUNCTIONS
90
91 externC clock_t
92 clock( void )
93 {
94     CYG_REPORT_FUNCNAMETYPE( "clock", "returning clock tick %d" );
95     CYG_REPORT_FUNCARGVOID();
96
97 #ifdef CYGSEM_LIBC_TIME_CLOCK_WORKING
98     cyg_tick_count curr_clock;            // kernel clock value
99     Cyg_Clock::cyg_resolution resolution; // kernel clock resolution
100     clock_t clocks;
101     unsigned long long temp;
102
103     CYG_TRACE0( TL1, "getting clock resolution" );
104     
105     // get the resolution
106     resolution = Cyg_Clock::real_time_clock->get_resolution();
107
108     CYG_TRACE2( TL1, "got resolution dividend %d divisor %d. Getting "
109                 "clock value", resolution.dividend, resolution.divisor );
110
111     // get the value
112     curr_clock = Cyg_Clock::real_time_clock->current_value();
113
114     CYG_TRACE1( TL1, "got clock value %d", curr_clock );
115     
116     // scale the value so that clock()/CLOCKS_PER_SEC works
117     // We use an unsigned long long to avoid overflow as the dividend
118     // and divisors tend to be huge
119     temp = (1000000000 / CLOCKS_PER_SEC);
120     temp *= resolution.divisor;
121     temp = (unsigned long long)curr_clock * resolution.dividend / temp;
122     clocks = (clock_t)temp;
123     
124     CYG_REPORT_RETVAL( clocks );
125     return clocks;
126
127 #else // i.e. ifndef CYGSEM_LIBC_TIME_CLOCK_WORKING
128     CYG_REPORT_RETVAL( -1 );
129     return (clock_t) -1;
130 #endif
131
132 } // clock()
133
134
135 // EOF clock.cxx