Skip to content

Commit a49532a

Browse files
committed
common: STM32 portability enhancement
Some systems such as micro-processor might not support the thread feature on the system. Enhance the portability by compiling the thorvg with toggling the threading depepdency through the build option. For this, thorvg newly introduced the internal Key/ScopedLock abstraction for transparent thread-locking dependnecy. To turn off the thread feature, please use the next build option: $meson setup build -Dthreads=false ... Note that, the thread feature is enabled in default. Turning off the thread feature could reduce the binary size by 7kb. issue: thorvg#1900
1 parent a3470e8 commit a49532a

File tree

11 files changed

+195
-68
lines changed

11 files changed

+195
-68
lines changed

meson.build

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ add_project_arguments('-DEXAMPLE_DIR="@0@/src/examples/resources"'.format(src_di
1313

1414
config_h.set_quoted('THORVG_VERSION_STRING', meson.project_version())
1515

16+
#Multi-Tasking
17+
if get_option('threads') == true
18+
config_h.set10('THORVG_THREAD_SUPPORT', true)
19+
endif
20+
1621
#Engines
1722
if get_option('engines').contains('sw') == true
1823
config_h.set10('THORVG_SW_RASTER_SUPPORT', true)
@@ -132,32 +137,34 @@ Summary:
132137
ThorVG version: @0@
133138
Build Type: @1@
134139
Prefix: @2@
135-
SIMD Instruction: @3@
136-
Raster Engine (SW): @4@
137-
Raster Engine (GL): @5@
138-
Raster Engine (WG): @6@
139-
Loader (TVG): @7@
140-
Loader (SVG): @8@
141-
Loader (TTF): @9@
142-
Loader (LOTTIE): @10@
143-
Loader (PNG): @11@
144-
Loader (JPG): @12@
145-
Loader (WEBP_BETA): @13@
146-
Saver (TVG): @14@
147-
Saver (GIF): @15@
148-
Binding (CAPI): @16@
149-
Binding (WASM_BETA): @17@
150-
Log Message: @18@
151-
Tests: @19@
152-
Examples: @20@
153-
Tool (Svg2Tvg): @21@
154-
Tool (Svg2Png): @22@
155-
Tool (Lottie2Gif): @23@
140+
Multi-Tasking: @3@
141+
SIMD Instruction: @4@
142+
Raster Engine (SW): @5@
143+
Raster Engine (GL): @6@
144+
Raster Engine (WG): @7@
145+
Loader (TVG): @8@
146+
Loader (SVG): @9@
147+
Loader (TTF): @10@
148+
Loader (LOTTIE): @11@
149+
Loader (PNG): @12@
150+
Loader (JPG): @13@
151+
Loader (WEBP_BETA): @14@
152+
Saver (TVG): @15@
153+
Saver (GIF): @16@
154+
Binding (CAPI): @17@
155+
Binding (WASM_BETA): @18@
156+
Log Message: @19@
157+
Tests: @20@
158+
Examples: @21@
159+
Tool (Svg2Tvg): @22@
160+
Tool (Svg2Png): @23@
161+
Tool (Lottie2Gif): @24@
156162
157163
'''.format(
158164
meson.project_version(),
159165
get_option('buildtype'),
160166
get_option('prefix'),
167+
get_option('threads'),
161168
simd_type,
162169
get_option('engines').contains('sw'),
163170
get_option('engines').contains('gl_beta'),

meson_options.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ option('savers',
1616
value: [''],
1717
description: 'Enable File Savers in thorvg')
1818

19+
option('threads',
20+
type: 'boolean',
21+
value: true,
22+
description: 'Enable the multi-threading task scheduler in thorvg')
23+
1924
option('vector',
2025
type: 'boolean',
2126
value: false,

src/common/meson.build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
source_file = [
22
'tvgArray.h',
33
'tvgBezier.h',
4-
'tvgFormat.h',
54
'tvgCompressor.h',
5+
'tvgFormat.h',
66
'tvgInlist.h',
7+
'tvgLock.h',
78
'tvgMath.h',
89
'tvgStr.h',
910
'tvgBezier.cpp',

src/common/tvgLock.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2024 the ThorVG project. All rights reserved.
3+
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
#ifndef _TVG_LOCK_H_
24+
#define _TVG_LOCK_H_
25+
26+
#ifdef THORVG_THREAD_SUPPORT
27+
28+
#include <mutex>
29+
30+
namespace tvg {
31+
32+
struct Key
33+
{
34+
std::mutex mtx;
35+
};
36+
37+
struct ScopedLock
38+
{
39+
Key* key = nullptr;
40+
41+
ScopedLock(Key& key)
42+
{
43+
key.mtx.lock();
44+
this->key = &key;
45+
}
46+
47+
~ScopedLock()
48+
{
49+
key->mtx.unlock();
50+
}
51+
};
52+
53+
}
54+
55+
#else //THORVG_THREAD_SUPPORT
56+
57+
namespace tvg {
58+
59+
struct Key {};
60+
61+
struct ScopedLock
62+
{
63+
ScopedLock(Key& key) {}
64+
};
65+
66+
}
67+
68+
#endif //THORVG_THREAD_SUPPORT
69+
70+
#endif //_TVG_LOCK_H_

src/meson.build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ subdir('loaders')
4242
subdir('savers')
4343

4444
thorvg_lib_dep = [common_dep, utils_dep, loader_dep, saver_dep]
45-
if host_machine.system() != 'windows' and host_machine.system() != 'android'
45+
46+
if get_option('threads') == true and host_machine.system() != 'windows' and host_machine.system() != 'android'
4647
thread_dep = meson.get_compiler('cpp').find_library('pthread')
4748
thorvg_lib_dep += [thread_dep]
4849
endif

src/renderer/sw_engine/tvgSwRaster.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,7 @@ void rasterUnpremultiply(Surface* surface)
18551855

18561856
void rasterPremultiply(Surface* surface)
18571857
{
1858-
unique_lock<mutex> lock{surface->mtx};
1858+
ScopedLock lock(surface->key);
18591859
if (surface->premultiplied || (surface->channelSize != sizeof(uint32_t))) return;
18601860
surface->premultiplied = true;
18611861

@@ -1936,7 +1936,7 @@ bool rasterImage(SwSurface* surface, SwImage* image, const RenderMesh* mesh, con
19361936

19371937
bool rasterConvertCS(Surface* surface, ColorSpace to)
19381938
{
1939-
unique_lock<mutex> lock{surface->mtx};
1939+
ScopedLock lock(surface->key);
19401940
if (surface->cs == to) return true;
19411941

19421942
//TOOD: Support SIMD accelerations

src/renderer/tvgLoader.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "tvgInlist.h"
2626
#include "tvgLoader.h"
27+
#include "tvgLock.h"
2728

2829
#ifdef THORVG_SVG_LOADER_SUPPORT
2930
#include "tvgSvgLoader.h"
@@ -65,7 +66,7 @@ uint64_t HASH_KEY(const char* data, uint64_t size)
6566
/* Internal Class Implementation */
6667
/************************************************************************/
6768

68-
static mutex mtx;
69+
static Key key;
6970
static Inlist<LoadModule> _activeLoaders;
7071

7172

@@ -211,7 +212,7 @@ static LoadModule* _findByType(const string& mimeType)
211212

212213
static LoadModule* _findFromCache(const string& path)
213214
{
214-
unique_lock<mutex> lock{mtx};
215+
ScopedLock lock(key);
215216

216217
auto loader = _activeLoaders.head;
217218

@@ -231,7 +232,7 @@ static LoadModule* _findFromCache(const char* data, uint32_t size, const string&
231232
auto type = _convert(mimeType);
232233
if (type == FileType::Unknown) return nullptr;
233234

234-
unique_lock<mutex> lock{mtx};
235+
ScopedLock lock(key);
235236
auto loader = _activeLoaders.head;
236237

237238
auto key = HASH_KEY(data, size);
@@ -279,7 +280,7 @@ bool LoaderMgr::retrieve(LoadModule* loader)
279280
if (!loader) return false;
280281
if (loader->close()) {
281282
{
282-
unique_lock<mutex> lock{mtx};
283+
ScopedLock lock(key);
283284
_activeLoaders.remove(loader);
284285
}
285286
delete(loader);
@@ -298,7 +299,7 @@ LoadModule* LoaderMgr::loader(const string& path, bool* invalid)
298299
if (loader->open(path)) {
299300
loader->hashpath = strdup(path.c_str());
300301
{
301-
unique_lock<mutex> lock{mtx};
302+
ScopedLock lock(key);
302303
_activeLoaders.back(loader);
303304
}
304305
return loader;
@@ -340,7 +341,7 @@ LoadModule* LoaderMgr::loader(const char* data, uint32_t size, const string& mim
340341
if (auto loader = _findByType(mimeType)) {
341342
if (loader->open(data, size, rpath, copy)) {
342343
loader->hashkey = HASH_KEY(data, size);
343-
unique_lock<mutex> lock{mtx};
344+
ScopedLock lock(key);
344345
_activeLoaders.back(loader);
345346
return loader;
346347
} else {
@@ -356,7 +357,7 @@ LoadModule* LoaderMgr::loader(const char* data, uint32_t size, const string& mim
356357
if (loader->open(data, size, rpath, copy)) {
357358
loader->hashkey = HASH_KEY(data, size);
358359
{
359-
unique_lock<mutex> lock{mtx};
360+
ScopedLock lock(key);
360361
_activeLoaders.back(loader);
361362
}
362363
return loader;
@@ -379,7 +380,7 @@ LoadModule* LoaderMgr::loader(const uint32_t *data, uint32_t w, uint32_t h, bool
379380
if (loader->open(data, w, h, premultiplied, copy)) {
380381
loader->hashkey = HASH_KEY((const char*)data, w * h);
381382
{
382-
unique_lock<mutex> lock{mtx};
383+
ScopedLock lock(key);
383384
_activeLoaders.back(loader);
384385
}
385386
return loader;

src/renderer/tvgRender.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
#ifndef _TVG_RENDER_H_
2424
#define _TVG_RENDER_H_
2525

26-
#include <mutex>
2726
#include "tvgCommon.h"
2827
#include "tvgArray.h"
28+
#include "tvgLock.h"
2929

3030
namespace tvg
3131
{
@@ -54,7 +54,7 @@ struct Surface
5454
uint32_t* buf32; //for explicit 32bits channels
5555
uint8_t* buf8; //for explicit 8bits grayscale
5656
};
57-
mutex mtx; //reserved for the thread safety
57+
Key key; //a reserved lock for the thread safety
5858
uint32_t stride = 0;
5959
uint32_t w = 0, h = 0;
6060
ColorSpace cs = ColorSpace::Unsupported;

0 commit comments

Comments
 (0)