Fix compilation without pthreads.
[oota-llvm.git] / lib / Support / Annotation.cpp
1 //===-- Annotation.cpp - Implement the Annotation Classes -----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the AnnotationManager class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Support/Annotation.h"
15 #include "llvm/Support/ManagedStatic.h"
16 #include "llvm/System/RWMutex.h"
17 #include <map>
18 #include <cstring>
19 using namespace llvm;
20
21 Annotation::~Annotation() {}  // Designed to be subclassed
22
23 Annotable::~Annotable() {   // Virtual because it's designed to be subclassed...
24   Annotation *A = AnnotationList;
25   while (A) {
26     Annotation *Next = A->getNext();
27     delete A;
28     A = Next;
29   }
30 }
31
32 namespace {
33   class StrCmp {
34   public:
35     bool operator()(const char *a, const char *b) const {
36       return strcmp(a, b) < 0;
37     }
38   };
39 }
40
41 typedef std::map<const char*, unsigned, StrCmp> IDMapType;
42 static unsigned IDCounter = 0;  // Unique ID counter
43
44 // Static member to ensure initialiation on demand.
45 static ManagedStatic<IDMapType> IDMap;
46 static ManagedStatic<sys::SmartRWMutex<true> > AnnotationsLock;
47
48 // On demand annotation creation support...
49 typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *);
50 typedef std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType;
51
52 static ManagedStatic<FactMapType> TheFactMap;
53 static FactMapType &getFactMap() {
54   return *TheFactMap;
55 }
56
57 static void eraseFromFactMap(unsigned ID) {
58   sys::SmartScopedWriter<true> Writer(&*AnnotationsLock);
59   TheFactMap->erase(ID);
60 }
61
62 AnnotationID AnnotationManager::getID(const char *Name) {  // Name -> ID
63   AnnotationsLock->reader_acquire();
64   IDMapType::iterator I = IDMap->find(Name);
65   IDMapType::iterator E = IDMap->end();
66   AnnotationsLock->reader_release();
67   
68   if (I == E) {
69     sys::SmartScopedWriter<true> Writer(&*AnnotationsLock);
70     I = IDMap->find(Name);
71     if (I == IDMap->end())
72       (*IDMap)[Name] = IDCounter++;   // Add a new element
73     return AnnotationID(IDCounter-1);
74   }
75   return AnnotationID(I->second);
76 }
77
78 // getID - Name -> ID + registration of a factory function for demand driven
79 // annotation support.
80 AnnotationID AnnotationManager::getID(const char *Name, Factory Fact,
81                                       void *Data) {
82   AnnotationID Result(getID(Name));
83   registerAnnotationFactory(Result, Fact, Data);
84   return Result;
85 }
86
87 // getName - This function is especially slow, but that's okay because it should
88 // only be used for debugging.
89 //
90 const char *AnnotationManager::getName(AnnotationID ID) {  // ID -> Name
91   sys::SmartScopedReader<true> Reader(&*AnnotationsLock);
92   IDMapType &TheMap = *IDMap;
93   for (IDMapType::iterator I = TheMap.begin(); ; ++I) {
94     assert(I != TheMap.end() && "Annotation ID is unknown!");
95     if (I->second == ID.ID) return I->first;
96   }
97 }
98
99 // registerAnnotationFactory - This method is used to register a callback
100 // function used to create an annotation on demand if it is needed by the
101 // Annotable::findOrCreateAnnotation method.
102 //
103 void AnnotationManager::registerAnnotationFactory(AnnotationID ID, AnnFactory F,
104                                                   void *ExtraData) {
105   if (F) {
106     sys::SmartScopedWriter<true> Writer(&*AnnotationsLock);
107     getFactMap()[ID.ID] = std::make_pair(F, ExtraData);
108   } else {
109     eraseFromFactMap(ID.ID);
110   }
111 }
112
113 // createAnnotation - Create an annotation of the specified ID for the
114 // specified object, using a register annotation creation function.
115 //
116 Annotation *AnnotationManager::createAnnotation(AnnotationID ID,
117                                                 const Annotable *Obj) {
118   AnnotationsLock->reader_acquire();
119   FactMapType::iterator I = getFactMap().find(ID.ID);
120   if (I == getFactMap().end()) {
121     AnnotationsLock->reader_release();
122     return 0;
123   }
124   
125   AnnotationsLock->reader_release();
126   return I->second.first(ID, Obj, I->second.second);
127 }