5
// Copyright (c) 2003-2007 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7
// Distributed under the Boost Software License, Version 1.0. (See accompanying
8
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11
#ifndef ASIO_DETAIL_WINSOCK_INIT_HPP
12
#define ASIO_DETAIL_WINSOCK_INIT_HPP
14
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
16
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18
#include "asio/detail/push_options.hpp"
20
#include "asio/detail/push_options.hpp"
21
#include <boost/config.hpp>
22
#include "asio/detail/pop_options.hpp"
24
#if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
26
#include "asio/detail/push_options.hpp"
27
#include <boost/shared_ptr.hpp>
28
#include <boost/throw_exception.hpp>
29
#include "asio/detail/pop_options.hpp"
31
#include "asio/error.hpp"
32
#include "asio/system_error.hpp"
33
#include "asio/detail/noncopyable.hpp"
34
#include "asio/detail/socket_types.hpp"
39
template <int Major = 2, int Minor = 0>
44
// Structure to perform the actual initialisation.
50
result_ = ::WSAStartup(MAKEWORD(Major, Minor), &wsa_data);
63
// Helper function to manage a do_init singleton. The static instance of the
64
// winsock_init object ensures that this function is always called before
65
// main, and therefore before any other threads can get started. The do_init
66
// instance must be static in this function to ensure that it gets
67
// initialised before any other global objects try to use it.
68
static boost::shared_ptr<do_init> instance()
70
static boost::shared_ptr<do_init> init(new do_init);
81
: ref_(do_init::instance())
83
// Check whether winsock was successfully initialised. This check is not
84
// performed for the global instance since there will be nobody around to
85
// catch the exception.
86
if (this != &instance_ && ref_->result() != 0)
89
asio::error_code(ref_->result(),
90
asio::error::system_category),
92
boost::throw_exception(e);
102
// Instance to force initialisation of winsock at global scope.
103
static winsock_init instance_;
105
// Reference to singleton do_init object to ensure that winsock does not get
106
// cleaned up until the last user has finished with it.
107
boost::shared_ptr<do_init> ref_;
110
template <int Major, int Minor>
111
winsock_init<Major, Minor> winsock_init<Major, Minor>::instance_;
113
} // namespace detail
116
#endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
118
#include "asio/detail/pop_options.hpp"
120
#endif // ASIO_DETAIL_WINSOCK_INIT_HPP