From 7e22cc38bbb415a65c9b852316cd6b4c2d1edd4b Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 13 Apr 2010 15:36:56 +0000 Subject: [PATCH] 2010-04-14 Tatsuhiro Tsujikawa Implemented clock_gettime() using timeGetTime in mingw. * configure.ac * src/Makefile.am * src/a2time.h * src/clock_gettime_mingw.cc * src/clock_gettime_mingw.h * src/timespec.h * test/Makefile.am --- ChangeLog | 11 ++++++++ config.h.in | 6 ++++ configure | 44 +++++++++++++++++++++++++---- configure.ac | 19 ++++++++++--- src/Makefile.am | 6 +++- src/Makefile.in | 17 +++++++---- src/a2time.h | 10 ++++--- src/clock_gettime_mingw.cc | 58 ++++++++++++++++++++++++++++++++++++++ src/clock_gettime_mingw.h | 41 +++++++++++++++++++++++++++ src/timespec.h | 43 ++++++++++++++++++++++++++++ test/Makefile.am | 2 +- test/Makefile.in | 2 +- 12 files changed, 237 insertions(+), 22 deletions(-) create mode 100644 src/clock_gettime_mingw.cc create mode 100644 src/clock_gettime_mingw.h create mode 100644 src/timespec.h diff --git a/ChangeLog b/ChangeLog index 6e8ce7c7..054e74c8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2010-04-14 Tatsuhiro Tsujikawa + + Implemented clock_gettime() using timeGetTime in mingw. + * configure.ac + * src/Makefile.am + * src/a2time.h + * src/clock_gettime_mingw.cc + * src/clock_gettime_mingw.h + * src/timespec.h + * test/Makefile.am + 2010-04-14 Tatsuhiro Tsujikawa Ensure that std::ofstream is destroyed before renaming. diff --git a/config.h.in b/config.h.in index c248efbe..8fcec06e 100644 --- a/config.h.in +++ b/config.h.in @@ -277,6 +277,9 @@ /* Define to 1 if you have a working `mmap' system call. */ #undef HAVE_MMAP +/* Define to 1 if you have the header file. */ +#undef HAVE_MMSYSTEM_H + /* Define to 1 if you have the `munmap' function. */ #undef HAVE_MUNMAP @@ -500,6 +503,9 @@ /* Define to 1 if you have the `wcslen' function. */ #undef HAVE_WCSLEN +/* Define to 1 if you have the header file. */ +#undef HAVE_WINDOWS_H + /* Define to 1 if you have the header file. */ #undef HAVE_WINSOCK2_H diff --git a/configure b/configure index eaf68eeb..87814249 100755 --- a/configure +++ b/configure @@ -600,6 +600,8 @@ ac_func_list= ac_subst_vars='am__EXEEXT_FALSE am__EXEEXT_TRUE LTLIBOBJS +HAVE_TIMEGETTIME_FALSE +HAVE_TIMEGETTIME_TRUE HAVE_DAEMON_FALSE HAVE_DAEMON_TRUE HAVE_TIMEGM_FALSE @@ -3579,8 +3581,9 @@ ac_config_headers="$ac_config_headers config.h" case "$target" in *mingw*|*cygwin*) - WINSOCK_LIBS="-lws2_32 -lpthread" + WINSOCK_LIBS="-lws2_32" + LIBS="-lws2_32 -lcrypto -lwsock32 -lgdi32 -lwinmm $LIBS" ;; esac @@ -7855,11 +7858,17 @@ fi case "$target" in *mingw*) - for ac_header in winsock2.h \ - ws2tcpip.h + for ac_header in windows.h \ + winsock2.h \ + ws2tcpip.h \ + mmsystem.h do : as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh` -ac_fn_cxx_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default" +ac_fn_cxx_check_header_compile "$LINENO" "$ac_header" "$as_ac_Header" "#ifdef HAVE_WINDOWS_H +# include +#endif + +" eval as_val=\$$as_ac_Header if test "x$as_val" = x""yes; then : cat >>confdefs.h <<_ACEOF @@ -14623,7 +14632,6 @@ for ac_func in __argz_count \ __argz_next \ __argz_stringify \ atexit \ - clock_gettime \ ftruncate \ getcwd \ gethostbyaddr \ @@ -14984,6 +14992,18 @@ else fi +for ac_func in clock_gettime +do : + ac_fn_cxx_check_func "$LINENO" "clock_gettime" "ac_cv_func_clock_gettime" +if test "x$ac_cv_func_clock_gettime" = x""yes; then : + cat >>confdefs.h <<_ACEOF +#define HAVE_CLOCK_GETTIME 1 +_ACEOF + have_clock_gettime=yes +fi +done + + case "$target" in *mingw*) if true; then @@ -15002,6 +15022,16 @@ else HAVE_GAI_STRERROR_FALSE= fi + if test "x$have_clock_gettime" != "xyes"; then + if true; then + HAVE_TIMEGETTIME_TRUE= + HAVE_TIMEGETTIME_FALSE='#' +else + HAVE_TIMEGETTIME_TRUE='#' + HAVE_TIMEGETTIME_FALSE= +fi + + fi ;; esac @@ -15339,6 +15369,10 @@ if test -z "${HAVE_GAI_STRERROR_TRUE}" && test -z "${HAVE_GAI_STRERROR_FALSE}"; as_fn_error "conditional \"HAVE_GAI_STRERROR\" was never defined. Usually this means the macro was only invoked conditionally." "$LINENO" 5 fi +if test -z "${HAVE_TIMEGETTIME_TRUE}" && test -z "${HAVE_TIMEGETTIME_FALSE}"; then + as_fn_error "conditional \"HAVE_TIMEGETTIME\" was never defined. +Usually this means the macro was only invoked conditionally." "$LINENO" 5 +fi : ${CONFIG_STATUS=./config.status} ac_write_fail=0 diff --git a/configure.ac b/configure.ac index 8d37fb09..025732ff 100644 --- a/configure.ac +++ b/configure.ac @@ -12,8 +12,9 @@ AC_CONFIG_HEADERS([config.h]) case "$target" in *mingw*|*cygwin*) - WINSOCK_LIBS="-lws2_32 -lpthread" + WINSOCK_LIBS="-lws2_32" AC_SUBST(WINSOCK_LIBS) + LIBS="-lws2_32 -lcrypto -lwsock32 -lgdi32 -lwinmm $LIBS" ;; esac @@ -222,8 +223,14 @@ AC_HEADER_STDC case "$target" in *mingw*) - AC_CHECK_HEADERS([winsock2.h \ - ws2tcpip.h]) + AC_CHECK_HEADERS([windows.h \ + winsock2.h \ + ws2tcpip.h \ + mmsystem.h], [], [], +[[#ifdef HAVE_WINDOWS_H +# include +#endif +]]) ;; esac @@ -299,7 +306,6 @@ AC_CHECK_FUNCS([__argz_count \ __argz_next \ __argz_stringify \ atexit \ - clock_gettime \ ftruncate \ getcwd \ gethostbyaddr \ @@ -379,12 +385,17 @@ AC_CHECK_FUNCS([timegm], AC_CHECK_FUNCS([daemon], [have_daemon=yes]) AM_CONDITIONAL([HAVE_DAEMON], [test "x$have_daemon" = "xyes"]) +AC_CHECK_FUNCS([clock_gettime], [have_clock_gettime=yes]) + case "$target" in *mingw*) dnl defined in ws2tcpip.h, but only if _WIN32_WINNT >= 0x0501 AM_CONDITIONAL([HAVE_GETADDRINFO], true) dnl defined in ws2tcpip.h, but missing in C:\mingw\lib\libws2_32.a AM_CONDITIONAL([HAVE_GAI_STRERROR], false) + if test "x$have_clock_gettime" != "xyes"; then + AM_CONDITIONAL([HAVE_TIMEGETTIME], true) + fi ;; esac diff --git a/src/Makefile.am b/src/Makefile.am index d1d89de4..581efbef 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -517,11 +517,15 @@ if !HAVE_DAEMON SRCS += daemon.cc daemon.h endif # !HAVE_DAEMON +if HAVE_TIMEGETTIME +SRCS += clock_gettime_mingw.cc clock_gettime_mingw.h +endif # !HAVE_TIMEGETTIME + noinst_LIBRARIES = libaria2c.a libaria2c_a_SOURCES = $(SRCS) aria2c_LDADD = libaria2c.a @LIBINTL@ @ALLOCA@ @LIBGNUTLS_LIBS@\ @LIBGCRYPT_LIBS@ @OPENSSL_LIBS@ @XML_LIBS@\ - @LIBCARES_LIBS@ @WINSOCK_LIBS@ @LIBEXPAT_LIBS@ @LIBZ_LIBS@\ + @LIBCARES_LIBS@ @LIBEXPAT_LIBS@ @LIBZ_LIBS@\ @SQLITE3_LIBS@ #-lprofiler #aria2c_LDFLAGS = -pg AM_CPPFLAGS = -Wall\ diff --git a/src/Makefile.in b/src/Makefile.in index 858c0658..1d65ced3 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -277,6 +277,7 @@ bin_PROGRAMS = aria2c$(EXEEXT) @HAVE_STRPTIME_FALSE@am__append_24 = strptime.c strptime.h @HAVE_TIMEGM_FALSE@am__append_25 = timegm.c timegm.h @HAVE_DAEMON_FALSE@am__append_26 = daemon.cc daemon.h +@HAVE_TIMEGETTIME_TRUE@am__append_27 = clock_gettime_mingw.cc clock_gettime_mingw.h subdir = src DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in alloca.c ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 @@ -602,7 +603,8 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \ libgen.h getaddrinfo.c getaddrinfo.h gai_strerror.c \ gai_strerror.h gettimeofday.c gettimeofday.h inet_aton.c \ inet_aton.h localtime_r.c localtime_r.h strptime.c strptime.h \ - timegm.c timegm.h daemon.cc daemon.h + timegm.c timegm.h daemon.cc daemon.h clock_gettime_mingw.cc \ + clock_gettime_mingw.h @ENABLE_XML_RPC_TRUE@am__objects_1 = \ @ENABLE_XML_RPC_TRUE@ XmlRpcRequestParserController.$(OBJEXT) \ @ENABLE_XML_RPC_TRUE@ XmlRpcRequestParserStateMachine.$(OBJEXT) \ @@ -780,7 +782,8 @@ am__objects_6 = @HAVE_STRPTIME_FALSE@am__objects_24 = strptime.$(OBJEXT) @HAVE_TIMEGM_FALSE@am__objects_25 = timegm.$(OBJEXT) @HAVE_DAEMON_FALSE@am__objects_26 = daemon.$(OBJEXT) -am__objects_27 = SocketCore.$(OBJEXT) Command.$(OBJEXT) \ +@HAVE_TIMEGETTIME_TRUE@am__objects_27 = clock_gettime_mingw.$(OBJEXT) +am__objects_28 = SocketCore.$(OBJEXT) Command.$(OBJEXT) \ AbstractCommand.$(OBJEXT) \ InitiateConnectionCommandFactory.$(OBJEXT) \ DownloadCommand.$(OBJEXT) \ @@ -866,8 +869,9 @@ am__objects_27 = SocketCore.$(OBJEXT) Command.$(OBJEXT) \ $(am__objects_15) $(am__objects_16) $(am__objects_17) \ $(am__objects_18) $(am__objects_19) $(am__objects_20) \ $(am__objects_21) $(am__objects_22) $(am__objects_23) \ - $(am__objects_24) $(am__objects_25) $(am__objects_26) -am_libaria2c_a_OBJECTS = $(am__objects_27) + $(am__objects_24) $(am__objects_25) $(am__objects_26) \ + $(am__objects_27) +am_libaria2c_a_OBJECTS = $(am__objects_28) libaria2c_a_OBJECTS = $(am_libaria2c_a_OBJECTS) am__installdirs = "$(DESTDIR)$(bindir)" PROGRAMS = $(bin_PROGRAMS) @@ -1202,12 +1206,12 @@ SRCS = Socket.h SocketCore.cc SocketCore.h BinaryStream.h Command.cc \ $(am__append_16) $(am__append_17) $(am__append_18) \ $(am__append_19) $(am__append_20) $(am__append_21) \ $(am__append_22) $(am__append_23) $(am__append_24) \ - $(am__append_25) $(am__append_26) + $(am__append_25) $(am__append_26) $(am__append_27) noinst_LIBRARIES = libaria2c.a libaria2c_a_SOURCES = $(SRCS) aria2c_LDADD = libaria2c.a @LIBINTL@ @ALLOCA@ @LIBGNUTLS_LIBS@\ @LIBGCRYPT_LIBS@ @OPENSSL_LIBS@ @XML_LIBS@\ - @LIBCARES_LIBS@ @WINSOCK_LIBS@ @LIBEXPAT_LIBS@ @LIBZ_LIBS@\ + @LIBCARES_LIBS@ @LIBEXPAT_LIBS@ @LIBZ_LIBS@\ @SQLITE3_LIBS@ #-lprofiler #aria2c_LDFLAGS = -pg @@ -1604,6 +1608,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/bencode.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/bitfield.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/bittorrent_helper.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/clock_gettime_mingw.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/daemon.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/download_helper.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gai_strerror.Po@am__quote@ diff --git a/src/a2time.h b/src/a2time.h index f8f355bc..093b2ed3 100644 --- a/src/a2time.h +++ b/src/a2time.h @@ -64,11 +64,13 @@ #ifndef CLOCK_MONOTONIC # define CLOCK_MONOTONIC 0 #endif // !CLOCK_MONOTONIC -#ifndef HAVE_STRUCT_TIMESPEC -struct timespec { time_t tv_sec; long tv_nsec; }; -#endif // !HAVE_STRUCT_TIMESPEC #ifndef HAVE_CLOCK_GETTIME -# define clock_gettime(ID, TP) (-1) +# ifdef __MINGW32__ +# include "clock_gettime_mingw.h" +# else // !__MINGW32__ +# include "timespec.h" +# define clock_gettime(ID, TP) (-1) +# endif // !__MINGW32__ #endif // !HAVE_CLOCK_GETTIME #endif // _D_A2TIME_H_ diff --git a/src/clock_gettime_mingw.cc b/src/clock_gettime_mingw.cc new file mode 100644 index 00000000..5d9a40a5 --- /dev/null +++ b/src/clock_gettime_mingw.cc @@ -0,0 +1,58 @@ +/* + * aria2 - The high speed download utility + * + * Copyright (C) 2010 Tatsuhiro Tsujikawa + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * In addition, as a special exception, the copyright holders give + * permission to link the code of portions of this program with the + * OpenSSL library under certain conditions as described in each + * individual source file, and distribute linked combinations + * including the two. + * You must obey the GNU General Public License in all respects + * for all of the code used other than OpenSSL. If you modify + * file(s) with this exception, you may extend this exception to your + * version of the file(s), but you are not obligated to do so. If you + * do not wish to do so, delete this exception statement from your + * version. If you delete this exception statement from all source + * files in the program, then also delete it here. + */ +/* copyright --> */ +#include "clock_gettime_mingw.h" + +#include +#include + +int clock_gettime(int dummyid, struct timespec* tp) +{ + timeBeginPeriod(1); + static DWORD lasttime = timeGetTime(); + timeEndPeriod(1); + static struct timespec monotime = {2678400, 0}; // 1month offset(24*3600*31) + timeBeginPeriod(1); + DWORD now = timeGetTime(); + timeEndPeriod(1); + DWORD elapsed = now-lasttime; + monotime.tv_sec += elapsed/1000; + monotime.tv_nsec += elapsed%1000*1000000; + if(monotime.tv_nsec >= 1000000000) { + monotime.tv_sec += monotime.tv_nsec/1000000000; + monotime.tv_nsec %= 1000000000; + } + lasttime = now; + *tp = monotime; + return 0; +} diff --git a/src/clock_gettime_mingw.h b/src/clock_gettime_mingw.h new file mode 100644 index 00000000..1b1f4815 --- /dev/null +++ b/src/clock_gettime_mingw.h @@ -0,0 +1,41 @@ +/* + * aria2 - The high speed download utility + * + * Copyright (C) 2010 Tatsuhiro Tsujikawa + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * In addition, as a special exception, the copyright holders give + * permission to link the code of portions of this program with the + * OpenSSL library under certain conditions as described in each + * individual source file, and distribute linked combinations + * including the two. + * You must obey the GNU General Public License in all respects + * for all of the code used other than OpenSSL. If you modify + * file(s) with this exception, you may extend this exception to your + * version of the file(s), but you are not obligated to do so. If you + * do not wish to do so, delete this exception statement from your + * version. If you delete this exception statement from all source + * files in the program, then also delete it here. + */ +/* copyright --> */ +#ifndef _D_CLOCK_GETTIME_MINGW_H_ +#define _D_CLOCK_GETTIME_MINGW_H_ + +#include "timespec.h" + +int clock_gettime(int dummyid, struct timespec* tp); + +#endif // _D_CLOCK_GETTIME_MINGW_H_ diff --git a/src/timespec.h b/src/timespec.h new file mode 100644 index 00000000..f152d064 --- /dev/null +++ b/src/timespec.h @@ -0,0 +1,43 @@ +/* + * aria2 - The high speed download utility + * + * Copyright (C) 2010 Tatsuhiro Tsujikawa + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * In addition, as a special exception, the copyright holders give + * permission to link the code of portions of this program with the + * OpenSSL library under certain conditions as described in each + * individual source file, and distribute linked combinations + * including the two. + * You must obey the GNU General Public License in all respects + * for all of the code used other than OpenSSL. If you modify + * file(s) with this exception, you may extend this exception to your + * version of the file(s), but you are not obligated to do so. If you + * do not wish to do so, delete this exception statement from your + * version. If you delete this exception statement from all source + * files in the program, then also delete it here. + */ +/* copyright --> */ +#ifndef _D_TIMESPEC_H_ +#define _D_TIMESPEC_H_ + +#include + +#ifndef HAVE_STRUCT_TIMESPEC +struct timespec { time_t tv_sec; long tv_nsec; }; +#endif // !HAVE_STRUCT_TIMESPEC + +#endif // _D_TIMESPEC_H_ diff --git a/test/Makefile.am b/test/Makefile.am index 9020241e..9633909a 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -213,7 +213,7 @@ endif # ENABLE_METALINK aria2c_LDADD = ../src/libaria2c.a\ @LIBINTL@ @LIBGNUTLS_LIBS@\ @LIBGCRYPT_LIBS@ @OPENSSL_LIBS@ @XML_LIBS@\ - @LIBCARES_LIBS@ @WINSOCK_LIBS@ @LIBEXPAT_LIBS@ @LIBZ_LIBS@\ + @LIBCARES_LIBS@ @LIBEXPAT_LIBS@ @LIBZ_LIBS@\ @SQLITE3_LIBS@\ ${CPPUNIT_LIBS} AM_CPPFLAGS = -Wall\ diff --git a/test/Makefile.in b/test/Makefile.in index cf9a40bd..42b87b28 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -651,7 +651,7 @@ aria2c_SOURCES = AllTest.cc TestUtil.cc TestUtil.h SocketCoreTest.cc \ aria2c_LDADD = ../src/libaria2c.a\ @LIBINTL@ @LIBGNUTLS_LIBS@\ @LIBGCRYPT_LIBS@ @OPENSSL_LIBS@ @XML_LIBS@\ - @LIBCARES_LIBS@ @WINSOCK_LIBS@ @LIBEXPAT_LIBS@ @LIBZ_LIBS@\ + @LIBCARES_LIBS@ @LIBEXPAT_LIBS@ @LIBZ_LIBS@\ @SQLITE3_LIBS@\ ${CPPUNIT_LIBS}