mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / storage / myisam / mi_close.c
blob4c75cc6555338712481fc5793afa939eb70a35fb
1 /*
2 Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 /* close a isam-database */
20 TODO:
21 We need to have a separate mutex on the closed file to allow other threads
22 to open other files during the time we flush the cache and close this file
25 #include "myisamdef.h"
27 int mi_close(register MI_INFO *info)
29 int error=0,flag;
30 MYISAM_SHARE *share=info->s;
31 DBUG_ENTER("mi_close");
32 DBUG_PRINT("enter",("base: 0x%lx reopen: %u locks: %u",
33 (long) info, (uint) share->reopen,
34 (uint) share->tot_locks));
36 pthread_mutex_lock(&THR_LOCK_myisam);
37 if (info->lock_type == F_EXTRA_LCK)
38 info->lock_type=F_UNLCK; /* HA_EXTRA_NO_USER_CHANGE */
40 if (info->lock_type != F_UNLCK)
42 if (mi_lock_database(info,F_UNLCK))
43 error=my_errno;
45 pthread_mutex_lock(&share->intern_lock);
47 if (share->options & HA_OPTION_READ_ONLY_DATA)
49 share->r_locks--;
50 share->tot_locks--;
52 if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
54 if (end_io_cache(&info->rec_cache))
55 error=my_errno;
56 info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
58 flag= !--share->reopen;
59 myisam_open_list=list_delete(myisam_open_list,&info->open_list);
60 pthread_mutex_unlock(&share->intern_lock);
62 my_free(mi_get_rec_buff_ptr(info, info->rec_buff), MYF(MY_ALLOW_ZERO_PTR));
63 if (flag)
65 DBUG_EXECUTE_IF("crash_before_flush_keys",
66 if (share->kfile >= 0) abort(););
67 if (share->kfile >= 0 &&
68 flush_key_blocks(share->key_cache, share->kfile,
69 share->temporary ? FLUSH_IGNORE_CHANGED :
70 FLUSH_RELEASE))
71 error=my_errno;
72 if (share->kfile >= 0)
75 If we are crashed, we can safely flush the current state as it will
76 not change the crashed state.
77 We can NOT write the state in other cases as other threads
78 may be using the file at this point
80 if (share->mode != O_RDONLY && mi_is_crashed(info))
81 mi_state_info_write(share->kfile, &share->state, 1);
82 /* Decrement open count must be last I/O on this file. */
83 _mi_decrement_open_count(info);
84 if (my_close(share->kfile,MYF(0)))
85 error = my_errno;
87 #ifdef HAVE_MMAP
88 if (share->file_map)
90 if (share->options & HA_OPTION_COMPRESS_RECORD)
91 _mi_unmap_file(info);
92 else
93 mi_munmap_file(info);
95 #endif
96 if (share->decode_trees)
98 my_free((uchar*) share->decode_trees,MYF(0));
99 my_free((uchar*) share->decode_tables,MYF(0));
101 #ifdef THREAD
102 thr_lock_delete(&share->lock);
103 VOID(pthread_mutex_destroy(&share->intern_lock));
105 int i,keys;
106 keys = share->state.header.keys;
107 VOID(rwlock_destroy(&share->mmap_lock));
108 for(i=0; i<keys; i++) {
109 VOID(rwlock_destroy(&share->key_root_lock[i]));
112 #endif
113 my_free((uchar*) info->s,MYF(0));
115 pthread_mutex_unlock(&THR_LOCK_myisam);
116 if (info->ftparser_param)
118 my_free((uchar*)info->ftparser_param, MYF(0));
119 info->ftparser_param= 0;
121 if (info->dfile >= 0 && my_close(info->dfile,MYF(0)))
122 error = my_errno;
124 myisam_log_command(MI_LOG_CLOSE,info,NULL,0,error);
125 my_free((uchar*) info,MYF(0));
127 if (error)
129 DBUG_RETURN(my_errno=error);
131 DBUG_RETURN(0);
132 } /* mi_close */