STXXL  1.4.0
 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 
33 
35 
36 void request_queue_impl_worker::start_thread(void* (* worker)(void*), void* arg, thread_type& t, state<thread_state>& s)
37 {
38  assert(s() == NOT_RUNNING);
39 #if STXXL_STD_THREADS
40  t = new std::thread(worker, arg);
41 #elif STXXL_BOOST_THREADS
42  t = new boost::thread(boost::bind(worker, arg));
43 #else
44  STXXL_CHECK_PTHREAD_CALL(pthread_create(&t, NULL, worker, arg));
45 #endif
46  s.set_to(RUNNING);
47 }
48 
49 void request_queue_impl_worker::stop_thread(thread_type& t, state<thread_state>& s, semaphore& sem)
50 {
51  assert(s() == RUNNING);
52  s.set_to(TERMINATING);
53  sem++;
54 #if STXXL_STD_THREADS
55 #if STXXL_MSVC >= 1700
56  // In the Visual C++ Runtime 2012 and 2013, there is a deadlock bug, which
57  // occurs when threads are joined after main() exits. Apparently, Microsoft
58  // thinks this is not a big issue. It has not been fixed in VC++RT 2013.
59  // https://connect.microsoft.com/VisualStudio/feedback/details/747145
60  //
61  // All STXXL threads are created by singletons, which are global variables
62  // that are deleted after main() exits. The fix applied here it to use
63  // std::thread::native_handle() and access the WINAPI to terminate the
64  // thread directly (after it finished handling its i/o requests).
65 
66  WaitForSingleObject(t->native_handle(), INFINITE);
67  CloseHandle(t->native_handle());
68 #else
69  t->join();
70  delete t;
71 #endif
72  t = NULL;
73 #elif STXXL_BOOST_THREADS
74  t->join();
75  delete t;
76  t = NULL;
77 #else
78  STXXL_CHECK_PTHREAD_CALL(pthread_join(t, NULL));
79 #endif
80  assert(s() == TERMINATED);
81  s.set_to(NOT_RUNNING);
82 }
83 
85 // vim: et:ts=4:sw=4
void set_to(const value_type &new_state)
Definition: state.h:44
#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