MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
mbsleep.c
Go to the documentation of this file.
1 /*
2  * MBDyn (C) is a multibody analysis code.
3  * http://www.mbdyn.org
4  *
5  * Copyright (C) 1996-2017
6  *
7  * Pierangelo Masarati <masarati@aero.polimi.it>
8  * Paolo Mantegazza <mantegazza@aero.polimi.it>
9  *
10  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
11  * via La Masa, 34 - 20156 Milano, Italy
12  * http://www.aero.polimi.it
13  *
14  * Changing this copyright notice is forbidden.
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation (version 2 of the License).
19  *
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29  */
30 
31 #include "mbconfig.h"
32 
33 #include <stdlib.h>
34 
35 #include "mbsleep.h"
36 
38 mbsleep_init(long t)
39 {
40 #if defined(HAVE_NANOSLEEP)
41  mbsleep_t v = { t, 0 };
42  return v;
43 #else /* ! HAVE_NANOSLEEP */
44  return t;
45 #endif /* ! HAVE_NANOSLEEP */
46 }
47 
48 int
50 {
51  if (d < 0) {
52  return -1;
53  }
54 
55 #if defined(HAVE_NANOSLEEP)
56  t->tv_sec = (time_t)floor(d);
57  t->tv_nsec = (long)floor((d - t->tv_sec)*1000000000);
58 #elif defined(HAVE_USLEEP)
59  *t = (unsigned long)floor(d*1000000);
60 #elif defined(HAVE_SLEEP)
61  if (d > 1.) {
62  *t = (unsigned int)floor(d);
63  } else {
64  *t = 1;
65  }
66 #else /* ! SLEEP */
67  *t = 0;
68 #endif /* ! SLEEP */
69 
70  return 0;
71 }
72 
73 int
75 {
76 #if defined(HAVE_NANOSLEEP)
77  *d = ((doublereal)(t->tv_nsec))/1000000000 + t->tv_sec;
78 #elif defined(HAVE_USLEEP)
79  *d = ((doublereal)*t)/1000000;
80 #elif defined(HAVE_SLEEP)
81  *d = (doublereal)*t;
82 #else /* ! SLEEP */
83  *d = 0.;
84 #endif /* ! SLEEP */
85 
86  return 0;
87 }
88 
89 int
90 mbsleep(const mbsleep_t *t)
91 {
92 #if defined(HAVE_NANOSLEEP)
93  return nanosleep(t, NULL);
94 #elif defined(HAVE_USLEEP)
95  return usleep(*t);
96 #elif defined(HAVE_SLEEP)
97  return sleep(*t);
98 #endif /* ! SLEEP */
99 
100  return -1;
101 }
102 
int mbsleep(const mbsleep_t *t)
Definition: mbsleep.c:90
unsigned long mbsleep_t
Definition: mbsleep.h:51
int mbsleep_sleep2real(const mbsleep_t *t, doublereal *d)
Definition: mbsleep.c:74
mbsleep_t mbsleep_init(long t)
Definition: mbsleep.c:38
int mbsleep_real2sleep(doublereal d, mbsleep_t *t)
Definition: mbsleep.c:49
double doublereal
Definition: colamd.c:52