1 // Copyright (c) the JPEG XL Project Authors. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5 
6 /// @addtogroup libjxl_threads
7 /// @{
8 ///
9 /// @file thread_parallel_runner_cxx.h
10 /// @brief C++ header-only helper for @ref thread_parallel_runner.h.
11 ///
12 /// There's no binary library associated with the header since this is a header
13 /// only library.
14 
15 #ifndef JXL_THREAD_PARALLEL_RUNNER_CXX_H_
16 #define JXL_THREAD_PARALLEL_RUNNER_CXX_H_
17 
18 #include <memory>
19 
20 #include "jxl/thread_parallel_runner.h"
21 
22 #if !(defined(__cplusplus) || defined(c_plusplus))
23 #error \
24     "This a C++ only header. Use jxl/jxl_thread_parallel_runner.h from C" \
25     "sources."
26 #endif
27 
28 /// Struct to call JxlThreadParallelRunnerDestroy from the
29 /// JxlThreadParallelRunnerPtr unique_ptr.
30 struct JxlThreadParallelRunnerDestroyStruct {
31   /// Calls @ref JxlThreadParallelRunnerDestroy() on the passed runner.
operatorJxlThreadParallelRunnerDestroyStruct32   void operator()(void* runner) { JxlThreadParallelRunnerDestroy(runner); }
33 };
34 
35 /// std::unique_ptr<> type that calls JxlThreadParallelRunnerDestroy() when
36 /// releasing the runner.
37 ///
38 /// Use this helper type from C++ sources to ensure the runner is destroyed and
39 /// their internal resources released.
40 typedef std::unique_ptr<void, JxlThreadParallelRunnerDestroyStruct>
41     JxlThreadParallelRunnerPtr;
42 
43 /// Creates an instance of JxlThreadParallelRunner into a
44 /// JxlThreadParallelRunnerPtr and initializes it.
45 ///
46 /// This function returns a unique_ptr that will call
47 /// JxlThreadParallelRunnerDestroy() when releasing the pointer. See @ref
48 /// JxlThreadParallelRunnerCreate for details on the instance creation.
49 ///
50 /// @param memory_manager custom allocator function. It may be NULL. The memory
51 ///        manager will be copied internally.
52 /// @param num_worker_threads the number of worker threads to create.
53 /// @return a @c NULL JxlThreadParallelRunnerPtr if the instance can not be
54 /// allocated or initialized
55 /// @return initialized JxlThreadParallelRunnerPtr instance otherwise.
JxlThreadParallelRunnerMake(const JxlMemoryManager * memory_manager,size_t num_worker_threads)56 static inline JxlThreadParallelRunnerPtr JxlThreadParallelRunnerMake(
57     const JxlMemoryManager* memory_manager, size_t num_worker_threads) {
58   return JxlThreadParallelRunnerPtr(
59       JxlThreadParallelRunnerCreate(memory_manager, num_worker_threads));
60 }
61 
62 #endif  // JXL_THREAD_PARALLEL_RUNNER_CXX_H_
63 
64 /// @}
65