package daemon: Move Volume::State to top level

... and rename it to VolumeState.
This commit is contained in:
Ingo Weinhold
2014-04-20 09:44:01 +02:00
parent 6cf0a86499
commit 273763d052
5 changed files with 218 additions and 145 deletions
+1
View File
@@ -18,6 +18,7 @@ Server package_daemon
ResultWindow.cpp ResultWindow.cpp
Root.cpp Root.cpp
Volume.cpp Volume.cpp
VolumeState.cpp
: :
be package be package
$(TARGET_LIBSTDC++) $(TARGET_LIBSTDC++)
+3 -142
View File
@@ -45,6 +45,7 @@
#include "DebugSupport.h" #include "DebugSupport.h"
#include "Exception.h" #include "Exception.h"
#include "FSTransaction.h" #include "FSTransaction.h"
#include "VolumeState.h"
using namespace BPackageKit::BPrivate; using namespace BPackageKit::BPrivate;
@@ -105,146 +106,6 @@ private:
}; };
// #pragma mark - State
struct Volume::State {
State()
:
fLock("volume state"),
fPackagesByFileName(),
fPackagesByNodeRef(),
fChangeCount(0),
fPendingPackageJobCount(0)
{
}
~State()
{
fPackagesByFileName.Clear();
Package* package = fPackagesByNodeRef.Clear(true);
while (package != NULL) {
Package* next = package->NodeRefHashTableNext();
delete package;
package = next;
}
}
bool Init()
{
return fLock.InitCheck() == B_OK && fPackagesByFileName.Init() == B_OK
&& fPackagesByNodeRef.Init() == B_OK;
}
bool Lock()
{
return fLock.Lock();
}
void Unlock()
{
fLock.Unlock();
}
int64 ChangeCount() const
{
return fChangeCount;
}
Package* FindPackage(const char* name) const
{
return fPackagesByFileName.Lookup(name);
}
Package* FindPackage(const node_ref& nodeRef) const
{
return fPackagesByNodeRef.Lookup(nodeRef);
}
PackageFileNameHashTable::Iterator ByFileNameIterator() const
{
return fPackagesByFileName.GetIterator();
}
PackageNodeRefHashTable::Iterator ByNodeRefIterator() const
{
return fPackagesByNodeRef.GetIterator();
}
void AddPackage(Package* package)
{
AutoLocker<BLocker> locker(fLock);
fPackagesByFileName.Insert(package);
fPackagesByNodeRef.Insert(package);
}
void RemovePackage(Package* package)
{
AutoLocker<BLocker> locker(fLock);
_RemovePackage(package);
}
void SetPackageActive(Package* package, bool active)
{
AutoLocker<BLocker> locker(fLock);
package->SetActive(active);
}
void ActivationChanged(const PackageSet& activatedPackage,
const PackageSet& deactivatePackages)
{
AutoLocker<BLocker> locker(fLock);
for (PackageSet::iterator it = activatedPackage.begin();
it != activatedPackage.end(); ++it) {
(*it)->SetActive(true);
fChangeCount++;
}
for (PackageSet::iterator it = deactivatePackages.begin();
it != deactivatePackages.end(); ++it) {
Package* package = *it;
_RemovePackage(package);
delete package;
}
}
void PackageJobPending()
{
atomic_add(&fPendingPackageJobCount, 1);
}
void PackageJobFinished()
{
atomic_add(&fPendingPackageJobCount, -1);
}
bool IsPackageJobPending() const
{
return fPendingPackageJobCount != 0;
}
private:
void _RemovePackage(Package* package)
{
fPackagesByFileName.Remove(package);
fPackagesByNodeRef.Remove(package);
fChangeCount++;
}
private:
BLocker fLock;
PackageFileNameHashTable fPackagesByFileName;
PackageNodeRefHashTable fPackagesByNodeRef;
int64 fChangeCount;
int32 fPendingPackageJobCount;
};
// #pragma mark - CommitTransactionHandler // #pragma mark - CommitTransactionHandler
@@ -1505,7 +1366,7 @@ Volume::~Volume()
status_t status_t
Volume::Init(const node_ref& rootDirectoryRef, node_ref& _packageRootRef) Volume::Init(const node_ref& rootDirectoryRef, node_ref& _packageRootRef)
{ {
fState = new(std::nothrow) State; fState = new(std::nothrow) VolumeState;
if (fState == NULL || !fState->Init()) if (fState == NULL || !fState->Init())
RETURN_ERROR(B_NO_MEMORY); RETURN_ERROR(B_NO_MEMORY);
@@ -1722,7 +1583,7 @@ INFORM("Volume::InitialVerify(%p, %p)\n", nextVolume, nextNextVolume);
void void
Volume::HandleGetLocationInfoRequest(BMessage* message) Volume::HandleGetLocationInfoRequest(BMessage* message)
{ {
AutoLocker<State> stateLocker(fState); AutoLocker<VolumeState> stateLocker(fState);
// If the cached reply message is up-to-date, just send it. // If the cached reply message is up-to-date, just send it.
int64 changeCount; int64 changeCount;
+3 -3
View File
@@ -33,7 +33,7 @@
// //
// The only thread synchronization needed is for the status information accessed // The only thread synchronization needed is for the status information accessed
// by HandleGetLocationInfoRequest() and modified by the job thread. The data // by HandleGetLocationInfoRequest() and modified by the job thread. The data
// are encapsulated in a Volume::State object which contains a lock. The lock // are encapsulated in a VolumeState object which contains a lock. The lock
// must be held by the app thread when accessing the data (it reads only) and // must be held by the app thread when accessing the data (it reads only) and
// by the job thread when modifying the data (not needed when reading). // by the job thread when modifying the data (not needed when reading).
@@ -44,6 +44,7 @@ using BPackageKit::BPrivate::BDaemonClient;
class BDirectory; class BDirectory;
class Root; class Root;
class VolumeState;
namespace BPackageKit { namespace BPackageKit {
class BSolver; class BSolver;
@@ -137,7 +138,6 @@ public:
private: private:
struct NodeMonitorEvent; struct NodeMonitorEvent;
struct State;
struct CommitTransactionHandler; struct CommitTransactionHandler;
friend struct CommitTransactionHandler; friend struct CommitTransactionHandler;
@@ -214,7 +214,7 @@ private:
node_ref fPackagesDirectoryRef; node_ref fPackagesDirectoryRef;
Root* fRoot; Root* fRoot;
Listener* fListener; Listener* fListener;
State* fState; VolumeState* fState;
BLocker fPendingNodeMonitorEventsLock; BLocker fPendingNodeMonitorEventsLock;
NodeMonitorEventList fPendingNodeMonitorEvents; NodeMonitorEventList fPendingNodeMonitorEvents;
bigtime_t fNodeMonitorEventHandleTime; bigtime_t fNodeMonitorEventHandleTime;
+99
View File
@@ -0,0 +1,99 @@
/*
* Copyright 2013-2014, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Ingo Weinhold <[email protected]>
*/
#include "VolumeState.h"
#include <AutoLocker.h>
VolumeState::VolumeState()
:
fLock("volume state"),
fPackagesByFileName(),
fPackagesByNodeRef(),
fChangeCount(0),
fPendingPackageJobCount(0)
{
}
VolumeState::~VolumeState()
{
fPackagesByFileName.Clear();
Package* package = fPackagesByNodeRef.Clear(true);
while (package != NULL) {
Package* next = package->NodeRefHashTableNext();
delete package;
package = next;
}
}
bool
VolumeState::Init()
{
return fLock.InitCheck() == B_OK && fPackagesByFileName.Init() == B_OK
&& fPackagesByNodeRef.Init() == B_OK;
}
void
VolumeState::AddPackage(Package* package)
{
AutoLocker<BLocker> locker(fLock);
fPackagesByFileName.Insert(package);
fPackagesByNodeRef.Insert(package);
}
void
VolumeState::RemovePackage(Package* package)
{
AutoLocker<BLocker> locker(fLock);
_RemovePackage(package);
}
void
VolumeState::SetPackageActive(Package* package, bool active)
{
AutoLocker<BLocker> locker(fLock);
package->SetActive(active);
}
void
VolumeState::ActivationChanged(const PackageSet& activatedPackage,
const PackageSet& deactivatePackages)
{
AutoLocker<BLocker> locker(fLock);
for (PackageSet::iterator it = activatedPackage.begin();
it != activatedPackage.end(); ++it) {
(*it)->SetActive(true);
fChangeCount++;
}
for (PackageSet::iterator it = deactivatePackages.begin();
it != deactivatePackages.end(); ++it) {
Package* package = *it;
_RemovePackage(package);
delete package;
}
}
void
VolumeState::_RemovePackage(Package* package)
{
fPackagesByFileName.Remove(package);
fPackagesByNodeRef.Remove(package);
fChangeCount++;
}
+112
View File
@@ -0,0 +1,112 @@
/*
* Copyright 2013-2014, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Ingo Weinhold <[email protected]>
*/
#ifndef VOLUME_STATE_H
#define VOLUME_STATE_H
#include <Locker.h>
#include "Package.h"
class VolumeState {
public:
VolumeState();
~VolumeState();
bool Init();
bool Lock()
{ return fLock.Lock(); }
void Unlock()
{ fLock.Unlock(); }
int64 ChangeCount() const
{ return fChangeCount; }
Package* FindPackage(const char* name) const;
Package* FindPackage(const node_ref& nodeRef) const;
PackageFileNameHashTable::Iterator ByFileNameIterator() const;
PackageNodeRefHashTable::Iterator ByNodeRefIterator() const;
void AddPackage(Package* package);
void RemovePackage(Package* package);
void SetPackageActive(Package* package, bool active);
void ActivationChanged(
const PackageSet& activatedPackage,
const PackageSet& deactivatePackages);
void PackageJobPending();
void PackageJobFinished();
bool IsPackageJobPending() const;
private:
void _RemovePackage(Package* package);
private:
BLocker fLock;
PackageFileNameHashTable fPackagesByFileName;
PackageNodeRefHashTable fPackagesByNodeRef;
int64 fChangeCount;
int32 fPendingPackageJobCount;
};
inline Package*
VolumeState::FindPackage(const char* name) const
{
return fPackagesByFileName.Lookup(name);
}
inline Package*
VolumeState::FindPackage(const node_ref& nodeRef) const
{
return fPackagesByNodeRef.Lookup(nodeRef);
}
inline PackageFileNameHashTable::Iterator
VolumeState::ByFileNameIterator() const
{
return fPackagesByFileName.GetIterator();
}
inline PackageNodeRefHashTable::Iterator
VolumeState::ByNodeRefIterator() const
{
return fPackagesByNodeRef.GetIterator();
}
inline void
VolumeState::PackageJobPending()
{
atomic_add(&fPendingPackageJobCount, 1);
}
inline void
VolumeState::PackageJobFinished()
{
atomic_add(&fPendingPackageJobCount, -1);
}
inline bool
VolumeState::IsPackageJobPending() const
{
return fPendingPackageJobCount != 0;
}
#endif // VOLUME_STATE_H