Merge pull request #60 from mihaip/upstream-postinit

machinebase: fully initialize devices registered by other devices
This commit is contained in:
Maxim Poliakovski 2023-11-08 13:55:09 +01:00 committed by GitHub
commit 37cca00e13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 3 deletions

View File

@ -24,6 +24,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <machines/machinebase.h>
#include <map>
#include <set>
#include <string>
std::unique_ptr<MachineBase> gMachineObj = 0;
@ -79,9 +80,18 @@ HWComponent* MachineBase::get_comp_by_type(HWCompType type) {
int MachineBase::postinit_devices()
{
for (auto it = this->device_map.begin(); it != this->device_map.end(); it++) {
if (it->second->device_postinit()) {
return -1;
// Allow additional devices to be registered by device_postinit, in which
// case we'll initialize them too.
std::set<std::string> initialized_devices;
while (initialized_devices.size() < this->device_map.size()) {
for (auto it = this->device_map.begin(); it != this->device_map.end(); it++) {
if (initialized_devices.find(it->first) == initialized_devices.end()) {
if (it->second->device_postinit()) {
LOG_F(ERROR, "Could not initialize device %s", it->first.c_str());
return -1;
}
initialized_devices.insert(it->first);
}
}
}