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

manifestthreadpool.cpp « Ogda « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c89d8e3da23288b51df457249f2b21afbf0e9426 (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
//-----------------------------------------------------------------------------
//           Name: manifestthreadpool.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 <Memory/allocation.h>
#include "manifestthreadpool.h"

#include <Logging/logdata.h>
#include <cstdlib>
#include <pthread.h>
#include <iostream>
#include "main.h"

#ifdef PLATFORM_LINUX
#include <unistd.h>
#endif

struct ManifestResultJob
{
    const char* base_path;
    ManifestResult* item;
    int step;
    int count;
    volatile int performed;
};

static void* CalculateManifestResultHash( void* item )
{
    ManifestResultJob* ij = static_cast<ManifestResultJob*>(item); 
    ij->performed = 0;
    for( int i = 0; i < ij->count; i++ )
    {
        ij->item[i*ij->step].CalculateHash(ij->base_path);
        ij->performed++;
    }
    return 0;
}

ManifestThreadPool::ManifestThreadPool( int thread_count ) : thread_count(thread_count)
{
    threads = static_cast<pthread_t*>(OG_MALLOC( sizeof(pthread_t) * thread_count ));
}

ManifestThreadPool::~ManifestThreadPool()
{
    OG_FREE(threads);
}

void ManifestThreadPool::RunHashCalculation(const std::string& base_path, std::vector<ManifestResult>& arr)
{
    ManifestResultJob* itemjobs = static_cast<ManifestResultJob*>(OG_MALLOC(sizeof(ManifestResultJob)*thread_count));
    
    int step = arr.size()/thread_count;

    for( int i = 0; i < thread_count; i++ )
    {
        itemjobs[i].item = &arr[i];
        itemjobs[i].step = thread_count;
        itemjobs[i].count = step;
        itemjobs[i].base_path = base_path.c_str();

        int err = pthread_create( &threads[i], NULL, CalculateManifestResultHash, &itemjobs[i]);

        if( err != 0 )
        {
            LOGE << "Thread create failed" << std::endl;
        }
    }

    //Do the remaining items in our main thread.
    for( int i = step*thread_count; i < arr.size(); i++ )
    {
        arr[i].CalculateHash(base_path.c_str());
    }

    int count = 0;
    while( count < arr.size() )
    {
#ifdef PLATFORM_LINUX
        sleep(1);
#endif
        std::stringstream ss;
        count = arr.size()-step*thread_count;
        for( int i = 0; i < thread_count; i++ )
        {
            count += itemjobs[i].performed;
            ss << ((itemjobs[i].performed == itemjobs[i].count) ? "d" : ".");
        }
        SetPercent(ss.str().c_str(), (int)(100.0f*((float)count/(float)arr.size())));
    }

    for( int i = 0; i < thread_count; i++ )
    {
        void* retval; 
        int err = pthread_join( threads[i], &retval );

        if( err != 0 )
        {
            LOGE << "Thread join failed" << std::endl;
        }
    }
    OG_FREE( itemjobs );
}