Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
fcleanup.c
Go to the documentation of this file.
00001 /*
00002 
00003   Copyright (C) 2000, 2001, Silicon Graphics, Inc.  All Rights Reserved.
00004 
00005   This program is free software; you can redistribute it and/or modify it
00006   under the terms of version 2.1 of the GNU Lesser General Public License 
00007   as published by the Free Software Foundation.
00008 
00009   This program is distributed in the hope that it would be useful, but
00010   WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013   Further, this software is distributed without any warranty that it is
00014   free of the rightful claim of any third person regarding infringement 
00015   or the like.  Any license provided herein, whether implied or 
00016   otherwise, applies only to this software file.  Patent licenses, if
00017   any, provided herein do not apply to combinations of this program with 
00018   other software, or any other product whatsoever.  
00019 
00020   You should have received a copy of the GNU Lesser General Public 
00021   License along with this program; if not, write the Free Software 
00022   Foundation, Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, 
00023   USA.
00024 
00025   Contact information:  Silicon Graphics, Inc., 1600 Amphitheatre Pky,
00026   Mountain View, CA 94043, or:
00027 
00028   http://www.sgi.com
00029 
00030   For further information regarding this notice, see:
00031 
00032   http://oss.sgi.com/projects/GenInfo/NoticeExplan
00033 
00034 */
00035 
00036 
00037 
00038 #pragma ident "@(#) libf/fio/fcleanup.c 92.1    06/18/99 16:08:47"
00039 
00040 #include <errno.h>
00041 #include <foreign.h>
00042 #ifndef _ABSOFT
00043 #include <stdlib.h>
00044 #endif
00045 #include <liberrno.h>
00046 #include "fio.h"
00047 
00048 int _print_statistics;
00049 
00050 /*
00051  *      _fcleanup - closes all connected Fortran units except 100, 101,
00052  *      and 102.
00053  *
00054  *      This routine aborts when errors are detected after printing error 
00055  *      messages for each error encountered.
00056  *
00057  *      This routine is called from exit() after all other active tasks have
00058  *      been terminated.
00059  */
00060 
00061 void
00062 _fcleanup(void)
00063 {
00064         register int    ret;
00065         register short  errflag;
00066         static short    pass = 0; /* incremented when _fcleanup is called */
00067         unit            *uptr;
00068 
00069         if (pass++ >= 1) return;
00070 
00071         errflag = 0; 
00072 
00073 /*
00074  *      Find all open Fortran units not connected by WOPEN/OPENMS/OPENDR/AQOPEN
00075  *      and close them.
00076  */
00077         uptr    = _get_next_unit(NULL, 0, 0);
00078 
00079         while (uptr != NULL) {     /* while more open units */
00080                 register unum_t unum;
00081                 
00082                 unum    = uptr->uid;
00083                 
00084                 if (OPEN_UPTR(uptr) && uptr->ufs != FS_AUX) {
00085                         ret     = _unit_close(uptr, CLST_UNSPEC, NULL);
00086                         if (ret != 0) {
00087                                 char    msgbuf[80]; 
00088 
00089                                 if (!_is_file_name(uptr->uid)) {
00090                                         sprintf(msgbuf,
00091                     "FATAL error closing unit %lld during program termination",
00092                                                 unum);
00093                                 }
00094                                 else {
00095                                         sprintf(msgbuf,
00096              "FATAL error closing a Hollerith unit during program termination");
00097                                 }
00098                                 _lmessage(ret, msgbuf, (va_list) NULL);
00099                                 errflag = 1;
00100                         }
00101                 }
00102                 uptr    = _get_next_unit(uptr, 0, 0);
00103         }
00104 
00105 /*
00106  *      Flush C files here for two reasons:
00107  *              1) On Solaris, the C cleanup routine will not be executed if
00108  *                 the code is loaded using the f90 command.   So Fortran 
00109  *                 termination processing must flush stdout and any user C 
00110  *                 files in addition to the Fortran files.
00111  *              2) When job-end "mtused" statistics are requested, the accuracy
00112  *                 of the statistics is improved by flushing as many files 
00113  *                 as possible.
00114  */      
00115         if (fflush(NULL) == EOF)
00116                 errflag = 1;
00117 
00118         if (errflag)
00119                 (void) abort();
00120 
00121 #ifdef  _CRAY1
00122         if (_print_statistics)
00123                 _mtcpu();
00124 #endif
00125 
00126         return;
00127 }
00128 
00129 #if     defined(__mips) || defined(_LITTLE_ENDIAN)
00130 /* On Irix 6.2 and 6.5, we see that _cleanup, the libc stdio cleanup routine,
00131  * is being called by each thread as it exits.  But, it is unlocked. 
00132  * Also, _cleanup is called before any of the routines registered through
00133  * atexit() are called.
00134  * So, we can have 2 or more threads in there at once, and we can get
00135  * duplicated output.  This routine, which is called at each thread's
00136  * exit before _cleanup, does an fflush(NULL).
00137  * Note that there are still possible problems:
00138  *  It's possible that another thread could already be in a stdio routine
00139  *  when this routine is called.
00140  */
00141 
00142 plock_t _fclock;
00143 void
00144 _fortclean(void)
00145 {
00146         static volatile int beenhere = 0;
00147 
00148         MEM_LOCK(&_fclock);
00149 
00150         if (beenhere) {
00151                 MEM_UNLOCK(&_fclock);
00152                 return;
00153         }
00154 
00155         beenhere++;
00156         fflush(NULL);
00157         MEM_UNLOCK(&_fclock);
00158 
00159         return;
00160 }
00161 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines