module: is_in_main_thread

This commit is contained in:
2018-12-17 20:00:30 +07:00
parent efb2d0fb60
commit 958236b7d0
5 changed files with 26 additions and 24 deletions

View File

@@ -150,7 +150,7 @@ namespace e2d
template < std::size_t N > template < std::size_t N >
render& render::execute(const command_block<N>& commands) { render& render::execute(const command_block<N>& commands) {
E2D_ASSERT(main_thread() == std::this_thread::get_id()); E2D_ASSERT(is_in_main_thread());
for ( std::size_t i = 0, e = commands.command_count(); i < e; ++i ) { for ( std::size_t i = 0, e = commands.command_count(); i < e; ++i ) {
execute(commands.command(i)); execute(commands.command(i));
} }

View File

@@ -29,14 +29,16 @@ namespace e2d
public: public:
using base_type = BaseT; using base_type = BaseT;
public: public:
module() noexcept { module() noexcept = default;
main_thread_ = std::this_thread::get_id();
}
virtual ~module() noexcept = default; virtual ~module() noexcept = default;
const std::thread::id& main_thread() const noexcept { const std::thread::id& main_thread() const noexcept {
return main_thread_; return main_thread_;
} }
bool is_in_main_thread() const noexcept {
return std::this_thread::get_id() == main_thread_;
}
public: public:
template < typename ImplT, typename... Args > template < typename ImplT, typename... Args >
static ImplT& initialize(Args&&... args) { static ImplT& initialize(Args&&... args) {
@@ -63,7 +65,7 @@ namespace e2d
} }
private: private:
static std::unique_ptr<BaseT> instance_; static std::unique_ptr<BaseT> instance_;
std::thread::id main_thread_; std::thread::id main_thread_ = std::this_thread::get_id();
}; };
template < typename BaseT > template < typename BaseT >

View File

