Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 1 | /* |
| 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 Meier | 4285ba2 | 2013-06-10 21:17:23 +0200 | [diff] [blame] | 20 | #include <thrift/windows/TWinsockSingleton.h> |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 21 | |
| 22 | // boost |
| 23 | #include <boost/assert.hpp> |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 24 | #include <stdexcept> |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 25 | |
| 26 | namespace apache { namespace thrift { namespace transport { |
| 27 | |
| 28 | TWinsockSingleton::instance_ptr TWinsockSingleton::instance_ptr_(NULL); |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 29 | #if USE_BOOST_THREAD |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 30 | boost::once_flag TWinsockSingleton::flags_ = BOOST_ONCE_INIT; |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 31 | #elif USE_STD_THREAD |
| 32 | std::once_flag TWinsockSingleton::flags_; |
| 33 | #else |
| 34 | #error For windows you must choose USE_BOOST_THREAD or USE_STD_THREAD |
| 35 | #endif |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 36 | |
| 37 | //------------------------------------------------------------------------------ |
| 38 | TWinsockSingleton::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 | //------------------------------------------------------------------------------ |
| 52 | TWinsockSingleton::~TWinsockSingleton(void) |
| 53 | { |
| 54 | WSACleanup(); |
| 55 | } |
| 56 | |
| 57 | //------------------------------------------------------------------------------ |
| 58 | void TWinsockSingleton::create(void) |
| 59 | { |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 60 | #if USE_BOOST_THREAD |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 61 | boost::call_once(init, flags_); |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 62 | #elif USE_STD_THREAD |
| 63 | std::call_once(flags_, init); |
| 64 | #endif |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | //------------------------------------------------------------------------------ |
| 68 | void TWinsockSingleton::init(void) |
| 69 | { |
| 70 | instance_ptr_.reset(new TWinsockSingleton); |
| 71 | } |
| 72 | |
| 73 | }}} // apache::thrift::transport |