summaryrefslogtreecommitdiff
path: root/TAO/performance-tests/CSD_Strategy/TestInf/AppShutdown.cpp
blob: c975dea0986512103f07542386e09774fa0467ae (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
// $Id$
#include "AppShutdown.h"
#include "TestAppExceptionC.h"
#include "ace/Log_Msg.h"
#include "ace/OS_NS_unistd.h"

AppShutdown::AppShutdown()
  : num_clients_(0),
    num_clients_shutdown_(0)
{
}


AppShutdown::~AppShutdown()
{
}


void
AppShutdown::init(CORBA::ORB_ptr orb,
                  unsigned num_clients)
{
  if ((!CORBA::is_nil(this->orb_.in())) ||
      (CORBA::is_nil(orb))              ||
      (num_clients == 0))
    {
      // Already init()'ed, or bad argument values.
      throw TestAppException();
    }
  else
    {
      this->orb_ = CORBA::ORB::_duplicate(orb);
      this->num_clients_ = num_clients;
      this->num_clients_shutdown_ = 0;
    }
}


void
AppShutdown::wait ()
{
  this->orb_shutdown_task_.wait();
}


void
AppShutdown::client_done()
{
  if ((this->num_clients_ == 0) || (CORBA::is_nil(this->orb_.in())))
    {
      ACE_ERROR((LM_ERROR, "(%P|%t) AppShutdown was never initialized.\n"));
      return;
    }

  unsigned cur_shutdown;

  {
    GuardType guard(this->lock_);
    cur_shutdown = ++this->num_clients_shutdown_;
  }

  if (cur_shutdown == this->num_clients_)
    {
      // Sleep for one second before shutting down the ORB.  This
      // is a poor-man version of "wait until the CSD request queue drains".
      ACE_OS::sleep(1);
      this->orb_shutdown_task_.orb(this->orb_.in());
      if (this->orb_shutdown_task_.open(0) != 0)
        {
          ACE_ERROR((LM_ERROR, "(%P|%t) AppShutdown failed to create orb "\
            "shutdown thread.\n"));
          return;
        }
    }
}


AppShutdown*
AppShutdown::instance ()
{
  static AppShutdown app_shutdown;
  return &app_shutdown;
}