summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Concurrency/CC_Lock.h
blob: f84ffd871dde8559d43faad14dba11823686f10f (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
/* -*- C++ -*- */

//=============================================================================
/**
 *  @file    CC_Lock.h
 *
 *  $Id$
 *
 *    This class implements a lock used by the lock set from the
 *    concurrency control service
 *
 *
 *  @author Torben Worm <tworm@cs.wustl.edu>
 */
//=============================================================================


#ifndef _CC_LOCK_H
#define _CC_LOCK_H
#include /**/ "ace/pre.h"

#include "ace/config-all.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

#include "orbsvcs/orbsvcs/CosConcurrencyControlC.h"
#include "concurrency_export.h"

/// This constant defines the number of lock modes. There is really no
/// way to set this constant dynamically because the nuber of lock
/// modes are not stated as part of the IDL.
#define NUMBER_OF_LOCK_MODES 5

/**
 * @class CC_Lock
 *
 * @brief CC_Lock
 *
 * This class implements the lock concept from the concurrency
 * control service. The lock holds its mode - this might later
 * be changed to subclasses depending on the differences of the
 * locks.  At present the is only a lock-pr-thread/client-type
 * which is essentially a write lock since it is not allowed to
 * have more than one lock pr. servant in this implementation.
 */
class TAO_Concurrency_Export CC_Lock
{
public:
  /// Creates the lock with mode = intention_read (weakest)
  CC_Lock (void);

  /// Creates the lock with the desired mode
  CC_Lock (CosConcurrencyControl::lock_mode mode);

  /// Deletes the lock
  ~CC_Lock (void);

  /// Acquires this lock. Blocks until lock is obtained
  void lock (ACE_ENV_SINGLE_ARG_DECL);

  /// Tries to acquire this lock. If it is not possible to acquire the
  /// lock, false is returned
  CORBA::Boolean try_lock (ACE_ENV_SINGLE_ARG_DECL);

  /// Releases this lock.
  void unlock (ACE_ENV_SINGLE_ARG_DECL);

  /// Changes the mode of this lock.
  void change_mode (CosConcurrencyControl::lock_mode new_mode
                   ACE_ENV_ARG_DECL);

  /// Sets the mode_ of the lock. Used in initialization
  void set_mode (CosConcurrencyControl::lock_mode mode);

  /// Returns true if this lock is compatible with the other lock.
  CORBA::Boolean Compatible (const CC_Lock &other);

  /// Returns true is this lock is compatible with the referenced mode.
  CORBA::Boolean Compatible (CosConcurrencyControl::lock_mode mode);

  /// Returns the mode of the lock.
  CosConcurrencyControl::lock_mode GetMode (void);

  /// Returns the number of times this lock have been locked
  int GetLocksHeld(void);

  /// Decrements the number of locks held in this mode. Used by change_mode.
  void DecLocksHeld(void);

  /// Dumps the state of the object to stdout
  void dump(void);

protected:
  /// Holds the lock's mode.
  CosConcurrencyControl::lock_mode mode_;

private:
  /// If greater than zero the lock is held (that number of times).
  int lock_held_;

  /// Defines the compatibility of the locks.
  static CORBA::Boolean compatible_[NUMBER_OF_LOCK_MODES][NUMBER_OF_LOCK_MODES];
};

/**
 * @class CC_LockModeIterator
 *
 * @brief CC_LockModeIterator
 *
 * This class implements an iterator over the lock modes in
 * order to make an ordered traversal over the locks from the
 * weakest (intention read) to the strongest (write).
 * Ordering: IR -> R -> U -> IW -> W
 */
class TAO_Concurrency_Export CC_LockModeIterator
{
public:
  /// Default constructor
  CC_LockModeIterator(void);

  /// Destructor
  ~CC_LockModeIterator(void);

  /// Reset the iterator to the first element
  void First(void);

  /// Advance the iterator to the next element
  /// Throws exception if out of range
  void Next(ACE_ENV_SINGLE_ARG_DECL);

  /// Returns true if the iterator has reached the last element
  CORBA::Boolean IsDone(void);

  /// Get the current element
  CosConcurrencyControl::lock_mode GetLockMode(void);

private:
  CosConcurrencyControl::lock_mode current_;
};

#include /**/ "ace/post.h"
#endif /* !defined (_CC_LOCK_H) */