@@ -383,7 +383,7 @@ namespace e2d
engine::~engine() noexcept = default; engine::~engine() noexcept = default;
bool engine::start(application_uptr app) { bool engine::start(application_uptr app) {
E2D_ASSERT(main_thread() == std::this_thread::get_id()); E2D_ASSERT(is_in_main_thread());
if ( !app || !app->initialize() ) { if ( !app || !app->initialize() ) {
the<debug>().error("ENGINE: Failed to initialize application"); the<debug>().error("ENGINE: Failed to initialize application");

View File

@@ -1112,7 +1112,7 @@ namespace e2d
// //
render& render::execute(const command_value& command) { render& render::execute(const command_value& command) {
E2D_ASSERT(main_thread() == std::this_thread::get_id()); E2D_ASSERT(is_in_main_thread());
E2D_ASSERT(!command.valueless_by_exception()); E2D_ASSERT(!command.valueless_by_exception());
stdex::visit(command_value_visitor(*this), command); stdex::visit(command_value_visitor(*this), command);
return *this; return *this;

View File

@@ -462,7 +462,7 @@ namespace e2d
const str& vertex_source, const str& vertex_source,
const str& fragment_source) const str& fragment_source)
{ {
E2D_ASSERT(main_thread() == std::this_thread::get_id()); E2D_ASSERT(is_in_main_thread());
gl_shader_id vs = gl_compile_shader( gl_shader_id vs = gl_compile_shader(
state_->dbg(), vertex_source, GL_VERTEX_SHADER); state_->dbg(), vertex_source, GL_VERTEX_SHADER);
@@ -491,7 +491,7 @@ namespace e2d
const input_stream_uptr& vertex, const input_stream_uptr& vertex,
const input_stream_uptr& fragment) const input_stream_uptr& fragment)
{ {
E2D_ASSERT(main_thread() == std::this_thread::get_id()); E2D_ASSERT(is_in_main_thread());
str vertex_source, fragment_source; str vertex_source, fragment_source;
return streams::try_read_tail(vertex_source, vertex) return streams::try_read_tail(vertex_source, vertex)
@@ -503,7 +503,7 @@ namespace e2d
texture_ptr render::create_texture( texture_ptr render::create_texture(
const image& image) const image& image)
{ {
E2D_ASSERT(main_thread() == std::this_thread::get_id()); E2D_ASSERT(is_in_main_thread());
const pixel_declaration decl = const pixel_declaration decl =
convert_image_data_format_to_pixel_declaration(image.format()); convert_image_data_format_to_pixel_declaration(image.format());
@@ -591,7 +591,7 @@ namespace e2d
texture_ptr render::create_texture( texture_ptr render::create_texture(
const input_stream_uptr& image_stream) const input_stream_uptr& image_stream)
{ {
E2D_ASSERT(main_thread() == std::this_thread::get_id()); E2D_ASSERT(is_in_main_thread());
image image; image image;
if ( !images::try_load_image(image, image_stream) ) { if ( !images::try_load_image(image, image_stream) ) {
@@ -604,7 +604,7 @@ namespace e2d
const v2u& size, const v2u& size,
const pixel_declaration& decl) const pixel_declaration& decl)
{ {
E2D_ASSERT(main_thread() == std::this_thread::get_id()); E2D_ASSERT(is_in_main_thread());
if ( !is_pixel_supported(decl) ) { if ( !is_pixel_supported(decl) ) {
state_->dbg().error("RENDER: Failed to create texture:\n" state_->dbg().error("RENDER: Failed to create texture:\n"
@@ -692,7 +692,7 @@ namespace e2d
const index_declaration& decl, const index_declaration& decl,
index_buffer::usage usage) index_buffer::usage usage)
{ {
E2D_ASSERT(main_thread() == std::this_thread::get_id()); E2D_ASSERT(is_in_main_thread());
E2D_ASSERT(indices.size() % decl.bytes_per_index() == 0); E2D_ASSERT(indices.size() % decl.bytes_per_index() == 0);
if ( !is_index_supported(decl) ) { if ( !is_index_supported(decl) ) {
@@ -728,7 +728,7 @@ namespace e2d
const vertex_declaration& decl, const vertex_declaration& decl,
vertex_buffer::usage usage) vertex_buffer::usage usage)
{ {
E2D_ASSERT(main_thread() == std::this_thread::get_id()); E2D_ASSERT(is_in_main_thread());
E2D_ASSERT(vertices.size() % decl.bytes_per_vertex() == 0); E2D_ASSERT(vertices.size() % decl.bytes_per_vertex() == 0);
if ( !is_vertex_supported(decl) ) { if ( !is_vertex_supported(decl) ) {
@@ -763,7 +763,7 @@ namespace e2d
const pixel_declaration& depth_decl, const pixel_declaration& depth_decl,
render_target::external_texture external_texture) render_target::external_texture external_texture)
{ {
E2D_ASSERT(main_thread() == std::this_thread::get_id()); E2D_ASSERT(is_in_main_thread());
E2D_ASSERT( E2D_ASSERT(
depth_decl.is_depth() && depth_decl.is_depth() &&
@@ -883,13 +883,13 @@ namespace e2d
} }
render& render::execute(const swap_command& command) { render& render::execute(const swap_command& command) {
E2D_ASSERT(main_thread() == std::this_thread::get_id()); E2D_ASSERT(is_in_main_thread());
state_->wnd().swap_buffers(command.vsync()); state_->wnd().swap_buffers(command.vsync());
return *this; return *this;
} }
render& render::execute(const draw_command& command) { render& render::execute(const draw_command& command) {
E2D_ASSERT(main_thread() == std::this_thread::get_id()); E2D_ASSERT(is_in_main_thread());
const material& mat = command.material_ref(); const material& mat = command.material_ref();
const geometry& geo = command.geometry_ref(); const geometry& geo = command.geometry_ref();
@@ -919,7 +919,7 @@ namespace e2d
} }
render& render::execute(const clear_command& command) { render& render::execute(const clear_command& command) {
E2D_ASSERT(main_thread() == std::this_thread::get_id()); E2D_ASSERT(is_in_main_thread());
bool clear_color = bool clear_color =
math::enum_to_number(command.clear_buffer()) math::enum_to_number(command.clear_buffer())
@@ -968,13 +968,13 @@ namespace e2d
} }
render& render::execute(const target_command& command) { render& render::execute(const target_command& command) {
E2D_ASSERT(main_thread() == std::this_thread::get_id()); E2D_ASSERT(is_in_main_thread());
state_->set_render_target(command.target()); state_->set_render_target(command.target());
return *this; return *this;
} }
render& render::execute(const viewport_command& command) { render& render::execute(const viewport_command& command) {
E2D_ASSERT(main_thread() == std::this_thread::get_id()); E2D_ASSERT(is_in_main_thread());
const b2u viewport = make_minmax_rect(command.viewport_rect()); const b2u viewport = make_minmax_rect(command.viewport_rect());
GL_CHECK_CODE(state_->dbg(), glViewport( GL_CHECK_CODE(state_->dbg(), glViewport(
@@ -999,12 +999,12 @@ namespace e2d
} }
const render::device_caps& render::device_capabilities() const noexcept { const render::device_caps& render::device_capabilities() const noexcept {
E2D_ASSERT(main_thread() == std::this_thread::get_id()); E2D_ASSERT(is_in_main_thread());
return state_->device_capabilities(); return state_->device_capabilities();
} }
bool render::is_pixel_supported(const pixel_declaration& decl) const noexcept { bool render::is_pixel_supported(const pixel_declaration& decl) const noexcept {
E2D_ASSERT(main_thread() == std::this_thread::get_id()); E2D_ASSERT(is_in_main_thread());
switch ( decl.type() ) { switch ( decl.type() ) {
case pixel_declaration::pixel_type::depth16: case pixel_declaration::pixel_type::depth16:
return true; return true;
@@ -1047,7 +1047,7 @@ namespace e2d
} }
bool render::is_index_supported(const index_declaration& decl) const noexcept { bool render::is_index_supported(const index_declaration& decl) const noexcept {
E2D_ASSERT(main_thread() == std::this_thread::get_id()); E2D_ASSERT(is_in_main_thread());
switch ( decl.type() ) { switch ( decl.type() ) {
case index_declaration::index_type::unsigned_byte: case index_declaration::index_type::unsigned_byte:
case index_declaration::index_type::unsigned_short: case index_declaration::index_type::unsigned_short:
@@ -1067,7 +1067,7 @@ namespace e2d
} }
bool render::is_vertex_supported(const vertex_declaration& decl) const noexcept { bool render::is_vertex_supported(const vertex_declaration& decl) const noexcept {
E2D_ASSERT(main_thread() == std::this_thread::get_id()); E2D_ASSERT(is_in_main_thread());
return decl.attribute_count() <= device_capabilities().max_vertex_attributes; return decl.attribute_count() <= device_capabilities().max_vertex_attributes;
} }
} }