~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to plugin/pool_of_threads/pool_of_threads.h

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 * Copyright (C) 2009 Sun Microsystems
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
18
 */
 
19
 
 
20
#ifndef PLUGIN_POOL_OF_THREADS_POOL_OF_THREADS_H
 
21
#define PLUGIN_POOL_OF_THREADS_POOL_OF_THREADS_H
 
22
 
 
23
#include <drizzled/gettext.h>
 
24
#include <drizzled/error.h>
 
25
#include <drizzled/plugin/scheduler.h>
 
26
#include <drizzled/sql_parse.h>
 
27
#include <drizzled/session.h>
 
28
#include <drizzled/plugin/client.h>
 
29
#include "session_scheduler.h"
 
30
#include <string>
 
31
#include <queue>
 
32
#include <set>
 
33
#include <event.h>
 
34
 
 
35
 
 
36
/**
 
37
 * @brief
 
38
 *  Derived class for pool of threads scheduler.
 
39
 */
 
40
class PoolOfThreadsScheduler: public drizzled::plugin::Scheduler
 
41
{
 
42
private:
 
43
  pthread_attr_t attr;
 
44
 
 
45
  pthread_mutex_t LOCK_session_add;    /* protects sessions_need_adding */
 
46
  pthread_mutex_t LOCK_session_kill;    /* protects sessions_to_be_killed */
 
47
  /**
 
48
   * LOCK_event_loop protects the non-thread safe libevent calls (event_add
 
49
   * and event_del) and sessions_need_processing and sessions_waiting_for_io.
 
50
   */
 
51
  pthread_mutex_t LOCK_event_loop;
 
52
 
 
53
  std::queue<drizzled::Session *> sessions_need_adding; /* queue of sessions to add to libevent queue */
 
54
  std::queue<drizzled::Session *> sessions_to_be_killed; /* queue of sessions to be killed */
 
55
  std::queue<drizzled::Session *> sessions_need_processing; /* queue of sessions that needs some processing */
 
56
  /**
 
57
   * Collection of sessions with added events
 
58
   *
 
59
   * This should really be a collection of unordered Sessions. No one is more
 
60
   * promising to encounter an io event earlier than another; so no order
 
61
   * indeed! We will change this to unordered_set/hash_set when c++0x comes.
 
62
   */
 
63
  std::set<drizzled::Session *> sessions_waiting_for_io;
 
64
 
 
65
public:
 
66
  PoolOfThreadsScheduler(const char *name_arg);
 
67
  ~PoolOfThreadsScheduler();
 
68
 
 
69
  /**
 
70
   * @brief
 
71
   *  Notify the thread pool about a new connection
 
72
   *
 
73
   * @param[in] the newly connected session
 
74
   *
 
75
   * @return 
 
76
   *  True if there is an error.
 
77
   */
 
78
  bool addSession(drizzled::Session *session);
 
79
  
 
80
  
 
81
  /**
 
82
   * @brief
 
83
   *  Signal a waiting connection it's time to die.
 
84
   *
 
85
   * @details 
 
86
   *  This function will signal libevent the Session should be killed.
 
87
   *
 
88
   * @param[in]  session The connection to kill
 
89
   */
 
90
  void killSession(drizzled::Session *session);
 
91
 
 
92
  /**
 
93
   * @brief
 
94
   *  Create all threads for the thread pool
 
95
   *
 
96
   * @details
 
97
   *  After threads are created we wait until all threads has signaled that
 
98
   *  they have started before we return
 
99
   *
 
100
   * @retval 0 Ok
 
101
   * @retval 1 We got an error creating the thread pool. In this case we will abort all created threads.
 
102
   */
 
103
  bool libevent_init(void);
 
104
 
 
105
  void doIO(session_scheduler *sched);
 
106
  void killSession(int Fd);
 
107
  void addSession(int Fd);
 
108
  void *mainLoop();
 
109
  void sessionAddToQueue(session_scheduler *sched);
 
110
 
 
111
 
 
112
};
 
113
 
 
114
#endif /* PLUGIN_POOL_OF_THREADS_POOL_OF_THREADS_H */