~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/scheduling.cc

  • Committer: Monty Taylor
  • Date: 2009-03-04 02:48:12 UTC
  • mto: (917.1.2 mordred)
  • mto: This revision was merged to the branch mainline in revision 918.
  • Revision ID: mordred@inaugust.com-20090304024812-5wb6wpye5c1iitbq
Applied atomic patch to current tree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
 
4
 *  Copyright (C) 2008 Mark Atwood
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include "config.h"
21
 
 
22
 
#include <algorithm>
23
 
 
24
 
#include "drizzled/plugin/scheduler.h"
25
 
 
26
 
#include "drizzled/gettext.h"
27
 
#include "drizzled/errmsg_print.h"
28
 
 
29
 
namespace drizzled
30
 
{
31
 
 
32
 
extern size_t my_thread_stack_size;
33
 
 
34
 
std::vector<plugin::Scheduler *> all_schedulers;
35
 
 
36
 
/* Globals (TBK) */
37
 
static plugin::Scheduler *scheduler= NULL;
38
 
 
39
 
 
40
 
class FindSchedulerByName : public std::unary_function<plugin::Scheduler *, bool>
41
 
{
42
 
  const std::string *name;
43
 
public:
44
 
  FindSchedulerByName(const std::string *name_arg)
45
 
    : name(name_arg) {}
46
 
  result_type operator() (argument_type sched)
47
 
  {
48
 
    return (bool)((name->compare(sched->getName()) == 0));
49
 
  }
50
 
};
51
 
 
52
 
 
53
 
bool plugin::Scheduler::addPlugin(plugin::Scheduler *sched)
54
 
{
55
 
  std::vector<plugin::Scheduler *>::iterator iter=
56
 
    std::find_if(all_schedulers.begin(), all_schedulers.end(), 
57
 
            FindSchedulerByName(&sched->getName()));
58
 
 
59
 
  if (iter != all_schedulers.end())
60
 
  {
61
 
    errmsg_printf(ERRMSG_LVL_ERROR,
62
 
                  _("Attempted to register a scheduler %s, but a scheduler "
63
 
                    "has already been registered with that name.\n"),
64
 
                    sched->getName().c_str());
65
 
    return true;
66
 
  }
67
 
 
68
 
  sched->deactivate();
69
 
  all_schedulers.push_back(sched);
70
 
 
71
 
  return false;
72
 
}
73
 
 
74
 
 
75
 
void plugin::Scheduler::removePlugin(plugin::Scheduler *sched)
76
 
{
77
 
  all_schedulers.erase(std::find(all_schedulers.begin(),
78
 
                            all_schedulers.end(),
79
 
                            sched));
80
 
}
81
 
 
82
 
 
83
 
bool plugin::Scheduler::setPlugin(const std::string& name)
84
 
{
85
 
  std::vector<plugin::Scheduler *>::iterator iter=
86
 
    std::find_if(all_schedulers.begin(), all_schedulers.end(), 
87
 
            FindSchedulerByName(&name));
88
 
 
89
 
  if (iter != all_schedulers.end())
90
 
  {
91
 
    if (scheduler != NULL)
92
 
      scheduler->deactivate();
93
 
    scheduler= *iter;
94
 
    scheduler->activate();
95
 
    return false;
96
 
  }
97
 
 
98
 
  errmsg_printf(ERRMSG_LVL_WARN,
99
 
                _("Attempted to configure %s as the scheduler, which did "
100
 
                  "not exist.\n"), name.c_str());
101
 
  return true;
102
 
}
103
 
 
104
 
 
105
 
plugin::Scheduler *plugin::Scheduler::getScheduler()
106
 
{
107
 
  return scheduler;
108
 
}
109
 
 
110
 
} /* namespace drizzled */
 
20
#include <drizzled/server_includes.h>
 
21
#include <drizzled/scheduling.h>
 
22
#include <drizzled/gettext.h>
 
23
#include <drizzled/plugin_scheduling.h>
 
24
#include <drizzled/connect.h>
 
25
 
 
26
scheduling_st thread_scheduler;
 
27
static bool scheduler_inited= false; /* We must insist that only one of these plugins get loaded at a time */
 
28
 
 
29
static void post_kill_dummy(Session *) { return; }
 
30
static bool end_thread_dummy(Session *, bool) { return false; }
 
31
 
 
32
extern char *opt_scheduler;
 
33
 
 
34
int scheduling_initializer(st_plugin_int *plugin)
 
35
{
 
36
  if (memcmp(plugin->plugin->name, opt_scheduler, strlen(opt_scheduler)))
 
37
    return 0;
 
38
 
 
39
  if (scheduler_inited)
 
40
  {
 
41
    fprintf(stderr, "You cannot load more then one scheduler plugin\n");
 
42
    exit(1);
 
43
  }
 
44
 
 
45
  memset(&thread_scheduler, 0, sizeof(scheduling_st));
 
46
 
 
47
  assert(plugin->plugin->init); /* Find poorly designed plugins */
 
48
  if (plugin->plugin->init)
 
49
  {
 
50
 
 
51
    thread_scheduler.post_kill_notification= post_kill_dummy;
 
52
    thread_scheduler.end_thread= end_thread_dummy;
 
53
    thread_scheduler.init_new_connection_thread= init_new_connection_handler_thread;
 
54
 
 
55
    if (plugin->plugin->init((void *)&thread_scheduler))
 
56
    {
 
57
      /* 
 
58
        TRANSLATORS> The leading word "scheduling" is the name
 
59
        of the plugin api, and so should not be translated. 
 
60
      */
 
61
      errmsg_printf(ERRMSG_LVL_ERROR, _("scheduling plugin '%s' init() failed"),
 
62
                      plugin->name.str);
 
63
      goto err;
 
64
    }
 
65
  }
 
66
 
 
67
  /* We are going to assert() on any plugin that is not well written. */
 
68
  assert(thread_scheduler.max_threads);
 
69
  assert(thread_scheduler.init_new_connection_thread);
 
70
  assert(thread_scheduler.add_connection);
 
71
  assert(thread_scheduler.post_kill_notification);
 
72
  assert(thread_scheduler.end_thread);
 
73
 
 
74
  scheduler_inited= true;
 
75
  /* We populate so we can find which plugin was initialized later on */
 
76
  plugin->data= (void *)&thread_scheduler;
 
77
  plugin->state= PLUGIN_IS_READY;
 
78
 
 
79
  return 0;
 
80
 
 
81
err:
 
82
 
 
83
  return 1;
 
84
}
 
85
 
 
86
int scheduling_finalizer(st_plugin_int *plugin)
 
87
{
 
88
  /* We know which one we initialized since its data pointer is filled */
 
89
  if (plugin->plugin->deinit && plugin->data)
 
90
  {
 
91
    if (plugin->plugin->deinit((void *)&thread_scheduler))
 
92
    {
 
93
      /* TRANSLATORS: The leading word "scheduling" is the name
 
94
         of the plugin api, and so should not be translated. */
 
95
      errmsg_printf(ERRMSG_LVL_ERROR, _("scheduling plugin '%s' deinit() failed"),
 
96
                    plugin->name.str);
 
97
    }
 
98
  }
 
99
 
 
100
  return 0;
 
101
}