Refactored tss_bool into separate classes for each platform.

This commit is contained in:
chris 2003-09-28 07:58:09 +00:00
parent cb91e6009e
commit 8d16489fcd
4 changed files with 162 additions and 70 deletions

View File

@ -29,6 +29,7 @@ nobase_include_HEADERS = \
asio/detail/pop_options.hpp \
asio/detail/pthread_mutex.hpp \
asio/detail/pthread_thread.hpp \
asio/detail/pthread_tss_bool.hpp \
asio/detail/push_options.hpp \
asio/detail/reactive_dgram_socket_service.hpp \
asio/detail/reactive_socket_acceptor_service.hpp \
@ -52,6 +53,7 @@ nobase_include_HEADERS = \
asio/detail/win_iocp_dgram_socket_service.hpp \
asio/detail/win_mutex.hpp \
asio/detail/win_thread.hpp \
asio/detail/win_tss_bool.hpp \
asio/detail/winsock_init.hpp \
asio/dgram_socket.hpp \
asio/inet_address_v4.hpp \

View File

@ -0,0 +1,77 @@
//
// pthread_tss_bool.hpp
// ~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003 Christopher M. Kohlhoff (chris@kohlhoff.com)
//
// Permission to use, copy, modify, distribute and sell this software and its
// documentation for any purpose is hereby granted without fee, provided that
// the above copyright notice appears in all copies and that both the copyright
// notice and this permission notice appear in supporting documentation. This
// software is provided "as is" without express or implied warranty, and with
// no claim as to its suitability for any purpose.
//
#ifndef ASIO_DETAIL_PTHREAD_TSS_BOOL_HPP
#define ASIO_DETAIL_PTHREAD_TSS_BOOL_HPP
#include "asio/detail/push_options.hpp"
#if !defined(_WIN32)
#include "asio/detail/push_options.hpp"
#include <stdexcept>
#include <pthread.h>
#include "asio/detail/pop_options.hpp"
namespace asio {
namespace detail {
class pthread_tss_bool
{
public:
// Constructor.
pthread_tss_bool()
{
if (::pthread_key_create(&tss_key_, 0) != 0)
throw std::runtime_error("Cannot create thread-local storage");
}
// Destructor.
~pthread_tss_bool()
{
::pthread_key_delete(tss_key_);
}
// Test the value of the flag.
operator bool() const
{
return ::pthread_getspecific(tss_key_) != 0;
}
// Test for the value of the flag being false.
bool operator!() const
{
return ::pthread_getspecific(tss_key_) == 0;
}
// Set the value of the flag.
void operator=(bool value)
{
::pthread_setspecific(tss_key_, value ? this : 0);
}
private:
// Thread-specific storage to allow unlocked access to determine whether a
// thread is a member of the pool.
mutable pthread_key_t tss_key_;
};
} // namespace detail
} // namespace asio
#endif // !defined(_WIN32)
#include "asio/detail/pop_options.hpp"
#endif // ASIO_DETAIL_PTHREAD_TSS_BOOL_HPP

View File

@ -17,83 +17,17 @@
#include "asio/detail/push_options.hpp"
#include "asio/detail/push_options.hpp"
#include <stdexcept>
#if defined(_WIN32)
#include "asio/detail/socket_types.hpp"
#else
#include <pthread.h>
#endif // defined(_WIN32)
#include "asio/detail/pop_options.hpp"
#include "asio/detail/pthread_tss_bool.hpp"
#include "asio/detail/win_tss_bool.hpp"
namespace asio {
namespace detail {
class tss_bool
{
public:
// Constructor.
tss_bool()
{
#if defined(_WIN32)
tss_key_ = ::TlsAlloc();
if (tss_key_ == TLS_OUT_OF_INDEXES)
throw std::runtime_error("Cannot create thread-local storage");
#else // defined(_WIN32)
if (::pthread_key_create(&tss_key_, 0) != 0)
throw std::runtime_error("Cannot create thread-local storage");
#endif // defined(_WIN32)
}
// Destructor.
~tss_bool()
{
#if defined(_WIN32)
::TlsFree(tss_key_);
#else // defined(_WIN32)
::pthread_key_delete(tss_key_);
#endif // defined(_WIN32)
}
// Test the value of the flag.
operator bool() const
{
#if defined(_WIN32)
return ::TlsGetValue(tss_key_) != 0;
#else // defined(_WIN32)
return ::pthread_getspecific(tss_key_) != 0;
#endif // defined(_WIN32)
}
// Test for the value of the flag being false.
bool operator!() const
{
#if defined(_WIN32)
return ::TlsGetValue(tss_key_) == 0;
#else // defined(_WIN32)
return ::pthread_getspecific(tss_key_) == 0;
#endif // defined(_WIN32)
}
// Set the value of the flag.
void operator=(bool value)
{
#if defined(_WIN32)
::TlsSetValue(tss_key_, value ? this : 0);
#else // defined(_WIN32)
::pthread_setspecific(tss_key_, value ? this : 0);
#endif // defined(_WIN32)
}
private:
// Thread-specific storage to allow unlocked access to determine whether a
// thread is a member of the pool.
#if defined(_WIN32)
mutable unsigned long tss_key_;
typedef win_tss_bool tss_bool;
#else
mutable pthread_key_t tss_key_;
typedef pthread_tss_bool tss_bool;
#endif
};
} // namespace detail
} // namespace asio

View File

@ -0,0 +1,79 @@
//
// win_tss_bool.hpp
// ~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003 Christopher M. Kohlhoff (chris@kohlhoff.com)
//
// Permission to use, copy, modify, distribute and sell this software and its
// documentation for any purpose is hereby granted without fee, provided that
// the above copyright notice appears in all copies and that both the copyright
// notice and this permission notice appear in supporting documentation. This
// software is provided "as is" without express or implied warranty, and with
// no claim as to its suitability for any purpose.
//
#ifndef ASIO_DETAIL_WIN_TSS_BOOL_HPP
#define ASIO_DETAIL_WIN_TSS_BOOL_HPP
#include "asio/detail/push_options.hpp"
#if defined(_WIN32)
#include "asio/detail/push_options.hpp"
#include <stdexcept>
#include "asio/detail/pop_options.hpp"
#include "asio/detail/socket_types.hpp"
namespace asio {
namespace detail {
class win_tss_bool
{
public:
// Constructor.
win_tss_bool()
{
tss_key_ = ::TlsAlloc();
if (tss_key_ == TLS_OUT_OF_INDEXES)
throw std::runtime_error("Cannot create thread-local storage");
}
// Destructor.
~win_tss_bool()
{
::TlsFree(tss_key_);
}
// Test the value of the flag.
operator bool() const
{
return ::TlsGetValue(tss_key_) != 0;
}
// Test for the value of the flag being false.
bool operator!() const
{
return ::TlsGetValue(tss_key_) == 0;
}
// Set the value of the flag.
void operator=(bool value)
{
::TlsSetValue(tss_key_, value ? this : 0);
}
private:
// Thread-specific storage to allow unlocked access to determine whether a
// thread is a member of the pool.
mutable unsigned long tss_key_;
};
} // namespace detail
} // namespace asio
#endif // defined(_WIN32)
#include "asio/detail/pop_options.hpp"
#endif // ASIO_DETAIL_WIN_TSS_BOOL_HPP