OSDN Git Service

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