Skip to content

Commit 0322ced

Browse files
grooverdanvuvova
authored andcommitted
MDEV-15635 mysys: THR_LOCK_open reduce usage
Change the following to statistic counters: * my_file_opened * my_file_total_opened * my_stream_opened * my_tmp_file_created There is one non-statistics use of my_file_opened/my_stream_opened in my_end which prints a warning if we shutdown and its still open. It seems excessive to hold locks to prevent this warning. A file descriptor is already a unique element per process - in Windows, protection occurs at fd allocation using THR_LOCK_open in my_win_{,f}open and in other OSes, a unique fd to file map exists at the OS level. So accesses to my_file_info[fd] don't need to be protected by the THR_LOCK_open. my_close/my_fclose where restructured to clear out the my_file_info before the close/my_win_close/my_win_fclose. After these calls another thread could gain the same file descriptor. So for Windows this the file_info elements available to the my_win_{,f}_open are released during the invalidate_fd call within my_win_close. No locking is needed as the my_win_{,f}open is searching for a invalidate entry which is determined by a single value change. my_fclose also changed for non-Windows to retry closing if EINTR was returned, same as my_close. Closes #657
1 parent 6ed6a04 commit 0322ced

File tree

4 files changed

+38
-32
lines changed

4 files changed

+38
-32
lines changed

mysys/mf_tempfile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,6 @@ File create_temp_file(char *to, const char *dir, const char *prefix,
145145
#error No implementation found for create_temp_file
146146
#endif
147147
if (file >= 0)
148-
thread_safe_increment(my_tmp_file_created,&THR_LOCK_open);
148+
statistic_increment(my_tmp_file_created,&THR_LOCK_open);
149149
DBUG_RETURN(file);
150150
}

mysys/my_fopen.c

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,13 @@ FILE *my_fopen(const char *filename, int flags, myf MyFlags)
6161
int filedesc= my_fileno(fd);
6262
if ((uint)filedesc >= my_file_limit)
6363
{
64-
thread_safe_increment(my_stream_opened,&THR_LOCK_open);
64+
statistic_increment(my_stream_opened,&THR_LOCK_open);
6565
DBUG_RETURN(fd); /* safeguard */
6666
}
67-
mysql_mutex_lock(&THR_LOCK_open);
6867
my_file_info[filedesc].name= (char*) my_strdup(filename,MyFlags);
69-
my_stream_opened++;
70-
my_file_total_opened++;
68+
statistic_increment(my_stream_opened, &THR_LOCK_open);
69+
statistic_increment(my_file_total_opened, &THR_LOCK_open);
7170
my_file_info[filedesc].type= STREAM_BY_FOPEN;
72-
mysql_mutex_unlock(&THR_LOCK_open);
7371
DBUG_PRINT("exit",("stream: %p", fd));
7472
DBUG_RETURN(fd);
7573
}
@@ -161,13 +159,22 @@ FILE *my_freopen(const char *path, const char *mode, FILE *stream)
161159
int my_fclose(FILE *fd, myf MyFlags)
162160
{
163161
int err,file;
162+
char *name= NULL;
164163
DBUG_ENTER("my_fclose");
165164
DBUG_PRINT("my",("stream: %p MyFlags: %lu", fd, MyFlags));
166165

167-
mysql_mutex_lock(&THR_LOCK_open);
168166
file= my_fileno(fd);
167+
if ((uint) file < my_file_limit && my_file_info[file].type != UNOPEN)
168+
{
169+
name= my_file_info[file].name;
170+
my_file_info[file].name= NULL;
171+
my_file_info[file].type= UNOPEN;
172+
}
169173
#ifndef _WIN32
170-
err= fclose(fd);
174+
do
175+
{
176+
err= fclose(fd);
177+
} while (err == -1 && errno == EINTR);
171178
#else
172179
err= my_win_fclose(fd);
173180
#endif
@@ -176,16 +183,15 @@ int my_fclose(FILE *fd, myf MyFlags)
176183
my_errno=errno;
177184
if (MyFlags & (MY_FAE | MY_WME))
178185
my_error(EE_BADCLOSE, MYF(ME_BELL+ME_WAITTANG),
179-
my_filename(file),errno);
186+
name,errno);
180187
}
181188
else
182-
my_stream_opened--;
183-
if ((uint) file < my_file_limit && my_file_info[file].type != UNOPEN)
189+
statistic_decrement(my_stream_opened, &THR_LOCK_open);
190+
191+
if (name)
184192
{
185-
my_file_info[file].type = UNOPEN;
186-
my_free(my_file_info[file].name);
193+
my_free(name);
187194
}
188-
mysql_mutex_unlock(&THR_LOCK_open);
189195
DBUG_RETURN(err);
190196
} /* my_fclose */
191197

