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