OSDN Git Service

Provide more POSIX conforming sleep() functions.
[mingw/mingw-org-wsl.git] / mingwrt / include / unistd.h
1 #ifndef _UNISTD_H
2 /*
3  * unistd.h
4  *
5  * Standard header file declaring MinGW's POSIX compatibility features.
6  *
7  * $Id$
8  *
9  * Written by Rob Savoye <rob@cygnus.com>
10  * Modified by Earnie Boyd <earnie@users.sourceforge.net>
11  *   Danny Smith <dannysmith@users.sourceforge.net>
12  *   Ramiro Polla <ramiro@lisha.ufsc.br>
13  *   Gregory McGarry  <gregorymcgarry@users.sourceforge.net>
14  *   Keith Marshall  <keithmarshall@users.sourceforge.net>
15  * Copyright (C) 1997, 1999, 2002-2004, 2007-2009, 2014, MinGW.org Project.
16  *
17  *
18  * Permission is hereby granted, free of charge, to any person obtaining a
19  * copy of this software and associated documentation files (the "Software"),
20  * to deal in the Software without restriction, including without limitation
21  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
22  * and/or sell copies of the Software, and to permit persons to whom the
23  * Software is furnished to do so, subject to the following conditions:
24  *
25  * The above copyright notice, this permission notice, and the following
26  * disclaimer shall be included in all copies or substantial portions of
27  * the Software.
28  *
29  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
32  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF OR OTHER
35  * DEALINGS IN THE SOFTWARE.
36  *
37  */
38 #define _UNISTD_H  1
39 #pragma GCC system_header
40
41 /* All MinGW headers MUST include _mingw.h before anything else,
42  * to ensure proper initialization of feature test macros.
43  */
44 #include <_mingw.h>
45
46 /* unistd.h maps (roughly) to io.h
47  * Other headers included by unistd.h may be selectively processed;
48  * __UNISTD_H_SOURCED__ enables such selective processing.
49  */
50 #define __UNISTD_H_SOURCED__ 1
51
52 #include <io.h>
53 #include <process.h>
54 #include <getopt.h>
55
56 /* These are also defined in stdio.h. */
57 #ifndef SEEK_SET
58 #define SEEK_SET 0
59 #endif
60
61 #ifndef SEEK_CUR
62 #define SEEK_CUR 1
63 #endif
64
65 #ifndef SEEK_END
66 #define SEEK_END 2
67 #endif
68
69 #if _POSIX_C_SOURCE
70 /*
71  * POSIX process/thread suspension functions; all are supported by a
72  * common MinGW API in libmingwex.a, providing for suspension periods
73  * ranging from mean values of ~7.5 milliseconds, (see comments below),
74  * extending up to a maximum of ~136 years.
75  *
76  * Note that, whereas POSIX supports early wake-up of any suspended
77  * process/thread, in response to a signal, this implementation makes
78  * no attempt to emulate this signalling behaviour, (since signals are
79  * not well supported by Windows); thus, unless impeded by an invalid
80  * argument, this implementation always returns an indication as if
81  * the sleeping period ran to completion.
82  */
83 _EXTERN_C _cdecl __MINGW_NOTHROW
84 int __mingw_sleep( unsigned long, unsigned long );
85
86 /* Structure timespec is mandated by POSIX, for specification of
87  * intervals with the greatest precision supported by the OS kernel.
88  * Although this allows for specification to nanosecond precision, do
89  * not be deluded into any false expectation that such short intervals
90  * can be realized on Windows; on Win9x derivatives, the metronome used
91  * by the process scheduler has a period of ~55 milliseconds, while for
92  * WinNT derivatives, the corresponding period is ~15 milliseconds; thus,
93  * the shortest intervals which can be realistically timed will range
94  * from 0..55 milliseconds on Win9x hosts, and from 0..15 ms on WinNT,
95  * with period values normally distributed around means of ~27.5 ms
96  * and ~7.5 ms, for the two system types respectively.
97  */
98 #define _FAKE_TIME_H_SOURCED    1
99 #define __need_struct_timespec  1
100 #include <parts/time.h>
101
102 _BEGIN_C_DECLS
103
104 /* The nanosleep() function provides the most general purpose API for
105  * process/thread suspension; it provides for specification of periods
106  * ranging from ~7.5 ms mean, (on WinNT derivatives; ~27.5 ms on Win9x),
107  * extending up to ~136 years, (effectively eternity).
108  */
109 _cdecl __MINGW_NOTHROW
110 int nanosleep( const struct timespec *, struct timespec * );
111
112 #ifndef __NO_INLINE__
113 __CRT_INLINE __LIBIMPL__(( FUNCTION = nanosleep ))
114 int nanosleep( const struct timespec *period, struct timespec *residual )
115 {
116   if( residual != (void *)(0) )
117     residual->tv_sec = (long long)(residual->tv_nsec = 0);
118   return __mingw_sleep((unsigned)(period->tv_sec), (period->tv_sec < 0LL)
119     ? (unsigned)(-1) : (unsigned)(period->tv_nsec));
120 }
121 #endif
122
123 /* The usleep() function, and its associated useconds_t type specifier
124  * were made obsolete in POSIX.1-2008; declared here, only for backward
125  * compatibility, its continued use is not recommended.  (It is limited
126  * to specification of suspension periods ranging from ~7.5 ms mean up
127  * to a maximum of 999,999 microseconds only).
128  */
129 typedef unsigned long useconds_t __MINGW_ATTRIB_DEPRECATED;
130 int _cdecl __MINGW_NOTHROW usleep( useconds_t )__MINGW_ATTRIB_DEPRECATED;
131
132 #ifndef __NO_INLINE__
133 __CRT_INLINE __LIBIMPL__(( FUNCTION = usleep ))
134 int usleep( useconds_t period ){ return __mingw_sleep( 0, 1000 * period ); }
135 #endif
136
137 /* The sleep() function is, perhaps, the most commonly used of all the
138  * process/thread suspension APIs; it provides support for specification
139  * of suspension periods ranging from 1 second to ~136 years.  (However,
140  * POSIX recommends limiting the maximum period to 65535 seconds, to
141  * maintain portability to platforms with only 16-bit ints).
142  */
143 unsigned _cdecl __MINGW_NOTHROW sleep( unsigned );
144
145 #ifndef __NO_INLINE__
146 __CRT_INLINE __LIBIMPL__(( FUNCTION = sleep ))
147 unsigned sleep( unsigned period ){ return __mingw_sleep( period, 0 ); }
148 #endif
149
150
151 /* POSIX ftruncate() function.
152  *
153  * Microsoft's _chsize() function is incorrectly described, on MSDN,
154  * as a preferred replacement for the POSIX chsize() function.  There
155  * never was any such POSIX function; the actual POSIX equivalent is
156  * the ftruncate() function.
157  */
158 int _cdecl ftruncate( int, off_t );
159
160 #ifndef __NO_INLINE__
161 __CRT_INLINE __JMPSTUB__(( FUNCTION = ftruncate, REMAPPED = _chsize ))
162 int ftruncate( int __fd, off_t __length ){ return _chsize( __fd, __length ); }
163 #endif
164
165 _END_C_DECLS
166
167 #endif /* _POSIX_C_SOURCE */
168
169 #undef __UNISTD_H_SOURCED__
170 #endif /* ! _UNISTD_H: $RCSfile$: end of file */