Welcome to mirror list, hosted at ThFree Co, Russian Federation.

assetmanagerthreadhandler.cpp « Asset « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 53bf7b57478a48c7023d2408878aa5eea3717362 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//-----------------------------------------------------------------------------
//           Name: assetmanagerthreadhandler.cpp
//      Developer: Wolfire Games LLC
//    Description:
//        License: Read below
//-----------------------------------------------------------------------------
//
//
//   Copyright 2022 Wolfire Games LLC
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
//-----------------------------------------------------------------------------

#include "assetmanagerthreadhandler.h"

#include <Memory/allocation.h>
#include <Asset/assetloaderrors.h>

#include <cstdlib>

AssetLoaderThreadedInstance::AssetLoaderThreadedInstance() : loader(NULL), load_id(0), step(0), return_state(kLoadOk) {
}

AssetLoaderThreadedInstance::AssetLoaderThreadedInstance(AssetLoaderBase* loader, uint32_t load_id, int step) : loader(loader), load_id(load_id), step(step), return_state(kLoadOk) {
}

AssetManagerThreadedInstanceQueue::AssetManagerThreadedInstanceQueue() : queue(NULL), queue_count(0), queue_size(0) {
    accessmutex = new std::mutex();
}

AssetManagerThreadedInstanceQueue::~AssetManagerThreadedInstanceQueue() {
    OG_FREE(queue);
    queue = NULL;
    queue_count = 0;
    queue_size = 0;

    delete accessmutex;
    accessmutex = NULL;
}

AssetLoaderThreadedInstance AssetManagerThreadedInstanceQueue::Pop() {
    AssetLoaderThreadedInstance instance;
    accessmutex->lock();
    if (queue_count > 0) {
        instance = queue[0];
        for (unsigned i = 1; i < queue_count; i++) {
            queue[i - 1] = queue[i];
        }
        queue_count -= 1;
    }
    accessmutex->unlock();

    return instance;
}

void AssetManagerThreadedInstanceQueue::Push(const AssetLoaderThreadedInstance& input) {
    accessmutex->lock();

    if (queue_size >= queue_count) {
        queue_size += 32;
        queue = static_cast<AssetLoaderThreadedInstance*>(realloc(queue, sizeof(AssetLoaderThreadedInstance) * queue_size));
    }

    queue[queue_count] = input;
    queue_count += 1;

    accessmutex->unlock();
}

size_t AssetManagerThreadedInstanceQueue::Count() {
    size_t ret;
    accessmutex->lock();
    ret = queue_count;
    accessmutex->unlock();
    return ret;
}

AssetManagerThreadInstance::AssetManagerThreadInstance(AssetManagerThreadedInstanceQueue* queue_in, AssetManagerThreadedInstanceQueue* queue_out, bool* stop) : queue_in(queue_in), queue_out(queue_out), stop(stop) {
}

void AssetManagerThreadHandler_Operate(void* userdata) {
    AssetManagerThreadInstance* data = static_cast<AssetManagerThreadInstance*>(userdata);
    while (*(data->stop) == false) {
        if (data->queue_in->Count() > 0) {
            AssetLoaderThreadedInstance instance = data->queue_in->Pop();

            instance.return_state = instance.loader->DoLoadStep(instance.step);

            data->queue_out->Push(instance);
        } else {
            std::this_thread::sleep_for(std::chrono::milliseconds(16));
        }
    }

    delete data;
}

AssetManagerThreadHandler::AssetManagerThreadHandler(int thread_count) : stop(false) {
    for (int i = 0; i < thread_count; i++) {
        AssetManagerThreadInstance* threadInstance = new AssetManagerThreadInstance(&queue_in, &queue_out, &stop);
        std::thread* thread = new std::thread(AssetManagerThreadHandler_Operate, threadInstance);
        threads.push_back(thread);
    }
}

AssetManagerThreadHandler::~AssetManagerThreadHandler() {
    stop = true;

    for (auto& thread : threads) {
        thread->join();
        thread = NULL;
    }
}