2 * @file session_thread_control.h
3 * @brief session used 2 threads. thread pooling unit is 2 thread control.
5 * L7VSD: Linux Virtual Server for Layer7 Load Balancing
6 * Copyright (C) 2009 NTT COMWARE Corporation.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 **********************************************************************/
25 #ifndef SESSION_THREAD_CONTROL_H
26 #define SESSION_THREAD_CONTROL_H
27 #include <boost/thread.hpp>
28 #include <boost/thread/condition.hpp>
29 #include <boost/bind.hpp>
30 #include <boost/noncopyable.hpp>
31 #include <boost/shared_ptr.hpp>
32 #include "tcp_session.h"
35 #include "parameter.h"
36 #include "error_code.h"
38 #define PARAM_SCHED_PRIORITY "task_scheduler_priority"
39 #define NUM_OF_CORE_USES "num_of_core_uses"
47 //! @brief session thread pooling utility class.
48 //! @class session_thread_control is session thread pool utility.
49 class session_thread_control : private boost::noncopyable
52 typedef boost::shared_ptr< boost::thread > thread_ptr; //! shared_ptr thread typedef
53 typedef boost::shared_ptr< tcp_session > session_ptr; //! shared_ptr session typedef
54 typedef boost::thread::id thread_id_type; //! thread id typedef
55 enum state_tag { //! @enum state_tag upthread and down thread state enum
56 WAIT = 0, //! thread pooling mode
57 RUNNING, //! thread running mode
58 EXIT //! thread exit mode
61 thread_ptr upthread; //! upstream thread
62 wr_mutex upthread_state_mutex; //! mutex for upstream thread status
63 state_tag upthread_state; //! upstream thread state
64 boost::mutex upthread_condition_mutex; //! upthread condition use mutex
65 boost::condition upthread_condition; //! upthread condition
66 boost::mutex upthread_running_mutex;
67 boost::mutex upthread_joining_mutex;
68 boost::condition upthread_joining_condition;
69 thread_ptr downthread; //! downstream thread
70 wr_mutex downthread_state_mutex; //! mutex for downstream thread status
71 state_tag downthread_state; //! downstream thread state
72 boost::mutex downthread_condition_mutex; //! downstream condition use mutex
73 boost::condition downthread_condition; //! downstream condition
74 boost::mutex downthread_running_mutex;
75 boost::mutex downthread_joining_mutex;
76 boost::condition downthread_joining_condition;
77 session_ptr session; //! session class shared pointer
78 void upstream_run(); //! upstream thread bind function
79 void downstream_run(); //! downstream thread bind function
81 cpu_set_t vsnic_cpumask;
82 cpu_set_t rsnic_cpumask;
89 //! @param session_ptr session class shared ptr
90 session_thread_control(tcp_session *ptr, cpu_set_t in_upcpu, cpu_set_t in_downcpu, int in_sched_algorithm) :
92 downthread_state(WAIT),
93 vsnic_cpumask(in_upcpu),
94 rsnic_cpumask(in_downcpu),
95 sched_algorithm(in_sched_algorithm) {
99 //get scheduler priority parameter
100 int_val = param.get_int(l7vs::PARAM_COMP_L7VSD, PARAM_SCHED_PRIORITY, err);
102 if ((int_val > sched_get_priority_min(in_sched_algorithm)) || (int_val < sched_get_priority_max(in_sched_algorithm)))
103 sched_priority = int_val;
105 sched_priority = sched_get_priority_min(in_sched_algorithm);
108 //get number of use cpu_cores
109 int_val = param.get_int(l7vs::PARAM_COMP_VIRTUALSERVICE, NUM_OF_CORE_USES, err);
111 num_of_core_uses = int_val;
113 num_of_core_uses = 0;
119 ~session_thread_control() {
122 //! create up down thread
125 //! session shared ptr getter
126 //! @return session shared ptr
127 session_ptr get_session() {
130 //! upstream thread start function
131 void startupstream();
132 //! downstream thread start function
133 void startdownstream();
134 //! upstream thread stop function
136 //! downstream thread stop function
137 void stopdownstream();
138 //! all thread destroy function.
140 //! upstream-thread id getter
141 //! @return thread_id_type upstream thread id
142 thread_id_type get_upthread_id() {
143 return upthread->get_id();
145 //! downstream-thread id getter
146 //! @return thread_id_type downstream thread id
147 thread_id_type get_downthread_id() {
148 return downthread->get_id();
150 //! upthread mutex reference getter
151 //! @return reference of boost::mutex
152 boost::mutex &get_upthread_mutex();
153 //! downthread mutex reference getter
154 //! @return reference of boost::mutex
155 boost::mutex &get_downthread_mutex();
157 void session_stop() {
158 session->set_virtual_service_message(tcp_session::SESSION_END, boost::asio::ip::tcp::endpoint());
160 void session_sorry_mode_change(int sorry_flag) {
161 if (INT_MAX == sorry_flag)
162 session->set_virtual_service_message(tcp_session::SORRY_STATE_DISABLE, boost::asio::ip::tcp::endpoint());
163 else if (0 != sorry_flag)
164 session->set_virtual_service_message(tcp_session::SORRY_STATE_ENABLE, boost::asio::ip::tcp::endpoint());
166 void session_sorry_enable() {
167 session->set_virtual_service_message(tcp_session::SORRY_STATE_ENABLE, boost::asio::ip::tcp::endpoint());
169 void session_sorry_disable() {
170 session->set_virtual_service_message(tcp_session::SORRY_STATE_DISABLE, boost::asio::ip::tcp::endpoint());
172 void session_pause_on() {
173 session->set_virtual_service_message(tcp_session::SESSION_PAUSE_ON, boost::asio::ip::tcp::endpoint());
175 void session_pause_off() {
176 session->set_virtual_service_message(tcp_session::SESSION_PAUSE_OFF, boost::asio::ip::tcp::endpoint());
179 void session_accesslog_output_mode_on() {
180 session->set_virtual_service_message(tcp_session::ACCESS_LOG_ON, boost::asio::ip::tcp::endpoint());
182 void session_accesslog_output_mode_off() {
183 session->set_virtual_service_message(tcp_session::ACCESS_LOG_OFF, boost::asio::ip::tcp::endpoint());
185 void session_access_log_output_mode_change(int accesslog_flag) {
186 if (accesslog_flag == true)
187 session->set_virtual_service_message(tcp_session::ACCESS_LOG_ON, boost::asio::ip::tcp::endpoint());
188 else if (accesslog_flag == false)
189 session->set_virtual_service_message(tcp_session::ACCESS_LOG_OFF, boost::asio::ip::tcp::endpoint());
192 void session_realserver_remove(const boost::asio::ip::tcp::endpoint &in) {
193 session->set_virtual_service_message(tcp_session::REALSERVER_REMOVE, in);
200 #endif //SESSION_THREAD_CONTROL_H