* 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*
BMediaRosterEx::MakeTimeSourceObject(media_node_id timesourceID)
BMediaRosterEx::MakeTimeSourceObject(media_node_id timeSourceID)
{
BTimeSource* source;
media_node clone;
status_t rv;
rv = GetNodeFor(timesourceID, &clone);
if (rv != B_OK) {
ERROR("BMediaRosterEx::MakeTimeSourceObject: GetNodeFor failed\n");
status_t status = GetNodeFor(timeSourceID, &clone);
if (status != B_OK) {
ERROR("BMediaRosterEx::MakeTimeSourceObject: GetNodeFor failed: %s\n",
strerror(status));
return NULL;
}
source = _TimeSourceObjectManager->GetTimeSource(clone);
BTimeSource* source = gTimeSourceObjectManager->GetTimeSource(clone);
if (source == NULL) {
ERROR("BMediaRosterEx::MakeTimeSourceObject: GetTimeSource failed\n");
return NULL;
+48 -37
View File
@@ -1,32 +1,42 @@
/***********************************************************************
* Copyright (c) 2002 Marcus Overhagen. All Rights Reserved.
* This file may be used under the terms of the OpenBeOS License.
*
* The object returned by BMediaRoster's
* BTimeSource * MakeTimeSourceFor(const media_node & for_node);
*
***********************************************************************/
/*
* Copyright 2002 Marcus Overhagen. All Rights Reserved.
* This file may be used under the terms of the MIT License.
*/
/*! The object returned by BMediaRoster's
MakeTimeSourceFor(const media_node& forNode);
*/
#include "TimeSourceObject.h"
#include <OS.h>
#include <stdio.h>
#include <string.h>
#include <MediaRoster.h>
#include "MediaMisc.h"
#include "TimeSourceObject.h"
#include <OS.h>
#include <MediaMisc.h>
#include <debug.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)
{
TRACE("TimeSourceObject::TimeSourceObject enter, id = %ld\n", node.node);
if (fControlPort > 0)
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,
// and this shadow object won't receive any.
fControlPort = node.port;
ASSERT(fNodeID == node.node);
ASSERT(fKinds == node.kind);
@@ -34,49 +44,50 @@ TimeSourceObject::TimeSourceObject(const media_node &node)
strcpy(fName, "System Clock");
fIsRealtime = true;
} else {
live_node_info lni;
if (B_OK == BMediaRoster::Roster()->GetLiveNodeInfo(node, &lni)) {
strcpy(fName, lni.name);
} else {
sprintf(fName, "timesource %ld", node.node);
}
live_node_info liveNodeInfo;
if (BMediaRoster::Roster()->GetLiveNodeInfo(node, &liveNodeInfo)
== B_OK)
strlcpy(fName, liveNodeInfo.name, B_MEDIA_NAME_LENGTH);
else
snprintf(fName, B_MEDIA_NAME_LENGTH, "timesource %ld", node.node);
}
AddNodeKind(NODE_KIND_SHADOW_TIMESOURCE);
AddNodeKind(NODE_KIND_NO_REFCOUNTING);
TRACE("TimeSourceObject::TimeSourceObject leave, node id %ld\n", fNodeID);
}
/* virtual */ status_t
TimeSourceObject::TimeSourceOp(
const time_source_op_info & op,
void * _reserved)
status_t
TimeSourceObject::TimeSourceOp(const time_source_op_info& op, void* _reserved)
{
// we don't get anything here
return B_OK;
}
/* virtual */ BMediaAddOn*
TimeSourceObject::AddOn(int32 *internal_id) const
BMediaAddOn*
TimeSourceObject::AddOn(int32* _id) const
{
if (internal_id)
*internal_id = 0;
if (_id != NULL)
*_id = 0;
return NULL;
}
/* virtual */ status_t
TimeSourceObject::DeleteHook(BMediaNode *node)
status_t
TimeSourceObject::DeleteHook(BMediaNode* node)
{
status_t status;
// if (fIsRealtime) {
// ERROR("TimeSourceObject::DeleteHook: system time source clone delete hook called\n");
// return B_ERROR;
// }
printf("TimeSourceObject::DeleteHook enter\n");
_TimeSourceObjectManager->ObjectDeleted(this);
status = BTimeSource::DeleteHook(node);
printf("TimeSourceObject::DeleteHook leave\n");
PRINT("TimeSourceObject::DeleteHook enter\n");
gTimeSourceObjectManager->ObjectDeleted(this);
status_t status = BTimeSource::DeleteHook(node);
PRINT("TimeSourceObject::DeleteHook leave\n");
return status;
}
+29 -25
View File
@@ -1,36 +1,40 @@
/***********************************************************************
* Copyright (c) 2002 Marcus Overhagen. All Rights Reserved.
* This file may be used under the terms of the OpenBeOS License.
*
* The object returned by BMediaRoster's
* BTimeSource * MakeTimeSourceFor(const media_node & for_node);
*
***********************************************************************/
#ifndef _TIME_SOURCE_OBJECT_H_
#define _TIME_SOURCE_OBJECT_H_
/*
* Copyright 2002 Marcus Overhagen. All Rights Reserved.
* This file may be used under the terms of the MIT License.
*/
#ifndef TIME_SOURCE_OBJECT_H
#define TIME_SOURCE_OBJECT_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:
TimeSourceObject(const media_node &node);
TimeSourceObject(const media_node& node);
protected:
virtual status_t TimeSourceOp(
const time_source_op_info & op,
void * _reserved);
virtual status_t TimeSourceOp(const time_source_op_info& op,
void* _reserved);
virtual BMediaAddOn* AddOn(
int32 * internal_id) const;
virtual BMediaAddOn* AddOn(int32* _id) const;
// override from BMediaNode
virtual status_t DeleteHook(BMediaNode * node);
// override from BMediaNode
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 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 <stdio.h>
#include <Autolock.h>
#include <MediaRoster.h>
#include <debug.h>
#include <MediaMisc.h>
#include "TimeSourceObject.h"
#include "MediaMisc.h"
#include "debug.h"
static BPrivate::media::TimeSourceObjectManager manager;
BPrivate::media::TimeSourceObjectManager *_TimeSourceObjectManager = &manager;
static BPrivate::media::TimeSourceObjectManager sManager;
BPrivate::media::TimeSourceObjectManager* gTimeSourceObjectManager = &sManager;
namespace BPrivate {
namespace media {
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()
{
CALLED();
delete fLock;
// force unloading all currently loaded
BTimeSource **pts;
for (fMap->Rewind(); fMap->GetNext(&pts); ) {
PRINT(1, "Forcing release of TimeSource id %ld...\n", (*pts)->ID());
int debugcnt = 0;
while ((*pts)->Release() != NULL)
debugcnt++;
PRINT(1, "Forcing release of TimeSource done, released %d times\n", debugcnt);
// force unloading all currently loaded time sources
NodeMap::iterator iterator = fMap.begin();
for (; iterator != fMap.end(); iterator++) {
BTimeSource* timeSource = iterator->second;
PRINT(1, "Forcing release of TimeSource id %ld...\n", timeSource->ID());
int32 debugCount = 0;
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
* Acquired(), if not, a new TimeSourceObject will be created.
*/
BTimeSource *
TimeSourceObjectManager::GetTimeSource(const media_node &node)
/*! BMediaRoster::MakeTimeSourceFor does use this function to request
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)
{
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;
if (fMap->Get(node.node, &pts))
return dynamic_cast<BTimeSource *>((*pts)->Acquire());
NodeMap::iterator found = fMap.find(node.node);
if (found != fMap.end())
return dynamic_cast<BTimeSource*>(found->second->Acquire());
// time sources are not accounted in node reference counting
BTimeSource *ts;
ts = new TimeSourceObject(node);
fMap->Insert(node.node, ts);
return ts;
BTimeSource* timeSource = new(std::nothrow) TimeSourceObject(node);
if (timeSource == NULL)
return NULL;
fMap.insert(std::make_pair(node.node, timeSource));
return timeSource;
}
/* 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.
*/
/*! This function is called during deletion of the time source object.
*/
void
TimeSourceObjectManager::ObjectDeleted(BTimeSource *timesource)
TimeSourceObjectManager::ObjectDeleted(BTimeSource* timeSource)
{
CALLED();
BAutolock lock(fLock);
BAutolock _(this);
// printf("TimeSourceObjectManager::ObjectDeleted, node id %ld\n", timesource->ID());
bool b;
b = fMap->Remove(timesource->ID());
if (!b) {
ERROR("TimeSourceObjectManager::ObjectDeleted, Remove failed\n");
}
status_t rv;
rv = BMediaRoster::Roster()->ReleaseNode(timesource->Node());
if (rv != B_OK) {
PRINT("TimeSourceObjectManager::ObjectDeleted, node id %ld\n",
timeSource->ID());
fMap.erase(timeSource->ID());
status_t status = BMediaRoster::Roster()->ReleaseNode(timeSource->Node());
if (status != B_OK) {
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.
*/
#ifndef _TIME_SOURCE_OBJECT_MANAGER_H_
#define _TIME_SOURCE_OBJECT_MANAGER_H_
#ifndef 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 media {
class TimeSourceObjectManager {
public:
TimeSourceObjectManager();
~TimeSourceObjectManager();
BTimeSource *GetTimeSource(const media_node &node);
void ObjectDeleted(BTimeSource *timesource);
class TimeSourceObjectManager : BLocker {
public:
TimeSourceObjectManager();
~TimeSourceObjectManager();
BTimeSource* GetTimeSource(const media_node& node);
void ObjectDeleted(BTimeSource* timeSource);
private:
Map<media_node_id, BTimeSource *> *fMap;
BLocker *fLock;
typedef std::map<media_node_id, BTimeSource*> NodeMap;
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_