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

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

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

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

struct ItemJob
{
    Item* item;
    int step;
    int count;
    volatile int performed;
};

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

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

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

void JobHandlerThreadPool::RunHashCalculation(std::vector<Item>& arr)
{
    ItemJob* itemjobs = static_cast<ItemJob*>(OG_MALLOC(sizeof(ItemJob)*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;

        int err = pthread_create( &threads[i], NULL, CalculateItemHash, &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();
    }

    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 );
}