xapian-core  2.0.0
safeunistd.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2007,2015 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see
18  * <https://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef XAPIAN_INCLUDED_SAFEUNISTD_H
22 #define XAPIAN_INCLUDED_SAFEUNISTD_H
23 
24 #ifndef PACKAGE
25 # error config.h must be included first in each C++ source file
26 #endif
27 
28 #ifndef _MSC_VER
29 # include <unistd.h>
30 #else
31 
32 // io.h is the nearest equivalent to unistd.h.
33 # include <io.h>
34 
35 // process.h is needed for getpid().
36 # include <process.h>
37 
38 // direct.h is needed for rmdir().
39 # include <direct.h>
40 
41 #endif
42 
43 // Under mingw we probably don't need to provide our own sleep().
44 #if defined __WIN32__ && !defined HAVE_SLEEP
45 
46 inline unsigned int
47 sleep(unsigned int seconds)
48 {
49  // Use our own little helper function to avoid pulling in <windows.h>.
50  extern void xapian_sleep_milliseconds(unsigned int millisecs);
51 
52  // Sleep takes a time interval in milliseconds, whereas POSIX sleep takes
53  // a time interval in seconds, so we need to multiply 'seconds' by 1000.
54  //
55  // But make sure the multiplication won't overflow! 4294967 seconds is
56  // nearly 50 days, so just sleep for that long and return the number of
57  // seconds left to sleep for. The common case of sleep(CONSTANT) should
58  // optimise to just xapian_sleep_milliseconds(CONSTANT).
59  if (seconds > 4294967u) {
60  xapian_sleep_milliseconds(4294967000u);
61  return seconds - 4294967u;
62  }
63  xapian_sleep_milliseconds(seconds * 1000u);
64  return 0;
65 }
66 
67 #endif
68 
69 #endif /* XAPIAN_INCLUDED_SAFEUNISTD_H */
void sleep(double t)
Sleep until the time represented by this object.
Definition: realtime.h:127