Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

693 lines
21KB

  1. /*
  2. * Carla Bridge Plugin
  3. * Copyright (C) 2012-2022 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef BUILD_BRIDGE
  18. # error This file should not be compiled if not building bridge
  19. #endif
  20. #include "CarlaEngine.hpp"
  21. #include "CarlaHost.h"
  22. #include "CarlaUtils.h"
  23. #include "CarlaBackendUtils.hpp"
  24. #include "CarlaJuceUtils.hpp"
  25. #include "CarlaMainLoop.hpp"
  26. #include "CarlaMIDI.h"
  27. #ifdef CARLA_OS_MAC
  28. # include "CarlaMacUtils.hpp"
  29. #endif
  30. #ifdef CARLA_OS_UNIX
  31. # include <signal.h>
  32. #endif
  33. #ifdef CARLA_OS_LINUX
  34. # include <sched.h>
  35. # define SCHED_RESET_ON_FORK 0x40000000
  36. #endif
  37. #ifdef CARLA_OS_WIN
  38. # include <pthread.h>
  39. # include <objbase.h>
  40. #endif
  41. #ifdef HAVE_X11
  42. # include <X11/Xlib.h>
  43. #endif
  44. #ifdef USING_JUCE
  45. # include "carla_juce/carla_juce.h"
  46. #endif
  47. #include "water/files/File.h"
  48. #include "water/misc/Time.h"
  49. // must be last
  50. #include "jackbridge/JackBridge.hpp"
  51. using CARLA_BACKEND_NAMESPACE::CarlaEngine;
  52. using CARLA_BACKEND_NAMESPACE::EngineCallbackOpcode;
  53. using CARLA_BACKEND_NAMESPACE::EngineCallbackOpcode2Str;
  54. using CARLA_BACKEND_NAMESPACE::runMainLoopOnce;
  55. using water::CharPointer_UTF8;
  56. using water::File;
  57. using water::String;
  58. // -------------------------------------------------------------------------
  59. static bool gIsInitiated = false;
  60. static volatile bool gCloseBridge = false;
  61. static volatile bool gCloseSignal = false;
  62. static volatile bool gSaveNow = false;
  63. #if defined(CARLA_OS_UNIX)
  64. static void closeSignalHandler(int) noexcept
  65. {
  66. gCloseSignal = true;
  67. }
  68. static void saveSignalHandler(int) noexcept
  69. {
  70. gSaveNow = true;
  71. }
  72. #elif defined(CARLA_OS_WIN)
  73. static LONG WINAPI winExceptionFilter(_EXCEPTION_POINTERS*)
  74. {
  75. return EXCEPTION_EXECUTE_HANDLER;
  76. }
  77. static BOOL WINAPI winSignalHandler(DWORD dwCtrlType) noexcept
  78. {
  79. if (dwCtrlType == CTRL_C_EVENT)
  80. {
  81. gCloseSignal = true;
  82. return TRUE;
  83. }
  84. return FALSE;
  85. }
  86. #endif
  87. static void initSignalHandler()
  88. {
  89. #if defined(CARLA_OS_UNIX)
  90. struct sigaction sig;
  91. carla_zeroStruct(sig);
  92. sig.sa_handler = closeSignalHandler;
  93. sig.sa_flags = SA_RESTART;
  94. sigemptyset(&sig.sa_mask);
  95. sigaction(SIGTERM, &sig, nullptr);
  96. sigaction(SIGINT, &sig, nullptr);
  97. sig.sa_handler = saveSignalHandler;
  98. sig.sa_flags = SA_RESTART;
  99. sigemptyset(&sig.sa_mask);
  100. sigaction(SIGUSR1, &sig, nullptr);
  101. #elif defined(CARLA_OS_WIN)
  102. SetConsoleCtrlHandler(winSignalHandler, TRUE);
  103. SetErrorMode(SEM_NOGPFAULTERRORBOX);
  104. SetUnhandledExceptionFilter(winExceptionFilter);
  105. #endif
  106. }
  107. // -------------------------------------------------------------------------
  108. static String gProjectFilename;
  109. static CarlaHostHandle gHostHandle;
  110. static void gIdle()
  111. {
  112. carla_engine_idle(gHostHandle);
  113. if (gSaveNow)
  114. {
  115. gSaveNow = false;
  116. if (gProjectFilename.isNotEmpty())
  117. {
  118. if (! carla_save_plugin_state(gHostHandle, 0, gProjectFilename.toRawUTF8()))
  119. carla_stderr("Plugin preset save failed, error was:\n%s", carla_get_last_error(gHostHandle));
  120. }
  121. }
  122. }
  123. // -------------------------------------------------------------------------
  124. class CarlaBridgePlugin
  125. {
  126. public:
  127. CarlaBridgePlugin(const bool useBridge, const char* const clientName, const char* const audioPoolBaseName,
  128. const char* const rtClientBaseName, const char* const nonRtClientBaseName, const char* const nonRtServerBaseName)
  129. : fEngine(nullptr),
  130. #ifdef USING_JUCE
  131. fJuceInitialiser(),
  132. #endif
  133. fUsingBridge(false),
  134. fUsingExec(false)
  135. {
  136. CARLA_ASSERT(clientName != nullptr && clientName[0] != '\0');
  137. carla_debug("CarlaBridgePlugin::CarlaBridgePlugin(%s, \"%s\", %s, %s, %s, %s)",
  138. bool2str(useBridge), clientName, audioPoolBaseName, rtClientBaseName, nonRtClientBaseName, nonRtServerBaseName);
  139. carla_set_engine_callback(gHostHandle, callback, this);
  140. if (useBridge)
  141. {
  142. carla_engine_init_bridge(gHostHandle,
  143. audioPoolBaseName,
  144. rtClientBaseName,
  145. nonRtClientBaseName,
  146. nonRtServerBaseName,
  147. clientName);
  148. }
  149. else if (std::getenv("CARLA_BRIDGE_DUMMY") != nullptr)
  150. {
  151. carla_engine_init(gHostHandle, "Dummy", clientName);
  152. }
  153. else
  154. {
  155. carla_engine_init(gHostHandle, "JACK", clientName);
  156. }
  157. fEngine = carla_get_engine_from_handle(gHostHandle);
  158. }
  159. ~CarlaBridgePlugin()
  160. {
  161. carla_debug("CarlaBridgePlugin::~CarlaBridgePlugin()");
  162. if (fEngine != nullptr && ! fUsingExec)
  163. carla_engine_close(gHostHandle);
  164. }
  165. bool isOk() const noexcept
  166. {
  167. return (fEngine != nullptr);
  168. }
  169. // ---------------------------------------------------------------------
  170. void exec(const bool useBridge)
  171. {
  172. fUsingBridge = useBridge;
  173. fUsingExec = true;
  174. const bool testing = std::getenv("CARLA_BRIDGE_TESTING") != nullptr;
  175. if (! useBridge && ! testing)
  176. {
  177. const CarlaPluginInfo* const pInfo = carla_get_plugin_info(gHostHandle, 0);
  178. CARLA_SAFE_ASSERT_RETURN(pInfo != nullptr,);
  179. gProjectFilename = CharPointer_UTF8(pInfo->name);
  180. gProjectFilename += ".carxs";
  181. if (! File::isAbsolutePath(gProjectFilename))
  182. gProjectFilename = File::getCurrentWorkingDirectory().getChildFile(gProjectFilename).getFullPathName();
  183. if (File(gProjectFilename).existsAsFile())
  184. {
  185. if (carla_load_plugin_state(gHostHandle, 0, gProjectFilename.toRawUTF8()))
  186. carla_stdout("Plugin state loaded successfully");
  187. else
  188. carla_stderr("Plugin state load failed, error was:\n%s", carla_get_last_error(gHostHandle));
  189. }
  190. else
  191. {
  192. carla_stdout("Previous plugin state in '%s' is non-existent, will start from default state",
  193. gProjectFilename.toRawUTF8());
  194. }
  195. }
  196. gIsInitiated = true;
  197. #if defined(USING_JUCE) && (defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN))
  198. CarlaJUCE::setupAndUseMainApplication(gIdle, &gCloseSignal);
  199. #else
  200. int64_t timeToEnd = 0;
  201. if (testing)
  202. {
  203. timeToEnd = water::Time::currentTimeMillis() + 5 * 1000;
  204. fEngine->transportPlay();
  205. }
  206. for (; runMainLoopOnce() && ! gCloseBridge;)
  207. {
  208. gIdle();
  209. # if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  210. // MacOS and Win32 have event-loops to run, so minimize sleep time
  211. carla_msleep(1);
  212. # else
  213. carla_msleep(5);
  214. # endif
  215. if (testing && timeToEnd - water::Time::currentTimeMillis() < 0)
  216. break;
  217. if (gCloseSignal && ! fUsingBridge)
  218. break;
  219. }
  220. #endif
  221. carla_engine_close(gHostHandle);
  222. }
  223. // ---------------------------------------------------------------------
  224. protected:
  225. void handleCallback(const EngineCallbackOpcode action,
  226. const int value1,
  227. const int, const int, const float, const char* const)
  228. {
  229. CARLA_BACKEND_USE_NAMESPACE;
  230. switch (action)
  231. {
  232. case ENGINE_CALLBACK_ENGINE_STOPPED:
  233. case ENGINE_CALLBACK_PLUGIN_REMOVED:
  234. case ENGINE_CALLBACK_QUIT:
  235. gCloseBridge = gCloseSignal = true;
  236. break;
  237. case ENGINE_CALLBACK_UI_STATE_CHANGED:
  238. if (gIsInitiated && value1 != 1 && ! fUsingBridge)
  239. gCloseBridge = gCloseSignal = true;
  240. break;
  241. default:
  242. break;
  243. }
  244. }
  245. private:
  246. CarlaEngine* fEngine;
  247. #ifdef USING_JUCE
  248. const CarlaJUCE::ScopedJuceInitialiser_GUI fJuceInitialiser;
  249. #endif
  250. bool fUsingBridge;
  251. bool fUsingExec;
  252. static void callback(void* ptr, EngineCallbackOpcode action, unsigned int pluginId,
  253. int value1, int value2, int value3,
  254. float valuef, const char* valueStr)
  255. {
  256. carla_debug("CarlaBridgePlugin::callback(%p, %i:%s, %i, %i, %i, %i, %f, \"%s\")",
  257. ptr, action, EngineCallbackOpcode2Str(action),
  258. pluginId, value1, value2, value3, static_cast<double>(valuef), valueStr);
  259. // ptr must not be null
  260. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr,);
  261. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  262. // pluginId must be 0 (first), except for patchbay things
  263. if (action < CARLA_BACKEND_NAMESPACE::ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED ||
  264. action > CARLA_BACKEND_NAMESPACE::ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED)
  265. #endif
  266. {
  267. CARLA_SAFE_ASSERT_UINT_RETURN(pluginId == 0, pluginId,);
  268. }
  269. return ((CarlaBridgePlugin*)ptr)->handleCallback(action, value1, value2, value3, valuef, valueStr);
  270. }
  271. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaBridgePlugin)
  272. };
  273. // -------------------------------------------------------------------------
  274. int main(int argc, char* argv[])
  275. {
  276. // ---------------------------------------------------------------------
  277. // Check argument count
  278. if (argc != 4 && argc != 5)
  279. {
  280. carla_stdout("usage: %s <type> <filename> <label> [uniqueId]", argv[0]);
  281. return 1;
  282. }
  283. #if defined(CARLA_OS_WIN) && defined(BUILDING_CARLA_FOR_WINE)
  284. // ---------------------------------------------------------------------
  285. // Test if bridge is working
  286. if (! jackbridge_is_ok())
  287. {
  288. carla_stderr("A JACK or Wine library is missing, cannot continue");
  289. return 1;
  290. }
  291. #endif
  292. // ---------------------------------------------------------------------
  293. // Get args
  294. const char* const stype = argv[1];
  295. const char* filename = argv[2];
  296. const char* label = argv[3];
  297. const int64_t uniqueId = (argc == 5) ? static_cast<int64_t>(std::atoll(argv[4])) : 0;
  298. if (filename[0] == '\0' || std::strcmp(filename, "(none)") == 0)
  299. filename = nullptr;
  300. if (label[0] == '\0' || std::strcmp(label, "(none)") == 0)
  301. label = nullptr;
  302. // ---------------------------------------------------------------------
  303. // Check binary type
  304. CARLA_BACKEND_NAMESPACE::BinaryType btype = CARLA_BACKEND_NAMESPACE::BINARY_NATIVE;
  305. if (const char* const binaryTypeStr = std::getenv("CARLA_BRIDGE_PLUGIN_BINARY_TYPE"))
  306. btype = CARLA_BACKEND_NAMESPACE::getBinaryTypeFromString(binaryTypeStr);
  307. if (btype == CARLA_BACKEND_NAMESPACE::BINARY_NONE)
  308. {
  309. carla_stderr("Invalid binary type '%i'", btype);
  310. return 1;
  311. }
  312. // ---------------------------------------------------------------------
  313. // Check plugin type
  314. CARLA_BACKEND_NAMESPACE::PluginType itype = CARLA_BACKEND_NAMESPACE::getPluginTypeFromString(stype);
  315. if (itype == CARLA_BACKEND_NAMESPACE::PLUGIN_NONE)
  316. {
  317. carla_stderr("Invalid plugin type '%s'", stype);
  318. return 1;
  319. }
  320. // ---------------------------------------------------------------------
  321. // Set file
  322. const File file(filename != nullptr ? filename : "");
  323. // ---------------------------------------------------------------------
  324. // Set name
  325. const char* name(std::getenv("CARLA_CLIENT_NAME"));
  326. if (name != nullptr && (name[0] == '\0' || std::strcmp(name, "(none)") == 0))
  327. name = nullptr;
  328. // ---------------------------------------------------------------------
  329. // Setup options
  330. const char* const shmIds(std::getenv("ENGINE_BRIDGE_SHM_IDS"));
  331. const bool useBridge = (shmIds != nullptr);
  332. // ---------------------------------------------------------------------
  333. // Setup bridge ids
  334. char audioPoolBaseName[6+1];
  335. char rtClientBaseName[6+1];
  336. char nonRtClientBaseName[6+1];
  337. char nonRtServerBaseName[6+1];
  338. if (useBridge)
  339. {
  340. CARLA_SAFE_ASSERT_RETURN(std::strlen(shmIds) == 6*4, 1);
  341. std::strncpy(audioPoolBaseName, shmIds+6*0, 6);
  342. std::strncpy(rtClientBaseName, shmIds+6*1, 6);
  343. std::strncpy(nonRtClientBaseName, shmIds+6*2, 6);
  344. std::strncpy(nonRtServerBaseName, shmIds+6*3, 6);
  345. audioPoolBaseName[6] = '\0';
  346. rtClientBaseName[6] = '\0';
  347. nonRtClientBaseName[6] = '\0';
  348. nonRtServerBaseName[6] = '\0';
  349. jackbridge_parent_deathsig(false);
  350. }
  351. else
  352. {
  353. audioPoolBaseName[0] = '\0';
  354. rtClientBaseName[0] = '\0';
  355. nonRtClientBaseName[0] = '\0';
  356. nonRtServerBaseName[0] = '\0';
  357. jackbridge_init();
  358. }
  359. // ---------------------------------------------------------------------
  360. // Set client name
  361. CarlaString clientName;
  362. if (name != nullptr)
  363. {
  364. clientName = name;
  365. }
  366. else if (itype == CARLA_BACKEND_NAMESPACE::PLUGIN_LV2)
  367. {
  368. // LV2 requires URI
  369. CARLA_SAFE_ASSERT_RETURN(label != nullptr && label[0] != '\0', 1);
  370. // LV2 URI is not usable as client name, create a usable name from URI
  371. CarlaString label2(label);
  372. // truncate until last valid char
  373. for (std::size_t i=label2.length()-1; i != 0; --i)
  374. {
  375. if (! std::isalnum(label2[i]))
  376. continue;
  377. label2.truncate(i+1);
  378. break;
  379. }
  380. // get last used separator
  381. bool found;
  382. std::size_t septmp, sep = 0;
  383. septmp = label2.rfind('#', &found)+1;
  384. if (found && septmp > sep)
  385. sep = septmp;
  386. septmp = label2.rfind('/', &found)+1;
  387. if (found && septmp > sep)
  388. sep = septmp;
  389. septmp = label2.rfind('=', &found)+1;
  390. if (found && septmp > sep)
  391. sep = septmp;
  392. septmp = label2.rfind(':', &found)+1;
  393. if (found && septmp > sep)
  394. sep = septmp;
  395. // make name starting from the separator and first valid char
  396. const char* name2 = label2.buffer() + sep;
  397. for (; *name2 != '\0' && ! std::isalnum(*name2); ++name2) {}
  398. if (*name2 != '\0')
  399. clientName = name2;
  400. }
  401. else if (label != nullptr)
  402. {
  403. clientName = label;
  404. }
  405. else
  406. {
  407. clientName = file.getFileNameWithoutExtension().toRawUTF8();
  408. }
  409. // if we still have no client name by now, use a dummy one
  410. if (clientName.isEmpty())
  411. clientName = "carla-plugin";
  412. // just to be safe
  413. clientName.toBasic();
  414. // ---------------------------------------------------------------------
  415. // Set extraStuff
  416. const void* extraStuff = nullptr;
  417. if (itype == CARLA_BACKEND_NAMESPACE::PLUGIN_SF2)
  418. {
  419. if (label == nullptr)
  420. label = clientName;
  421. if (std::strstr(label, " (16 outs)") != nullptr)
  422. extraStuff = "true";
  423. }
  424. // ---------------------------------------------------------------------
  425. // Initialize OS features
  426. const bool dummy = std::getenv("CARLA_BRIDGE_DUMMY") != nullptr;
  427. const bool testing = std::getenv("CARLA_BRIDGE_TESTING") != nullptr;
  428. #ifdef CARLA_OS_MAC
  429. CARLA_BACKEND_NAMESPACE::initStandaloneApplication();
  430. #endif
  431. #ifdef CARLA_OS_WIN
  432. OleInitialize(nullptr);
  433. CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
  434. # ifndef __WINPTHREADS_VERSION
  435. // (non-portable) initialization of statically linked pthread library
  436. pthread_win32_process_attach_np();
  437. pthread_win32_thread_attach_np();
  438. # endif
  439. #endif
  440. #ifdef HAVE_X11
  441. if (std::getenv("DISPLAY") != nullptr)
  442. XInitThreads();
  443. #endif
  444. // ---------------------------------------------------------------------
  445. // Set ourselves with high priority
  446. if (!dummy && !testing)
  447. {
  448. #ifdef CARLA_OS_LINUX
  449. // reset scheduler to normal mode
  450. struct sched_param sparam;
  451. carla_zeroStruct(sparam);
  452. sched_setscheduler(0, SCHED_OTHER|SCHED_RESET_ON_FORK, &sparam);
  453. // try niceness first, if it fails, try SCHED_RR
  454. if (nice(-5) < 0)
  455. {
  456. sparam.sched_priority = (sched_get_priority_max(SCHED_RR) + sched_get_priority_min(SCHED_RR*7)) / 8;
  457. if (sparam.sched_priority > 0)
  458. {
  459. if (sched_setscheduler(0, SCHED_RR|SCHED_RESET_ON_FORK, &sparam) < 0)
  460. {
  461. CarlaString error(std::strerror(errno));
  462. carla_stderr("Failed to set high priority, error %i: %s", errno, error.buffer());
  463. }
  464. }
  465. }
  466. #endif
  467. #ifdef CARLA_OS_WIN
  468. if (! SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS))
  469. carla_stderr("Failed to set high priority.");
  470. #endif
  471. }
  472. // ---------------------------------------------------------------------
  473. // Listen for ctrl+c or sigint/sigterm events
  474. initSignalHandler();
  475. // ---------------------------------------------------------------------
  476. // Init plugin bridge
  477. int ret;
  478. {
  479. gHostHandle = carla_standalone_host_init();
  480. CarlaBridgePlugin bridge(useBridge, clientName,
  481. audioPoolBaseName, rtClientBaseName, nonRtClientBaseName, nonRtServerBaseName);
  482. if (! bridge.isOk())
  483. {
  484. carla_stderr("Failed to init engine, error was:\n%s", carla_get_last_error(gHostHandle));
  485. return 1;
  486. }
  487. if (! useBridge && ! testing)
  488. {
  489. #ifdef HAVE_X11
  490. if (std::getenv("DISPLAY") != nullptr)
  491. #endif
  492. carla_set_engine_option(gHostHandle,
  493. CARLA_BACKEND_NAMESPACE::ENGINE_OPTION_FRONTEND_UI_SCALE,
  494. static_cast<int>(carla_get_desktop_scale_factor()*1000+0.5),
  495. nullptr);
  496. }
  497. // -----------------------------------------------------------------
  498. // Init plugin
  499. if (carla_add_plugin(gHostHandle,
  500. btype, itype,
  501. file.getFullPathName().toRawUTF8(), name, label, uniqueId, extraStuff,
  502. CARLA_BACKEND_NAMESPACE::PLUGIN_OPTIONS_NULL))
  503. {
  504. ret = 0;
  505. if (! useBridge)
  506. {
  507. carla_set_active(gHostHandle, 0, true);
  508. carla_set_engine_option(gHostHandle, CARLA_BACKEND_NAMESPACE::ENGINE_OPTION_PLUGINS_ARE_STANDALONE, 1, nullptr);
  509. if (const CarlaPluginInfo* const pluginInfo = carla_get_plugin_info(gHostHandle, 0))
  510. {
  511. if (itype == CARLA_BACKEND_NAMESPACE::PLUGIN_INTERNAL && (std::strcmp(label, "audiofile") == 0 || std::strcmp(label, "midifile") == 0))
  512. {
  513. if (file.exists())
  514. carla_set_custom_data(gHostHandle, 0,
  515. CARLA_BACKEND_NAMESPACE::CUSTOM_DATA_TYPE_STRING,
  516. "file", file.getFullPathName().toRawUTF8());
  517. }
  518. else if (pluginInfo->hints & CARLA_BACKEND_NAMESPACE::PLUGIN_HAS_CUSTOM_UI)
  519. {
  520. #ifdef HAVE_X11
  521. if (std::getenv("DISPLAY") != nullptr)
  522. #endif
  523. if (! testing)
  524. carla_show_custom_ui(gHostHandle, 0, true);
  525. }
  526. // on standalone usage, enable everything that makes sense
  527. const uint optsAvailable = pluginInfo->optionsAvailable;
  528. if (optsAvailable & CARLA_BACKEND_NAMESPACE::PLUGIN_OPTION_FIXED_BUFFERS)
  529. carla_set_option(gHostHandle, 0, CARLA_BACKEND_NAMESPACE::PLUGIN_OPTION_FIXED_BUFFERS, true);
  530. }
  531. }
  532. bridge.exec(useBridge);
  533. }
  534. else
  535. {
  536. ret = 1;
  537. const char* const lastError(carla_get_last_error(gHostHandle));
  538. carla_stderr("Plugin failed to load, error was:\n%s", lastError);
  539. if (useBridge)
  540. {
  541. // do a single idle so that we can send error message to server
  542. gIdle();
  543. #ifdef CARLA_OS_UNIX
  544. // kill ourselves now if we can't load plugin in bridge mode
  545. ::kill(::getpid(), SIGKILL);
  546. #endif
  547. }
  548. }
  549. }
  550. #ifdef CARLA_OS_WIN
  551. #ifndef __WINPTHREADS_VERSION
  552. pthread_win32_thread_detach_np();
  553. pthread_win32_process_detach_np();
  554. #endif
  555. CoUninitialize();
  556. OleUninitialize();
  557. #endif
  558. return ret;
  559. }