EventSvc.C
1// -*- C++ -*-
2
3// Copyright (c) 2005-2010, Isode Limited, London, England.
4// All rights reserved.
5//
6// Acquisition and use of this software and related materials for any
7// purpose requires a written licence agreement from Isode Limited,
8// or a written licence from an organisation licenced by Isode Limited
9// to grant such a licence.
10
11//
12//
13// EventSvc.C
14//
15// Event Service Manager
16//
17// @VERSION@
18
19
20#include <isode/base/compat.h>
21
22#define DEFAULTMANAGER EVENTSVC_POLLMANAGER
23
24// These depends on platform
25#define DEFAULTSTREAMPROVIDER "socketpoll"
26#define DEFAULTDATAGRAMPROVIDER "socketpoll"
27
28#include "Event_p.h"
29
30#include "../include/AsyncObject.h"
31
32#include <isode/messages/base.h>
33
34class MutexLock {
35 pthread_mutex_t *mutex;
36
37public:
38 MutexLock (pthread_mutex_t *mp) : mutex(mp) {
39 if ( pthread_mutex_lock (mutex) != 0 ) {
40 MSG_Base_Tmutexlock_LOG ("event mutex");
41 }
42 }
43
44 ~MutexLock () {
45 if ( pthread_mutex_unlock (mutex) != 0 ) {
46 MSG_Base_Tunlock_LOG ("event mutex");
47 }
48 }
49};
50
51IC_CDECL int EventServiceInit (
52 const char *managertype,
53 const char *pollertype,
54 MSGstruct *msp
55)
56{
57 int rc;
58
59 rc = ::Event::Manager::ManagerFactory (managertype, msp);
60 if ( rc != 0 )
61 return rc;
62
63 rc = ::Poll::Provider::ProviderFactory (pollertype, msp);
64 if ( rc != 0 )
65 return rc;
66
67 return rc;
68}
69//␌
70
71namespace Event {
72#if defined(__clang__)
73 const int Manager::MAXPRIO;
74#endif
75 Manager *Manager::_instance;
76 static pthread_mutex_t manager_mutex = PTHREAD_MUTEX_INITIALIZER;
77
78 int Manager::ManagerFactory (const char *type, MSGstruct *msp)
79 {
80 MutexLock mlock (&manager_mutex);
81
82 if ( _instance != 0 ) {
83 MSG_IOevent_ManagerCreated_LOG ();
84 return 0;
85 }
86
87 if ( type == NULL )
88 type = DEFAULTMANAGER;
89
90 if ( strcmp (type, EVENTSVC_THREADMANAGER) == 0 ) {
91 _instance = CreateThreadManager ();
92 if ( _instance != 0 )
93 return 0;
94 }
95
96 if ( strcmp (type, EVENTSVC_POLLMANAGER) == 0 ) {
97 _instance = CreatePollManager ();
98 if ( _instance != 0 )
99 return 0;
100 }
101
102 MSG_IOevent_Managertype_SET (msp, type);
103 return MSG_IOevent_Managertype;
104 }
105}
106//␌
107
108namespace Poll {
109 Provider *Provider::instance;
110 static pthread_mutex_t poller_mutex = PTHREAD_MUTEX_INITIALIZER;
111
112 int Provider::ProviderFactory (const char *type, MSGstruct *msp)
113 {
114 MutexLock mlock (&poller_mutex);
115
116 if ( instance != 0 ) {
117 MSG_IOevent_PollProviderCreated_LOG ();
118 return 0;
119 }
120
121 if ( type == 0 || strcmp (type, "epoll") == 0 ) {
122 instance = CreatePollepollProvider ();
123 if ( instance != 0 )
124 return 0;
125 }
126
127 if ( type == 0 || strcmp (type, "port") == 0 ) {
128 instance = CreatePollportProvider ();
129 if ( instance != 0 )
130 return 0;
131 }
132
133 if ( type == 0 || strcmp (type, "poll") == 0 ) {
134 instance = CreatePollpollProvider ();
135 if ( instance != 0 )
136 return 0;
137 }
138
139 if ( type == 0 || strcmp (type, "select") == 0 ) {
140 instance = CreatePollselectProvider ();
141 if ( instance != 0 )
142 return 0;
143 }
144
145 MSG_IOevent_PollProvidertype_SET (msp, type);
146 return MSG_IOevent_PollProvidertype;
147 }
148}
149//␌
150
151namespace Stream {
152
153 Provider *Provider::ProviderFactory (const char *type, MSGstruct *msp)
154 {
155 if ( type == NULL )
156 type = DEFAULTSTREAMPROVIDER;
157
158 if ( strcmp (type, "socketpoll") == 0 ) {
159 // If no poll provider, create using the default
162 return 0;
163
164 return CreateSocketPollProvider ();
165 }
166
167 MSG_IOevent_StreamProvidertype_SET (msp, type);
168 return 0;
169 }
170}
171//␌
172
173namespace Datagram {
174
175 Provider *Provider::ProviderFactory (const char *type, MSGstruct *msp)
176 {
177 if ( type == NULL )
178 type = DEFAULTDATAGRAMPROVIDER;
179
180 if ( strcmp (type, "socketpoll") == 0 ) {
181 // If no poll provider, create using the default
184 return 0;
185
186 return CreateSocketPollProvider ();
187 }
188
189 MSG_IOevent_StreamProvidertype_SET (msp, type);
190 return 0;
191 }
192}
193
194//␌
195// C interfaces
196
197IC_CDECL void EventServiceRun (void)
198{
199 Event::Manager::GetManager()->Run(false);
200}
201
202
203IC_CDECL void EventAsyncDispatch (void (*func)(void *), void *param)
204{
205 Event::AsyncObject::Invoke (reinterpret_cast<Event::AsyncObject::FuncPtr>(func),
206 param);
207
208}
209
210IC_CDECL void EventServiceLongLived (void)
211{
212 Event::Manager::GetManager()->LongLived();
213}
214
215IC_CDECL void EventServiceTerminate (int millisecs)
216{
217 Event::Manager::GetManager()->Terminate(millisecs);
218}
Provider of a stream interface.
static EVENTSVC_DLL Provider * ProviderFactory(const char *type, MSGstruct *msp)
Factory method for Stream provider.
Definition EventSvc.C:175
static int ProviderFactory(const char *type, MSGstruct *msg)
Factory method.for stream provider.
Definition EventSvc.C:112
static Provider * GetPollService()
Implement as a singleton.
Provider of a stream interface.
static EVENTSVC_DLL Provider * ProviderFactory(const char *type, MSGstruct *msp)
Factory method for Stream provider.
Definition EventSvc.C:153
Interface between a user of a datagram service and its provider.
Interface between a user of a stream and the provider of a stream.

All rights reserved © 2002 - 2024 Isode Ltd.