summaryrefslogtreecommitdiff
path: root/mysys/my_fopen.c
diff options
context:
space:
mode:
authorDaniel Black <daniel@linux.vnet.ibm.com>2018-03-14 15:33:47 +1100
committerVladislav Vaintroub <wlad@mariadb.com>2018-05-21 16:34:11 +0000
commit5c81cb880a054f34803e2821489533274ebf6c4e (patch)
treed2e55ef82e57cbe0777590c8cb63a2341591d928 /mysys/my_fopen.c
parentf165077aa9b9df874a712d731e16968eb30875d4 (diff)
downloadmariadb-git-5c81cb880a054f34803e2821489533274ebf6c4e.tar.gz
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
Diffstat (limited to 'mysys/my_fopen.c')
-rw-r--r--mysys/my_fopen.c38
1 files changed, 21 insertions, 17 deletions
diff --git a/mysys/my_fopen.c b/mysys/my_fopen.c
index 213eac82040..59baeaec744 100644
--- a/mysys/my_fopen.c
+++ b/mysys/my_fopen.c
@@ -61,15 +61,13 @@ FILE *my_fopen(const char *filename, int flags, myf MyFlags)
int filedesc= my_fileno(fd);
if ((uint)filedesc >= my_file_limit)
{
- thread_safe_increment(my_stream_opened,&THR_LOCK_open);
+ statistic_increment(my_stream_opened,&THR_LOCK_open);
DBUG_RETURN(fd); /* safeguard */
}
- mysql_mutex_lock(&THR_LOCK_open);
my_file_info[filedesc].name= (char*) my_strdup(filename,MyFlags);
- my_stream_opened++;
- my_file_total_opened++;
+ statistic_increment(my_stream_opened, &THR_LOCK_open);
+ statistic_increment(my_file_total_opened, &THR_LOCK_open);
my_file_info[filedesc].type= STREAM_BY_FOPEN;
- mysql_mutex_unlock(&THR_LOCK_open);
DBUG_PRINT("exit",("stream: %p", fd));
DBUG_RETURN(fd);
}
@@ -161,13 +159,22 @@ FILE *my_freopen(const char *path, const char *mode, FILE *stream)
int my_fclose(FILE *fd, myf MyFlags)
{
int err,file;
+ char *name= NULL;
DBUG_ENTER("my_fclose");
DBUG_PRINT("my",("stream: %p MyFlags: %lu", fd, MyFlags));
- mysql_mutex_lock(&THR_LOCK_open);
file= my_fileno(fd);
+ if ((uint) file < my_file_limit && my_file_info[file].type != UNOPEN)
+ {
+ name= my_file_info[file].name;
+ my_file_info[file].name= NULL;
+ my_file_info[file].type= UNOPEN;
+ }
#ifndef _WIN32
- err= fclose(fd);
+ do
+ {
+ err= fclose(fd);
+ } while (err == -1 && errno == EINTR);
#else
err= my_win_fclose(fd);
#endif
@@ -176,16 +183,15 @@ int my_fclose(FILE *fd, myf MyFlags)
my_errno=errno;
if (MyFlags & (MY_FAE | MY_WME))
my_error(EE_BADCLOSE, MYF(ME_BELL+ME_WAITTANG),
- my_filename(file),errno);
+ name,errno);
}
else
- my_stream_opened--;
- if ((uint) file < my_file_limit && my_file_info[file].type != UNOPEN)
+ statistic_decrement(my_stream_opened, &THR_LOCK_open);
+
+ if (name)
{
- my_file_info[file].type = UNOPEN;
- my_free(my_file_info[file].name);
+ my_free(name);
}
- mysql_mutex_unlock(&THR_LOCK_open);
DBUG_RETURN(err);
} /* my_fclose */
@@ -215,13 +221,12 @@ FILE *my_fdopen(File Filedes, const char *name, int Flags, myf MyFlags)
}
else
{
- mysql_mutex_lock(&THR_LOCK_open);
- my_stream_opened++;
+ statistic_increment(my_stream_opened, &THR_LOCK_open);
if ((uint) Filedes < (uint) my_file_limit)
{
if (my_file_info[Filedes].type != UNOPEN)
{
- my_file_opened--; /* File is opened with my_open ! */
+ statistic_decrement(my_file_opened, &THR_LOCK_open); /* File is opened with my_open ! */
}
else
{
@@ -229,7 +234,6 @@ FILE *my_fdopen(File Filedes, const char *name, int Flags, myf MyFlags)
}
my_file_info[Filedes].type = STREAM_BY_FDOPEN;
}
- mysql_mutex_unlock(&THR_LOCK_open);
}
DBUG_PRINT("exit",("stream: %p", fd));