2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
6 #include "AbstractModelLoader.h"
8 #include <AutoLocker.h>
10 #include "MessageCodes.h"
13 AbstractModelLoader::AbstractModelLoader(const BMessenger
& target
,
16 fLock("main model loader"),
18 fTargetCookie(targetCookie
),
26 AbstractModelLoader::~AbstractModelLoader()
32 AbstractModelLoader::StartLoading()
34 // check initialization
35 status_t error
= fLock
.InitCheck();
39 AutoLocker
<BLocker
> locker(fLock
);
44 // prepare for loading
45 error
= PrepareForLoading();
49 // spawn the loader thread
50 fLoaderThread
= spawn_thread(&_LoaderEntry
, "model loader",
51 B_NORMAL_PRIORITY
, this);
52 if (fLoaderThread
< 0)
58 resume_thread(fLoaderThread
);
65 AbstractModelLoader::Abort(bool wait
)
67 AutoLocker
<BLocker
> locker(fLock
);
69 if (fLoaderThread
< 0)
72 thread_id thread
= fLoaderThread
;
80 wait_for_thread(thread
, NULL
);
85 AbstractModelLoader::Delete()
92 /*! Called from StartLoading() with the lock held.
95 AbstractModelLoader::PrepareForLoading()
102 AbstractModelLoader::Load()
108 /*! Called after loading Load() is done with the lock held.
111 AbstractModelLoader::FinishLoading(bool success
)
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
);
130 AbstractModelLoader::_LoaderEntry(void* data
)
132 return ((AbstractModelLoader
*)data
)->_Loader();
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
);