summaryrefslogtreecommitdiff
path: root/ace/Asynch_Pseudo_Task.cpp
blob: 1e795b8c7e944af3a7381457ccf0153c707fd1a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// $Id$

#include "ace/Asynch_Pseudo_Task.h"

ACE_RCSID(ace, Asynch_Pseudo_Task, "$Id$")

ACE_Asynch_Pseudo_Task::ACE_Asynch_Pseudo_Task()
  : flg_active_ (0),
    select_reactor_ (),               // should be initialized before reactor_
    reactor_ (&select_reactor_, 0),   // don't delete implementation
    token_ (select_reactor_.lock ()), // we can use reactor token	
    finish_count_ (0)
{
}

ACE_Asynch_Pseudo_Task::~ACE_Asynch_Pseudo_Task()
{
  stop();
}

int 
ACE_Asynch_Pseudo_Task::is_active (void)
{
  ACE_MT (ACE_GUARD_RETURN (ACE_Lock, ace_mon, this->token_, -1));
  return flg_active_;
}

int 
ACE_Asynch_Pseudo_Task::start (void)
{
  ACE_MT (ACE_GUARD_RETURN (ACE_Lock, ace_mon, this->token_, -1));

  if (this->flg_active_)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_LIB_TEXT ("%N:%l:%p\n"),
                       ACE_LIB_TEXT ("ACE_Asynch_Pseudo_Task::start already started")),
                       -1);

  if (this->reactor_.initialized () == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_LIB_TEXT ("%N:%l:%p\n"),
                       ACE_LIB_TEXT ("ACE_Asynch_Pseudo_Task::start reactor is not initialized")),
                       -1);


  if (this->activate (THR_NEW_LWP | THR_JOINABLE, 1) != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_LIB_TEXT ("%N:%l:%p\n"),
                       ACE_LIB_TEXT ("ACE_Asynch_Pseudo_Task::start failed")),
                       -1);

  this->flg_active_ = 1;
  return 0;
}

int 
ACE_Asynch_Pseudo_Task::stop (void)
{
  {
    ACE_MT (ACE_GUARD_RETURN (ACE_Lock, ace_mon, this->token_, -1));

    if (this->flg_active_ == 0)  // already stopped 
      return 0;

    reactor_.end_reactor_event_loop ();
  }

  int rc = this->wait ();

  if  (rc != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_LIB_TEXT ("%N:%l:%p\n"),
                       ACE_LIB_TEXT ("ACE_Asynch_Pseudo_Task::stop failed")),
                       -1);

  {
    ACE_MT (ACE_GUARD_RETURN (ACE_Lock, ace_mon, this->token_, -1));
    this->flg_active_ = 0;

    if (this->reactor_.initialized ())
      this->reactor_.close ();

    while (finish_count_ > 0)
      {
        ACE_MT (ace_mon.release ());
        finish_event_.wait ();

        ACE_MT (ace_mon.acquire ());
        finish_event_.reset ();
      }
  }

  return rc;
}

int
ACE_Asynch_Pseudo_Task::lock_finish (void)
{
  ACE_MT (ACE_GUARD_RETURN (ACE_Lock, ace_mon, this->token_, -1));
  finish_count_ ++;
  return 0;
}

int
ACE_Asynch_Pseudo_Task::unlock_finish (void)
{
  ACE_MT (ACE_GUARD_RETURN (ACE_Lock, ace_mon, this->token_, -1));

  --finish_count_;
  finish_event_.signal ();

  return 0;
}

int
ACE_Asynch_Pseudo_Task::svc (void)
{
#if !defined (ACE_WIN32) 

  sigset_t RT_signals;

  if (sigemptyset (&RT_signals) == -1)
    ACE_ERROR ((LM_ERROR,
                ACE_LIB_TEXT ("Error:(%P | %t):%p\n"),
                ACE_LIB_TEXT ("sigemptyset failed")));

  int member = 0;

  for (int si = ACE_SIGRTMIN; si <= ACE_SIGRTMAX; si++)
    {
      member = sigismember (& RT_signals , si);
      if (member == 1)
        {
          sigaddset (&RT_signals, si);
        }
    }

  if (ACE_OS::pthread_sigmask (SIG_BLOCK, &RT_signals, 0) != 0)
    ACE_ERROR ((LM_ERROR,
                ACE_LIB_TEXT ("Error:(%P | %t):%p\n"),
                ACE_LIB_TEXT ("pthread_sigmask failed")));
#endif

  reactor_.owner (ACE_Thread::self());

  reactor_.run_reactor_event_loop ();

  return 0;
}



