Blender V4.3
BLI_subprocess.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
18#if defined(_WIN32) || defined(__linux__)
19# define BLI_SUBPROCESS_SUPPORT 1
20#else
21# define BLI_SUBPROCESS_SUPPORT 0
22#endif
23
24#if BLI_SUBPROCESS_SUPPORT
25
26# include "BLI_span.hh"
27# include "BLI_string_ref.hh"
28# include "BLI_sys_types.h"
29# include "BLI_utility_mixins.hh"
30# include <string>
31
32# ifdef _WIN32
33typedef void *HANDLE;
34# else
35# include <semaphore.h>
36# endif
37
38namespace blender {
39
46class BlenderSubprocess : NonCopyable {
47 private:
48# ifdef _WIN32
49 HANDLE handle_ = nullptr;
50# else
51 pid_t pid_ = 0;
52# endif
53 public:
54 ~BlenderSubprocess();
55
63 bool create(Span<StringRefNull> args);
69 bool is_running();
70};
71
78class SharedMemory : NonCopyable {
79 private:
80 std::string name_;
81# ifdef _WIN32
82 HANDLE handle_;
83# else
84 int handle_;
85# endif
86 void *data_;
87 size_t data_size_;
88 bool is_owner_;
89
90 public:
100 SharedMemory(std::string name, size_t size, bool is_owner);
101 ~SharedMemory();
102
107 void *get_data()
108 {
109 return data_;
110 }
111
112 size_t get_size()
113 {
114 return data_size_;
115 }
116};
117
121class SharedSemaphore : NonCopyable {
122 private:
123 std::string name_;
124# if defined(_WIN32)
125 HANDLE handle_;
126# else
127 sem_t *handle_;
128# endif
129 bool is_owner_;
130
131 public:
141 SharedSemaphore(std::string name, bool is_owner);
142 ~SharedSemaphore();
143
144 /* Increment the semaphore value. */
145 void increment();
146 /* Decrement the semaphore value (Blocks until the semaphore value is greater than 0). */
147 void decrement();
152 bool try_decrement(int wait_ms = 0);
153};
154
155} // namespace blender
156
157#endif