* style cleanup

* DefaultManager: added a lock around rescan thread start and exit:
this should fix the possible race condition spotted by Ingo.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@36588 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Jérôme Duval
2010-05-02 22:35:18 +00:00
parent 7b63fd1c11
commit 4da0916c58
5 changed files with 122 additions and 82 deletions
+3 -1
View File
@@ -3,7 +3,8 @@
* Copyright 2009, Axel Dörfler, [email protected]. * Copyright 2009, Axel Dörfler, [email protected].
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#ifndef _BUFFER_MANAGER_H
#define _BUFFER_MANAGER_H
#include <set> #include <set>
@@ -110,3 +111,4 @@ private:
SourceInfoMap fSourceInfoMap; SourceInfoMap fSourceInfoMap;
}; };
#endif // _BUFFER_MANAGER_H
+47 -39
View File
@@ -55,8 +55,9 @@ DefaultManager::DefaultManager()
fTimeSource(-1), fTimeSource(-1),
fAudioMixer(-1), fAudioMixer(-1),
fPhysicalAudioOutInputID(0), fPhysicalAudioOutInputID(0),
fThreadId(-1), fRescanThread(-1),
fRescanRequested(0) fRescanRequested(0),
fRescanLock("rescan default manager")
{ {
strcpy(fPhysicalAudioOutInputName, "default"); strcpy(fPhysicalAudioOutInputName, "default");
fBeginHeader[0] = 0xab00150b; fBeginHeader[0] = 0xab00150b;
@@ -95,7 +96,7 @@ DefaultManager::LoadState()
TRACE("0x%08lx %ld\n", fBeginHeader[1], fBeginHeader[1]); TRACE("0x%08lx %ld\n", fBeginHeader[1], fBeginHeader[1]);
TRACE("0x%08lx %ld\n", fBeginHeader[2], fBeginHeader[2]); TRACE("0x%08lx %ld\n", fBeginHeader[2], fBeginHeader[2]);
if (file.Read(&category_count, sizeof(uint32)) < (int32)sizeof(uint32)) { if (file.Read(&category_count, sizeof(uint32)) < (int32)sizeof(uint32)) {
fprintf(stderr, fprintf(stderr,
"DefaultManager::LoadState() failed to read category_count\n"); "DefaultManager::LoadState() failed to read category_count\n");
return B_ERROR; return B_ERROR;
} }
@@ -105,12 +106,12 @@ DefaultManager::LoadState()
uint32 msg_header; uint32 msg_header;
uint32 default_type; uint32 default_type;
if (file.Read(&msg_header, sizeof(uint32)) < (int32)sizeof(uint32)) { if (file.Read(&msg_header, sizeof(uint32)) < (int32)sizeof(uint32)) {
fprintf(stderr, fprintf(stderr,
"DefaultManager::LoadState() failed to read msg_header\n"); "DefaultManager::LoadState() failed to read msg_header\n");
return B_ERROR; return B_ERROR;
} }
if (file.Read(&default_type, sizeof(uint32)) < (int32)sizeof(uint32)) { if (file.Read(&default_type, sizeof(uint32)) < (int32)sizeof(uint32)) {
fprintf(stderr, fprintf(stderr,
"DefaultManager::LoadState() failed to read default_type\n"); "DefaultManager::LoadState() failed to read default_type\n");
return B_ERROR; return B_ERROR;
} }
@@ -121,11 +122,11 @@ DefaultManager::LoadState()
fprintf(stderr, "DefaultManager::LoadState() failed to unflatten\n"); fprintf(stderr, "DefaultManager::LoadState() failed to unflatten\n");
} }
if (file.Read(fEndHeader, sizeof(uint32)*3) < (int32)sizeof(uint32)*3) { if (file.Read(fEndHeader, sizeof(uint32)*3) < (int32)sizeof(uint32)*3) {
fprintf(stderr, fprintf(stderr,
"DefaultManager::LoadState() failed to read fEndHeader\n"); "DefaultManager::LoadState() failed to read fEndHeader\n");
return B_ERROR; return B_ERROR;
} }
TRACE("LoadState returns B_OK\n"); TRACE("LoadState returns B_OK\n");
return B_OK; return B_OK;
} }
@@ -144,14 +145,14 @@ DefaultManager::SaveState(NodeManager *node_manager)
if ((err = create_directory(path.Path(), 0755)) != B_OK) if ((err = create_directory(path.Path(), 0755)) != B_OK)
return err; return err;
path.Append(kDefaultManagerSettingsFile); path.Append(kDefaultManagerSettingsFile);
uint32 default_types[] = {kMsgTypeVideoIn, kMsgTypeVideoOut, uint32 default_types[] = {kMsgTypeVideoIn, kMsgTypeVideoOut,
kMsgTypeAudioIn, kMsgTypeAudioOut}; kMsgTypeAudioIn, kMsgTypeAudioOut};
media_node_id media_node_ids[] = {fPhysicalVideoIn, fPhysicalVideoOut, media_node_id media_node_ids[] = {fPhysicalVideoIn, fPhysicalVideoOut,
fPhysicalAudioIn, fPhysicalAudioOut}; fPhysicalAudioIn, fPhysicalAudioOut};
for (uint32 i = 0; i < sizeof(default_types) / sizeof(default_types[0]); for (uint32 i = 0; i < sizeof(default_types) / sizeof(default_types[0]);
i++) { i++) {
// we call the node manager to have more infos about nodes // we call the node manager to have more infos about nodes
dormant_node_info info; dormant_node_info info;
media_node node; media_node node;
@@ -328,13 +329,14 @@ DefaultManager::Get(media_node_id *nodeid, char *input_name, int32 *inputid,
status_t status_t
DefaultManager::Rescan() DefaultManager::Rescan()
{ {
BAutolock locker(fRescanLock);
atomic_add(&fRescanRequested, 1); atomic_add(&fRescanRequested, 1);
if (fThreadId < 0) { if (fRescanThread < 0) {
fThreadId = spawn_thread(rescan_thread, "rescan defaults", fRescanThread = spawn_thread(rescan_thread, "rescan defaults",
B_NORMAL_PRIORITY - 2, this); B_NORMAL_PRIORITY - 2, this);
resume_thread(fThreadId); resume_thread(fRescanThread);
} }
return B_OK; return B_OK;
} }
@@ -342,50 +344,54 @@ DefaultManager::Rescan()
int32 int32
DefaultManager::rescan_thread(void *arg) DefaultManager::rescan_thread(void *arg)
{ {
reinterpret_cast<DefaultManager *>(arg)->RescanThread(); reinterpret_cast<DefaultManager *>(arg)->_RescanThread();
return 0; return 0;
} }
void void
DefaultManager::RescanThread() DefaultManager::_RescanThread()
{ {
TRACE("DefaultManager::RescanThread() enter\n"); TRACE("DefaultManager::_RescanThread() enter\n");
BAutolock locker(fRescanLock);
while (atomic_and(&fRescanRequested, 0) != 0) { while (atomic_and(&fRescanRequested, 0) != 0) {
locker.Unlock();
// We do not search for the system time source, // We do not search for the system time source,
// it should already exist // it should already exist
ASSERT(fSystemTimeSource != -1); ASSERT(fSystemTimeSource != -1);
if (fPhysicalVideoOut == -1) { if (fPhysicalVideoOut == -1) {
FindPhysical(&fPhysicalVideoOut, kMsgTypeVideoOut, false, _FindPhysical(&fPhysicalVideoOut, kMsgTypeVideoOut, false,
B_MEDIA_RAW_VIDEO); B_MEDIA_RAW_VIDEO);
FindPhysical(&fPhysicalVideoOut, kMsgTypeVideoOut, false, _FindPhysical(&fPhysicalVideoOut, kMsgTypeVideoOut, false,
B_MEDIA_ENCODED_VIDEO); B_MEDIA_ENCODED_VIDEO);
} }
if (fPhysicalVideoIn == -1) { if (fPhysicalVideoIn == -1) {
FindPhysical(&fPhysicalVideoIn, kMsgTypeVideoIn, true, _FindPhysical(&fPhysicalVideoIn, kMsgTypeVideoIn, true,
B_MEDIA_RAW_VIDEO); B_MEDIA_RAW_VIDEO);
FindPhysical(&fPhysicalVideoIn, kMsgTypeVideoIn, true, _FindPhysical(&fPhysicalVideoIn, kMsgTypeVideoIn, true,
B_MEDIA_ENCODED_VIDEO); B_MEDIA_ENCODED_VIDEO);
} }
if (fPhysicalAudioOut == -1) if (fPhysicalAudioOut == -1)
FindPhysical(&fPhysicalAudioOut, kMsgTypeAudioOut, false, _FindPhysical(&fPhysicalAudioOut, kMsgTypeAudioOut, false,
B_MEDIA_RAW_AUDIO); B_MEDIA_RAW_AUDIO);
if (fPhysicalAudioIn == -1) if (fPhysicalAudioIn == -1)
FindPhysical(&fPhysicalAudioIn, kMsgTypeAudioIn, true, _FindPhysical(&fPhysicalAudioIn, kMsgTypeAudioIn, true,
B_MEDIA_RAW_AUDIO); B_MEDIA_RAW_AUDIO);
if (fAudioMixer == -1) if (fAudioMixer == -1)
FindAudioMixer(); _FindAudioMixer();
// The normal time source is searched for after the // The normal time source is searched for after the
// Physical Audio Out has been created. // Physical Audio Out has been created.
if (fTimeSource == -1) if (fTimeSource == -1)
FindTimeSource(); _FindTimeSource();
// Connect the mixer and physical audio out (soundcard) // Connect the mixer and physical audio out (soundcard)
if (!fMixerConnected && fAudioMixer != -1 && fPhysicalAudioOut != -1) { if (!fMixerConnected && fAudioMixer != -1 && fPhysicalAudioOut != -1) {
fMixerConnected = B_OK == ConnectMixerToOutput(); fMixerConnected = B_OK == _ConnectMixerToOutput();
if (!fMixerConnected) if (!fMixerConnected)
ERROR("DefaultManager: failed to connect mixer and" ERROR("DefaultManager: failed to connect mixer and"
"soundcard\n"); "soundcard\n");
@@ -393,22 +399,24 @@ DefaultManager::RescanThread()
ERROR("DefaultManager: Did not try to connect mixer and" ERROR("DefaultManager: Did not try to connect mixer and"
"soundcard\n"); "soundcard\n");
} }
if (fMixerConnected) { if (fMixerConnected) {
add_on_server_rescan_finished_notify_command cmd; add_on_server_rescan_finished_notify_command cmd;
SendToAddOnServer(ADD_ON_SERVER_RESCAN_FINISHED_NOTIFY, &cmd, SendToAddOnServer(ADD_ON_SERVER_RESCAN_FINISHED_NOTIFY, &cmd,
sizeof(cmd)); sizeof(cmd));
} }
}
fThreadId = -1;
TRACE("DefaultManager::RescanThread() leave\n"); locker.Lock();
}
fRescanThread = -1;
TRACE("DefaultManager::_RescanThread() leave\n");
} }
void void
DefaultManager::FindPhysical(volatile media_node_id *id, uint32 default_type, DefaultManager::_FindPhysical(volatile media_node_id *id, uint32 default_type,
bool isInput, media_type type) bool isInput, media_type type)
{ {
live_node_info info[MAX_NODE_INFOS]; live_node_info info[MAX_NODE_INFOS];
@@ -419,7 +427,7 @@ DefaultManager::FindPhysical(volatile media_node_id *id, uint32 default_type,
BPath msgPath; BPath msgPath;
dormant_node_info msgDninfo; dormant_node_info msgDninfo;
int32 input_id; int32 input_id;
bool isAudio = (type == B_MEDIA_RAW_AUDIO) bool isAudio = (type == B_MEDIA_RAW_AUDIO)
|| (type == B_MEDIA_ENCODED_AUDIO); || (type == B_MEDIA_ENCODED_AUDIO);
for (int32 i = 0; i < fMsgList.CountItems(); i++) { for (int32 i = 0; i < fMsgList.CountItems(); i++) {
@@ -512,7 +520,7 @@ DefaultManager::FindPhysical(volatile media_node_id *id, uint32 default_type,
void void
DefaultManager::FindTimeSource() DefaultManager::_FindTimeSource()
{ {
live_node_info info[MAX_NODE_INFOS]; live_node_info info[MAX_NODE_INFOS];
media_format input; /* a physical audio output has a logical data input (DAC)*/ media_format input; /* a physical audio output has a logical data input (DAC)*/
@@ -577,7 +585,7 @@ DefaultManager::FindTimeSource()
void void
DefaultManager::FindAudioMixer() DefaultManager::_FindAudioMixer()
{ {
live_node_info info; live_node_info info;
int32 count; int32 count;
@@ -596,7 +604,7 @@ DefaultManager::FindAudioMixer()
status_t status_t
DefaultManager::ConnectMixerToOutput() DefaultManager::_ConnectMixerToOutput()
{ {
BMediaRoster *roster; BMediaRoster *roster;
media_node timesource; media_node timesource;
+63 -38
View File
@@ -1,56 +1,81 @@
/* /*
* Copyright 2010, Haiku. All rights reserved.
* Distributed under the terms of the MIT license.
*
* Authors:
* Marcus Overhagen
* Jérôme Duval
*/
/*
* Copyright 2002, Marcus Overhagen. All rights reserved. * Copyright 2002, Marcus Overhagen. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#ifndef _DEFAULT_MANAGER_H
#define _DEFAULT_MANAGER_H
/*! Manager for defaults (audio and video, input and output)
*/
#include "DataExchange.h" #include "DataExchange.h"
#include <Autolock.h>
#include <Message.h> #include <Message.h>
class NodeManager; class NodeManager;
class DefaultManager
{ class DefaultManager {
public: public:
DefaultManager(); DefaultManager();
~DefaultManager(); ~DefaultManager();
status_t LoadState(); status_t LoadState();
status_t SaveState(NodeManager *node_manager); status_t SaveState(NodeManager *node_manager);
status_t Set(media_node_id nodeid, const char *input_name, int32 input_id, node_type type); status_t Set(media_node_id nodeid,
status_t Get(media_node_id *nodeid, char *input_name, int32 *input_id, node_type type); const char *input_name, int32 input_id,
status_t Rescan(); node_type type);
status_t Get(media_node_id *nodeid, char *input_name,
void Dump(); int32 *input_id, node_type type);
status_t Rescan();
void CleanupTeam(team_id team); void Dump();
void CleanupTeam(team_id team);
private: private:
static int32 rescan_thread(void *arg); static int32 rescan_thread(void *arg);
void RescanThread(); void _RescanThread();
void _FindPhysical(volatile media_node_id *id,
uint32 default_type, bool isInput,
media_type type);
void _FindAudioMixer();
void _FindTimeSource();
status_t _ConnectMixerToOutput();
void FindPhysical(volatile media_node_id *id, uint32 default_type, bool isInput, media_type type);
void FindAudioMixer();
void FindTimeSource();
status_t ConnectMixerToOutput();
private: private:
volatile bool fMixerConnected; volatile bool fMixerConnected;
volatile media_node_id fPhysicalVideoOut; volatile media_node_id fPhysicalVideoOut;
volatile media_node_id fPhysicalVideoIn; volatile media_node_id fPhysicalVideoIn;
volatile media_node_id fPhysicalAudioOut; volatile media_node_id fPhysicalAudioOut;
volatile media_node_id fPhysicalAudioIn; volatile media_node_id fPhysicalAudioIn;
volatile media_node_id fSystemTimeSource; volatile media_node_id fSystemTimeSource;
volatile media_node_id fTimeSource; volatile media_node_id fTimeSource;
volatile media_node_id fAudioMixer; volatile media_node_id fAudioMixer;
volatile int32 fPhysicalAudioOutInputID; volatile int32 fPhysicalAudioOutInputID;
char fPhysicalAudioOutInputName[B_MEDIA_NAME_LENGTH]; char fPhysicalAudioOutInputName[B_MEDIA_NAME_LENGTH];
BList fMsgList; BList fMsgList;
uint32 fBeginHeader[3]; uint32 fBeginHeader[3];
uint32 fEndHeader[3]; uint32 fEndHeader[3];
thread_id fThreadId; thread_id fRescanThread;
vint32 fRescanRequested; vint32 fRescanRequested;
BLocker fRescanLock;
}; };
#endif // _DEFAULT_MANAGER_H
+5 -1
View File
@@ -2,6 +2,8 @@
* Copyright 2003, Jérôme Duval. All rights reserved. * Copyright 2003, Jérôme Duval. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#ifndef _MEDIA_FILES_MANAGER_H
#define _MEDIA_FILES_MANAGER_H
#include <map> #include <map>
@@ -12,7 +14,7 @@
#include <MessageRunner.h> #include <MessageRunner.h>
#include <String.h> #include <String.h>
#include <DataExchange.h> #include "DataExchange.h"
#define MEDIA_FILES_MANAGER_SAVE_TIMER 'mmst' #define MEDIA_FILES_MANAGER_SAVE_TIMER 'mmst'
@@ -70,3 +72,5 @@ private:
TypeMap fMap; TypeMap fMap;
BMessageRunner* fSaveTimerRunner; BMessageRunner* fSaveTimerRunner;
}; };
#endif // _MEDIA_FILES_MANAGER_H
+4 -3
View File
@@ -1,10 +1,11 @@
/* /*
* Copyright 2002, Marcus Overhagen. All rights reserved. * Copyright 2002, Marcus Overhagen. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#ifndef NOTIFICATION_MANAGER_H #ifndef NOTIFICATION_MANAGER_H
#define NOTIFICATION_MANAGER_H #define NOTIFICATION_MANAGER_H
#include <Locker.h> #include <Locker.h>
#include <MediaNode.h> #include <MediaNode.h>
#include <Messenger.h> #include <Messenger.h>
@@ -24,9 +25,9 @@ class NotificationManager {
public: public:
NotificationManager(); NotificationManager();
~NotificationManager(); ~NotificationManager();
void Dump(); void Dump();
void EnqueueMessage(BMessage* message); void EnqueueMessage(BMessage* message);
void CleanupTeam(team_id team); void CleanupTeam(team_id team);