From 303d4543a0d54b9ce08715ebebce1b82436e0a3e Mon Sep 17 00:00:00 2001 From: Sander Pronk Date: Mon, 16 Aug 2010 12:09:25 +0200 Subject: [PATCH] Moved the OS-dependent part of gmx_fio_fsync() to futil.c --- include/futil.h | 4 ++++ src/gmxlib/checkpoint.c | 7 +++++-- src/gmxlib/futil.c | 49 +++++++++++++++++++++++++++++++++++++++++++++ src/gmxlib/gmxfio.c | 53 +++++-------------------------------------------- 4 files changed, 63 insertions(+), 50 deletions(-) diff --git a/include/futil.h b/include/futil.h index c8e1bd7add..16047c3392 100644 --- a/include/futil.h +++ b/include/futil.h @@ -190,6 +190,10 @@ int gmx_file_rename(const char *oldname, const char *newname); the file won't be copied if it's empty.*/ int gmx_file_copy(const char *oldname, const char *newname, bool copy_if_empty); +/* do an fsync() on an open file pointer. + Only use this during checkpointing! */ +int gmx_fsync(FILE *fp); + #ifdef __cplusplus } #endif diff --git a/src/gmxlib/checkpoint.c b/src/gmxlib/checkpoint.c index 72e08b3729..39785550f4 100644 --- a/src/gmxlib/checkpoint.c +++ b/src/gmxlib/checkpoint.c @@ -1199,9 +1199,12 @@ void write_checkpoint(const char *fn,bool bNumberAndKeep, do_cpt_footer(gmx_fio_getxdr(fp),FALSE,file_version); - /* we really, REALLY, want the checkpoint file and all files it depends - on to be physically written out do disk: */ + /* we really, REALLY, want to make sure to physically write the checkpoint, + and all the files it depends on, out to disk. Because we've + opened the checkpoint with gmx_fio_open(), it's in our list + of open files. */ ret=gmx_fio_all_output_fsync(); + if (ret) { char buf[STRLEN]; diff --git a/src/gmxlib/futil.c b/src/gmxlib/futil.c index 5b683dc094..06a5902f15 100644 --- a/src/gmxlib/futil.c +++ b/src/gmxlib/futil.c @@ -1055,3 +1055,52 @@ error: } +int gmx_fsync(FILE *fp) +{ + int rc=0; + +#ifdef GMX_FAHCORE + /* the fahcore defines its own os-independent fsync */ + rc=gmx_fsync_lowlevel(fio->fp); +#else /* GMX_FAHCORE */ + { + int fn=-1; + + /* get the file number */ +#if defined(HAVE_FILENO) + fn= fileno(fp); +#elif defined(HAVE__FILENO) + fn= _fileno(fp); +#endif + + /* do the actual fsync */ + if (fn >= 0) + { +#if (defined(HAVE_FSYNC)) + rc=fsync(fn); +#elif (defined(HAVE__COMMIT)) + rc=_commit(fno); +#endif + } + } +#endif /* GMX_FAHCORE */ + + /* We check for these error codes this way because POSIX requires them + to be defined, and using anything other than macros is unlikely: */ +#ifdef EINTR + /* we don't want to report an error just because fsync() caught a signal. + For our purposes, we can just ignore this. */ + if (rc && errno==EINTR) + rc=0; +#endif +#ifdef EINVAL + /* we don't want to report an error just because we tried to fsync() + stdout, a socket or a pipe. */ + if (rc && errno==EINVAL) + rc=0; +#endif + return rc; +} + + + diff --git a/src/gmxlib/gmxfio.c b/src/gmxlib/gmxfio.c index dbf57a1ba1..dd1de26f46 100644 --- a/src/gmxlib/gmxfio.c +++ b/src/gmxlib/gmxfio.c @@ -997,60 +997,17 @@ static int gmx_fio_int_fsync(t_fileio *fio) int rc = 0; int filen=-1; -#if ( ( (defined(HAVE_FILENO) || (defined(HAVE__FILENO) ) ) && \ - (defined(HAVE_FSYNC)) || defined(HAVE__COMMIT) ) || \ - defined(FAHCORE) ) + if (fio->fp) { -#ifdef GMX_FAHCORE - /* the fahcore defines its own os-independent fsync */ - rc=fah_fsync(fio->fp); -#elif defined(HAVE_FILENO) - filen=fileno(fio->fp); -#elif defined(HAVE__FILENO) - filen=_fileno(fio->fp); -#endif + rc=gmx_fsync(fio->fp); } - else if (fio->xdr) + else if (fio->xdr) /* this should normally not happen */ { -#ifdef GMX_FAHCORE - /* the fahcore defines its own os-independent fsync */ - rc=fah_fsync((FILE *) fio->xdr->x_private); -#elif defined(HAVE_FILENO) - filen=fileno((FILE *) fio->xdr->x_private); -#elif defined(HAVE__FILENO) - filen=_fileno((FILE *) fio->xdr->x_private); -#endif + rc=gmx_fsync((FILE*) fio->xdr->x_private); + /* ^ is this actually OK? */ } -#ifndef GMX_FAHCORE - if (filen>0) - { -#if (defined(HAVE_FSYNC)) - /* fahcore also defines HAVE_FSYNC */ - rc=fsync(filen); -#elif (defined(HAVE__COMMIT)) - rc=_commit(filen); -#endif - } -#endif - - /* We check for these error codes this way because POSIX requires them - to be defined, and using anything other than macros is unlikely: */ -#ifdef EINTR - /* we don't want to report an error just because fsync() caught a signal. - For our purposes, we can just ignore this. */ - if (rc && errno==EINTR) - rc=0; -#endif -#ifdef EINVAL - /* we don't want to report an error just because we tried to fsync() - stdout, a socket or a pipe. */ - if (rc && errno==EINVAL) - rc=0; -#endif -#endif - return rc; } -- 2.11.4.GIT