summaryrefslogtreecommitdiff
path: root/plugins/threaded/src/threaded_scheduler.c
blob: 6c540765549f78d516ec0e575356e05c6e905727 (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
//---------------------------------------------------------------------------------
//
//  Little Color Management System, multithreaded extensions
//  Copyright (c) 1998-2022 Marti Maria Saguer, all rights reserved
//
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------------

#include "threaded_internal.h"

// The scheduler is responsible to split the work in several portions in a way that each
// portion can be calculated by a different thread. All loacking is already done by lcms 
// mutexes, and memory should not overlap.
void  _cmsThrScheduler(struct _cmstransform_struct* CMMcargo,
                       const void* InputBuffer,
                       void* OutputBuffer,
                       cmsUInt32Number PixelsPerLine,
                       cmsUInt32Number LineCount,
                       const cmsStride* Stride)
{
    cmsContext ContextID = cmsGetTransformContextID(CMMcargo);
    _cmsTransform2Fn worker = _cmsGetTransformWorker(CMMcargo);
    cmsInt32Number   MaxWorkers = _cmsGetTransformMaxWorkers(CMMcargo);

    // flags are not actually being used
    // cmsUInt32Number  flags = _cmsGetTransformWorkerFlags(CMMcargo);

    _cmsWorkSlice master;
    _cmsWorkSlice* slices;
    cmsStride FixedStride = *Stride;
    cmsHANDLE* handles;

    //  Count the number of threads needed for this job. MaxWorkers is the upper limit or -1 to auto
    cmsUInt32Number nSlices = _cmsThrCountSlices(CMMcargo, MaxWorkers, PixelsPerLine, LineCount, &FixedStride);
    
    // Abort early if no threaded code
    if (nSlices <= 1) {

        worker(CMMcargo, InputBuffer, OutputBuffer, PixelsPerLine, LineCount, Stride);
        return;
    }

    // Setup master thread
    master.CMMcargo = CMMcargo;
    master.InputBuffer = InputBuffer;
    master.OutputBuffer = OutputBuffer;
    master.PixelsPerLine = PixelsPerLine;
    master.LineCount = LineCount;
    master.Stride = &FixedStride;

    // Create memory for the slices
    slices  = (_cmsWorkSlice*)_cmsCalloc(ContextID, nSlices, sizeof(_cmsWorkSlice));
    handles = (cmsHANDLE*) _cmsCalloc(ContextID, nSlices, sizeof(cmsHANDLE));

    if (slices == NULL || handles == NULL)
    {
        if (slices)  _cmsFree(ContextID, slices);
        if (handles) _cmsFree(ContextID, handles);

        // Out of memory in this case only can come from a corruption, but we do the work anyway
        worker(CMMcargo, InputBuffer, OutputBuffer, PixelsPerLine, LineCount, Stride);
        return;
    }

    // All seems ok so far
    if (_cmsThrSplitWork(&master, nSlices, slices))
    {
        // Work is splitted. Create threads
        cmsUInt32Number i;

        for (i = 1; i < nSlices; i++)
        {
            handles[i] = _cmsThrCreateWorker(ContextID, worker, &slices[i]);
        }

        // Do our portion of work 
        worker(CMMcargo, slices[0].InputBuffer, slices[0].OutputBuffer, 
            slices[0].PixelsPerLine, slices[0].LineCount, slices[0].Stride);

        // Wait until all threads are finished
        for (i = 1; i < nSlices; i++)
        {
            _cmsThrJoinWorker(ContextID, handles[i]);
        }
    }
    else
    {
        // Not able to split the work, so don't thread
        worker(CMMcargo, InputBuffer, OutputBuffer, PixelsPerLine, LineCount, Stride);
    }

    _cmsFree(ContextID, slices);
    _cmsFree(ContextID, handles);
}