00001 /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; either version 2 of the License, or 00006 (at your option) any later version. 00007 00008 This program is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 GNU General Public License for more details. 00012 00013 You should have received a copy of the GNU General Public License 00014 along with this program; if not, write to the Free Software 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00016 00017 /* close a isam-database */ 00018 /* 00019 TODO: 00020 We need to have a separate mutex on the closed file to allow other threads 00021 to open other files during the time we flush the cache and close this file 00022 */ 00023 00024 #include "myisamdef.h" 00025 00026 int mi_close(register MI_INFO *info) 00027 { 00028 int error=0,flag; 00029 MYISAM_SHARE *share=info->s; 00030 DBUG_ENTER("mi_close"); 00031 DBUG_PRINT("enter",("base: %lx reopen: %u locks: %u", 00032 info,(uint) share->reopen, (uint) share->tot_locks)); 00033 00034 pthread_mutex_lock(&THR_LOCK_myisam); 00035 if (info->lock_type == F_EXTRA_LCK) 00036 info->lock_type=F_UNLCK; /* HA_EXTRA_NO_USER_CHANGE */ 00037 00038 if (share->reopen == 1 && share->kfile >= 0) 00039 _mi_decrement_open_count(info); 00040 00041 if (info->lock_type != F_UNLCK) 00042 { 00043 if (mi_lock_database(info,F_UNLCK)) 00044 error=my_errno; 00045 } 00046 pthread_mutex_lock(&share->intern_lock); 00047 00048 if (share->options & HA_OPTION_READ_ONLY_DATA) 00049 { 00050 share->r_locks--; 00051 share->tot_locks--; 00052 } 00053 if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED)) 00054 { 00055 if (end_io_cache(&info->rec_cache)) 00056 error=my_errno; 00057 info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED); 00058 } 00059 flag= !--share->reopen; 00060 myisam_open_list=list_delete(myisam_open_list,&info->open_list); 00061 pthread_mutex_unlock(&share->intern_lock); 00062 00063 my_free(mi_get_rec_buff_ptr(info, info->rec_buff), MYF(MY_ALLOW_ZERO_PTR)); 00064 if (flag) 00065 { 00066 if (share->kfile >= 0 && 00067 flush_key_blocks(share->key_cache, share->kfile, 00068 share->temporary ? FLUSH_IGNORE_CHANGED : 00069 FLUSH_RELEASE)) 00070 error=my_errno; 00071 if (share->kfile >= 0) 00072 { 00073 /* 00074 If we are crashed, we can safely flush the current state as it will 00075 not change the crashed state. 00076 We can NOT write the state in other cases as other threads 00077 may be using the file at this point 00078 */ 00079 if (share->mode != O_RDONLY && mi_is_crashed(info)) 00080 mi_state_info_write(share->kfile, &share->state, 1); 00081 if (my_close(share->kfile,MYF(0))) 00082 error = my_errno; 00083 } 00084 #ifdef HAVE_MMAP 00085 if (share->file_map) 00086 _mi_unmap_file(info); 00087 #endif 00088 if (share->decode_trees) 00089 { 00090 my_free((gptr) share->decode_trees,MYF(0)); 00091 my_free((gptr) share->decode_tables,MYF(0)); 00092 } 00093 #ifdef THREAD 00094 thr_lock_delete(&share->lock); 00095 VOID(pthread_mutex_destroy(&share->intern_lock)); 00096 { 00097 int i,keys; 00098 keys = share->state.header.keys; 00099 VOID(rwlock_destroy(&share->mmap_lock)); 00100 for(i=0; i<keys; i++) { 00101 VOID(rwlock_destroy(&share->key_root_lock[i])); 00102 } 00103 } 00104 #endif 00105 my_free((gptr) info->s,MYF(0)); 00106 } 00107 pthread_mutex_unlock(&THR_LOCK_myisam); 00108 if (info->ftparser_param) 00109 { 00110 my_free((gptr)info->ftparser_param, MYF(0)); 00111 info->ftparser_param= 0; 00112 } 00113 if (info->dfile >= 0 && my_close(info->dfile,MYF(0))) 00114 error = my_errno; 00115 00116 myisam_log_command(MI_LOG_CLOSE,info,NULL,0,error); 00117 my_free((gptr) info,MYF(0)); 00118 00119 if (error) 00120 { 00121 DBUG_RETURN(my_errno=error); 00122 } 00123 DBUG_RETURN(0); 00124 } /* mi_close */
1.4.7

