1 /*
2  * $Id: AmSessionProcessor.h 1585 2009-10-28 22:31:08Z sayer $
3  *
4  * Copyright (C) 2010 Stefan Sayer
5  *
6  * This file is part of SEMS, a free SIP media server.
7  *
8  * SEMS is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version. This program is released under
12  * the GPL with the additional exemption that compiling, linking,
13  * and/or using OpenSSL is allowed.
14  *
15  * For a license to use the SEMS software under conditions
16  * other than those described here, or to purchase support for this
17  * software, please contact iptel.org by e-mail at the following addresses:
18  *    info@iptel.org
19  *
20  * SEMS is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28  */
29 
30 #ifdef SESSION_THREADPOOL
31 
32 #ifndef _AmSessionProcessor_h_
33 #define _AmSessionProcessor_h_
34 
35 #include "AmThread.h"
36 #include "AmEventQueue.h"
37 
38 #include <vector>
39 #include <list>
40 #include <set>
41 class AmSessionProcessorThread;
42 class AmSession;
43 
44 class AmSessionProcessor {
45   static vector<AmSessionProcessorThread*> threads;
46   static AmMutex threads_mut;
47   static vector<AmSessionProcessorThread*>::iterator
48     threads_it;
49 
50  public:
51   static AmSessionProcessorThread* getProcessorThread();
52   static void addThreads(unsigned int num_threads);
53 };
54 
55 struct AmSessionProcessorThreadAddEvent
56   : AmEvent
57 {
58   AmSession* s;
AmSessionProcessorThreadAddEventAmSessionProcessorThreadAddEvent59   AmSessionProcessorThreadAddEvent(AmSession* s)
60     : s(s), AmEvent(120) { }
61 };
62 
63 class AmSessionProcessorThread
64 : public AmThread,
65   public AmEventHandler,
66   public AmEventNotificationSink
67 {
68   AmEventQueue    events;
69   std::list<AmSession*> sessions;
70   std::vector<AmSession*> startup_sessions;
71   AmSharedVar<bool> stop_requested;
72 
73   AmCondition<bool> runcond;
74   std::set<AmEventQueue*> process_sessions;
75   AmMutex process_sessions_mut;
76 
77   // AmEventHandler interface
78   void process(AmEvent* e);
79 
80  public:
81   AmSessionProcessorThread();
82   ~AmSessionProcessorThread();
83 
84   // AmThread interface
85   void run();
86   void on_stop();
87 
88   // AmEventNotificationSink interface
89   void notify(AmEventQueue* sender);
90 
91   void startSession(AmSession* s);
92 };
93 
94 #endif // _AmSessionProcessor_h_
95 
96 #endif // #ifdef SESSION_THREADPOOL
97