1*ded186e9SIngo Weinhold /* 2*ded186e9SIngo Weinhold * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. 3*ded186e9SIngo Weinhold * Distributed under the terms of the MIT License. 4*ded186e9SIngo Weinhold */ 5*ded186e9SIngo Weinhold #ifndef THREAD_MODEL_H 6*ded186e9SIngo Weinhold #define THREAD_MODEL_H 7*ded186e9SIngo Weinhold 8*ded186e9SIngo Weinhold #include <string.h> 9*ded186e9SIngo Weinhold 10*ded186e9SIngo Weinhold #include "Model.h" 11*ded186e9SIngo Weinhold 12*ded186e9SIngo Weinhold 13*ded186e9SIngo Weinhold class ThreadModel { 14*ded186e9SIngo Weinhold public: 15*ded186e9SIngo Weinhold struct type_and_name; 16*ded186e9SIngo Weinhold class WaitObjectGroup; 17*ded186e9SIngo Weinhold 18*ded186e9SIngo Weinhold public: 19*ded186e9SIngo Weinhold ThreadModel(Model* model, 20*ded186e9SIngo Weinhold Model::Thread* thread); 21*ded186e9SIngo Weinhold ~ThreadModel(); 22*ded186e9SIngo Weinhold 23*ded186e9SIngo Weinhold Model* GetModel() const { return fModel; } 24*ded186e9SIngo Weinhold Model::Thread* GetThread() const { return fThread; } 25*ded186e9SIngo Weinhold 26*ded186e9SIngo Weinhold WaitObjectGroup* AddWaitObjectGroup( 27*ded186e9SIngo Weinhold const BObjectList<Model::ThreadWaitObject>& 28*ded186e9SIngo Weinhold waitObjects, 29*ded186e9SIngo Weinhold int32 start, int32 end); 30*ded186e9SIngo Weinhold inline int32 CountWaitObjectGroups() const; 31*ded186e9SIngo Weinhold inline WaitObjectGroup* WaitObjectGroupAt(int32 index) const; 32*ded186e9SIngo Weinhold 33*ded186e9SIngo Weinhold private: 34*ded186e9SIngo Weinhold typedef BObjectList<WaitObjectGroup> WaitObjectGroupList; 35*ded186e9SIngo Weinhold 36*ded186e9SIngo Weinhold private: 37*ded186e9SIngo Weinhold Model* fModel; 38*ded186e9SIngo Weinhold Model::Thread* fThread; 39*ded186e9SIngo Weinhold WaitObjectGroupList fWaitObjectGroups; 40*ded186e9SIngo Weinhold }; 41*ded186e9SIngo Weinhold 42*ded186e9SIngo Weinhold 43*ded186e9SIngo Weinhold struct ThreadModel::type_and_name { 44*ded186e9SIngo Weinhold uint32 type; 45*ded186e9SIngo Weinhold const char* name; 46*ded186e9SIngo Weinhold }; 47*ded186e9SIngo Weinhold 48*ded186e9SIngo Weinhold 49*ded186e9SIngo Weinhold class ThreadModel::WaitObjectGroup { 50*ded186e9SIngo Weinhold public: 51*ded186e9SIngo Weinhold WaitObjectGroup( 52*ded186e9SIngo Weinhold Model::ThreadWaitObject** waitObjects, 53*ded186e9SIngo Weinhold int32 count); 54*ded186e9SIngo Weinhold ~WaitObjectGroup(); 55*ded186e9SIngo Weinhold 56*ded186e9SIngo Weinhold inline uint32 Type() const; 57*ded186e9SIngo Weinhold inline const char* Name() const; 58*ded186e9SIngo Weinhold 59*ded186e9SIngo Weinhold inline int32 CountWaitObjects() const; 60*ded186e9SIngo Weinhold inline Model::ThreadWaitObject* WaitObjectAt(int32 index) const; 61*ded186e9SIngo Weinhold 62*ded186e9SIngo Weinhold static inline int CompareByTypeName(const WaitObjectGroup* a, 63*ded186e9SIngo Weinhold const WaitObjectGroup* b); 64*ded186e9SIngo Weinhold static inline int CompareWithTypeName( 65*ded186e9SIngo Weinhold const type_and_name* key, 66*ded186e9SIngo Weinhold const WaitObjectGroup* group); 67*ded186e9SIngo Weinhold 68*ded186e9SIngo Weinhold private: 69*ded186e9SIngo Weinhold Model::ThreadWaitObject** fWaitObjects; 70*ded186e9SIngo Weinhold int32 fCount; 71*ded186e9SIngo Weinhold }; 72*ded186e9SIngo Weinhold 73*ded186e9SIngo Weinhold 74*ded186e9SIngo Weinhold // #pragma mark - ThreadModel 75*ded186e9SIngo Weinhold 76*ded186e9SIngo Weinhold 77*ded186e9SIngo Weinhold int32 78*ded186e9SIngo Weinhold ThreadModel::CountWaitObjectGroups() const 79*ded186e9SIngo Weinhold { 80*ded186e9SIngo Weinhold return fWaitObjectGroups.CountItems(); 81*ded186e9SIngo Weinhold } 82*ded186e9SIngo Weinhold 83*ded186e9SIngo Weinhold 84*ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup* 85*ded186e9SIngo Weinhold ThreadModel::WaitObjectGroupAt(int32 index) const 86*ded186e9SIngo Weinhold { 87*ded186e9SIngo Weinhold return fWaitObjectGroups.ItemAt(index); 88*ded186e9SIngo Weinhold } 89*ded186e9SIngo Weinhold 90*ded186e9SIngo Weinhold 91*ded186e9SIngo Weinhold // #pragma mark - WaitObjectGroup 92*ded186e9SIngo Weinhold 93*ded186e9SIngo Weinhold 94*ded186e9SIngo Weinhold uint32 95*ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::Type() const 96*ded186e9SIngo Weinhold { 97*ded186e9SIngo Weinhold return fWaitObjects[0]->Type(); 98*ded186e9SIngo Weinhold } 99*ded186e9SIngo Weinhold 100*ded186e9SIngo Weinhold 101*ded186e9SIngo Weinhold const char* 102*ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::Name() const 103*ded186e9SIngo Weinhold { 104*ded186e9SIngo Weinhold return fWaitObjects[0]->Name(); 105*ded186e9SIngo Weinhold } 106*ded186e9SIngo Weinhold 107*ded186e9SIngo Weinhold 108*ded186e9SIngo Weinhold int32 109*ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::CountWaitObjects() const 110*ded186e9SIngo Weinhold { 111*ded186e9SIngo Weinhold return fCount; 112*ded186e9SIngo Weinhold } 113*ded186e9SIngo Weinhold 114*ded186e9SIngo Weinhold 115*ded186e9SIngo Weinhold Model::ThreadWaitObject* 116*ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::WaitObjectAt(int32 index) const 117*ded186e9SIngo Weinhold { 118*ded186e9SIngo Weinhold return index >= 0 && index < fCount ? fWaitObjects[0] : NULL; 119*ded186e9SIngo Weinhold } 120*ded186e9SIngo Weinhold 121*ded186e9SIngo Weinhold 122*ded186e9SIngo Weinhold /*static*/ int 123*ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::CompareByTypeName(const WaitObjectGroup* a, 124*ded186e9SIngo Weinhold const WaitObjectGroup* b) 125*ded186e9SIngo Weinhold { 126*ded186e9SIngo Weinhold type_and_name key; 127*ded186e9SIngo Weinhold key.type = a->Type(); 128*ded186e9SIngo Weinhold key.name = a->Name(); 129*ded186e9SIngo Weinhold return CompareWithTypeName(&key, b); 130*ded186e9SIngo Weinhold } 131*ded186e9SIngo Weinhold 132*ded186e9SIngo Weinhold 133*ded186e9SIngo Weinhold /*static*/ int 134*ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::CompareWithTypeName(const type_and_name* key, 135*ded186e9SIngo Weinhold const WaitObjectGroup* group) 136*ded186e9SIngo Weinhold { 137*ded186e9SIngo Weinhold if (key->type != group->Type()) 138*ded186e9SIngo Weinhold return key->type < group->Type() ? -1 : 1; 139*ded186e9SIngo Weinhold 140*ded186e9SIngo Weinhold return strcmp(key->name, group->Name()); 141*ded186e9SIngo Weinhold } 142*ded186e9SIngo Weinhold 143*ded186e9SIngo Weinhold 144*ded186e9SIngo Weinhold #endif // THREAD_MODEL_h 145