mirror of
https://github.com/MariaDB/server.git
synced 2025-07-30 16:24:05 +03:00
First implementation for signal handling and multi-threading:
safe shutdown and signal deliverence to all threads in the manager process
This commit is contained in:
@ -14,57 +14,52 @@
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
||||
|
||||
#include "manager.h"
|
||||
|
||||
#include <my_global.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include "thread_repository.h"
|
||||
#include "listener.h"
|
||||
#include "log.h"
|
||||
|
||||
//#include "mysql_connection.h"
|
||||
|
||||
void manager(const char *socket_file_name)
|
||||
{
|
||||
while (true)
|
||||
Thread_repository thread_repository;
|
||||
Listener_thread_args listener_args(thread_repository, socket_file_name);
|
||||
|
||||
/* write pid file */
|
||||
|
||||
/* block signals */
|
||||
sigset_t mask;
|
||||
sigemptyset(&mask);
|
||||
sigaddset(&mask, SIGINT);
|
||||
sigaddset(&mask, SIGTERM);
|
||||
sigaddset(&mask, SIGHUP);
|
||||
|
||||
/* all new threads will inherite this signal mask */
|
||||
pthread_sigmask(SIG_BLOCK, &mask, NULL);
|
||||
{
|
||||
log_info("manager is alive");
|
||||
sleep(2);
|
||||
/* create the listener */
|
||||
pthread_t listener_thd_id;
|
||||
pthread_attr_t listener_thd_attr;
|
||||
|
||||
if (pthread_attr_init(&listener_thd_attr))
|
||||
die("manager(): pthread_attr_init(listener) failed");
|
||||
if (pthread_attr_setdetachstate(&listener_thd_attr,
|
||||
PTHREAD_CREATE_DETACHED))
|
||||
die("manager(): pthread_attr_setdetachstate(listener) failed");
|
||||
if (pthread_create(&listener_thd_id, &listener_thd_attr, listener,
|
||||
&listener_args))
|
||||
die("manager(): pthread_create(listener) failed");
|
||||
}
|
||||
#if 0
|
||||
/*
|
||||
Dummy manager implementation: listens on a UNIX socket and
|
||||
starts echo server in a dedicated thread for each accepted connection.
|
||||
Enough to test startup/shutdown/options/logging of the instance manager.
|
||||
To work nicely with LinuxThreads, the signal thread is the first thread
|
||||
in the process.
|
||||
*/
|
||||
|
||||
int fd= socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
|
||||
if (!fd)
|
||||
die("socket(): failed");
|
||||
|
||||
struct sockaddr_un address;
|
||||
bzero(&address, sizeof(address));
|
||||
address.sun_family= AF_UNIX;
|
||||
strcpy(address.sun_path, socket_path);
|
||||
int opt= 1;
|
||||
|
||||
if (unlink(socket_path) ||
|
||||
bind(fd, (struct sockaddr *) &address, sizeof(address)) ||
|
||||
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)))
|
||||
die("unlink | bind | setsockopt failed");
|
||||
|
||||
if (listen(fd, 5))
|
||||
die("listen() failed");
|
||||
|
||||
int client_fd;
|
||||
while ((client_fd= accept(fd, 0, 0)) != -1);
|
||||
{
|
||||
printf("accepted\n");
|
||||
const char *message= "\n10hel";
|
||||
send(client_fd, message, strlen(message), 0);
|
||||
|
||||
int sleep_seconds= argc > 1 && atoi(argv[1]) ? atoi(argv[1]) : 1;
|
||||
printf("sleeping %d seconds\n", sleep_seconds);
|
||||
sleep(sleep_seconds);
|
||||
close(client_fd);
|
||||
}
|
||||
printf("accept(): failed\n");
|
||||
close(fd);
|
||||
#endif
|
||||
int signo;
|
||||
sigwait(&mask, &signo);
|
||||
thread_repository.deliver_shutdown();
|
||||
/* don't pthread_exit to kill all threads who did not shut down in time */
|
||||
}
|
||||
|
Reference in New Issue
Block a user