summaryrefslogtreecommitdiff
path: root/ACE/ace/DA_Strategy_Base.h
blob: c2cc5b32c8edb0ddc30d7b6c034551697af0866f (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
// -*- C++ -*-

//=============================================================================
/**
 *  @file    DA_Strategy_Base.h
 *
 *
 *
 *  The Deadlock Avoidance Strategy Base (DA_Strategy_Base) class
 *  is an abstract base class for Strategies that implement deadlock
 *  avoidance algorithms.  This class provides interfaces for passing
 *  annotations for call graph annotations, number of available threads, as well 
 *  as methods to determine whether a call is safe to make.
 *
 *
 *  @author Paul Oberlin <pauloberlin@gmail.com>
 */
//=============================================================================

#ifndef DA_STRATEGY_BASE_H
#define DA_STRATEGY_BASE_H

#include /**/ "ace/pre.h"
#include "ace/Hash_Map_Manager.h"
#include "ace/Thread_Mutex.h"
#include "ace/Atomic_Op_T.h"

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

class ACE_Event_Handler;

template <typename AnnotationId>
class DA_Strategy_Base {

  //The annotations consist of an identifier and a resource cost value
typedef ACE_Hash_Map_Entry<ACE_Event_Handler *, int> HASH_EH_ENTRY;


typedef ACE_Hash_Map_Manager_Ex<AnnotationId,
                                int,
                                ACE_Hash<AnnotationId>,
                                ACE_Equal_To<AnnotationId>,
                                ACE_Thread_Mutex> HASH_ANNOTATIONS_MAP;

typedef ACE_Hash_Map_Iterator_Ex<AnnotationId,
                                 int,
                                 ACE_Hash<AnnotationId>,
                                 ACE_Equal_To<AnnotationId>,
                                 ACE_Thread_Mutex> HASH_ANNOTATIONS_ITER;

typedef ACE_Hash_Map_Const_Iterator_Ex<AnnotationId,
                                       int,
                                       ACE_Hash<AnnotationId>,
                                       ACE_Equal_To<AnnotationId>,
                                       ACE_Thread_Mutex> HASH_ANNOTATIONS_CONST_ITER;

typedef ACE_Hash_Map_Reverse_Iterator_Ex<AnnotationId,
                                         int,
                                         ACE_Hash<AnnotationId>,
                                         ACE_Equal_To<AnnotationId>,
                                         ACE_Thread_Mutex> HASH_ANNOTATIONS_REVERSE_ITER;

typedef HASH_ANNOTATIONS_MAP Annotations_Table;

public:
    DA_Strategy_Base(int maxThreads);
    virtual ~DA_Strategy_Base();
 
    virtual bool is_deadlock_potential(AnnotationId handle)=0;
    virtual void grant(AnnotationId handle)=0;
    virtual void release(AnnotationId upcall_handle)=0;
    int get_max_threads() { return num_avail_threads_.value();}
    HASH_ANNOTATIONS_CONST_ITER get_annotations_iter() const;
    virtual int get_annotation (AnnotationId handle) const;
    virtual int add_annotation (AnnotationId handle, int annotation);
    virtual int remove_annotation (AnnotationId handle);
    virtual int set_annotations_table (const HASH_ANNOTATIONS_REVERSE_ITER& table);

private:
  HASH_ANNOTATIONS_MAP annotations_repo_;
  ACE_RW_Thread_Mutex lock_;
  ACE_Atomic_Op<ACE_Thread_Mutex, long> num_avail_threads_;

};

#if defined (__ACE_INLINE__)
#include "ace/DA_Strategy_Base.inl"
#endif /* __ACE_INLINE__ */

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

#endif /* DA_STRATEGY_BASE_H */