+ Added ThreadManager object to MIMEManager

+ Reimplemented mime update calls to now handle both synchronous
  and asynchronous calls, as well as work with RegistrarThreads and
  the ThreadManager.


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@1277 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Tyler Dauwalder 2002-09-29 07:17:29 +00:00
parent 5720cd9c75
commit 4a94b7bbb3
2 changed files with 85 additions and 22 deletions

View File

@ -3,6 +3,9 @@
#include <Bitmap.h> #include <Bitmap.h>
#include <ClassInfo.h> #include <ClassInfo.h>
#include <Message.h> #include <Message.h>
#include <Messenger.h>
#include <mime/UpdateMimeInfoThread.h>
#include <mime/CreateAppMetaMimeThread.h>
#include <RegistrarDefs.h> #include <RegistrarDefs.h>
#include <String.h> #include <String.h>
#include <TypeConstants.h> #include <TypeConstants.h>
@ -25,7 +28,9 @@
MIMEManager::MIMEManager() MIMEManager::MIMEManager()
: BLooper("main_mime") : BLooper("main_mime")
, fDatabase() , fDatabase()
, fThreadManager()
{ {
AddHandler(&fThreadManager);
} }
// destructor // destructor
@ -166,34 +171,89 @@ MIMEManager::MessageReceived(BMessage *message)
break; break;
} }
case B_REG_MIME_UPDATE_MIME_INFO_ASYNC: case B_REG_MIME_CREATE_APP_META_MIME:
case B_REG_MIME_UPDATE_MIME_INFO:
{ {
entry_ref ref; using BPrivate::Storage::Mime::MimeUpdateThread;
using BPrivate::Storage::Mime::CreateAppMetaMimeThread;
using BPrivate::Storage::Mime::UpdateMimeInfoThread;
entry_ref root;
bool recursive, force; bool recursive, force;
err = message->FindRef("entry", &ref); bool synchronous = false;
MimeUpdateThread *thread = NULL;
status_t threadStatus = B_NO_INIT;
bool messageIsDetached = false;
bool stillOwnThread = true;
// Gather our arguments
err = message->FindRef("entry", &root);
if (!err) if (!err)
err = message->FindBool("recursive", &recursive); err = message->FindBool("recursive", &recursive);
if (!err)
err = message->FindBool("synchronous", &synchronous);
if (!err) if (!err)
err = message->FindBool("force", &force); err = message->FindBool("force", &force);
// Detach the message for synchronous calls
if (!err && synchronous) {
DetachCurrentMessage();
messageIsDetached = true;
}
// Create the appropriate flavor of mime update thread
if (!err) {
switch (message->what) {
case B_REG_MIME_CREATE_APP_META_MIME:
thread = new(nothrow) CreateAppMetaMimeThread((synchronous ?
"create_app_meta_mime (s)" : "create_app_meta_mime (a)"),
B_NORMAL_PRIORITY, BMessenger(&fThreadManager), &root, recursive,
force, synchronous ? message : NULL);
break;
case B_REG_MIME_UPDATE_MIME_INFO:
thread = new(nothrow) UpdateMimeInfoThread((synchronous ?
"update_mime_info (s)" : "update_mime_info (a)"),
B_NORMAL_PRIORITY, BMessenger(&fThreadManager), &root, recursive,
force, synchronous ? message : NULL);
break;
default:
err = B_BAD_VALUE;
break;
}
}
if (!err) if (!err)
err = fDatabase.UpdateMimeInfoAsync(&ref, recursive, force); err = thread ? B_OK : B_NO_MEMORY;
reply.what = B_REG_RESULT;
reply.AddInt32("result", err);
message->SendReply(&reply, this);
break;
}
case B_REG_MIME_ASYNC_THREAD_FINISHED:
{
thread_id id;
err = message->FindInt32("thread id", &id);
if (!err) if (!err)
err = fDatabase.CleanupAfterAsyncThread(id); err = threadStatus = thread->InitCheck();
reply.what = B_REG_RESULT; // Launch the thread
reply.AddInt32("result", err); if (!err) {
message->SendReply(&reply, this); err = fThreadManager.LaunchThread(thread);
if (!err)
stillOwnThread = false;
}
// If something went wrong, we need to notify the sender regardless. However,
// if this is a synchronous call, we've already detached the message, and must
// be careful that it gets deleted once and only once. Thus, if the MimeUpdateThread
// object was created successfully, we don't need to delete the message, as that
// object has assumed control of it. Otherwise, we are still responsible.
if (err || !synchronous) {
// Send the reply
reply.what = B_REG_RESULT;
reply.AddInt32("result", err);
message->SendReply(&reply, this);
}
// Delete the message if necessary
if (messageIsDetached && threadStatus != B_OK)
delete message;
// Delete the thread if necessary
if (stillOwnThread)
delete thread;
break; break;
} }

View File

@ -5,6 +5,7 @@
#include <Looper.h> #include <Looper.h>
#include <mime/Database.h> #include <mime/Database.h>
#include <ThreadManager.h>
class MIMEManager : public BLooper { class MIMEManager : public BLooper {
public: public:
@ -17,6 +18,8 @@ private:
void HandleDeleteParam(BMessage *message); void HandleDeleteParam(BMessage *message);
BPrivate::Storage::Mime::Database fDatabase; BPrivate::Storage::Mime::Database fDatabase;
ThreadManager fThreadManager;
BMessenger fManagerMessenger;
}; };
#endif // MIME_MANAGER_H #endif // MIME_MANAGER_H