BTRFS: Implement BTree::Path and change _Find.
[haiku.git] / src / apps / debuganalyzer / model_loader / AbstractModelLoader.cpp
blob8956375ea32bf3fe76e29dbc03edb1875fccf1d6
1 /*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
6 #include "AbstractModelLoader.h"
8 #include <AutoLocker.h>
10 #include "MessageCodes.h"
13 AbstractModelLoader::AbstractModelLoader(const BMessenger& target,
14 void* targetCookie)
16 fLock("main model loader"),
17 fTarget(target),
18 fTargetCookie(targetCookie),
19 fLoaderThread(-1),
20 fLoading(false),
21 fAborted(false)
26 AbstractModelLoader::~AbstractModelLoader()
31 status_t
32 AbstractModelLoader::StartLoading()
34 // check initialization
35 status_t error = fLock.InitCheck();
36 if (error != B_OK)
37 return error;
39 AutoLocker<BLocker> locker(fLock);
41 if (fLoading)
42 return B_BAD_VALUE;
44 // prepare for loading
45 error = PrepareForLoading();
46 if (error != B_OK)
47 return error;
49 // spawn the loader thread
50 fLoaderThread = spawn_thread(&_LoaderEntry, "model loader",
51 B_NORMAL_PRIORITY, this);
52 if (fLoaderThread < 0)
53 return fLoaderThread;
55 fLoading = true;
56 fAborted = false;
58 resume_thread(fLoaderThread);
60 return B_OK;
64 void
65 AbstractModelLoader::Abort(bool wait)
67 AutoLocker<BLocker> locker(fLock);
69 if (fLoaderThread < 0)
70 return;
72 thread_id thread = fLoaderThread;
74 if (fLoading)
75 fAborted = true;
77 locker.Unlock();
79 if (wait)
80 wait_for_thread(thread, NULL);
84 void
85 AbstractModelLoader::Delete()
87 Abort(true);
88 delete this;
92 /*! Called from StartLoading() with the lock held.
94 status_t
95 AbstractModelLoader::PrepareForLoading()
97 return B_OK;
101 status_t
102 AbstractModelLoader::Load()
104 return B_OK;
108 /*! Called after loading Load() is done with the lock held.
110 void
111 AbstractModelLoader::FinishLoading(bool success)
116 void
117 AbstractModelLoader::NotifyTarget(bool success)
119 BMessage message(success
120 ? MSG_MODEL_LOADED_SUCCESSFULLY
121 : fAborted ? MSG_MODEL_LOADED_ABORTED : MSG_MODEL_LOADED_FAILED);
123 message.AddPointer("loader", this);
124 message.AddPointer("targetCookie", fTargetCookie);
125 fTarget.SendMessage(&message);
129 /*static*/ status_t
130 AbstractModelLoader::_LoaderEntry(void* data)
132 return ((AbstractModelLoader*)data)->_Loader();
136 status_t
137 AbstractModelLoader::_Loader()
139 bool success = Load() == B_OK;
141 // clean up and notify the target
142 AutoLocker<BLocker> locker(fLock);
144 FinishLoading(success);
145 NotifyTarget(success);
146 fLoading = false;
148 return B_OK;