Mercurial > hg > octave-nkf
comparison src/cutils.c @ 10253:8cf32587d8f1
liboctave/cutils.c (octave_usleep): implement with nanosleep
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 03 Feb 2010 05:18:25 -0500 |
parents | fa7b5751730c |
children | e4e82740e9cd |
comparison
equal
deleted
inserted
replaced
10252:2fcc927a8757 | 10253:8cf32587d8f1 |
---|---|
22 | 22 |
23 #ifdef HAVE_CONFIG_H | 23 #ifdef HAVE_CONFIG_H |
24 #include <config.h> | 24 #include <config.h> |
25 #endif | 25 #endif |
26 | 26 |
27 #include <stdarg.h> | |
28 #include <stdio.h> | 27 #include <stdio.h> |
29 #include <stdlib.h> | 28 #include <time.h> |
30 #include <string.h> | |
31 | 29 |
32 #if defined (__WIN32__) && ! defined (_POSIX_VERSION) | |
33 | |
34 #include <windows.h> | |
35 | |
36 #else | |
37 | |
38 #include <sys/time.h> | |
39 #include <sys/types.h> | 30 #include <sys/types.h> |
40 #include <unistd.h> | 31 #include <unistd.h> |
41 | |
42 #ifdef HAVE_POLL_H | |
43 #include <poll.h> | |
44 #elif HAVE_SYS_POLL_H | |
45 #include <sys/poll.h> | |
46 #endif | |
47 | |
48 #endif | |
49 | 32 |
50 void | 33 void |
51 octave_sleep (unsigned int seconds) | 34 octave_sleep (unsigned int seconds) |
52 { | 35 { |
53 #if defined (__WIN32__) && ! defined (_POSIX_VERSION) | |
54 Sleep (1000 * seconds); | |
55 #else | |
56 sleep (seconds); | 36 sleep (seconds); |
57 #endif | |
58 } | 37 } |
59 | 38 |
60 void | 39 void |
61 octave_usleep (unsigned int useconds) | 40 octave_usleep (unsigned int useconds) |
62 { | 41 { |
42 struct timespec delay; | |
43 struct timespec remaining; | |
44 | |
63 unsigned int sec = useconds / 1000000; | 45 unsigned int sec = useconds / 1000000; |
64 unsigned int usec = useconds % 1000000; | 46 unsigned int usec = useconds % 1000000; |
65 | 47 |
66 if (sec > 0) | 48 delay.tv_sec = sec; |
67 octave_sleep (sec); | 49 delay.tv_nsec = usec * 1000; |
68 | 50 |
69 #if defined (__WIN32__) && ! defined (_POSIX_VERSION) | 51 nanosleep (&delay, &remaining); |
70 | |
71 /* Round to the nearest millisecond, with a minimum of 1 millisecond | |
72 if usleep was called with a a non-zero value. */ | |
73 | |
74 if (usec > 500) | |
75 Sleep ((usec+500)/1000); | |
76 else if (usec > 0) | |
77 Sleep (1); | |
78 else | |
79 Sleep (0); | |
80 | |
81 #elif defined (HAVE_USLEEP) | |
82 | |
83 usleep (usec); | |
84 | |
85 #elif defined (HAVE_SELECT) | |
86 | |
87 { | |
88 struct timeval delay; | |
89 | |
90 delay.tv_sec = 0; | |
91 delay.tv_usec = usec; | |
92 | |
93 select (0, 0, 0, 0, &delay); | |
94 } | |
95 | |
96 #elif defined (HAVE_POLL) | |
97 | |
98 { | |
99 struct pollfd pfd; | |
100 | |
101 int delay = usec / 1000; | |
102 | |
103 if (delay > 0) | |
104 poll (&pfd, 0, delay); | |
105 } | |
106 | |
107 #endif | |
108 } | 52 } |
109 | 53 |
110 int | 54 int |
111 octave_raw_vsnprintf (char *buf, size_t n, const char *fmt, va_list args) | 55 octave_raw_vsnprintf (char *buf, size_t n, const char *fmt, va_list args) |
112 { | 56 { |