summaryrefslogtreecommitdiff
path: root/ACE/ace/k_Efficient_P_Strategy.inl
blob: b1fe2c17a18ef8bf404691e7aa23246fe271c14a (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
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 
bool k_Efficient_P_Strategy<AnnotationId>::is_deadlock_potential(AnnotationId handle)
{
  int annotation = DA_Strategy_Base<AnnotationId>::get_annotation(handle);
  return (annotation >= get_min_illegal());
}

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