Commit cbfff730 authored by unknown's avatar unknown
Browse files

Polishing:

1) add support for joinable threads to Thread class;
2) move checking of thread model to Manager from mysqlmanager.cc,
because it is needed only for IM-main process.


server-tools/instance-manager/instance.cc:
  Use Manager::is_linux_threads() instead of global variable.
server-tools/instance-manager/listener.cc:
  Use Thread::start(DETACHED) instead of Thread::start_detached().
server-tools/instance-manager/manager.cc:
  1. Use Thread::start(DETACHED) instead of Thread::start_detached();
  2. Move checking of thread model to Manager from mysqlmanager.cc,
  because it is needed only for IM-main process.
server-tools/instance-manager/manager.h:
  Move checking of thread model to Manager from mysqlmanager.cc,
  because it is needed only for IM-main process.
server-tools/instance-manager/mysqlmanager.cc:
  Move checking of thread model to Manager from mysqlmanager.cc,
  because it is needed only for IM-main process.
server-tools/instance-manager/priv.cc:
  Move checking of thread model to Manager from mysqlmanager.cc,
  because it is needed only for IM-main process.
server-tools/instance-manager/priv.h:
  Move checking of thread model to Manager from mysqlmanager.cc,
  because it is needed only for IM-main process.
server-tools/instance-manager/thread_registry.cc:
  Add support of joinable threads to Thread class.
server-tools/instance-manager/thread_registry.h:
  Add support of joinable threads to Thread class.
parent 817c6a4f
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -105,7 +105,7 @@ static int wait_process(My_process_info *pi)
    couldn't use wait(), because it could return in any wait() in the program.
  */

  if (linuxthreads)
  if (Manager::is_linux_threads())
    wait(NULL);                               /* LinuxThreads were detected */
  else
    waitpid(*pi, NULL, 0);
@@ -564,7 +564,7 @@ int Instance::start()

    instance_monitor= new Instance_monitor(this);

    if (instance_monitor == NULL || instance_monitor->start_detached())
    if (instance_monitor == NULL || instance_monitor->start(Thread::DETACHED))
    {
      delete instance_monitor;
      log_error("Instance::start(): failed to create the monitoring thread"
+1 −1
Original line number Diff line number Diff line
@@ -323,7 +323,7 @@ void Listener::handle_new_mysql_connection(struct st_vio *vio)
  Mysql_connection *mysql_connection=
    new Mysql_connection(thread_registry, user_map,
                         vio, ++total_connection_count);
  if (mysql_connection == NULL || mysql_connection->start_detached())
  if (mysql_connection == NULL || mysql_connection->start(Thread::DETACHED))
  {
    log_error("handle_one_mysql_connection() failed");
    delete mysql_connection;
+70 −10
Original line number Diff line number Diff line
@@ -93,6 +93,65 @@ int my_sigwait(const sigset_t *set, int *sig)
#endif


/**********************************************************************
  Implementation of checking the actual thread model.
***********************************************************************/

namespace { /* no-indent */

class ThreadModelChecker: public Thread
{
public:
  ThreadModelChecker()
    :main_pid(getpid())
  { }

public:
  inline bool is_linux_threads() const
  {
    return linux_threads;
  }

protected:
  virtual void run()
  {
    linux_threads= main_pid != getpid();
  }

private:
  pid_t main_pid;
  bool linux_threads;
};

bool check_if_linux_threads(bool *linux_threads)
{
  ThreadModelChecker checker;

  if (checker.start() || checker.join())
    return TRUE;

  *linux_threads= checker.is_linux_threads();

  return FALSE;
}

}


/**********************************************************************
  Manager implementation
***********************************************************************/

Guardian *Manager::p_guardian;
Instance_map *Manager::p_instance_map;
Thread_registry *Manager::p_thread_registry;
User_map *Manager::p_user_map;

#ifndef __WIN__
bool Manager::linux_threads;
#endif // __WIN__


void Manager::stop_all_threads()
{
  /*
@@ -106,14 +165,6 @@ void Manager::stop_all_threads()
  p_thread_registry->deliver_shutdown();
}

/**********************************************************************
  Manager implementation
***********************************************************************/

Guardian *Manager::p_guardian;
Instance_map *Manager::p_instance_map;
Thread_registry *Manager::p_thread_registry;
User_map *Manager::p_user_map;

/*
  manager - entry point to the main instance manager process: start
@@ -132,6 +183,15 @@ int Manager::main()
  bool shutdown_complete= FALSE;
  pid_t manager_pid= getpid();

  if (check_if_linux_threads(&linux_threads))
  {
    log_error("Error: can not check if Linux Threads are used.");
    return 1;
  }

  log_info("Detected threads model: %s.",
           (const char *) (linux_threads ? "LINUX threads" : "POSIX threads"));

  Thread_registry thread_registry;
  /*
    All objects created in the manager() function live as long as
@@ -228,7 +288,7 @@ int Manager::main()
    permitted to process instances. And before flush_instances() has
    completed, there are no instances to guard.
  */
  if (guardian.start_detached())
  if (guardian.start(Thread::DETACHED))
  {
    log_error("Error: can not start Guardian thread.");
    goto err;
@@ -255,7 +315,7 @@ int Manager::main()

  /* Initialize the Listener. */

  if (listener.start_detached())
  if (listener.start(Thread::DETACHED))
  {
    log_error("Error: can not start Listener thread.");
    stop_all_threads();
+12 −0
Original line number Diff line number Diff line
@@ -39,6 +39,10 @@ class Manager
  static Thread_registry *get_thread_registry() { return p_thread_registry; }
  static User_map *get_user_map() { return p_user_map; }

#ifndef __WIN__
  static bool is_linux_threads() { return linux_threads; }
#endif // __WIN__

private:
  static void stop_all_threads();

@@ -47,6 +51,14 @@ class Manager
  static Instance_map *p_instance_map;
  static Thread_registry *p_thread_registry;
  static User_map *p_user_map;

#ifndef __WIN__
  /*
    This flag is set if Instance Manager is running on the system using
    LinuxThreads.
  */
  static bool linux_threads;
#endif // __WIN__
};

#endif // INCLUDES_MYSQL_INSTANCE_MANAGER_MANAGER_H
+0 −28
Original line number Diff line number Diff line
@@ -71,7 +71,6 @@ static void daemonize(const char *log_file_name);
static void angel();
static struct passwd *check_user(const char *user);
static int set_user(const char *user, struct passwd *user_info);
static bool check_if_linuxthreads();
#endif


@@ -111,9 +110,6 @@ int main(int argc, char *argv[])
      }
  }

  if (check_if_linuxthreads())
    goto main_end; /* out of resources */

  if (Options::Daemon::run_as_service)
  {
    /* forks, and returns only in child */
@@ -395,28 +391,4 @@ static void angel()
    exit(0);
  }
}

extern "C" {
static void *check_if_linuxthreads_thread_func(void *arg)
{
  pid_t main_pid= *(pid_t*) arg;
  linuxthreads= getpid() != main_pid;
  return NULL; 
}
} /* extern "C" */


static bool check_if_linuxthreads()
{
  pid_t pid= getpid();
  pthread_t thread_id;
  int rc;

  rc= pthread_create(&thread_id, NULL, check_if_linuxthreads_thread_func,
                     (void*) &pid);
  if (rc == 0)
    rc= pthread_join(thread_id, NULL);

  return test(rc);
}
#endif
Loading