summaryrefslogtreecommitdiff
path: root/ACE/examples/Reactor/WFMO_Reactor/APC.cpp
blob: 00d21aa59c55bbd78e698218dd61b9e6e4f9ee95 (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

//=============================================================================
/**
 *  @file    APC.cpp
 *
 *  Tests the WFMO_Reactor's ability to handle regular APC
 *  notifications.
 *
 *  @author Irfan Pyarali <irfan@cs.wustl.edu>
 */
//=============================================================================

#include "ace/OS_main.h"

#if defined (ACE_WIN32) && _WIN32_WINNT >= 0x400

#include "ace/Reactor.h"
#include "ace/Auto_Event.h"
#include "ace/Log_Msg.h"

class Event_Handler : public ACE_Event_Handler
{
public:
  int handle_signal (int signum, siginfo_t * = 0, ucontext_t * = 0);

  int handle_timeout (const ACE_Time_Value &tv, const void *arg = 0);

  ACE_Auto_Event handle_;
  int iterations_;
};

static Event_Handler *global_event_handler;

static void WINAPI
apc_callback (DWORD)
{
  ACE_DEBUG ((LM_DEBUG,
              "(%t) apc occurred @ %T\n"));

  global_event_handler->handle_.signal ();
}

void
queue_apc ()
{
  DWORD result = ::QueueUserAPC (reinterpret_cast<PAPCFUNC> (&apc_callback),
                                                         // pointer to APC function
                                 ::GetCurrentThread (),  // handle to the thread
                                 0);                     // argument for the APC function
  if (result == FALSE)
    ACE_OS::exit (-1);
}

int
Event_Handler::handle_signal (int, siginfo_t *, ucontext_t *)
{
  --this->iterations_;

  if (this->iterations_ == 0)
    {
      ACE_Reactor::instance ()->remove_handler (this->handle_.handle (),
                                                ACE_Event_Handler::DONT_CALL);
      ACE_Reactor::end_event_loop ();
    }

  return 0;
}

int
Event_Handler::handle_timeout (const ACE_Time_Value &,
                               const void *)
{
  ACE_DEBUG ((LM_DEBUG,
              "(%t) timeout occurred @ %T\n"));
  queue_apc ();
  return 0;
}

int
ACE_TMAIN (int, ACE_TCHAR *[])
{
  Event_Handler event_handler;
  event_handler.iterations_ = 5;
  global_event_handler = &event_handler;

  int result = ACE_Reactor::instance ()->register_handler (&event_handler,
                                                           event_handler.handle_.handle ());
  ACE_TEST_ASSERT (result == 0);

  ACE_Time_Value timeout (2);
  result = ACE_Reactor::instance ()->schedule_timer (&event_handler,
                                                     0,
                                                     timeout,
                                                     timeout);
  ACE_TEST_ASSERT (result != -1);

  ACE_Reactor::run_alertable_event_loop ();

  ACE_Reactor::instance ()->cancel_timer(&event_handler);

  return 0;
}
#else /* !ACE_WIN32 */
int
ACE_TMAIN (int, ACE_TCHAR *[])
{
  return 0;
}
#endif /* ACE_WIN32 */