Commit 59785df5 authored by Daniel Black's avatar Daniel Black Committed by Vladislav Vaintroub

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
parent 20fadaac
......@@ -145,6 +145,6 @@ File create_temp_file(char *to, const char *dir, const char *prefix,
#error No implementation found for create_temp_file
#endif
if (file >= 0)
thread_safe_increment(my_tmp_file_created,&THR_LOCK_open);
statistic_increment(my_tmp_file_created,&THR_LOCK_open);
DBUG_RETURN(file);
}
......@@ -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));
......
......@@ -76,12 +76,18 @@ File my_open(const char *FileName, int Flags, myf MyFlags)
int my_close(File fd, myf MyFlags)
{
int err;
char *name= NULL;
DBUG_ENTER("my_close");
DBUG_PRINT("my",("fd: %d MyFlags: %lu",fd, MyFlags));
if (!(MyFlags & (MY_WME | MY_FAE)))
MyFlags|= my_global_flags;
mysql_mutex_lock(&THR_LOCK_open);
if ((uint) fd < my_file_limit && my_file_info[fd].type != UNOPEN)
{
name= my_file_info[fd].name;
my_file_info[fd].name= NULL;
my_file_info[fd].type= UNOPEN;
}
#ifndef _WIN32
do
{
......@@ -96,15 +102,13 @@ int my_close(File fd, myf MyFlags)
my_errno=errno;
if (MyFlags & (MY_FAE | MY_WME))
my_error(EE_BADCLOSE, MYF(ME_BELL | ME_WAITTANG | (MyFlags & (ME_JUST_INFO | ME_NOREFRESH))),
my_filename(fd),errno);
name,errno);
}
if ((uint) fd < my_file_limit && my_file_info[fd].type != UNOPEN)
if (name)
{
my_free(my_file_info[fd].name);
my_file_info[fd].type = UNOPEN;
my_free(name);
}
my_file_opened--;
mysql_mutex_unlock(&THR_LOCK_open);
statistic_decrement(my_file_opened, &THR_LOCK_open);
DBUG_RETURN(err);
} /* my_close */
......@@ -134,15 +138,13 @@ File my_register_filename(File fd, const char *FileName, enum file_type
{
if ((uint) fd >= my_file_limit)
{
thread_safe_increment(my_file_opened,&THR_LOCK_open);
statistic_increment(my_file_opened,&THR_LOCK_open);
DBUG_RETURN(fd); /* safeguard */
}
mysql_mutex_lock(&THR_LOCK_open);
my_file_info[fd].name = (char*) my_strdup(FileName, MyFlags);
my_file_opened++;
my_file_total_opened++;
statistic_increment(my_file_opened,&THR_LOCK_open);
statistic_increment(my_file_total_opened,&THR_LOCK_open);
my_file_info[fd].type = type_of_file;
mysql_mutex_unlock(&THR_LOCK_open);
DBUG_PRINT("exit",("fd: %d",fd));
DBUG_RETURN(fd);
}
......
......@@ -528,7 +528,7 @@ FILE *my_win_fopen(const char *filename, const char *type)
{
FILE *file;
int flags= 0;
DBUG_ENTER("my_win_open");
DBUG_ENTER("my_win_fopen");
/*
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)
{
File fd;
DBUG_ENTER("my_win_close");
DBUG_ENTER("my_win_fclose");
fd= my_fileno(file);
if(fd < 0)
DBUG_RETURN(-1);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment