1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-30 16:24:05 +03:00

Replace the approach using Foo_thread_args + Foo_thread and manually

spawned threads with a reusable class Thread.

This is the second idea implemented in the Alik's patch for
BUG#22306: STOP INSTANCE can not be applied for instances in Crashed,
Failed and Abandoned.
Commiting separately to ease review process. 


server-tools/instance-manager/commands.cc:
  Remove an unused header.
server-tools/instance-manager/guardian.cc:
  Use Thread framework instead of manually spawning the Guardian thread.
  Tidy up.
server-tools/instance-manager/guardian.h:
  Use Thread framework instead of manually spawning the Guardian thread.
server-tools/instance-manager/instance.cc:
  Use Thread framework instead of manually spawning the instance
  monitoring thread.
server-tools/instance-manager/listener.cc:
  Use Thread framework instead of manually spawning the 
  mysql connection thread.
server-tools/instance-manager/listener.h:
  Use Thread framework instead of manually spawning the 
  mysql connection thread.
  Rename Listener_thread to Listener for brevity.
server-tools/instance-manager/manager.cc:
  Change references to pointers, as per the coding style.
  Use Thread framework instead of manually spawning threads.
server-tools/instance-manager/mysql_connection.cc:
  Get rid of Mysql_connection_thread_args. Use class Thread framework
  instead. Rename Mysql_connection_thread to Mysql_connection for brevity.
server-tools/instance-manager/mysql_connection.h:
  Get rid of Mysql_connection_thread_args. Use class Thread framework
  instead. Rename Mysql_connection_thread to Mysql_connection for brevity.
server-tools/instance-manager/priv.cc:
  Move set_stacksize_and_create_thread to thread_registry.cc and make it
  static: it is not used anywhere else now.
server-tools/instance-manager/priv.h:
  No public set_stacksize_n_create_thread
server-tools/instance-manager/thread_registry.cc:
  Implement a base Thread class to be used for all Instance Manager
  threads.
server-tools/instance-manager/thread_registry.h:
  Implement a base Thread class to be used for all Instance Manager
  threads.
This commit is contained in:
unknown
2006-11-17 16:11:04 +03:00
parent 211b2bc92a
commit a163ae30f2
13 changed files with 337 additions and 421 deletions

View File

@ -29,7 +29,6 @@
#include <sys/un.h>
#endif
#include "instance_map.h"
#include "log.h"
#include "mysql_connection.h"
#include "options.h"
@ -59,47 +58,18 @@ static void set_no_inherit(int socket)
}
/*
Listener_thread - incapsulates listening functionality
*/
class Listener_thread: public Listener_thread_args
{
public:
Listener_thread(const Listener_thread_args &args);
~Listener_thread();
void run();
private:
static const int LISTEN_BACK_LOG_SIZE= 5; /* standard backlog size */
ulong total_connection_count;
Thread_info thread_info;
int sockets[2];
int num_sockets;
fd_set read_fds;
private:
void handle_new_mysql_connection(Vio *vio);
int create_tcp_socket();
int create_unix_socket(struct sockaddr_un &unix_socket_address);
};
Listener_thread::Listener_thread(const Listener_thread_args &args) :
Listener_thread_args(args.thread_registry, args.user_map, args.instance_map)
,total_connection_count(0)
,thread_info(pthread_self(), TRUE)
,num_sockets(0)
{
}
Listener_thread::~Listener_thread()
Listener::Listener(Thread_registry *thread_registry_arg,
User_map *user_map_arg)
:thread_registry(thread_registry_arg),
user_map(user_map_arg),
total_connection_count(0),
num_sockets(0)
{
}
/*
Listener_thread::run() - listen all supported sockets and spawn a thread
Listener::run() - listen all supported sockets and spawn a thread
to handle incoming connection.
Using 'die' in case of syscall failure is OK now - we don't hold any
resources and 'die' kills the signal thread automatically. To be rewritten
@ -108,11 +78,11 @@ Listener_thread::~Listener_thread()
architecture.
*/
void Listener_thread::run()
void Listener::run()
{
int i, n= 0;
log_info("Listener_thread: started.");
log_info("Listener: started.");
#ifndef __WIN__
/* we use this var to check whether we are running on LinuxThreads */
@ -125,9 +95,7 @@ void Listener_thread::run()
linuxthreads= (thread_pid != manager_pid);
#endif
thread_registry.register_thread(&thread_info);
my_thread_init();
thread_registry->register_thread(&thread_info);
FD_ZERO(&read_fds);
@ -146,7 +114,7 @@ void Listener_thread::run()
n++;
timeval tv;
while (!thread_registry.is_shutdown())
while (!thread_registry->is_shutdown())
{
fd_set read_fds_arg= read_fds;
/*
@ -166,7 +134,7 @@ void Listener_thread::run()
if (rc == 0 || rc == -1)
{
if (rc == -1 && errno != EINTR)
log_error("Listener_thread: select() failed, %s",
log_error("Listener: select() failed, %s",
strerror(errno));
continue;
}
@ -200,7 +168,7 @@ void Listener_thread::run()
/* III. Release all resources and exit */
log_info("Listener_thread: shutdown requested, exiting...");
log_info("Listener: shutdown requested, exiting...");
for (i= 0; i < num_sockets; i++)
close(sockets[i]);
@ -209,10 +177,9 @@ void Listener_thread::run()
unlink(unix_socket_address.sun_path);
#endif
thread_registry.unregister_thread(&thread_info);
my_thread_end();
thread_registry->unregister_thread(&thread_info);
log_info("Listener_thread: finished.");
log_info("Listener: finished.");
return;
err:
@ -220,13 +187,12 @@ err:
for (i= 0; i < num_sockets; i++)
close(sockets[i]);
thread_registry.unregister_thread(&thread_info);
thread_registry.request_shutdown();
my_thread_end();
thread_registry->unregister_thread(&thread_info);
thread_registry->request_shutdown();
return;
}
int Listener_thread::create_tcp_socket()
int Listener::create_tcp_socket()
{
/* value to be set by setsockopt */
int arg= 1;
@ -265,7 +231,7 @@ int Listener_thread::create_tcp_socket()
if (bind(ip_socket, (struct sockaddr *) &ip_socket_address,
sizeof(ip_socket_address)))
{
log_error("Listener_thread: bind(ip socket) failed, '%s'",
log_error("Listener: bind(ip socket) failed, '%s'",
strerror(errno));
close(ip_socket);
return -1;
@ -273,7 +239,7 @@ int Listener_thread::create_tcp_socket()
if (listen(ip_socket, LISTEN_BACK_LOG_SIZE))
{
log_error("Listener_thread: listen(ip socket) failed, %s",
log_error("Listener: listen(ip socket) failed, %s",
strerror(errno));
close(ip_socket);
return -1;
@ -292,7 +258,7 @@ int Listener_thread::create_tcp_socket()
}
#ifndef __WIN__
int Listener_thread::
int Listener::
create_unix_socket(struct sockaddr_un &unix_socket_address)
{
int unix_socket= socket(AF_UNIX, SOCK_STREAM, 0);
@ -318,7 +284,7 @@ create_unix_socket(struct sockaddr_un &unix_socket_address)
if (bind(unix_socket, (struct sockaddr *) &unix_socket_address,
sizeof(unix_socket_address)))
{
log_error("Listener_thread: bind(unix socket) failed, "
log_error("Listener: bind(unix socket) failed, "
"socket file name is '%s', error '%s'",
unix_socket_address.sun_path, strerror(errno));
close(unix_socket);
@ -329,7 +295,7 @@ create_unix_socket(struct sockaddr_un &unix_socket_address)
if (listen(unix_socket, LISTEN_BACK_LOG_SIZE))
{
log_error("Listener_thread: listen(unix socket) failed, %s",
log_error("Listener: listen(unix socket) failed, %s",
strerror(errno));
close(unix_socket);
return -1;
@ -357,46 +323,16 @@ create_unix_socket(struct sockaddr_un &unix_socket_address)
handle_new_mysql_connection()
*/
void Listener_thread::handle_new_mysql_connection(Vio *vio)
void Listener::handle_new_mysql_connection(Vio *vio)
{
if (Mysql_connection_thread_args *mysql_thread_args=
new Mysql_connection_thread_args(vio, thread_registry, user_map,
++total_connection_count,
instance_map)
)
Mysql_connection *mysql_connection=
new Mysql_connection(thread_registry, user_map,
vio, ++total_connection_count);
if (mysql_connection == NULL || mysql_connection->start_detached())
{
/*
Initialize thread attributes to create detached thread; it seems
easier to do it ad-hoc than have a global variable for attributes.
*/
pthread_t mysql_thd_id;
pthread_attr_t mysql_thd_attr;
pthread_attr_init(&mysql_thd_attr);
pthread_attr_setdetachstate(&mysql_thd_attr, PTHREAD_CREATE_DETACHED);
if (set_stacksize_n_create_thread(&mysql_thd_id, &mysql_thd_attr,
mysql_connection, mysql_thread_args))
{
delete mysql_thread_args;
vio_delete(vio);
log_error("handle_one_mysql_connection():"
"set_stacksize_n_create_thread(mysql) failed");
}
pthread_attr_destroy(&mysql_thd_attr);
}
else
log_error("handle_one_mysql_connection() failed");
delete mysql_connection;
vio_delete(vio);
}
/* The connection will delete itself when the thread is finished */
}
pthread_handler_t listener(void *arg)
{
Listener_thread_args *args= (Listener_thread_args *) arg;
Listener_thread listener(*args);
listener.run();
/*
args is a stack variable because listener thread lives as long as the
manager process itself
*/
return 0;
}