19
19
20
20
#include < hpx/condition_variable.hpp>
21
21
#include < hpx/functional.hpp>
22
- #include < hpx/init .hpp>
22
+ #include < hpx/manage_runtime .hpp>
23
23
#include < hpx/modules/runtime_local.hpp>
24
24
#include < hpx/mutex.hpp>
25
25
#include < hpx/thread.hpp>
@@ -66,109 +66,6 @@ char** __argv = *_NSGetArgv();
66
66
67
67
#endif
68
68
69
- class manage_global_runtime_impl
70
- {
71
- public:
72
- manage_global_runtime_impl ()
73
- : running_(false )
74
- , rts_(nullptr )
75
- {
76
- #if defined(HPX_WINDOWS)
77
- hpx::detail::init_winsocket ();
78
- #endif
79
-
80
- std::vector<std::string> const cfg = {
81
- // make sure hpx_main is always executed
82
- " hpx.run_hpx_main!=1" ,
83
- // allow for unknown command line options
84
- " hpx.commandline.allow_unknown!=1" ,
85
- // disable HPX' short options
86
- " hpx.commandline.aliasing!=0" };
87
-
88
- using hpx::placeholders::_1;
89
- using hpx::placeholders::_2;
90
- hpx::function<int (int , char **)> start_function =
91
- hpx::bind (&manage_global_runtime_impl::hpx_main, this , _1, _2);
92
- hpx::init_params init_args;
93
- init_args.cfg = cfg;
94
- init_args.mode = hpx::runtime_mode::default_;
95
-
96
- if (!hpx::start (start_function, __argc, __argv, init_args))
97
- {
98
- // Something went wrong while initializing the runtime.
99
- // This early we can't generate any output, just bail out.
100
- std::abort ();
101
- }
102
-
103
- // Wait for the main HPX thread (hpx_main below) to have started running
104
- std::unique_lock<std::mutex> lk (startup_mtx_);
105
- while (!running_)
106
- startup_cond_.wait (lk);
107
- }
108
-
109
- ~manage_global_runtime_impl ()
110
- {
111
- // notify hpx_main above to tear down the runtime
112
- {
113
- std::lock_guard<hpx::spinlock> lk (mtx_);
114
- rts_ = nullptr ; // reset pointer
115
- }
116
-
117
- cond_.notify_one (); // signal exit
118
-
119
- // wait for the runtime to exit
120
- hpx::stop ();
121
- }
122
-
123
- // registration of external (to HPX) threads
124
- void register_thread (char const * name)
125
- {
126
- hpx::register_thread (rts_, name);
127
- }
128
- void unregister_thread ()
129
- {
130
- hpx::unregister_thread (rts_);
131
- }
132
-
133
- protected:
134
- // Main HPX thread, does nothing but wait for the application to exit
135
- int hpx_main (int , char *[])
136
- {
137
- // Store a pointer to the runtime here.
138
- rts_ = hpx::get_runtime_ptr ();
139
-
140
- // Signal to constructor that thread has started running.
141
- {
142
- std::lock_guard<std::mutex> lk (startup_mtx_);
143
- running_ = true ;
144
- }
145
-
146
- startup_cond_.notify_one ();
147
-
148
- // Here other HPX specific functionality could be invoked...
149
-
150
- // Now, wait for destructor to be called.
151
- {
152
- std::unique_lock<hpx::spinlock> lk (mtx_);
153
- if (rts_ != nullptr )
154
- cond_.wait (lk);
155
- }
156
-
157
- // tell the runtime it's ok to exit
158
- return hpx::finalize ();
159
- }
160
-
161
- private:
162
- hpx::spinlock mtx_;
163
- hpx::condition_variable_any cond_;
164
-
165
- std::mutex startup_mtx_;
166
- std::condition_variable startup_cond_;
167
- bool running_;
168
-
169
- hpx::runtime* rts_;
170
- };
171
-
172
69
// This class demonstrates how to initialize a console instance of HPX
173
70
// (locality 0). In order to create an HPX instance which connects to a running
174
71
// HPX application two changes have to be made:
@@ -189,25 +86,61 @@ class manage_global_runtime_impl
189
86
// sequencing of destructors.
190
87
class manage_global_runtime
191
88
{
192
- manage_global_runtime_impl& get ()
89
+ struct init
90
+ {
91
+ hpx::manage_runtime rts;
92
+
93
+ init ()
94
+ {
95
+ #if defined(HPX_WINDOWS)
96
+ hpx::detail::init_winsocket ();
97
+ #endif
98
+
99
+ hpx::init_params init_args;
100
+ init_args.cfg = {
101
+ // make sure hpx_main is always executed
102
+ " hpx.run_hpx_main!=1" ,
103
+ // allow for unknown command line options
104
+ " hpx.commandline.allow_unknown!=1" ,
105
+ // disable HPX' short options
106
+ " hpx.commandline.aliasing!=0" ,
107
+ };
108
+ init_args.mode = hpx::runtime_mode::default_;
109
+
110
+ if (!rts.start (__argc, __argv, init_args))
111
+ {
112
+ // Something went wrong while initializing the runtime.
113
+ // This early we can't generate any output, just bail out.
114
+ std::abort ();
115
+ }
116
+ }
117
+
118
+ ~init ()
119
+ {
120
+ // Something went wrong while stopping the runtime. Ignore.
121
+ (void ) rts.stop ();
122
+ }
123
+ };
124
+
125
+ hpx::manage_runtime& get ()
193
126
{
194
- static thread_local manage_global_runtime_impl m;
195
- return m;
127
+ static thread_local init m;
128
+ return m. rts ;
196
129
}
197
130
198
131
hpx::execution_base::agent_base& agent =
199
132
hpx::execution_base::detail::get_default_agent ();
200
- manage_global_runtime_impl & m = get();
133
+ hpx::manage_runtime & m = get();
201
134
202
135
public:
203
136
void register_thread (char const * name)
204
137
{
205
- m. register_thread ( name);
138
+ hpx::register_thread (m. get_runtime_ptr (), name);
206
139
}
207
140
208
141
void unregister_thread ()
209
142
{
210
- m. unregister_thread ( );
143
+ hpx::unregister_thread (m. get_runtime_ptr () );
211
144
}
212
145
};
213
146
0 commit comments