summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Trader/Monitor.h
blob: 21741ca7bda3e85c4a8514c04f284637c7798ccc (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
/* -*- C++ -*- */

// ============================================================================
// $Id$
//
// = LIBRARY
//    orsvcs
// 
// = FILENAME
//    Monitor.h
//
// = AUTHOR
//    Irfan Pyarali <irfan@cs.wustl.edu>
//   
// ============================================================================

#ifndef TAO_MONITOR_H
#define TAO_MONITOR_H

template <class TYPE, class TAO_LOCK>
class TAO_Monitor : public TYPE
{
  //
  // = TITLE
  //
  //     A utility class for writing applications where access to an
  //     object must be synchronized in a multithreaded environment.
  //     
  //
  // = DESCRIPTION
  //
  //     A monitor associates a lock of type TAO_LOCK with an object
  //     of type TYPE.  The user is fully responsible for invoking all
  //     necessary acquire/release operations on that lock (i.e. the
  //     monitor does not automate any lock operations - all
  //     operations are invoked explicitly by the user).  The
  //     usefullness of a monitor is in associating one lock with one
  //     object without the need for inheritance or much code.
  //  
  //
public:
  
  typedef TAO_LOCK LOCK_TYPE;

  TAO_Monitor (void)
    : delete_ (1)
    {
      ACE_NEW (this->lock_, TAO_LOCK);
    }
  
  TAO_Monitor (const TAO_Monitor& monitor)
    {
      // Assume control of their lock.
      TAO_Monitor* mon = (TAO_Monitor*)&monitor;
      mon->delete_ = 0;
      this->delete_ = 1;
      this->lock_ = mon->lock_;
    }

  ~TAO_Monitor (void)
    {
      if (this->delete_)
	delete this->lock_;
    }
  
  // Return a reference to the lock that I use.
  TAO_LOCK &lock (void)
  {
    return *this->lock_;
  }

  // Return a reference to the lock that I use.
  const TAO_LOCK &lock (void) const
  {
    return *this->lock_;
  }

protected:

  int delete_;
  
  TAO_LOCK* lock_;
  // Lock used to monitor the object.
};

#endif /* #define TAO_MONITOR_H */