summaryrefslogtreecommitdiff
path: root/ACE/ace/k_Efficient_P_Strategy.h
blob: 846c234323d83ccb386b7566ec8481b1e2947817 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// -*- C++ -*-

//=============================================================================
/**
 *  @file    k_Efficient_P_Strategy.h
 *
 *
 *
 *
 *
 *  @author Paul Oberlin <pauloberlin@gmail.com>
 */
//=============================================================================

#ifndef ACE_K_EFFICIENT_P_STRATEGY_H
#define ACE_K_EFFICIENT_P_STRATEGY_H

#include /**/ "ace/pre.h"

#include "ace/DA_Strategy_Base.h"
#include "ace/Mutex.h"
#include <vector>

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

template <typename AnnotationId>
class k_Efficient_P_Strategy : public DA_Strategy_Base<AnnotationId> {

  //The annotations consist of an identifier and a resource cost value

public:
    //note: k must be less than maxThreads
    k_Efficient_P_Strategy(int maxThreads, int k);
    virtual ~k_Efficient_P_Strategy();
    virtual int is_deadlock_potential(AnnotationId handle);
  virtual void grant(AnnotationId handle);
    virtual void release(AnnotationId upcall_handle);
private:
    int compute_min_illegal();
    int get_min_illegal();
    int min_illegal_;
    ACE_Mutex computation_mutex_;
    int k_;
    bool min_illegal_is_computed_ ;
    std::vector<int> a;
    std::vector<int> A;
};
//#if defined (__ACE_INLINE__)
//#include "ace/k_Efficient_P_Strategy.inl"
//#endif /* __ACE_INLINE__ */

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

template <typename AnnotationId>
ACE_INLINE 
k_Efficient_P_Strategy<AnnotationId>::k_Efficient_P_Strategy(int maxThreads, int k)
:DA_Strategy_Base<AnnotationId>(maxThreads),
 k_(k)
  {
    a.resize(k_ + 1);
    A.resize(k_ + 1);
    for (int i=0; i<k_; ++i) {
      a[i] = 0;
      A[i] = 0;
    }
    min_illegal_ = maxThreads;
    min_illegal_is_computed_ = true;
}

template <typename AnnotationId>
ACE_INLINE 
k_Efficient_P_Strategy<AnnotationId>::~k_Efficient_P_Strategy()
{

}

template <typename AnnotationId>
ACE_INLINE 
int k_Efficient_P_Strategy<AnnotationId>::is_deadlock_potential(AnnotationId handle)
{
  int annotation = DA_Strategy_Base<AnnotationId>::get_annotation(handle);
  
  int min_illegal = get_min_illegal();
  if (annotation >= min_illegal)
  {
     return annotation - min_illegal + 1;
  }

  return 0;
}

template <typename AnnotationId>
ACE_INLINE
int
k_Efficient_P_Strategy<AnnotationId>::compute_min_illegal() 
{
  int T = this->get_max_threads();
  for (int i=0; i<k_; ++i) {
    if (!(A[i] < (T - i))) {
      return i;
    }
  }
  if (A[k_]>0) {
    return (T - A[k_]);
  } 
  return  T;
}

template <typename AnnotationId>
ACE_INLINE
int
k_Efficient_P_Strategy<AnnotationId>::get_min_illegal() 
{
  computation_mutex_.acquire();
  if (!min_illegal_is_computed_) {
	  min_illegal_ = compute_min_illegal();
	  min_illegal_is_computed_ = true;
  }
  computation_mutex_.release();
  return min_illegal_;
}

template <typename AnnotationId>
ACE_INLINE 
void k_Efficient_P_Strategy<AnnotationId>::grant(AnnotationId handle)
{
    int annotation = get_annotation(handle);
    computation_mutex_.acquire();
    if (annotation < k_) 
    {
      a[annotation] ++;
      for (int i=0; i<=annotation; ++i) 
      {
	      A[i]++;
      }
    } 
    else
    {
      a[k_] ++;
      for (int i=0; i<=k_ ; ++i)
      {
	      A[i]++;
      }
    }
    min_illegal_is_computed_ = false;
    computation_mutex_.release();
}

template <typename AnnotationId>
ACE_INLINE 
void k_Efficient_P_Strategy<AnnotationId>::release(AnnotationId handle)
{
    int annotation = get_annotation(handle);
    computation_mutex_.acquire();
/*    if (annotation < k ) {
      assert(a[annotation]>0);
    } else {
      assert(a[k] >0);
    }
*/
    if (annotation < k_)
    {
      a[annotation] --;
      for (int i=0; i<=annotation; ++i) 
      {
	      A[i]--;
      }
    } 
    else 
    {
      a[k_] --;
      for (int i=0; i<=k_ ; ++i) 
      {
	      A[i]--;
      }
    }
    min_illegal_is_computed_ = false;
    computation_mutex_.release();
}


ACE_END_VERSIONED_NAMESPACE_DECL

#include /**/ "ace/post.h"

#endif /* ACE_BASIC_P_STRATEGY_H */