STXXL  1.4-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
request_queue_impl_worker.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * lib/io/request_queue_impl_worker.cpp
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002-2005 Roman Dementiev <[email protected]>
7  * Copyright (C) 2008, 2009 Andreas Beckmann <[email protected]>
8  * Copyright (C) 2009 Johannes Singler <[email protected]>
9  * Copyright (C) 2013 Timo Bingmann <[email protected]>
10  *
11  * Distributed under the Boost Software License, Version 1.0.
12  * (See accompanying file LICENSE_1_0.txt or copy at
13  * http://www.boost.org/LICENSE_1_0.txt)
14  **************************************************************************/
15 
19 #include <stxxl/bits/config.h>
21 #include <stxxl/bits/namespace.h>
22 
23 #include <cassert>
24 #include <cstddef>
25 
26 #if STXXL_BOOST_THREADS
27  #include <boost/bind.hpp>
28 #endif
29 #if STXXL_STD_THREADS && STXXL_MSVC >= 1700
30  #include <windows.h>
31 #endif
32 
34 
35 void request_queue_impl_worker::start_thread(void* (*worker)(void*), void* arg, thread_type& t, state<thread_state>& s)
36 {
37  assert(s() == NOT_RUNNING);
38 #if STXXL_STD_THREADS
39  t = new std::thread(worker, arg);
40 #elif STXXL_BOOST_THREADS
41  t = new boost::thread(boost::bind(worker, arg));
42 #else
43  STXXL_CHECK_PTHREAD_CALL(pthread_create(&t, NULL, worker, arg));
44 #endif
45  s.set_to(RUNNING);
46 }
47 
48 void request_queue_impl_worker::stop_thread(thread_type& t, state<thread_state>& s, semaphore& sem)
49 {
50  assert(s() == RUNNING);
51  s.set_to(TERMINATING);
52  sem++;
53 #if STXXL_STD_THREADS
54 #if STXXL_MSVC >= 1700
55  // In the Visual C++ Runtime 2012 and 2013, there is a deadlock bug, which
56  // occurs when threads are joined after main() exits. Apparently, Microsoft
57  // thinks this is not a big issue. It has not been fixed in VC++RT 2013.
58  // https://connect.microsoft.com/VisualStudio/feedback/details/747145
59  //
60  // All STXXL threads are created by singletons, which are global variables
61  // that are deleted after main() exits. The fix applied here it to use
62  // std::thread::native_handle() and access the WINAPI to terminate the
63  // thread directly (after it finished handling its i/o requests).
64 
65  WaitForSingleObject(t->native_handle(), INFINITE);
66  CloseHandle(t->native_handle());
67 #else
68  t->join();
69  delete t;
70 #endif
71  t = NULL;
72 #elif STXXL_BOOST_THREADS
73  t->join();
74  delete t;
75  t = NULL;
76 #else
77  STXXL_CHECK_PTHREAD_CALL(pthread_join(t, NULL));
78 #endif
79  assert(s() == TERMINATED);
80  s.set_to(NOT_RUNNING);
81 }
82 
84 // vim: et:ts=4:sw=4
void set_to(const value_type &new_state)
Definition: state.h:43
#define STXXL_CHECK_PTHREAD_CALL(expr)
Checks pthread call, if return != 0, throws stxxl::resource_error with "Error in [function] : [pthrea...
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
#define STXXL_END_NAMESPACE
Definition: namespace.h:17