From d9e5875753b96398e12cf824fe19119b3034a210 Mon Sep 17 00:00:00 2001 From: chris_kohlhoff Date: Thu, 3 Mar 2005 12:49:32 +0000 Subject: [PATCH] Ensure that low-level system resource wrappers throw exceptions on failure (Sourceforge Request ID 1152841). --- asio/include/asio/detail/posix_thread.hpp | 4 +++- asio/include/asio/detail/posix_tss_bool.hpp | 4 ++-- asio/include/asio/detail/win_event.hpp | 6 ++++++ asio/include/asio/detail/win_mutex.hpp | 4 ++-- asio/include/asio/detail/win_thread.hpp | 3 +++ asio/include/asio/detail/win_tss_bool.hpp | 4 ++-- 6 files changed, 18 insertions(+), 7 deletions(-) diff --git a/asio/include/asio/detail/posix_thread.hpp b/asio/include/asio/detail/posix_thread.hpp index 5a137d5f..aad66962 100644 --- a/asio/include/asio/detail/posix_thread.hpp +++ b/asio/include/asio/detail/posix_thread.hpp @@ -16,6 +16,7 @@ #if !defined(_WIN32) #include "asio/detail/push_options.hpp" +#include #include #include #include "asio/detail/pop_options.hpp" @@ -37,7 +38,8 @@ public: : joined_(false) { func_base* arg = new func(f); - ::pthread_create(&thread_, 0, asio_detail_posix_thread_function, arg); + if (::pthread_create(&thread_, 0, asio_detail_posix_thread_function, arg)) + throw std::bad_alloc(); } // Destructor. diff --git a/asio/include/asio/detail/posix_tss_bool.hpp b/asio/include/asio/detail/posix_tss_bool.hpp index 86cce90c..3084f81b 100644 --- a/asio/include/asio/detail/posix_tss_bool.hpp +++ b/asio/include/asio/detail/posix_tss_bool.hpp @@ -16,7 +16,7 @@ #if !defined(_WIN32) #include "asio/detail/push_options.hpp" -#include +#include #include #include "asio/detail/pop_options.hpp" @@ -30,7 +30,7 @@ public: posix_tss_bool() { if (::pthread_key_create(&tss_key_, 0) != 0) - throw std::runtime_error("Cannot create thread-local storage"); + throw std::bad_alloc(); } // Destructor. diff --git a/asio/include/asio/detail/win_event.hpp b/asio/include/asio/detail/win_event.hpp index 434f521e..dcfa2011 100644 --- a/asio/include/asio/detail/win_event.hpp +++ b/asio/include/asio/detail/win_event.hpp @@ -15,6 +15,10 @@ #if defined(_WIN32) +#include "asio/detail/push_options.hpp" +#include +#include "asio/detail/pop_options.hpp" + #include "asio/detail/socket_types.hpp" namespace asio { @@ -28,6 +32,8 @@ public: win_event() : event_(::CreateEvent(0, true, false, 0)) { + if (!event_) + throw std::bad_alloc(); } // Destructor. diff --git a/asio/include/asio/detail/win_mutex.hpp b/asio/include/asio/detail/win_mutex.hpp index 79941b6d..06f2b1b9 100644 --- a/asio/include/asio/detail/win_mutex.hpp +++ b/asio/include/asio/detail/win_mutex.hpp @@ -16,7 +16,7 @@ #if defined(_WIN32) #include "asio/detail/push_options.hpp" -#include +#include #include #include "asio/detail/pop_options.hpp" @@ -36,7 +36,7 @@ public: win_mutex() { if (!do_init()) - throw std::runtime_error("Unable to create mutex"); + throw std::bad_alloc(); } // Destructor. diff --git a/asio/include/asio/detail/win_thread.hpp b/asio/include/asio/detail/win_thread.hpp index a8fb061b..d9023aed 100644 --- a/asio/include/asio/detail/win_thread.hpp +++ b/asio/include/asio/detail/win_thread.hpp @@ -18,6 +18,7 @@ #include "asio/detail/socket_types.hpp" #include "asio/detail/push_options.hpp" +#include #include #include "asio/detail/pop_options.hpp" @@ -38,6 +39,8 @@ public: unsigned int thread_id = 0; thread_ = reinterpret_cast(::_beginthreadex(0, 0, asio_detail_win_thread_function, arg, 0, &thread_id)); + if (!thread_) + throw std::bad_alloc(); } // Destructor. diff --git a/asio/include/asio/detail/win_tss_bool.hpp b/asio/include/asio/detail/win_tss_bool.hpp index d1f30cb8..79289096 100644 --- a/asio/include/asio/detail/win_tss_bool.hpp +++ b/asio/include/asio/detail/win_tss_bool.hpp @@ -16,7 +16,7 @@ #if defined(_WIN32) #include "asio/detail/push_options.hpp" -#include +#include #include "asio/detail/pop_options.hpp" #include "asio/detail/socket_types.hpp" @@ -32,7 +32,7 @@ public: { tss_key_ = ::TlsAlloc(); if (tss_key_ == TLS_OUT_OF_INDEXES) - throw std::runtime_error("Cannot create thread-local storage"); + throw std::bad_alloc(); } // Destructor.