@@ -215,21 +221,19 @@ FILE *my_fdopen(File Filedes, const char *name, int Flags, myf MyFlags)
215221
}
216222
else
217223
{
218-
mysql_mutex_lock(&THR_LOCK_open);
219-
my_stream_opened++;
224+
statistic_increment(my_stream_opened, &THR_LOCK_open);
220225
if ((uint) Filedes < (uint) my_file_limit)
221226
{
222227
if (my_file_info[Filedes].type != UNOPEN)
223228
{
224-
my_file_opened--; /* File is opened with my_open ! */
229+
statistic_decrement(my_file_opened, &THR_LOCK_open); /* File is opened with my_open ! */
225230
}
226231
else
227232
{
228233
my_file_info[Filedes].name= my_strdup(name,MyFlags);
229234
}
230235
my_file_info[Filedes].type = STREAM_BY_FDOPEN;
231236
}
232-
mysql_mutex_unlock(&THR_LOCK_open);
233237
}
234238

235239
DBUG_PRINT("exit",("stream: %p", fd));

mysys/my_open.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,18 @@ File my_open(const char *FileName, int Flags, myf MyFlags)
7676
int my_close(File fd, myf MyFlags)
7777
{
7878
int err;
79+
char *name= NULL;
7980
DBUG_ENTER("my_close");
8081
DBUG_PRINT("my",("fd: %d MyFlags: %lu",fd, MyFlags));
8182
if (!(MyFlags & (MY_WME | MY_FAE)))
8283
MyFlags|= my_global_flags;
8384

84-
mysql_mutex_lock(&THR_LOCK_open);
85+
if ((uint) fd < my_file_limit && my_file_info[fd].type != UNOPEN)
86+
{
87+
name= my_file_info[fd].name;
88+
my_file_info[fd].name= NULL;
89+
my_file_info[fd].type= UNOPEN;
90+
}
8591
#ifndef _WIN32
8692
do
8793
{
@@ -96,15 +102,13 @@ int my_close(File fd, myf MyFlags)
96102
my_errno=errno;
97103
if (MyFlags & (MY_FAE | MY_WME))
98104
my_error(EE_BADCLOSE, MYF(ME_BELL | ME_WAITTANG | (MyFlags & (ME_JUST_INFO | ME_NOREFRESH))),
99-
my_filename(fd),errno);
105+
name,errno);
100106
}
101-
if ((uint) fd < my_file_limit && my_file_info[fd].type != UNOPEN)
107+
if (name)
102108
{
103-
my_free(my_file_info[fd].name);
104-
my_file_info[fd].type = UNOPEN;
109+
my_free(name);
105110
}
106-
my_file_opened--;
107-
mysql_mutex_unlock(&THR_LOCK_open);
111+
statistic_decrement(my_file_opened, &THR_LOCK_open);
108112
DBUG_RETURN(err);
109113
} /* my_close */
110114

@@ -134,15 +138,13 @@ File my_register_filename(File fd, const char *FileName, enum file_type
134138
{
135139
if ((uint) fd >= my_file_limit)
136140
{
137-
thread_safe_increment(my_file_opened,&THR_LOCK_open);
141+
statistic_increment(my_file_opened,&THR_LOCK_open);
138142
DBUG_RETURN(fd); /* safeguard */
139143
}
140-
mysql_mutex_lock(&THR_LOCK_open);
141144
my_file_info[fd].name = (char*) my_strdup(FileName, MyFlags);
142-
my_file_opened++;
143-
my_file_total_opened++;
145+
statistic_increment(my_file_opened,&THR_LOCK_open);
146+
statistic_increment(my_file_total_opened,&THR_LOCK_open);
144147
my_file_info[fd].type = type_of_file;
145-
mysql_mutex_unlock(&THR_LOCK_open);
146148
DBUG_PRINT("exit",("fd: %d",fd));
147149
DBUG_RETURN(fd);
148150
}

mysys/my_winfile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ FILE *my_win_fopen(const char *filename, const char *type)
528528
{
529529
FILE *file;
530530
int flags= 0;
531-
DBUG_ENTER("my_win_open");
531+
DBUG_ENTER("my_win_fopen");
532532

533533
/*
534534
If we are not creating, then we need to use my_access to make sure
@@ -585,7 +585,7 @@ int my_win_fclose(FILE *file)
585585
{
586586
File fd;
587587

588-
DBUG_ENTER("my_win_close");
588+
DBUG_ENTER("my_win_fclose");
589589
fd= my_fileno(file);
590590
if(fd < 0)
591591
DBUG_RETURN(-1);

0 commit comments

Comments
 (0)