summaryrefslogtreecommitdiff
path: root/ACE/ace/Asynch_Pseudo_Task.cpp
blob: 72dff50ab0f5a5cf491c625c6197f427fd723e97 (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
#include "ace/Asynch_Pseudo_Task.h"

#include "ace/OS_NS_errno.h"
#include "ace/OS_NS_signal.h"

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

ACE_Asynch_Pseudo_Task::ACE_Asynch_Pseudo_Task ()
  : select_reactor_ (),               // should be initialized before reactor_
    reactor_ (&select_reactor_, 0)    // don't delete implementation
{
}

ACE_Asynch_Pseudo_Task::~ACE_Asynch_Pseudo_Task ()
{
  this->stop ();
}

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

  return this->activate () == -1 ? -1 : 0;   // If started, return 0
}

int
ACE_Asynch_Pseudo_Task::stop ()
{
  if (this->thr_count () == 0)  // already stopped
    return 0;

  if (this->reactor_.end_reactor_event_loop () == -1)
    return -1;

  this->wait ();
  this->reactor_.close ();
  return 0;
}

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

  sigset_t RT_signals;

  sigemptyset (&RT_signals);
  for (int si = ACE_SIGRTMIN; si <= ACE_SIGRTMAX; si++)
    sigaddset (&RT_signals, si);

  if (ACE_OS::pthread_sigmask (SIG_BLOCK, &RT_signals, 0) != 0)
    ACELIB_ERROR ((LM_ERROR,
                ACE_TEXT ("Error:(%P | %t):%p\n"),
                ACE_TEXT ("pthread_sigmask")));
#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)
{
  // Register the handler with the reactor.
  if (-1 == this->reactor_.register_handler (handle, handler, mask))
    return -1;

  if (flg_suspend == 0)
    return 0;

  // Suspend the handle now. Enable only when the accept is issued
  // by the application.
  if (this->reactor_.suspend_handler (handle) == -1)
    {
      ACELIB_ERROR
        ((LM_ERROR,
          ACE_TEXT ("%N:%l:%p\n"),
          ACE_TEXT ("register_io_handler (suspended)")));
      this->reactor_.remove_handler (handle, ACE_Event_Handler::ALL_EVENTS_MASK
                                     | ACE_Event_Handler::DONT_CALL);
      return -1;
    }

  return 0;
}

int
ACE_Asynch_Pseudo_Task::remove_io_handler (ACE_HANDLE handle)
{
  return this->reactor_.remove_handler (handle,
                                        ACE_Event_Handler::ALL_EVENTS_MASK
                                        | ACE_Event_Handler::DONT_CALL);
}

int
ACE_Asynch_Pseudo_Task::remove_io_handler (ACE_Handle_Set &set)
{
  return this->reactor_.remove_handler (set, ACE_Event_Handler::ALL_EVENTS_MASK
                                        | ACE_Event_Handler::DONT_CALL);
}

int
ACE_Asynch_Pseudo_Task::suspend_io_handler (ACE_HANDLE handle)
{
  return this->reactor_.suspend_handler (handle);
}

int
ACE_Asynch_Pseudo_Task::resume_io_handler (ACE_HANDLE handle)
{
  return this->reactor_.resume_handler (handle);
}

ACE_END_VERSIONED_NAMESPACE_DECL