LCOV - code coverage report
Current view: top level - src/unix - utime.c (source / functions) Hit Total Coverage
Test: allegro_auto.info Lines: 14 26 53.8 %
Date: 2018-08-11 00:50:28 Functions: 3 4 75.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*         ______   ___    ___
       2             :  *        /\  _  \ /\_ \  /\_ \
       3             :  *        \ \ \L\ \\//\ \ \//\ \      __     __   _ __   ___
       4             :  *         \ \  __ \ \ \ \  \ \ \   /'__`\ /'_ `\/\`'__\/ __`\
       5             :  *          \ \ \/\ \ \_\ \_ \_\ \_/\  __//\ \L\ \ \ \//\ \L\ \
       6             :  *           \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
       7             :  *            \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
       8             :  *                                           /\____/
       9             :  *                                           \_/__/
      10             :  *
      11             :  *      Unix time module.
      12             :  *
      13             :  *      By Peter Wang.
      14             :  *
      15             :  *      See readme.txt for copyright information.
      16             :  */
      17             : 
      18             : 
      19             : #include <sys/time.h>
      20             : #include <math.h>
      21             : 
      22             : #include "allegro5/altime.h"
      23             : #include "allegro5/debug.h"
      24             : #include "allegro5/platform/aintunix.h"
      25             : #include "allegro5/platform/aintuthr.h"
      26             : 
      27             : ALLEGRO_STATIC_ASSERT(utime,
      28             :    sizeof(ALLEGRO_TIMEOUT_UNIX) <= sizeof(ALLEGRO_TIMEOUT));
      29             : 
      30             : 
      31             : /* Marks the time Allegro was initialised, for al_get_time(). */
      32             : struct timeval _al_unix_initial_time;
      33             : 
      34             : 
      35             : 
      36             : /* _al_unix_init_time:
      37             :  *  Called by the system driver to mark the beginning of time.
      38             :  */
      39           6 : void _al_unix_init_time(void)
      40             : {
      41           6 :    gettimeofday(&_al_unix_initial_time, NULL);
      42           6 : }
      43             : 
      44             : 
      45             : 
      46             : /* Function: al_get_time
      47             :  */
      48        1296 : double al_get_time(void)
      49             : {
      50             :    struct timeval now;
      51             :    double time;
      52             : 
      53        1296 :    gettimeofday(&now, NULL);
      54        2592 :    time = (double) (now.tv_sec - _al_unix_initial_time.tv_sec)
      55        1296 :       + (double) (now.tv_usec - _al_unix_initial_time.tv_usec) * 1.0e-6;
      56        1296 :    return time;
      57             : }
      58             : 
      59             : 
      60             : 
      61             : /* Function: al_rest
      62             :  */
      63         654 : void al_rest(double seconds)
      64             : {
      65             :    struct timespec timeout;
      66         654 :    double fsecs = floor(seconds);
      67         654 :    timeout.tv_sec = (time_t) fsecs;
      68         654 :    timeout.tv_nsec = (suseconds_t) ((seconds - fsecs) * 1e9);
      69         654 :    nanosleep(&timeout, 0);
      70         654 : }
      71             : 
      72             : 
      73             : 
      74             : /* Function: al_init_timeout
      75             :  */
      76           0 : void al_init_timeout(ALLEGRO_TIMEOUT *timeout, double seconds)
      77             : {
      78           0 :     ALLEGRO_TIMEOUT_UNIX *ut = (ALLEGRO_TIMEOUT_UNIX *) timeout;
      79             :     struct timeval now;
      80             :     double integral;
      81             :     double frac;
      82             : 
      83             :     ASSERT(ut);
      84             : 
      85           0 :     gettimeofday(&now, NULL);
      86             : 
      87           0 :     if (seconds <= 0.0) {
      88           0 :         ut->abstime.tv_sec = now.tv_sec;
      89           0 :         ut->abstime.tv_nsec = now.tv_usec * 1000;
      90             :     }
      91             :     else {
      92           0 :         frac = modf(seconds, &integral);
      93             : 
      94           0 :         ut->abstime.tv_sec = now.tv_sec + integral;
      95           0 :         ut->abstime.tv_nsec = (now.tv_usec * 1000) + (frac * 1000000000L);
      96           0 :         ut->abstime.tv_sec += ut->abstime.tv_nsec / 1000000000L;
      97           0 :         ut->abstime.tv_nsec = ut->abstime.tv_nsec % 1000000000L;
      98             :     }
      99           0 : }
     100             : 
     101             : /* vim: set sts=3 sw=3 et */

Generated by: LCOV version 1.13