Merge branch 'v0.22.x'

This commit is contained in:
Max Kellermann
2021-03-05 16:05:56 +01:00
13 changed files with 142 additions and 74 deletions

View File

@@ -155,7 +155,7 @@ public:
void Finish() noexcept { return SetStatus(Status::FINISH); }
void Play() noexcept { return SetStatus(Status::PLAY); }
void Pause() noexcept { return SetStatus(Status::PAUSE); }
void WaitDataPoped() noexcept { data_poped.Wait(INFINITE); }
void WaitDataPoped() noexcept { data_poped.Wait(); }
void CheckException() {
if (error.occur.load()) {
auto err = std::exchange(error.ptr, nullptr);
@@ -269,7 +269,7 @@ void WasapiOutputThread::Work() noexcept {
COM com{true};
while (true) {
try {
event.Wait(INFINITE);
event.Wait();
Status current_state = status.load();
if (current_state == Status::FINISH) {
@@ -322,7 +322,7 @@ void WasapiOutputThread::Work() noexcept {
} catch (...) {
error.ptr = std::current_exception();
error.occur.store(true);
error.thrown.Wait(INFINITE);
error.thrown.Wait();
}
}
}

View File

@@ -159,6 +159,7 @@ if is_windows
wasapi_dep = [
c_compiler.find_library('ksuser', required: true),
c_compiler.find_library('ole32', required: true),
win32_dep,
]
else
wasapi_dep = dependency('', required: false)

View File

@@ -22,7 +22,7 @@
#include "CriticalSection.hxx"
#include "WindowsCond.hxx"
#include <atomic>
#include <memory>
#include <variant>

View File

@@ -16,27 +16,20 @@
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "ComWorker.hxx"
#include "Log.hxx"
#include "thread/Name.hxx"
#include "util/Domain.hxx"
#include "win32/Com.hxx"
namespace {
static constexpr Domain com_worker_domain("com_worker");
}
#include "ComWorker.hxx"
#include "Com.hxx"
#include "thread/Name.hxx"
Mutex COMWorker::mutex;
unsigned int COMWorker::reference_count = 0;
std::optional<COMWorker::COMWorkerThread> COMWorker::thread;
void COMWorker::COMWorkerThread::Work() noexcept {
FormatDebug(com_worker_domain, "Working thread started");
SetThreadName("COM Worker");
COM com{true};
while (true) {
if (!running_flag.test_and_set()) {
FormatDebug(com_worker_domain, "Working thread ended");
return;
}
while (!spsc_buffer.empty()) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020 The Music Player Daemon Project
* Copyright 2020-2021 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
@@ -20,16 +20,15 @@
#ifndef MPD_WIN32_COM_WORKER_HXX
#define MPD_WIN32_COM_WORKER_HXX
#include <boost/lockfree/spsc_queue.hpp>
#include <condition_variable>
#include <mutex>
#include <optional>
#include "WinEvent.hxx"
#include "thread/Future.hxx"
#include "thread/Mutex.hxx"
#include "thread/Thread.hxx"
#include "win32/WinEvent.hxx"
#include <objbase.h>
#include <boost/lockfree/spsc_queue.hpp>
#include <mutex>
#include <optional>
#include <windows.h>
// Worker thread for all COM operation

57
src/win32/HResult.cxx Normal file
View File

@@ -0,0 +1,57 @@
/*
* Copyright 2020-2021 The Music Player Daemon Project
* http://www.musicpd.org
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "HResult.hxx"
#include <cassert>
#include <cstdarg>
#include <cstdio>
#include <memory>
std::string
HResultCategory::message(int Errcode) const
{
const auto msg = HRESULTToString(Errcode);
if (!msg.empty())
return std::string(msg);
char buffer[11]; // "0x12345678\0"
int size = snprintf(buffer, sizeof(buffer), "0x%1x", Errcode);
assert(2 <= size && size <= 10);
return std::string(buffer, size);
}
std::system_error
FormatHResultError(HRESULT result, const char *fmt, ...) noexcept
{
std::va_list args1, args2;
va_start(args1, fmt);
va_copy(args2, args1);
const int size = vsnprintf(nullptr, 0, fmt, args1);
va_end(args1);
assert(size >= 0);
auto buffer = std::make_unique<char[]>(size + 1);
vsprintf(buffer.get(), fmt, args2);
va_end(args2);
return std::system_error(std::error_code(result, hresult_category()),
std::string(buffer.get(), size));
}

View File

@@ -22,13 +22,8 @@
#include "util/Compiler.h"
#include <cassert>
#include <cstdarg>
#include <cstdio>
#include <stdexcept>
#include <string_view>
#include <system_error>
#include <vector>
#include <audiopolicy.h>
@@ -69,16 +64,7 @@ static inline const std::error_category &hresult_category() noexcept;
class HResultCategory : public std::error_category {
public:
const char *name() const noexcept override { return "HRESULT"; }
std::string message(int Errcode) const override {
const auto msg = HRESULTToString(Errcode);
if (!msg.empty()) {
return std::string(msg);
}
char buffer[11]; // "0x12345678\0"
int size = snprintf(buffer, sizeof(buffer), "0x%1x", Errcode);
assert(2 <= size && size <= 10);
return std::string(buffer, size);
}
std::string message(int Errcode) const override;
std::error_condition default_error_condition(int code) const noexcept override {
return std::error_condition(code, hresult_category());
}
@@ -88,22 +74,7 @@ static inline const std::error_category &hresult_category() noexcept {
return hresult_category_instance;
}
gcc_printf(2, 3) static inline std::runtime_error
FormatHResultError(HRESULT result, const char *fmt, ...) noexcept {
std::va_list args1, args2;
va_start(args1, fmt);
va_copy(args2, args1);
const int size = vsnprintf(nullptr, 0, fmt, args1);
va_end(args1);
assert(size >= 0);
auto buffer = std::make_unique<char[]>(size + 1);
vsprintf(buffer.get(), fmt, args2);
va_end(args2);
return std::system_error(std::error_code(result, hresult_category()),
std::string(buffer.get(), size));
}
gcc_printf(2, 3) std::system_error
FormatHResultError(HRESULT result, const char *fmt, ...) noexcept;
#endif

28
src/win32/WinEvent.cxx Normal file
View File

@@ -0,0 +1,28 @@
/*
* Copyright 2020-2021 The Music Player Daemon Project
* http://www.musicpd.org
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "WinEvent.hxx"
#include "system/Error.hxx"
WinEvent::WinEvent()
:event(CreateEventW(nullptr, false, false, nullptr))
{
if (!event)
throw FormatLastError("Error creating events");
}

View File

@@ -20,7 +20,6 @@
#ifndef MPD_WIN32_WINEVENT_HXX
#define MPD_WIN32_WINEVENT_HXX
#include "system/Error.hxx"
#include <windows.h>
// RAII for Windows unnamed event object
@@ -28,11 +27,11 @@
class WinEvent {
public:
WinEvent() : event(CreateEventW(nullptr, false, false, nullptr)) {
if (!event) {
throw FormatLastError("Error creating events");
}
}
/**
* Throws on error.
*/
WinEvent();
~WinEvent() noexcept { CloseHandle(event); }
WinEvent(WinEvent &&) = delete;
WinEvent(const WinEvent &) = delete;
@@ -41,7 +40,7 @@ public:
HANDLE handle() noexcept { return event; }
DWORD Wait(DWORD milliseconds) noexcept {
DWORD Wait(DWORD milliseconds=INFINITE) noexcept {
return WaitForSingleObject(event, milliseconds);
}

19
src/win32/meson.build Normal file
View File

@@ -0,0 +1,19 @@
if not is_windows
win32_dep = dependency('', required: false)
subdir_done()
endif
win32 = static_library(
'win32',
'ComWorker.cxx',
'HResult.cxx',
'WinEvent.cxx',
include_directories: inc,
)
win32_dep = declare_dependency(
link_with: win32,
dependencies: [
thread_dep,
],
)