summaryrefslogtreecommitdiff
path: root/plugins/threaded/src/threaded_core.c
blob: 88f617ba6269ce9ac2bffb4d25635cd5246c8407 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//---------------------------------------------------------------------------------
//
//  Little Color Management System, multithread 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"

// This is the threading support. Unfortunately, it has to be platform-dependent because 
// windows does not support pthreads. 
#ifdef CMS_IS_WINDOWS_

#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>

// To pass parameter to the thread
typedef struct
{
    _cmsTransform2Fn worker;
    _cmsWorkSlice* param;

} thread_adaptor_param;


// This is an adaptor to the native thread on windows
static
DWORD WINAPI thread_adaptor(LPVOID p)
{
    thread_adaptor_param* ap = (thread_adaptor_param*)p;
    _cmsWorkSlice* s = ap->param;

    ap->worker(s->CMMcargo, s->InputBuffer, s->OutputBuffer, 
               s->PixelsPerLine, s->LineCount, s->Stride);
    _cmsFree(0, p);
    return 0;
}

// This function creates a thread and executes it. The thread calls the worker function
// with the given parameters.
cmsHANDLE _cmsThrCreateWorker(cmsContext ContextID, _cmsTransform2Fn worker, _cmsWorkSlice* param)
{
    DWORD ThreadID;
    thread_adaptor_param* p;
    HANDLE handle;

    p = (thread_adaptor_param*)_cmsMalloc(0, sizeof(thread_adaptor_param));
    if (p == NULL) return NULL;

    p->worker = worker;
    p->param = param;

    handle  = CreateThread(NULL, 0, thread_adaptor, (LPVOID) p, 0, &ThreadID);
    if (handle == NULL)
    {
        cmsSignalError(ContextID, cmsERROR_UNDEFINED, "Cannot create thread");
    }

    return (cmsHANDLE)handle;
}

// Waits until given thread is ended
void _cmsThrJoinWorker(cmsContext ContextID, cmsHANDLE hWorker)
{
    if (WaitForSingleObject((HANDLE)hWorker, INFINITE) != WAIT_OBJECT_0)
    {
        cmsSignalError(ContextID, cmsERROR_UNDEFINED, "Cannot join thread");
    }
}

// Returns the ideal number of threads the system can run
cmsInt32Number _cmsThrIdealThreadCount(void)
{
    SYSTEM_INFO sysinfo;

    GetSystemInfo(&sysinfo);
    return sysinfo.dwNumberOfProcessors; //Returns the number of processors in the system.
}

#else

// Rest of the wide world
#include <pthread.h>
#include <unistd.h>

// To pass parameter to the thread
typedef struct
{
    _cmsTransform2Fn worker;
    _cmsWorkSlice* param;

} thread_adaptor_param;


// This is the native thread on pthread
static
void thread_adaptor(void* p)
{
    thread_adaptor_param* ap = (thread_adaptor_param*)p;
    _cmsWorkSlice* s = ap->param;

    ap->worker(s->CMMcargo, s->InputBuffer, s->OutputBuffer,
               s->PixelsPerLine, s->LineCount, &s->Stride);
}

// This function creates a thread and executes it. The thread calls the worker function
// with the given parameters.
cmsHANDLE _cmsThrCreateWorker(cmsContext ContextID, _cmsTransform2Fn worker, _cmsWorkSlice* param)
{
    pthread_t threadId;

    int err = pthread_create(&threadId, NULL, thread_adaptor, param);
    if (err != 0)
    {
        cmsSignalError(ContextID, cmsERROR_UNDEFINED, "Cannot create thread [pthread error %d]", err);
        return NULL;
    }
    else
        return (cmsHANDLE) threadId;
}

// Waits until given thread is ended
void _cmsThrJoinWorker(cmsContext ContextID, cmsHANDLE hWorker)
{
    int err = pthread_join((pthread_t)hWorker, NULL);
    if (err != 0)
    {
        cmsSignalError(ContextID, cmsERROR_UNDEFINED, "Cannot join thread [pthread error %d]", err); 
    }
}

cmsInt32Number _cmsThrIdealThreadCount(void)
{    
    long cores = sysconf(_SC_NPROCESSORS_ONLN);
    if (cores == -1L)
        return 1;
    else
        return (cmsInt32Number)cores;
}

#endif