2019-05-29 04:18:11 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
#include <RB_INC_MAGICK_API_H
|
|
|
|
|
|
|
|
namespace ircd::magick
|
|
|
|
{
|
2019-05-30 14:14:34 +02:00
|
|
|
struct display;
|
2019-05-30 11:44:12 +02:00
|
|
|
struct transform;
|
|
|
|
|
2019-05-29 07:08:46 +02:00
|
|
|
static void handle_exception(const ::ExceptionType, const char *, const char *);
|
|
|
|
static void handle_fatal(const ::ExceptionType, const char *, const char *) noexcept;
|
|
|
|
static void handle_error(const ::ExceptionType, const char *, const char *) noexcept;
|
|
|
|
static void handle_warning(const ::ExceptionType, const char *, const char *) noexcept;
|
|
|
|
static void handle_log(const ::ExceptionType, const char *) noexcept;
|
2019-05-30 07:27:55 +02:00
|
|
|
static void *handle_realloc(void *, size_t) noexcept;
|
|
|
|
static void *handle_malloc(size_t) noexcept;
|
|
|
|
static void handle_free(void *) noexcept;
|
2019-05-30 03:08:19 +02:00
|
|
|
static uint handle_progress(const char *, const int64_t, const uint64_t, ExceptionInfo *) noexcept;
|
2019-05-29 07:08:46 +02:00
|
|
|
|
|
|
|
template<class R, class F, class... A> static R call(F&&, A&&...);
|
|
|
|
template<class R, class F, class... A> static R callex(F&&, A&&...);
|
|
|
|
template<class F, class... A> static void callpf(F&&, A&&...);
|
|
|
|
|
2019-05-29 04:18:11 +02:00
|
|
|
static void init();
|
|
|
|
static void fini();
|
2019-05-29 07:08:46 +02:00
|
|
|
|
2019-05-30 07:14:50 +02:00
|
|
|
extern conf::item<uint64_t> yield_threshold;
|
|
|
|
extern conf::item<uint64_t> yield_interval;
|
2019-05-29 07:08:46 +02:00
|
|
|
extern log::log log;
|
2019-05-29 04:18:11 +02:00
|
|
|
}
|
|
|
|
|
2019-05-30 14:14:34 +02:00
|
|
|
struct ircd::magick::display
|
|
|
|
{
|
|
|
|
display(const ::ImageInfo &, ::Image &);
|
|
|
|
display(const const_buffer &);
|
|
|
|
};
|
|
|
|
|
2019-05-30 11:44:12 +02:00
|
|
|
struct ircd::magick::transform
|
|
|
|
{
|
|
|
|
using input = std::tuple<const ::ImageInfo &, const ::Image *>;
|
|
|
|
using output = std::function<void (const const_buffer &)>;
|
|
|
|
using transformer = std::function<::Image *(const input &)>;
|
|
|
|
|
|
|
|
transform(const const_buffer &, const output &, const transformer &);
|
|
|
|
};
|
|
|
|
|
2019-05-29 04:18:11 +02:00
|
|
|
ircd::mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"GraphicsMagick Library support for media manipulation",
|
|
|
|
ircd::magick::init,
|
|
|
|
ircd::magick::fini
|
|
|
|
};
|
|
|
|
|
2019-05-29 07:08:46 +02:00
|
|
|
decltype(ircd::magick::log)
|
|
|
|
ircd::magick::log
|
|
|
|
{
|
|
|
|
"magick"
|
|
|
|
};
|
|
|
|
|
2019-05-30 07:14:50 +02:00
|
|
|
decltype(ircd::magick::yield_threshold)
|
|
|
|
ircd::magick::yield_threshold
|
|
|
|
{
|
|
|
|
{ "name", "ircd.magick.yield.threshold" },
|
|
|
|
{ "default", 1000L },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::magick::yield_interval)
|
|
|
|
ircd::magick::yield_interval
|
|
|
|
{
|
|
|
|
{ "name", "ircd.magick.yield.interval" },
|
|
|
|
{ "default", 768L },
|
|
|
|
};
|
|
|
|
|
2019-06-01 01:06:55 +02:00
|
|
|
decltype(ircd::magick::version_api)
|
|
|
|
ircd::magick::version_api
|
|
|
|
{
|
|
|
|
"magick", info::versions::API, MagickLibVersion, {0}, MagickLibVersionText
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::magick::version_abi)
|
|
|
|
ircd::magick::version_abi
|
|
|
|
{
|
|
|
|
"magick", info::versions::ABI, 0, {0}, []
|
|
|
|
(auto &version, const auto &buf)
|
|
|
|
{
|
|
|
|
ulong monotonic(0);
|
|
|
|
strlcpy(buf, ::GetMagickVersion(&monotonic));
|
|
|
|
version.monotonic = monotonic;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-29 04:18:11 +02:00
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::magick::init()
|
|
|
|
{
|
2019-06-01 01:06:55 +02:00
|
|
|
log::info
|
2019-05-29 04:18:11 +02:00
|
|
|
{
|
2019-06-01 01:06:55 +02:00
|
|
|
log, "Initializing Magick Library version API:%lu [%s] ABI:%lu [%s]",
|
|
|
|
long(version_api),
|
|
|
|
string_view{version_api},
|
|
|
|
long(version_abi),
|
|
|
|
string_view{version_abi},
|
2019-05-29 04:18:11 +02:00
|
|
|
};
|
|
|
|
|
2019-06-01 01:06:55 +02:00
|
|
|
if(long(version_api) != long(version_abi))
|
2019-05-29 04:18:11 +02:00
|
|
|
log::warning
|
|
|
|
{
|
2019-05-29 13:38:33 +02:00
|
|
|
log, "Magick Library version mismatch headers:%lu library:%lu",
|
2019-06-01 01:06:55 +02:00
|
|
|
long(version_api),
|
|
|
|
long(version_abi),
|
2019-05-29 04:18:11 +02:00
|
|
|
};
|
|
|
|
|
2019-05-30 07:27:55 +02:00
|
|
|
::MagickAllocFunctions(handle_free, handle_malloc, handle_realloc);
|
|
|
|
::SetFatalErrorHandler(handle_fatal);
|
|
|
|
::SetErrorHandler(handle_error);
|
|
|
|
::SetWarningHandler(handle_warning);
|
2019-05-29 04:18:11 +02:00
|
|
|
::InitializeMagick(nullptr);
|
2019-05-29 13:40:58 +02:00
|
|
|
::SetLogMethod(handle_log);
|
2019-05-30 08:31:55 +02:00
|
|
|
//::SetLogEventMask("all"); // Pollutes stderr :/ can't fix
|
2019-05-30 03:08:19 +02:00
|
|
|
::SetMonitorHandler(handle_progress);
|
2019-05-29 13:40:58 +02:00
|
|
|
::SetMagickResourceLimit(ThreadsResource, 1UL);
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "resource settings: pixel max:%lu:%lu height:%lu:%lu width:%lu:%lu; threads:%lu:%lu",
|
|
|
|
::GetMagickResource(PixelsResource),
|
|
|
|
::GetMagickResourceLimit(PixelsResource),
|
|
|
|
::GetMagickResource(HeightResource),
|
|
|
|
::GetMagickResourceLimit(HeightResource),
|
|
|
|
::GetMagickResource(WidthResource),
|
|
|
|
::GetMagickResourceLimit(WidthResource),
|
|
|
|
::GetMagickResource(ThreadsResource),
|
|
|
|
::GetMagickResourceLimit(ThreadsResource),
|
|
|
|
};
|
2019-05-29 04:18:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::magick::fini()
|
|
|
|
{
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
"Shutting down Magick Library..."
|
|
|
|
};
|
|
|
|
|
|
|
|
::DestroyMagick();
|
|
|
|
}
|
2019-05-29 07:08:46 +02:00
|
|
|
|
2019-05-30 15:07:39 +02:00
|
|
|
//
|
|
|
|
// thumbcrop
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::magick::thumbcrop::thumbcrop(const const_buffer &in,
|
|
|
|
const dimensions &req,
|
|
|
|
const result_closure &out)
|
|
|
|
{
|
|
|
|
crop::offset offset;
|
|
|
|
const auto scaler{[&req, &offset]
|
|
|
|
(const auto &image)
|
|
|
|
{
|
|
|
|
const auto &img_p
|
|
|
|
{
|
|
|
|
std::get<const ::Image *>(image)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &req_x(req.first);
|
|
|
|
const auto &req_y(req.second);
|
|
|
|
const auto &img_x(img_p->columns);
|
|
|
|
const auto &img_y(img_p->rows);
|
|
|
|
|
|
|
|
const bool aspect
|
|
|
|
{
|
|
|
|
req_x * img_y < req_y * img_x
|
|
|
|
};
|
|
|
|
|
|
|
|
const dimensions scaled
|
|
|
|
{
|
|
|
|
aspect? req_y * img_x / img_y : req_x,
|
|
|
|
aspect? req_y : req_x * img_y / img_x,
|
|
|
|
};
|
|
|
|
|
|
|
|
offset =
|
|
|
|
{
|
|
|
|
aspect? (scaled.first - req_x) / 2.0 : 0,
|
|
|
|
aspect? 0 : (scaled.second - req_y) / 2.0,
|
|
|
|
};
|
|
|
|
|
|
|
|
return callex<::Image *>(::ThumbnailImage, img_p, scaled.first, scaled.second);
|
|
|
|
}};
|
|
|
|
|
|
|
|
const auto cropper{[&req, &out, &offset]
|
|
|
|
(const const_buffer &in)
|
|
|
|
{
|
|
|
|
crop
|
|
|
|
{
|
|
|
|
in, req, offset, out
|
|
|
|
};
|
|
|
|
}};
|
|
|
|
|
|
|
|
transform
|
|
|
|
{
|
|
|
|
in, cropper, scaler
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-29 08:25:03 +02:00
|
|
|
//
|
|
|
|
// thumbnail
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::magick::thumbnail::thumbnail(const const_buffer &in,
|
2019-05-30 11:44:12 +02:00
|
|
|
const dimensions &dim,
|
|
|
|
const result_closure &out)
|
2019-05-29 08:25:03 +02:00
|
|
|
{
|
2019-05-30 11:44:12 +02:00
|
|
|
transform
|
2019-05-30 11:11:31 +02:00
|
|
|
{
|
2019-05-30 11:44:12 +02:00
|
|
|
in, out, [&dim](const auto &image)
|
|
|
|
{
|
|
|
|
return callex<::Image *>(::ThumbnailImage, std::get<const ::Image *>(image), dim.first, dim.second);
|
|
|
|
}
|
2019-05-30 11:11:31 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// scale
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::magick::scale::scale(const const_buffer &in,
|
|
|
|
const dimensions &dim,
|
2019-05-30 11:44:12 +02:00
|
|
|
const result_closure &out)
|
2019-05-30 11:11:31 +02:00
|
|
|
{
|
2019-05-30 11:44:12 +02:00
|
|
|
transform
|
2019-05-30 11:11:31 +02:00
|
|
|
{
|
2019-05-30 11:44:12 +02:00
|
|
|
in, out, [&dim](const auto &image)
|
|
|
|
{
|
|
|
|
return callex<::Image *>(::ScaleImage, std::get<const ::Image *>(image), dim.first, dim.second);
|
|
|
|
}
|
2019-05-30 10:29:07 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// shave
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::magick::shave::shave(const const_buffer &in,
|
|
|
|
const dimensions &dim,
|
|
|
|
const offset &off,
|
2019-05-30 11:44:12 +02:00
|
|
|
const result_closure &out)
|
2019-05-30 10:29:07 +02:00
|
|
|
{
|
2019-05-30 11:44:12 +02:00
|
|
|
const ::RectangleInfo geometry
|
2019-05-30 10:29:07 +02:00
|
|
|
{
|
2019-05-30 11:44:12 +02:00
|
|
|
dim.first, // width
|
|
|
|
dim.second, // height
|
|
|
|
off.first, // x
|
|
|
|
off.second, // y
|
2019-05-30 10:29:07 +02:00
|
|
|
};
|
|
|
|
|
2019-05-30 11:44:12 +02:00
|
|
|
transform
|
2019-05-30 10:29:07 +02:00
|
|
|
{
|
2019-05-30 11:44:12 +02:00
|
|
|
in, out, [&geometry](const auto &image)
|
|
|
|
{
|
|
|
|
return callex<::Image *>(::ShaveImage, std::get<const ::Image *>(image), &geometry);
|
|
|
|
}
|
2019-05-30 10:29:07 +02:00
|
|
|
};
|
2019-05-30 11:44:12 +02:00
|
|
|
}
|
2019-05-30 10:29:07 +02:00
|
|
|
|
2019-05-30 11:44:12 +02:00
|
|
|
//
|
|
|
|
// crop
|
|
|
|
//
|
2019-05-30 10:29:07 +02:00
|
|
|
|
2019-05-30 11:44:12 +02:00
|
|
|
ircd::magick::crop::crop(const const_buffer &in,
|
|
|
|
const dimensions &dim,
|
|
|
|
const offset &off,
|
|
|
|
const result_closure &out)
|
|
|
|
{
|
2019-05-30 10:29:07 +02:00
|
|
|
const ::RectangleInfo geometry
|
|
|
|
{
|
|
|
|
dim.first, // width
|
|
|
|
dim.second, // height
|
|
|
|
off.first, // x
|
|
|
|
off.second, // y
|
|
|
|
};
|
|
|
|
|
2019-05-30 11:44:12 +02:00
|
|
|
transform
|
2019-05-29 08:25:03 +02:00
|
|
|
{
|
2019-05-30 11:44:12 +02:00
|
|
|
in, out, [&geometry](const auto &image)
|
|
|
|
{
|
|
|
|
return callex<::Image *>(::CropImage, std::get<const ::Image *>(image), &geometry);
|
|
|
|
}
|
2019-05-29 08:25:03 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-30 09:51:50 +02:00
|
|
|
//
|
2019-05-30 11:44:12 +02:00
|
|
|
// transform (internal)
|
2019-05-30 09:51:50 +02:00
|
|
|
//
|
|
|
|
|
2019-05-30 11:44:12 +02:00
|
|
|
ircd::magick::transform::transform(const const_buffer &input,
|
|
|
|
const output &output,
|
|
|
|
const transformer &transformer)
|
2019-05-30 09:51:50 +02:00
|
|
|
{
|
|
|
|
const custom_ptr<::ImageInfo> input_info
|
|
|
|
{
|
2019-05-30 11:44:12 +02:00
|
|
|
::CloneImageInfo(nullptr),
|
|
|
|
::DestroyImageInfo
|
2019-05-30 09:51:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const custom_ptr<::ImageInfo> output_info
|
|
|
|
{
|
2019-05-30 11:44:12 +02:00
|
|
|
::CloneImageInfo(nullptr),
|
|
|
|
::DestroyImageInfo
|
2019-05-30 09:51:50 +02:00
|
|
|
};
|
|
|
|
|
2019-05-30 11:44:12 +02:00
|
|
|
const custom_ptr<::Image> input_image
|
2019-05-30 09:51:50 +02:00
|
|
|
{
|
2019-05-30 11:44:12 +02:00
|
|
|
callex<::Image *>(::BlobToImage, input_info.get(), data(input), size(input)),
|
|
|
|
::DestroyImage // pollock
|
2019-05-30 09:51:50 +02:00
|
|
|
};
|
|
|
|
|
2019-05-30 11:44:12 +02:00
|
|
|
const custom_ptr<::Image> output_image
|
2019-05-30 09:51:50 +02:00
|
|
|
{
|
2019-05-30 11:44:12 +02:00
|
|
|
transformer({*input_info, input_image.get()}),
|
|
|
|
::DestroyImage
|
2019-05-30 09:51:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
size_t output_size(0);
|
|
|
|
const auto output_data
|
|
|
|
{
|
2019-05-30 11:44:12 +02:00
|
|
|
callex<void *>(::ImageToBlob, output_info.get(), output_image.get(), &output_size)
|
2019-05-30 09:51:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const const_buffer result
|
|
|
|
{
|
|
|
|
reinterpret_cast<char *>(output_data), output_size
|
|
|
|
};
|
|
|
|
|
2019-05-30 11:44:12 +02:00
|
|
|
output(result);
|
2019-05-30 09:51:50 +02:00
|
|
|
}
|
|
|
|
|
2019-05-30 14:14:34 +02:00
|
|
|
//
|
|
|
|
// display (internal)
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::magick::display::display(const const_buffer &input)
|
|
|
|
{
|
|
|
|
const custom_ptr<::ImageInfo> input_info
|
|
|
|
{
|
|
|
|
::CloneImageInfo(nullptr),
|
|
|
|
::DestroyImageInfo
|
|
|
|
};
|
|
|
|
|
|
|
|
const custom_ptr<::Image> input_image
|
|
|
|
{
|
|
|
|
callex<::Image *>(::BlobToImage, input_info.get(), data(input), size(input)),
|
|
|
|
::DestroyImage // pollock
|
|
|
|
};
|
|
|
|
|
|
|
|
display
|
|
|
|
{
|
|
|
|
*input_info, *input_image
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::magick::display::display(const ::ImageInfo &info,
|
|
|
|
::Image &image)
|
|
|
|
{
|
|
|
|
callpf(::DisplayImages, &info, &image);
|
|
|
|
}
|
|
|
|
|
2019-05-29 07:08:46 +02:00
|
|
|
//
|
2019-05-30 11:44:12 +02:00
|
|
|
// util (internal)
|
2019-05-29 07:08:46 +02:00
|
|
|
//
|
|
|
|
|
2019-05-30 03:08:19 +02:00
|
|
|
namespace ircd::magick
|
|
|
|
{
|
|
|
|
// It is likely that we can't have two contexts enter libmagick
|
|
|
|
// simultaneously. This race is possible if the progress callback yields
|
|
|
|
// and another context starts an operation. It is highly unlikely the lib
|
|
|
|
// can handle reentrancy on the same thread. Hitting thread mutexes within
|
2019-05-30 16:31:12 +02:00
|
|
|
// magick will also be catastrophic to ircd::ctx.
|
2019-05-30 03:08:19 +02:00
|
|
|
ctx::mutex call_mutex;
|
|
|
|
}
|
|
|
|
|
2019-05-29 07:08:46 +02:00
|
|
|
template<class return_t,
|
|
|
|
class function,
|
|
|
|
class... args>
|
|
|
|
return_t
|
|
|
|
ircd::magick::callex(function&& f,
|
|
|
|
args&&... a)
|
|
|
|
{
|
2019-05-30 03:08:19 +02:00
|
|
|
const std::lock_guard lock
|
|
|
|
{
|
|
|
|
call_mutex
|
|
|
|
};
|
|
|
|
|
2019-05-29 07:08:46 +02:00
|
|
|
::ExceptionInfo ei;
|
|
|
|
GetExceptionInfo(&ei); // initializer
|
|
|
|
const unwind destroy{[&ei]
|
|
|
|
{
|
|
|
|
::DestroyExceptionInfo(&ei);
|
|
|
|
}};
|
|
|
|
|
|
|
|
const auto ret
|
|
|
|
{
|
|
|
|
f(std::forward<args>(a)..., &ei)
|
|
|
|
};
|
|
|
|
|
2019-05-30 08:31:55 +02:00
|
|
|
const auto their_handler
|
|
|
|
{
|
|
|
|
::SetErrorHandler(handle_exception)
|
|
|
|
};
|
|
|
|
|
|
|
|
const unwind reset{[&their_handler]
|
|
|
|
{
|
|
|
|
::SetErrorHandler(their_handler);
|
|
|
|
}};
|
|
|
|
|
2019-05-29 07:08:46 +02:00
|
|
|
// exception comes out of here; if this is not safe we'll have to
|
|
|
|
// convey with a global or inspect ExceptionInfo manually.
|
|
|
|
::CatchException(&ei);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class function,
|
|
|
|
class... args>
|
|
|
|
void
|
|
|
|
ircd::magick::callpf(function&& f,
|
|
|
|
args&&... a)
|
|
|
|
{
|
2019-05-30 14:13:59 +02:00
|
|
|
if(!call<MagickPassFail>(f, std::forward<args>(a)...))
|
2019-05-29 07:08:46 +02:00
|
|
|
throw error{};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class return_t,
|
|
|
|
class function,
|
|
|
|
class... args>
|
|
|
|
return_t
|
|
|
|
ircd::magick::call(function&& f,
|
|
|
|
args&&... a)
|
|
|
|
{
|
2019-05-30 03:08:19 +02:00
|
|
|
const std::lock_guard lock
|
|
|
|
{
|
|
|
|
call_mutex
|
|
|
|
};
|
|
|
|
|
2019-05-29 07:08:46 +02:00
|
|
|
return f(std::forward<args>(a)...);
|
|
|
|
}
|
|
|
|
|
2019-05-30 07:14:50 +02:00
|
|
|
namespace ircd::magick
|
|
|
|
{
|
|
|
|
static thread_local uint64_t job_ctr;
|
|
|
|
static thread_local int64_t last_quantum;
|
|
|
|
static thread_local uint64_t last_yield;
|
|
|
|
}
|
|
|
|
|
2019-05-29 13:55:09 +02:00
|
|
|
uint
|
2019-05-30 03:08:19 +02:00
|
|
|
ircd::magick::handle_progress(const char *text,
|
|
|
|
const int64_t quantum,
|
|
|
|
const uint64_t span,
|
|
|
|
ExceptionInfo *ei)
|
2019-05-30 08:31:55 +02:00
|
|
|
noexcept try
|
2019-05-29 13:55:09 +02:00
|
|
|
{
|
2019-05-30 07:14:50 +02:00
|
|
|
// This is a new job; reset any global state here
|
|
|
|
if(quantum == 0)
|
|
|
|
{
|
|
|
|
++job_ctr;
|
|
|
|
last_quantum = 0;
|
|
|
|
last_yield = 0;
|
|
|
|
}
|
|
|
|
|
2019-05-30 03:08:19 +02:00
|
|
|
#ifdef IRCD_MAGICK_DEBUG_PROGRESS
|
2019-05-29 13:55:09 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2019-06-01 11:30:29 +02:00
|
|
|
log, "job:%lu progress %2.2lf%% (%ld/%ld) :%s",
|
2019-05-30 07:14:50 +02:00
|
|
|
job_ctr,
|
|
|
|
(quantum / double(span) * 100.0),
|
2019-05-29 13:55:09 +02:00
|
|
|
quantum,
|
2019-05-30 03:08:19 +02:00
|
|
|
span,
|
|
|
|
text,
|
2019-05-29 13:55:09 +02:00
|
|
|
};
|
2019-05-30 03:08:19 +02:00
|
|
|
#endif
|
|
|
|
|
2019-05-30 07:14:50 +02:00
|
|
|
// This is a larger job; we yield this ircd::ctx at interval
|
|
|
|
if(span > uint64_t(yield_threshold))
|
2019-05-30 03:08:19 +02:00
|
|
|
{
|
2019-05-30 07:14:50 +02:00
|
|
|
if(quantum - last_yield > uint64_t(yield_interval))
|
|
|
|
{
|
|
|
|
last_yield = quantum;
|
|
|
|
ctx::yield();
|
|
|
|
}
|
2019-05-30 03:08:19 +02:00
|
|
|
}
|
|
|
|
|
2019-05-30 07:14:50 +02:00
|
|
|
last_quantum = quantum;
|
2019-05-29 13:55:09 +02:00
|
|
|
|
2019-05-30 07:14:50 +02:00
|
|
|
// return false to interrupt the job; set the exception saying why.
|
2019-05-30 08:31:55 +02:00
|
|
|
//
|
|
|
|
// If MonitorEvent (or any *Event) is the code the interruption is
|
|
|
|
// not an error and the operation will silently complete, possibly with
|
|
|
|
// incomplete or corrupt results (i guess? this might be ok for raster
|
|
|
|
// or optimization operations maybe which can go on indefinitely)
|
|
|
|
//
|
|
|
|
// If MonitorError (or any *Error) is the code we propagate the exception
|
|
|
|
// all the way back through our user.
|
|
|
|
//
|
2019-05-30 07:14:50 +02:00
|
|
|
return true;
|
2019-05-29 13:55:09 +02:00
|
|
|
}
|
2019-05-30 08:31:55 +02:00
|
|
|
catch(const ctx::interrupted &e)
|
|
|
|
{
|
|
|
|
::ThrowException(ei, MonitorError, "interrupted", e.what());
|
|
|
|
ei->signature = MagickSignature; // ???
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch(const ctx::terminated &)
|
|
|
|
{
|
|
|
|
::ThrowException(ei, MonitorError, "terminated", nullptr);
|
|
|
|
ei->signature = MagickSignature; // ???
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
::ThrowLoggedException(ei, MonitorError, "error", e.what(), __FILE__, __FUNCTION__, __LINE__);
|
|
|
|
ei->signature = MagickSignature; // ???
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
::ThrowLoggedException(ei, MonitorFatalError, "unknown", nullptr, __FILE__, __FUNCTION__, __LINE__);
|
|
|
|
ei->signature = MagickSignature; // ???
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-29 13:55:09 +02:00
|
|
|
|
2019-05-30 07:27:55 +02:00
|
|
|
void
|
|
|
|
ircd::magick::handle_free(void *const ptr)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
std::free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
ircd::magick::handle_malloc(size_t size)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return std::malloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
ircd::magick::handle_realloc(void *const ptr,
|
|
|
|
size_t size)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return std::realloc(ptr, size);
|
|
|
|
}
|
|
|
|
|
2019-05-29 07:08:46 +02:00
|
|
|
void
|
|
|
|
ircd::magick::handle_log(const ::ExceptionType type,
|
|
|
|
const char *const message)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
log::debug
|
|
|
|
{
|
2019-05-30 08:31:55 +02:00
|
|
|
log, "(%d) %s :%s",
|
|
|
|
int(type),
|
|
|
|
::GetLocaleExceptionMessage(type, ""),
|
|
|
|
message,
|
2019-05-29 07:08:46 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::magick::handle_warning(const ::ExceptionType type,
|
|
|
|
const char *const reason,
|
|
|
|
const char *const description)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
log::warning
|
|
|
|
{
|
2019-05-30 08:31:55 +02:00
|
|
|
log, "(#%d) %s :%s :%s",
|
|
|
|
int(type),
|
|
|
|
::GetLocaleExceptionMessage(type, ""),
|
2019-05-29 07:08:46 +02:00
|
|
|
reason,
|
2019-05-30 08:31:55 +02:00
|
|
|
description,
|
2019-05-29 07:08:46 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::magick::handle_error(const ::ExceptionType type,
|
|
|
|
const char *const reason,
|
|
|
|
const char *const description)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2019-05-30 08:31:55 +02:00
|
|
|
log, "(#%d) %s :%s :%s",
|
|
|
|
int(type),
|
|
|
|
::GetLocaleExceptionMessage(type, ""),
|
2019-05-29 07:08:46 +02:00
|
|
|
reason,
|
2019-05-30 08:31:55 +02:00
|
|
|
description,
|
2019-05-29 07:08:46 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
[[noreturn]] void
|
|
|
|
ircd::magick::handle_fatal(const ::ExceptionType type,
|
|
|
|
const char *const reason,
|
|
|
|
const char *const description)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
2019-05-30 08:31:55 +02:00
|
|
|
log, "(#%d) %s :%s :%s",
|
|
|
|
int(type),
|
|
|
|
::GetLocaleExceptionMessage(type, ""),
|
2019-05-29 07:08:46 +02:00
|
|
|
reason,
|
2019-05-30 08:31:55 +02:00
|
|
|
description,
|
2019-05-29 07:08:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ircd::terminate();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[noreturn]] void
|
|
|
|
ircd::magick::handle_exception(const ::ExceptionType type,
|
|
|
|
const char *const reason,
|
|
|
|
const char *const description)
|
|
|
|
{
|
2019-05-30 08:31:55 +02:00
|
|
|
const auto &message
|
2019-05-29 07:08:46 +02:00
|
|
|
{
|
2019-05-30 08:31:55 +02:00
|
|
|
::GetLocaleExceptionMessage(type, "")?: "???"
|
|
|
|
};
|
|
|
|
|
|
|
|
thread_local char buf[exception::BUFSIZE];
|
|
|
|
const string_view what{fmt::sprintf
|
|
|
|
{
|
|
|
|
buf, "(#%d) %s :%s :%s",
|
|
|
|
int(type),
|
|
|
|
message,
|
2019-05-29 07:08:46 +02:00
|
|
|
reason,
|
2019-05-30 08:31:55 +02:00
|
|
|
description,
|
|
|
|
}};
|
|
|
|
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
log, "%s", what
|
|
|
|
};
|
|
|
|
|
|
|
|
if(reason == "terminated"_sv)
|
|
|
|
throw ctx::terminated{};
|
|
|
|
|
|
|
|
if(reason == "interrupted"_sv)
|
|
|
|
throw ctx::interrupted
|
|
|
|
{
|
|
|
|
"%s", what
|
|
|
|
};
|
|
|
|
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"%s", what
|
2019-05-29 07:08:46 +02:00
|
|
|
};
|
|
|
|
}
|