2006-12-31 01:02:27 +01:00
|
|
|
/* Copyright (C) 2004-2006 MySQL AB
|
2004-10-23 09:32:52 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2006-12-23 20:17:15 +01:00
|
|
|
the Free Software Foundation; version 2 of the License.
|
2004-10-23 09:32:52 +02:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
|
2006-12-31 01:02:27 +01:00
|
|
|
#ifndef INCLUDES_MYSQL_INSTANCE_MANAGER_THREAD_REGISTRY_H
|
|
|
|
#define INCLUDES_MYSQL_INSTANCE_MANAGER_THREAD_REGISTRY_H
|
|
|
|
|
2004-10-23 09:32:52 +02:00
|
|
|
/*
|
|
|
|
A multi-threaded application shall nicely work with signals.
|
|
|
|
|
|
|
|
This means it shall, first of all, shut down nicely on ``quit'' signals:
|
|
|
|
stop all running threads, cleanup and exit.
|
|
|
|
|
|
|
|
Note, that a thread can't be shut down nicely if it doesn't want to be.
|
2005-08-29 21:29:35 +02:00
|
|
|
That's why to perform clean shutdown, all threads constituting a process
|
2004-10-23 09:32:52 +02:00
|
|
|
must observe certain rules. Here we use the rules, described in Butenhof
|
|
|
|
book 'Programming with POSIX threads', namely:
|
|
|
|
- all user signals are handled in 'signal thread' in synchronous manner
|
|
|
|
(by means of sigwait). To guarantee that the signal thread is the only who
|
|
|
|
can receive user signals, all threads block them, and signal thread is
|
|
|
|
the only who calls sigwait() with an apporpriate sigmask.
|
|
|
|
To propogate a signal to the workers the signal thread sets
|
|
|
|
a variable, corresponding to the signal. Additionally the signal thread
|
|
|
|
sends each worker an internal signal (by means of pthread_kill) to kick it
|
|
|
|
out from possible blocking syscall, and possibly pthread_cond_signal if
|
|
|
|
some thread is blocked in pthread_cond_[timed]wait.
|
|
|
|
- a worker handles only internal 'kick' signal (the handler does nothing).
|
|
|
|
In case when a syscall returns 'EINTR' the worker checks all
|
|
|
|
signal-related variables and behaves accordingly.
|
|
|
|
Also these variables shall be checked from time to time in long
|
|
|
|
CPU-bounded operations, and before/after pthread_cond_wait. (It's supposed
|
|
|
|
that a worker thread either waits in a syscall/conditional variable, or
|
|
|
|
computes something.)
|
|
|
|
- to guarantee signal deliverence, there should be some kind of feedback,
|
|
|
|
e. g. all workers shall account in the signal thread Thread Repository and
|
|
|
|
unregister from it on exit.
|
|
|
|
|
|
|
|
Configuration reload (on SIGHUP) and thread timeouts/alarms can be handled
|
|
|
|
in manner, similar to ``quit'' signals.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <my_global.h>
|
|
|
|
#include <my_pthread.h>
|
|
|
|
|
2005-10-18 22:31:00 +02:00
|
|
|
#if defined(__GNUC__) && defined(USE_PRAGMA_INTERFACE)
|
|
|
|
#pragma interface
|
|
|
|
#endif
|
2004-10-23 09:32:52 +02:00
|
|
|
|
2006-11-17 14:11:04 +01:00
|
|
|
/**
|
2004-10-23 09:32:52 +02:00
|
|
|
Thread_info - repository entry for each worker thread
|
|
|
|
All entries comprise double-linked list like:
|
|
|
|
0 -- entry -- entry -- entry - 0
|
|
|
|
Double-linked list is used to unregister threads easy.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Thread_info
|
|
|
|
{
|
2005-10-16 06:49:19 +02:00
|
|
|
public:
|
2006-11-17 14:11:04 +01:00
|
|
|
Thread_info() {}
|
2005-10-16 06:49:19 +02:00
|
|
|
friend class Thread_registry;
|
2006-10-24 16:23:16 +02:00
|
|
|
private:
|
2006-11-17 14:11:04 +01:00
|
|
|
void init(bool send_signal_on_shutdown);
|
2005-10-16 06:49:19 +02:00
|
|
|
private:
|
2004-10-23 09:32:52 +02:00
|
|
|
pthread_cond_t *current_cond;
|
|
|
|
Thread_info *prev, *next;
|
|
|
|
pthread_t thread_id;
|
2006-10-24 16:23:16 +02:00
|
|
|
bool send_signal_on_shutdown;
|
2004-10-23 09:32:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-11-17 14:11:04 +01:00
|
|
|
/**
|
|
|
|
A base class for a detached thread.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Thread
|
|
|
|
{
|
|
|
|
public:
|
2006-11-21 15:31:03 +01:00
|
|
|
enum enum_thread_type
|
|
|
|
{
|
|
|
|
DETACHED,
|
|
|
|
JOINABLE
|
|
|
|
};
|
|
|
|
public:
|
|
|
|
Thread()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
public:
|
|
|
|
inline bool is_detached() const;
|
|
|
|
|
|
|
|
bool start(enum_thread_type thread_type = JOINABLE);
|
|
|
|
bool join();
|
|
|
|
|
2006-11-17 14:11:04 +01:00
|
|
|
protected:
|
|
|
|
virtual void run()= 0;
|
|
|
|
virtual ~Thread();
|
2006-11-21 15:31:03 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
pthread_t id;
|
|
|
|
bool detached;
|
|
|
|
|
2006-11-17 14:11:04 +01:00
|
|
|
private:
|
|
|
|
static void *thread_func(void *arg);
|
2006-11-21 15:31:03 +01:00
|
|
|
|
|
|
|
private:
|
2006-11-17 14:11:04 +01:00
|
|
|
Thread(const Thread & /* rhs */); /* not implemented */
|
|
|
|
Thread &operator=(const Thread & /* rhs */); /* not implemented */
|
|
|
|
};
|
|
|
|
|
2006-11-21 15:31:03 +01:00
|
|
|
inline bool Thread::is_detached() const
|
|
|
|
{
|
|
|
|
return detached;
|
|
|
|
}
|
|
|
|
|
2006-11-17 14:11:04 +01:00
|
|
|
|
|
|
|
/**
|
2004-10-23 09:32:52 +02:00
|
|
|
Thread_registry - contains handles for each worker thread to deliver
|
|
|
|
signal information to workers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Thread_registry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Thread_registry();
|
|
|
|
~Thread_registry();
|
|
|
|
|
2006-11-17 14:11:04 +01:00
|
|
|
void register_thread(Thread_info *info, bool send_signal_on_shutdown= TRUE);
|
2004-10-23 09:32:52 +02:00
|
|
|
void unregister_thread(Thread_info *info);
|
|
|
|
void deliver_shutdown();
|
|
|
|
void request_shutdown();
|
|
|
|
inline bool is_shutdown();
|
|
|
|
int cond_wait(Thread_info *info, pthread_cond_t *cond,
|
2005-08-29 21:29:35 +02:00
|
|
|
pthread_mutex_t *mutex);
|
|
|
|
int cond_timedwait(Thread_info *info, pthread_cond_t *cond,
|
|
|
|
pthread_mutex_t *mutex, struct timespec *wait_time);
|
Fix for BUG#24415: Instance manager test im_daemon_life_cycle fails randomly.
The cause of im_daemon_life_cycle.imtest random failures was the following
behaviour of some implementations of LINUX threads: let's suppose that a
process has several threads (in LINUX threads, there is a separate process for
each thread). When the main process gets killed, the parent receives SIGCHLD
before all threads (child processes) die. In other words, the parent receives
SIGCHLD, when its child is not completely dead.
In terms of IM, that means that IM-angel receives SIGCHLD when IM-main is not dead
and still holds some resources. After receiving SIGCHLD, IM-angel restarts
IM-main, but IM-main failed to initialize, because previous instance (copy) of
IM-main still holds server socket (TCP-port).
Another problem here was that IM-angel restarted IM-main only if it was killed
by signal. If it exited with error, IM-angel thought it's intended / graceful
shutdown and exited itself.
So, when the second instance of IM-main failed to initialize, IM-angel thought
it's intended shutdown and quit.
The fix is
1. to change IM-angel so that it restarts IM-main if it exited with error code;
2. to change IM-main so that it returns proper exit code in case of failure.
2007-02-20 20:31:50 +01:00
|
|
|
int get_error_status();
|
|
|
|
void set_error_status();
|
2006-11-16 21:36:20 +01:00
|
|
|
|
2006-10-24 16:23:16 +02:00
|
|
|
private:
|
|
|
|
void interrupt_threads();
|
|
|
|
void wait_for_threads_to_unregister();
|
|
|
|
|
2004-10-23 09:32:52 +02:00
|
|
|
private:
|
|
|
|
Thread_info head;
|
|
|
|
bool shutdown_in_progress;
|
|
|
|
pthread_mutex_t LOCK_thread_registry;
|
|
|
|
pthread_cond_t COND_thread_registry_is_empty;
|
2004-12-15 18:29:17 +01:00
|
|
|
pthread_t sigwait_thread_pid;
|
Fix for BUG#24415: Instance manager test im_daemon_life_cycle fails randomly.
The cause of im_daemon_life_cycle.imtest random failures was the following
behaviour of some implementations of LINUX threads: let's suppose that a
process has several threads (in LINUX threads, there is a separate process for
each thread). When the main process gets killed, the parent receives SIGCHLD
before all threads (child processes) die. In other words, the parent receives
SIGCHLD, when its child is not completely dead.
In terms of IM, that means that IM-angel receives SIGCHLD when IM-main is not dead
and still holds some resources. After receiving SIGCHLD, IM-angel restarts
IM-main, but IM-main failed to initialize, because previous instance (copy) of
IM-main still holds server socket (TCP-port).
Another problem here was that IM-angel restarted IM-main only if it was killed
by signal. If it exited with error, IM-angel thought it's intended / graceful
shutdown and exited itself.
So, when the second instance of IM-main failed to initialize, IM-angel thought
it's intended shutdown and quit.
The fix is
1. to change IM-angel so that it restarts IM-main if it exited with error code;
2. to change IM-main so that it returns proper exit code in case of failure.
2007-02-20 20:31:50 +01:00
|
|
|
bool error_status;
|
2006-11-16 21:36:20 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
Thread_registry(const Thread_registry &);
|
|
|
|
Thread_registry &operator =(const Thread_registry &);
|
2004-10-23 09:32:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Thread_registry::is_shutdown()
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&LOCK_thread_registry);
|
|
|
|
bool res= shutdown_in_progress;
|
|
|
|
pthread_mutex_unlock(&LOCK_thread_registry);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* INCLUDES_MYSQL_INSTANCE_MANAGER_THREAD_REGISTRY_H */
|