blob: 2e306c627d61593edddf35f20b92482c5ff12cc2 [file] [log] [blame]
Roger Meier84e4a3c2011-09-16 20:58:44 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
Roger Meier4285ba22013-06-10 21:17:23 +020020#include <thrift/windows/TWinsockSingleton.h>
Roger Meier84e4a3c2011-09-16 20:58:44 +000021
22// boost
23#include <boost/assert.hpp>
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040024#include <stdexcept>
Roger Meier84e4a3c2011-09-16 20:58:44 +000025
26namespace apache { namespace thrift { namespace transport {
27
28TWinsockSingleton::instance_ptr TWinsockSingleton::instance_ptr_(NULL);
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040029#if USE_BOOST_THREAD
Roger Meier84e4a3c2011-09-16 20:58:44 +000030boost::once_flag TWinsockSingleton::flags_ = BOOST_ONCE_INIT;
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040031#elif USE_STD_THREAD
32std::once_flag TWinsockSingleton::flags_;
33#else
34#error For windows you must choose USE_BOOST_THREAD or USE_STD_THREAD
35#endif
Roger Meier84e4a3c2011-09-16 20:58:44 +000036
37//------------------------------------------------------------------------------
38TWinsockSingleton::TWinsockSingleton(void)
39{
40 WORD version(MAKEWORD(2, 2));
41 WSAData data = {0};
42
43 int error(WSAStartup(version, &data));
44 if (error != 0)
45 {
46 BOOST_ASSERT(false);
47 throw std::runtime_error("Failed to initialise Winsock.");
48 }
49}
50
51//------------------------------------------------------------------------------
52TWinsockSingleton::~TWinsockSingleton(void)
53{
54 WSACleanup();
55}
56
57//------------------------------------------------------------------------------
58void TWinsockSingleton::create(void)
59{
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040060#if USE_BOOST_THREAD
Roger Meier84e4a3c2011-09-16 20:58:44 +000061 boost::call_once(init, flags_);
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040062#elif USE_STD_THREAD
63 std::call_once(flags_, init);
64#endif
Roger Meier84e4a3c2011-09-16 20:58:44 +000065}
66
67//------------------------------------------------------------------------------
68void TWinsockSingleton::init(void)
69{
70 instance_ptr_.reset(new TWinsockSingleton);
71}
72
73}}} // apache::thrift::transport