dtime.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Implementation of the dtime intrinsic.
  2. Copyright (C) 2004-2015 Free Software Foundation, Inc.
  3. This file is part of the GNU Fortran runtime library (libgfortran).
  4. Libgfortran is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public
  6. License as published by the Free Software Foundation; either
  7. version 3 of the License, or (at your option) any later version.
  8. Libgfortran is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "libgfortran.h"
  20. #include "time_1.h"
  21. #include <gthr.h>
  22. #ifdef __GTHREAD_MUTEX_INIT
  23. static __gthread_mutex_t dtime_update_lock = __GTHREAD_MUTEX_INIT;
  24. #else
  25. static __gthread_mutex_t dtime_update_lock;
  26. #endif
  27. extern void dtime_sub (gfc_array_r4 *t, GFC_REAL_4 *result);
  28. iexport_proto(dtime_sub);
  29. void
  30. dtime_sub (gfc_array_r4 *t, GFC_REAL_4 *result)
  31. {
  32. GFC_REAL_4 *tp;
  33. long user_sec, user_usec, system_sec, system_usec;
  34. static long us = 0, uu = 0, ss = 0 , su = 0;
  35. GFC_REAL_4 tu, ts, tt;
  36. if (((GFC_DESCRIPTOR_EXTENT(t,0))) < 2)
  37. runtime_error ("Insufficient number of elements in TARRAY.");
  38. __gthread_mutex_lock (&dtime_update_lock);
  39. if (gf_cputime (&user_sec, &user_usec, &system_sec, &system_usec) == 0)
  40. {
  41. tu = (GFC_REAL_4) ((user_sec - us) + 1.e-6 * (user_usec - uu));
  42. ts = (GFC_REAL_4) ((system_sec - ss) + 1.e-6 * (system_usec - su));
  43. tt = tu + ts;
  44. us = user_sec;
  45. uu = user_usec;
  46. ss = system_sec;
  47. su = system_usec;
  48. }
  49. else
  50. {
  51. tu = -1;
  52. ts = -1;
  53. tt = -1;
  54. }
  55. tp = t->base_addr;
  56. *tp = tu;
  57. tp += GFC_DESCRIPTOR_STRIDE(t,0);
  58. *tp = ts;
  59. *result = tt;
  60. __gthread_mutex_unlock (&dtime_update_lock);
  61. }
  62. iexport(dtime_sub);
  63. extern GFC_REAL_4 dtime (gfc_array_r4 *t);
  64. export_proto(dtime);
  65. GFC_REAL_4
  66. dtime (gfc_array_r4 *t)
  67. {
  68. GFC_REAL_4 val;
  69. dtime_sub (t, &val);
  70. return val;
  71. }