andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "channel_manager_base.h" |
| 12 | |
| 13 | #include "critical_section_wrapper.h" |
| 14 | #include "rw_lock_wrapper.h" |
| 15 | #include <cassert> |
| 16 | |
| 17 | namespace webrtc |
| 18 | { |
| 19 | |
| 20 | namespace voe |
| 21 | { |
| 22 | |
| 23 | ChannelManagerBase::ChannelManagerBase() : |
| 24 | _itemsCritSectPtr(CriticalSectionWrapper::CreateCriticalSection()), |
| 25 | _itemsRWLockPtr(RWLockWrapper::CreateRWLock()) |
| 26 | { |
| 27 | for (int i = 0; i < KMaxNumberOfItems; i++) |
| 28 | { |
| 29 | _freeItemIds[i] = true; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | ChannelManagerBase::~ChannelManagerBase() |
| 34 | { |
| 35 | if (_itemsRWLockPtr) |
| 36 | { |
| 37 | delete _itemsRWLockPtr; |
| 38 | _itemsRWLockPtr = NULL; |
| 39 | } |
| 40 | if (_itemsCritSectPtr) |
| 41 | { |
| 42 | delete _itemsCritSectPtr; |
| 43 | _itemsCritSectPtr = NULL; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | bool ChannelManagerBase::GetFreeItemId(WebRtc_Word32& itemId) |
| 48 | { |
| 49 | CriticalSectionScoped cs(_itemsCritSectPtr); |
| 50 | WebRtc_Word32 i(0); |
| 51 | while (i < KMaxNumberOfItems) |
| 52 | { |
| 53 | if (_freeItemIds[i]) |
| 54 | { |
| 55 | itemId = i; |
| 56 | _freeItemIds[i] = false; |
| 57 | return true; |
| 58 | } |
| 59 | i++; |
| 60 | } |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | void ChannelManagerBase::AddFreeItemId(WebRtc_Word32 itemId) |
| 65 | { |
| 66 | assert(itemId < KMaxNumberOfItems); |
| 67 | _freeItemIds[itemId] = true; |
| 68 | } |
| 69 | |
| 70 | void ChannelManagerBase::RemoveFreeItemIds() |
| 71 | { |
| 72 | for (int i = 0; i < KMaxNumberOfItems; i++) |
| 73 | { |
| 74 | _freeItemIds[i] = false; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | bool ChannelManagerBase::CreateItem(WebRtc_Word32& itemId) |
| 79 | { |
| 80 | _itemsCritSectPtr->Enter(); |
| 81 | void* itemPtr; |
| 82 | itemId = -1; |
| 83 | const bool success = GetFreeItemId(itemId); |
| 84 | if (!success) |
| 85 | { |
| 86 | _itemsCritSectPtr->Leave(); |
| 87 | return false; |
| 88 | } |
| 89 | itemPtr = NewItem(itemId); |
| 90 | if (!itemPtr) |
| 91 | { |
| 92 | _itemsCritSectPtr->Leave(); |
| 93 | return false; |
| 94 | } |
| 95 | _itemsCritSectPtr->Leave(); |
| 96 | InsertItem(itemId, itemPtr); |
| 97 | |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | void ChannelManagerBase::InsertItem(WebRtc_Word32 itemId, void* item) |
| 102 | { |
| 103 | CriticalSectionScoped cs(_itemsCritSectPtr); |
| 104 | assert(!_items.Find(itemId)); |
| 105 | _items.Insert(itemId, item); |
| 106 | } |
| 107 | |
| 108 | void* |
| 109 | ChannelManagerBase::RemoveItem(WebRtc_Word32 itemId) |
| 110 | { |
| 111 | CriticalSectionScoped cs(_itemsCritSectPtr); |
| 112 | WriteLockScoped wlock(*_itemsRWLockPtr); |
| 113 | MapItem* it = _items.Find(itemId); |
| 114 | if (!it) |
| 115 | { |
| 116 | return 0; |
| 117 | } |
| 118 | void* returnItem = it->GetItem(); |
| 119 | _items.Erase(it); |
| 120 | AddFreeItemId(itemId); |
| 121 | |
| 122 | return returnItem; |
| 123 | } |
| 124 | |
| 125 | void ChannelManagerBase::DestroyAllItems() |
| 126 | { |
| 127 | CriticalSectionScoped cs(_itemsCritSectPtr); |
| 128 | MapItem* it = _items.First(); |
| 129 | while (it) |
| 130 | { |
| 131 | DeleteItem(it->GetItem()); |
| 132 | _items.Erase(it); |
| 133 | it = _items.First(); |
| 134 | } |
| 135 | RemoveFreeItemIds(); |
| 136 | } |
| 137 | |
| 138 | WebRtc_Word32 ChannelManagerBase::NumOfItems() const |
| 139 | { |
| 140 | return _items.Size(); |
| 141 | } |
| 142 | |
| 143 | WebRtc_Word32 ChannelManagerBase::MaxNumOfItems() const |
| 144 | { |
| 145 | return static_cast<WebRtc_Word32> (KMaxNumberOfItems); |
| 146 | } |
| 147 | |
| 148 | void* |
| 149 | ChannelManagerBase::GetItem(WebRtc_Word32 itemId) const |
| 150 | { |
| 151 | CriticalSectionScoped cs(_itemsCritSectPtr); |
| 152 | MapItem* it = _items.Find(itemId); |
| 153 | if (!it) |
| 154 | { |
| 155 | return 0; |
| 156 | } |
| 157 | _itemsRWLockPtr->AcquireLockShared(); |
| 158 | return it->GetItem(); |
| 159 | } |
| 160 | |
| 161 | void* |
| 162 | ChannelManagerBase::GetFirstItem(void*& iterator) const |
| 163 | { |
| 164 | CriticalSectionScoped cs(_itemsCritSectPtr); |
| 165 | MapItem* it = _items.First(); |
| 166 | iterator = (void*) it; |
| 167 | if (!it) |
| 168 | { |
| 169 | return 0; |
| 170 | } |
| 171 | return it->GetItem(); |
| 172 | } |
| 173 | |
| 174 | void* |
| 175 | ChannelManagerBase::GetNextItem(void*& iterator) const |
| 176 | { |
| 177 | CriticalSectionScoped cs(_itemsCritSectPtr); |
| 178 | MapItem* it = (MapItem*) iterator; |
| 179 | if (!it) |
| 180 | { |
| 181 | iterator = 0; |
| 182 | return 0; |
| 183 | } |
| 184 | it = _items.Next(it); |
| 185 | iterator = (void*) it; |
| 186 | if (!it) |
| 187 | { |
| 188 | return 0; |
| 189 | } |
| 190 | return it->GetItem(); |
| 191 | } |
| 192 | |
| 193 | void ChannelManagerBase::ReleaseItem() |
| 194 | { |
| 195 | _itemsRWLockPtr->ReleaseLockShared(); |
| 196 | } |
| 197 | |
| 198 | void ChannelManagerBase::GetItemIds(WebRtc_Word32* channelsArray, |
| 199 | WebRtc_Word32& numOfChannels) const |
| 200 | { |
| 201 | MapItem* it = _items.First(); |
| 202 | numOfChannels = (numOfChannels <= _items.Size()) ? |
| 203 | numOfChannels : _items.Size(); |
| 204 | for (int i = 0; i < numOfChannels && it != NULL; i++) |
| 205 | { |
| 206 | channelsArray[i] = it->GetId(); |
| 207 | it = _items.Next(it); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | void ChannelManagerBase::GetChannels(MapWrapper& channels) const |
| 212 | { |
| 213 | CriticalSectionScoped cs(_itemsCritSectPtr); |
| 214 | if (_items.Size() == 0) |
| 215 | { |
| 216 | return; |
| 217 | } |
| 218 | _itemsRWLockPtr->AcquireLockShared(); |
| 219 | for (MapItem* it = _items.First(); it != NULL; it = _items.Next(it)) |
| 220 | { |
| 221 | channels.Insert(it->GetId(), it->GetItem()); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | } // namespace voe |
| 226 | |
| 227 | } // namespace webrtc |