int
ACE_Asynch_Pseudo_Task::register_io_handler (ACE_HANDLE handle,
                                             ACE_Event_Handler *handler,
                                             ACE_Reactor_Mask mask,
                                             int flg_suspend)
{
  //  Return codes : 
  //   0  success
  //  -1  reactor errors
  //  -2  task not active 

  ACE_MT (ACE_GUARD_RETURN (ACE_Lock, ace_mon, this->token_, -1));

  if (this->flg_active_ == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_LIB_TEXT ("%N:%l:ACE_Asynch_Pseudo_Task::register_io_handler \n")
                       ACE_LIB_TEXT ("task not active \n")),
                      -2);
    
  // Register the handler with the reactor.
  int retval = this->reactor_.register_handler (handle, handler, mask);
                                                    
  if (retval == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_LIB_TEXT ("%N:%l:ACE_Asynch_Pseudo_Task::register_io_handler \n")
                       ACE_LIB_TEXT ("register_handler failed \n")),
                      -1);

  if (flg_suspend == 0 )
    return 0;

  // Suspend the <handle> now. Enable only when the <accept> is issued
  // by the application.
  retval = this->reactor_.suspend_handler (handle);

  if (retval == -1)
    {
      this->reactor_.remove_handler (handle,
                                     ACE_Event_Handler::ALL_EVENTS_MASK
                                     | ACE_Event_Handler::DONT_CALL); 
 
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_LIB_TEXT ("%N:%l:ACE_Asynch_Pseudo_Task::register_io_handler \n")
                         ACE_LIB_TEXT ("suspend_handler failed \n")),
                        -1);
    }

  return 0;
}

int
ACE_Asynch_Pseudo_Task::remove_io_handler (ACE_HANDLE handle)
{
  //  Return codes : 
  //   0  success
  //  -1  reactor errors
  //  -2  task not active 

  ACE_MT (ACE_GUARD_RETURN (ACE_Lock, ace_mon, this->token_, -1));

  if (this->flg_active_ == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_LIB_TEXT ("%N:%l:ACE_Asynch_Pseudo_Task::remove_io_handler \n")
                       ACE_LIB_TEXT ("task not active \n")),
                      -2);
    
  int retval =
    this->reactor_.remove_handler (handle ,
                                   ACE_Event_Handler::ALL_EVENTS_MASK
                                   | ACE_Event_Handler::DONT_CALL); 
  if (retval == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_LIB_TEXT ("%N:%l:ACE_Asynch_Pseudo_Task::remove_io_handler \n")
                       ACE_LIB_TEXT ("remove_handler failed \n")),
                      -1);

  return 0;
}

int
ACE_Asynch_Pseudo_Task::remove_io_handler (ACE_Handle_Set &set)
{
  //  Return codes : 
  //   0  success
  //  -1  reactor errors
  //  -2  task not active 

  ACE_MT (ACE_GUARD_RETURN (ACE_Lock, ace_mon, this->token_, -1));

  if (this->flg_active_ == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_LIB_TEXT ("%N:%l:ACE_Asynch_Pseudo_Task::remove_io_handler \n")
                       ACE_LIB_TEXT ("task not active \n")),
                      -2);
    
  int retval =
    this->reactor_.remove_handler (set ,
                                   ACE_Event_Handler::ALL_EVENTS_MASK
                                   | ACE_Event_Handler::DONT_CALL); 
  if (retval == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_LIB_TEXT ("%N:%l:ACE_Asynch_Pseudo_Task::remove_io_handler \n")
                       ACE_LIB_TEXT ("remove_handler failed \n")),
                      -1);

  return 0;
}

int
ACE_Asynch_Pseudo_Task::suspend_io_handler (ACE_HANDLE handle)
{
  //  Return codes : 
  //   0  success
  //  -1  reactor errors
  //  -2  task not active 

  ACE_MT (ACE_GUARD_RETURN (ACE_Lock, ace_mon, this->token_, -1));

  if (this->flg_active_ == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_LIB_TEXT ("%N:%l:ACE_Asynch_Pseudo_Task::suspend_io_handler \n")
                       ACE_LIB_TEXT ("task not active \n")),
                      -2);
    
  int retval = this->reactor_.suspend_handler (handle);

  if (retval == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_LIB_TEXT ("%N:%l:ACE_Asynch_Pseudo_Task::suspend_io_handler \n")
                       ACE_LIB_TEXT ("suspend_handler failed \n")),
                      -1);

  return 0;
}

int
ACE_Asynch_Pseudo_Task::resume_io_handler (ACE_HANDLE handle)
{
  //  Return codes : 
  //   0  success
  //  -1  reactor errors
  //  -2  task not active 

  ACE_MT (ACE_GUARD_RETURN (ACE_Lock, ace_mon, this->token_, -1));

  if (this->flg_active_ == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_LIB_TEXT ("%N:%l:ACE_Asynch_Pseudo_Task::resume_io_handler \n")
                       ACE_LIB_TEXT ("task not active \n")),
                      -2);
    
  int retval = this->reactor_.resume_handler (handle);

  if (retval == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_LIB_TEXT ("%N:%l:ACE_Asynch_Pseudo_Task::resume_io_handler \n")
                       ACE_LIB_TEXT ("resume_handler failed \n")),
                      -1);

  return 0;
}