* Got rid of Map use in TimeSourceObjectManager.

* Renamed global variable _TimeSourceObjectManager to gTimeSourceObjectManager.
* Cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34544 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2009-12-07 21:57:45 +00:00
parent 34a48c70ef
commit f4ec236cad
5 changed files with 180 additions and 150 deletions
+6 -8
View File
@@ -495,19 +495,17 @@ BMediaRosterEx::PublishInputs(const media_node& node, List<media_input>* list)
BTimeSource* BTimeSource*
BMediaRosterEx::MakeTimeSourceObject(media_node_id timesourceID) BMediaRosterEx::MakeTimeSourceObject(media_node_id timeSourceID)
{ {
BTimeSource* source;
media_node clone; media_node clone;
status_t rv; status_t status = GetNodeFor(timeSourceID, &clone);
if (status != B_OK) {
rv = GetNodeFor(timesourceID, &clone); ERROR("BMediaRosterEx::MakeTimeSourceObject: GetNodeFor failed: %s\n",
if (rv != B_OK) { strerror(status));
ERROR("BMediaRosterEx::MakeTimeSourceObject: GetNodeFor failed\n");
return NULL; return NULL;
} }
source = _TimeSourceObjectManager->GetTimeSource(clone); BTimeSource* source = gTimeSourceObjectManager->GetTimeSource(clone);
if (source == NULL) { if (source == NULL) {
ERROR("BMediaRosterEx::MakeTimeSourceObject: GetTimeSource failed\n"); ERROR("BMediaRosterEx::MakeTimeSourceObject: GetTimeSource failed\n");
return NULL; return NULL;
+48 -37
View File
@@ -1,32 +1,42 @@
/*********************************************************************** /*
* Copyright (c) 2002 Marcus Overhagen. All Rights Reserved. * Copyright 2002 Marcus Overhagen. All Rights Reserved.
* This file may be used under the terms of the OpenBeOS License. * This file may be used under the terms of the MIT License.
* */
* The object returned by BMediaRoster's
* BTimeSource * MakeTimeSourceFor(const media_node & for_node);
* /*! The object returned by BMediaRoster's
***********************************************************************/ MakeTimeSourceFor(const media_node& forNode);
*/
#include "TimeSourceObject.h"
#include <OS.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <MediaRoster.h> #include <MediaRoster.h>
#include "MediaMisc.h" #include <OS.h>
#include "TimeSourceObject.h"
#include <MediaMisc.h>
#include <debug.h>
#include "TimeSourceObjectManager.h" #include "TimeSourceObjectManager.h"
TimeSourceObject::TimeSourceObject(const media_node &node)
: BMediaNode("some timesource object", node.node, node.kind), TimeSourceObject::TimeSourceObject(const media_node& node)
:
BMediaNode("some timesource object", node.node, node.kind),
BTimeSource(node.node) BTimeSource(node.node)
{ {
TRACE("TimeSourceObject::TimeSourceObject enter, id = %ld\n", node.node); TRACE("TimeSourceObject::TimeSourceObject enter, id = %ld\n", node.node);
if (fControlPort > 0) if (fControlPort > 0)
delete_port(fControlPort); delete_port(fControlPort);
// we use the control port of the real time source object. // We use the control port of the real time source object.
// this way, all messages are send to the real time source, // this way, all messages are send to the real time source,
// and this shadow object won't receive any. // and this shadow object won't receive any.
fControlPort = node.port; fControlPort = node.port;
ASSERT(fNodeID == node.node); ASSERT(fNodeID == node.node);
ASSERT(fKinds == node.kind); ASSERT(fKinds == node.kind);
@@ -34,49 +44,50 @@ TimeSourceObject::TimeSourceObject(const media_node &node)
strcpy(fName, "System Clock"); strcpy(fName, "System Clock");
fIsRealtime = true; fIsRealtime = true;
} else { } else {
live_node_info lni; live_node_info liveNodeInfo;
if (B_OK == BMediaRoster::Roster()->GetLiveNodeInfo(node, &lni)) { if (BMediaRoster::Roster()->GetLiveNodeInfo(node, &liveNodeInfo)
strcpy(fName, lni.name); == B_OK)
} else { strlcpy(fName, liveNodeInfo.name, B_MEDIA_NAME_LENGTH);
sprintf(fName, "timesource %ld", node.node); else
} snprintf(fName, B_MEDIA_NAME_LENGTH, "timesource %ld", node.node);
} }
AddNodeKind(NODE_KIND_SHADOW_TIMESOURCE); AddNodeKind(NODE_KIND_SHADOW_TIMESOURCE);
AddNodeKind(NODE_KIND_NO_REFCOUNTING); AddNodeKind(NODE_KIND_NO_REFCOUNTING);
TRACE("TimeSourceObject::TimeSourceObject leave, node id %ld\n", fNodeID); TRACE("TimeSourceObject::TimeSourceObject leave, node id %ld\n", fNodeID);
} }
/* virtual */ status_t
TimeSourceObject::TimeSourceOp( status_t
const time_source_op_info & op, TimeSourceObject::TimeSourceOp(const time_source_op_info& op, void* _reserved)
void * _reserved)
{ {
// we don't get anything here // we don't get anything here
return B_OK; return B_OK;
} }
/* virtual */ BMediaAddOn*
TimeSourceObject::AddOn(int32 *internal_id) const BMediaAddOn*
TimeSourceObject::AddOn(int32* _id) const
{ {
if (internal_id) if (_id != NULL)
*internal_id = 0; *_id = 0;
return NULL; return NULL;
} }
/* virtual */ status_t
TimeSourceObject::DeleteHook(BMediaNode *node) status_t
TimeSourceObject::DeleteHook(BMediaNode* node)
{ {
status_t status;
// if (fIsRealtime) { // if (fIsRealtime) {
// ERROR("TimeSourceObject::DeleteHook: system time source clone delete hook called\n"); // ERROR("TimeSourceObject::DeleteHook: system time source clone delete hook called\n");
// return B_ERROR; // return B_ERROR;
// } // }
printf("TimeSourceObject::DeleteHook enter\n"); PRINT("TimeSourceObject::DeleteHook enter\n");
_TimeSourceObjectManager->ObjectDeleted(this); gTimeSourceObjectManager->ObjectDeleted(this);
status = BTimeSource::DeleteHook(node); status_t status = BTimeSource::DeleteHook(node);
printf("TimeSourceObject::DeleteHook leave\n"); PRINT("TimeSourceObject::DeleteHook leave\n");
return status; return status;
} }
+29 -25
View File
@@ -1,36 +1,40 @@
/*********************************************************************** /*
* Copyright (c) 2002 Marcus Overhagen. All Rights Reserved. * Copyright 2002 Marcus Overhagen. All Rights Reserved.
* This file may be used under the terms of the OpenBeOS License. * This file may be used under the terms of the MIT License.
* */
* The object returned by BMediaRoster's #ifndef TIME_SOURCE_OBJECT_H
* BTimeSource * MakeTimeSourceFor(const media_node & for_node); #define TIME_SOURCE_OBJECT_H
*
***********************************************************************/
#ifndef _TIME_SOURCE_OBJECT_H_
#define _TIME_SOURCE_OBJECT_H_
#include <TimeSource.h> #include <TimeSource.h>
#include "MediaMisc.h"
namespace BPrivate { namespace media { #include <MediaMisc.h>
class TimeSourceObject : public BTimeSource
{ namespace BPrivate {
namespace media {
class TimeSourceObject : public BTimeSource {
public: public:
TimeSourceObject(const media_node &node); TimeSourceObject(const media_node& node);
protected: protected:
virtual status_t TimeSourceOp( virtual status_t TimeSourceOp(const time_source_op_info& op,
const time_source_op_info & op, void* _reserved);
void * _reserved);
virtual BMediaAddOn* AddOn( virtual BMediaAddOn* AddOn(int32* _id) const;
int32 * internal_id) const;
// override from BMediaNode // override from BMediaNode
virtual status_t DeleteHook(BMediaNode * node); virtual status_t DeleteHook(BMediaNode* node);
}; };
} } using namespace BPrivate::media;
#endif } // namespace media
} // namespace BPrivate
using namespace BPrivate::media;
#endif // TIME_SOURCE_OBJECT_H
+69 -64
View File
@@ -1,102 +1,107 @@
/*********************************************************************** /*
* Copyright (c) 2002 Marcus Overhagen. All Rights Reserved. * Copyright 2002 Marcus Overhagen. All Rights Reserved.
* This file may be used under the terms of the MIT License. * This file may be used under the terms of the MIT License.
* */
* This works like a cache for time source objects, to make sure
* each team only has one object representation for each time source.
* /*! This works like a cache for time source objects, to make sure
***********************************************************************/ each team only has one object representation for each time source.
*/
#include <OS.h>
#include <stdio.h>
#include <MediaRoster.h>
#include <Autolock.h>
#include "TimeSourceObjectManager.h" #include "TimeSourceObjectManager.h"
#include <stdio.h>
#include <Autolock.h>
#include <MediaRoster.h>
#include <debug.h>
#include <MediaMisc.h>
#include "TimeSourceObject.h" #include "TimeSourceObject.h"
#include "MediaMisc.h"
#include "debug.h"
static BPrivate::media::TimeSourceObjectManager manager; static BPrivate::media::TimeSourceObjectManager sManager;
BPrivate::media::TimeSourceObjectManager *_TimeSourceObjectManager = &manager; BPrivate::media::TimeSourceObjectManager* gTimeSourceObjectManager = &sManager;
namespace BPrivate { namespace BPrivate {
namespace media { namespace media {
TimeSourceObjectManager::TimeSourceObjectManager() TimeSourceObjectManager::TimeSourceObjectManager()
// : fSystemTimeSource(0) :
BLocker("time source object manager")
{ {
CALLED();
fLock = new BLocker("timesource object manager locker");
fMap = new Map<media_node_id, BTimeSource *>;
} }
TimeSourceObjectManager::~TimeSourceObjectManager() TimeSourceObjectManager::~TimeSourceObjectManager()
{ {
CALLED(); CALLED();
delete fLock;
// force unloading all currently loaded // force unloading all currently loaded time sources
BTimeSource **pts; NodeMap::iterator iterator = fMap.begin();
for (fMap->Rewind(); fMap->GetNext(&pts); ) { for (; iterator != fMap.end(); iterator++) {
PRINT(1, "Forcing release of TimeSource id %ld...\n", (*pts)->ID()); BTimeSource* timeSource = iterator->second;
int debugcnt = 0;
while ((*pts)->Release() != NULL) PRINT(1, "Forcing release of TimeSource id %ld...\n", timeSource->ID());
debugcnt++; int32 debugCount = 0;
PRINT(1, "Forcing release of TimeSource done, released %d times\n", debugcnt); while (timeSource->Release() != NULL)
debugCount++;
PRINT(1, "Forcing release of TimeSource done, released %d times\n",
debugCount);
} }
delete fMap;
} }
/* BMediaRoster::MakeTimeSourceFor does use this function to request
* a time source object. If it is already in memory, it will be /*! BMediaRoster::MakeTimeSourceFor does use this function to request
* Acquired(), if not, a new TimeSourceObject will be created. a time source object. If it is already in memory, it will be
*/ Acquired(), if not, a new TimeSourceObject will be created.
BTimeSource * */
TimeSourceObjectManager::GetTimeSource(const media_node &node) BTimeSource*
TimeSourceObjectManager::GetTimeSource(const media_node& node)
{ {
CALLED(); CALLED();
BAutolock lock(fLock); BAutolock _(this);
// printf("TimeSourceObjectManager::GetTimeSource, node id %ld\n", node.node); PRINT("TimeSourceObjectManager::GetTimeSource, node id %ld\n", node.node);
BTimeSource **pts; NodeMap::iterator found = fMap.find(node.node);
if (fMap->Get(node.node, &pts)) if (found != fMap.end())
return dynamic_cast<BTimeSource *>((*pts)->Acquire()); return dynamic_cast<BTimeSource*>(found->second->Acquire());
// time sources are not accounted in node reference counting // time sources are not accounted in node reference counting
BTimeSource *ts; BTimeSource* timeSource = new(std::nothrow) TimeSourceObject(node);
ts = new TimeSourceObject(node); if (timeSource == NULL)
fMap->Insert(node.node, ts); return NULL;
return ts;
fMap.insert(std::make_pair(node.node, timeSource));
return timeSource;
} }
/* This function is called during deletion of the time source object.
* /*! This function is called during deletion of the time source object.
* I'm not sure if there is a race condition with the function above. */
*/
void void
TimeSourceObjectManager::ObjectDeleted(BTimeSource *timesource) TimeSourceObjectManager::ObjectDeleted(BTimeSource* timeSource)
{ {
CALLED(); CALLED();
BAutolock lock(fLock); BAutolock _(this);
// printf("TimeSourceObjectManager::ObjectDeleted, node id %ld\n", timesource->ID()); PRINT("TimeSourceObjectManager::ObjectDeleted, node id %ld\n",
timeSource->ID());
bool b;
b = fMap->Remove(timesource->ID()); fMap.erase(timeSource->ID());
if (!b) {
ERROR("TimeSourceObjectManager::ObjectDeleted, Remove failed\n"); status_t status = BMediaRoster::Roster()->ReleaseNode(timeSource->Node());
} if (status != B_OK) {
status_t rv;
rv = BMediaRoster::Roster()->ReleaseNode(timesource->Node());
if (rv != B_OK) {
ERROR("TimeSourceObjectManager::ObjectDeleted, ReleaseNode failed\n"); ERROR("TimeSourceObjectManager::ObjectDeleted, ReleaseNode failed\n");
} }
} }
}; // namespace media
}; // namespace BPrivate } // namespace media
} // namespace BPrivate
+28 -16
View File
@@ -1,35 +1,47 @@
/* /*
* Copyright (c) 2002 Marcus Overhagen. All Rights Reserved. * Copyright 2002 Marcus Overhagen. All Rights Reserved.
* This file may be used under the terms of the MIT License. * This file may be used under the terms of the MIT License.
*/ */
#ifndef _TIME_SOURCE_OBJECT_MANAGER_H_ #ifndef TIME_SOURCE_OBJECT_MANAGER_H
#define _TIME_SOURCE_OBJECT_MANAGER_H_ #define TIME_SOURCE_OBJECT_MANAGER_H
#include "TMap.h" #include <map>
class BLocker; #include <Locker.h>
#include <MediaDefs.h>
class BTimeSource;
namespace BPrivate { namespace BPrivate {
namespace media { namespace media {
class TimeSourceObjectManager {
public:
TimeSourceObjectManager();
~TimeSourceObjectManager();
BTimeSource *GetTimeSource(const media_node &node); class TimeSourceObjectManager : BLocker {
void ObjectDeleted(BTimeSource *timesource); public:
TimeSourceObjectManager();
~TimeSourceObjectManager();
BTimeSource* GetTimeSource(const media_node& node);
void ObjectDeleted(BTimeSource* timeSource);
private: private:
Map<media_node_id, BTimeSource *> *fMap; typedef std::map<media_node_id, BTimeSource*> NodeMap;
BLocker *fLock;
NodeMap fMap;
}; };
} // namespace media
} // namespace BPrivate
extern BPrivate::media::TimeSourceObjectManager *_TimeSourceObjectManager; extern TimeSourceObjectManager* gTimeSourceObjectManager;
} // namespace media
} // namespace BPrivate
using BPrivate::media::gTimeSourceObjectManager;
#endif // _TIME_SOURCE_OBJECT_MANAGER_H_ #endif // _TIME_SOURCE_OBJECT_MANAGER_H_