From d191fe153c29168087ae76c7d672d7fcf217fbad Mon Sep 17 00:00:00 2001 From: Teemu Murtola Date: Thu, 11 Sep 2014 05:59:11 +0300 Subject: [PATCH] Add gmx_getpid() This allows getting rid of all conditional includes and some conditional compilation in two different source files. Change-Id: I3095843a2155b087730362a4f3ef409ccf24018c --- src/gromacs/gmxlib/main.cpp | 21 ++--------- src/gromacs/random/random.c | 16 ++------- src/gromacs/utility.h | 5 ++- src/gromacs/utility/sysinfo.cpp | 62 ++++++++++++++++++++++++++++++++ src/gromacs/utility/sysinfo.h | 64 +++++++++++++++++++++++++++++++++ 5 files changed, 135 insertions(+), 33 deletions(-) create mode 100644 src/gromacs/utility/sysinfo.cpp create mode 100644 src/gromacs/utility/sysinfo.h diff --git a/src/gromacs/gmxlib/main.cpp b/src/gromacs/gmxlib/main.cpp index 9a3078310d..deea126674 100644 --- a/src/gromacs/gmxlib/main.cpp +++ b/src/gromacs/gmxlib/main.cpp @@ -46,16 +46,6 @@ #include #include -#ifdef GMX_NATIVE_WINDOWS -#include -#endif -#ifdef HAVE_SYS_TIME_H -#include -#endif -#ifdef HAVE_UNISTD_H -#include -#endif - #include "gromacs/fileio/filenm.h" #include "gromacs/fileio/gmxfio.h" #include "gromacs/legacyheaders/copyrite.h" @@ -70,6 +60,7 @@ #include "gromacs/utility/gmxmpi.h" #include "gromacs/utility/programcontext.h" #include "gromacs/utility/smalloc.h" +#include "gromacs/utility/sysinfo.h" /* The source code in this file should be thread-safe. Please keep it that way. */ @@ -243,15 +234,7 @@ void gmx_log_open(const char *lognm, const t_commrec *cr, time(&t); -#ifndef NO_GETPID -# ifdef GMX_NATIVE_WINDOWS - pid = _getpid(); -# else - pid = getpid(); -# endif -#else - pid = 0; -#endif + pid = gmx_getpid(); if (bAppendFiles) { diff --git a/src/gromacs/random/random.c b/src/gromacs/random/random.c index b8295a73d7..1e8390abe2 100644 --- a/src/gromacs/random/random.c +++ b/src/gromacs/random/random.c @@ -45,17 +45,11 @@ #include #include -#ifdef HAVE_UNISTD_H -#include -#endif -#ifdef GMX_NATIVE_WINDOWS -#include -#endif - #include "external/Random123-1.08/include/Random123/threefry.h" #include "gromacs/math/utilities.h" #include "gromacs/random/random_gausstable.h" +#include "gromacs/utility/sysinfo.h" #define RNG_N 624 #define RNG_M 397 @@ -237,12 +231,8 @@ gmx_rng_make_seed(void) else { /* No random device available, use time-of-day and process id */ -#ifdef GMX_NATIVE_WINDOWS - my_pid = (long)_getpid(); -#else - my_pid = (long)getpid(); -#endif - data = (unsigned int)(((long)time(NULL)+my_pid) % (long)1000000); + my_pid = gmx_getpid(); + data = (unsigned int)(((long)time(NULL)+my_pid) % (long)1000000); } return data; } diff --git a/src/gromacs/utility.h b/src/gromacs/utility.h index a25f70cb10..c0bdd6a91c 100644 --- a/src/gromacs/utility.h +++ b/src/gromacs/utility.h @@ -112,7 +112,7 @@ *

Other Functionality

* * The header init.h declares gmx::init() and gmx::finalize() for initializing - * and deinitializing the Gromacs library. + * and deinitializing the \Gromacs library. * * The header arrayref.h implements a gmx::ConstArrayRef class for exposing a * C array or part of a std::vector (basically, any continuous stretch of @@ -133,6 +133,9 @@ * The header messagestringcollector.h declares a gmx::MessageStringCollector * class for composing messages with context information. * + * The header sysinfo.h declares gmx_getpid() for getting the current process + * id. + * * The header programcontext.h declares a gmx::ProgramContextInterface that is * used to * initialize and access information about the running program, such as the diff --git a/src/gromacs/utility/sysinfo.cpp b/src/gromacs/utility/sysinfo.cpp new file mode 100644 index 0000000000..64f9ff29bf --- /dev/null +++ b/src/gromacs/utility/sysinfo.cpp @@ -0,0 +1,62 @@ +/* + * This file is part of the GROMACS molecular simulation package. + * + * Copyright (c) 2014, by the GROMACS development team, led by + * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, + * and including many others, as listed in the AUTHORS file in the + * top-level source directory and at http://www.gromacs.org. + * + * GROMACS is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 + * of the License, or (at your option) any later version. + * + * GROMACS 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with GROMACS; if not, see + * http://www.gnu.org/licenses, or write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * If you want to redistribute modifications to GROMACS, please + * consider that scientific software is very special. Version + * control is crucial - bugs must be traceable. We will be happy to + * consider code for inclusion in the official distribution, but + * derived work must not be called official GROMACS. Details are found + * in the README & COPYING files - if they are missing, get the + * official version at http://www.gromacs.org. + * + * To help us fund GROMACS development, we humbly ask that you cite + * the research papers on the package. Check out http://www.gromacs.org. + */ +/*! \internal \file + * \brief + * Implements functions from sysinfo.h. + * + * \author Teemu Murtola + * \ingroup module_utility + */ +#include "gmxpre.h" + +#include "sysinfo.h" + +#include "config.h" + +#ifdef GMX_NATIVE_WINDOWS +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif + +int gmx_getpid() +{ +#ifdef GMX_NATIVE_WINDOWS + return _getpid(); +#else + return getpid(); +#endif +} diff --git a/src/gromacs/utility/sysinfo.h b/src/gromacs/utility/sysinfo.h new file mode 100644 index 0000000000..919c7b623f --- /dev/null +++ b/src/gromacs/utility/sysinfo.h @@ -0,0 +1,64 @@ +/* + * This file is part of the GROMACS molecular simulation package. + * + * Copyright (c) 2014, by the GROMACS development team, led by + * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, + * and including many others, as listed in the AUTHORS file in the + * top-level source directory and at http://www.gromacs.org. + * + * GROMACS is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 + * of the License, or (at your option) any later version. + * + * GROMACS 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with GROMACS; if not, see + * http://www.gnu.org/licenses, or write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * If you want to redistribute modifications to GROMACS, please + * consider that scientific software is very special. Version + * control is crucial - bugs must be traceable. We will be happy to + * consider code for inclusion in the official distribution, but + * derived work must not be called official GROMACS. Details are found + * in the README & COPYING files - if they are missing, get the + * official version at http://www.gromacs.org. + * + * To help us fund GROMACS development, we humbly ask that you cite + * the research papers on the package. Check out http://www.gromacs.org. + */ +/*! \libinternal \file + * \brief + * Declares functions for obtaining information about the operating environment + * and the current process. + * + * \author Teemu Murtola + * \inlibraryapi + * \ingroup module_utility + */ +#ifndef GMX_UTILITY_SYSINFO_H +#define GMX_UTILITY_SYSINFO_H + +#ifdef __cplusplus +extern "C" { +#endif + +/*! \brief + * Returns the process ID of the current process. + * + * Does not throw. + * + * \ingroup module_utility + */ +int gmx_getpid(); + +#ifdef __cplusplus +} +#endif + +#endif -- 2.22.0