Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita4a2825

Browse files
committed
destroy binded actors at thread exit
1 parent45a305e commita4a2825

File tree

10 files changed

+230
-231
lines changed

10 files changed

+230
-231
lines changed

‎lib/acto.cpp

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
namespaceacto {
55

6-
static std::atomic_longstartup_counter(0);
7-
86
actor_ref::actor_ref(
97
core::object_t*const an_object,constbool acquire)noexcept
108
: m_object(an_object) {
@@ -117,14 +115,6 @@ void destroy(actor_ref& object) {
117115
}
118116
}
119117

120-
voidfinalize_thread() {
121-
core::runtime_t::instance()->destroy_thread_binding();
122-
}
123-
124-
voidinitialize_thread() {
125-
core::runtime_t::instance()->create_thread_binding();
126-
}
127-
128118
voidjoin(actor_ref& obj) {
129119
obj.join();
130120
}
@@ -134,24 +124,16 @@ void process_messages() {
134124
}
135125

136126
voidshutdown() {
137-
if (startup_counter >0 && (--startup_counter ==0)) {
138-
core::runtime_t::instance()->shutdown();
139-
}
140-
}
141-
142-
voidstartup() {
143-
if (++startup_counter ==1) {
144-
core::runtime_t::instance()->startup();
145-
}
127+
core::runtime_t::instance()->shutdown();
146128
}
147129

148130
namespacecore {
149131

150-
object_t::object_t(constuint32_t options, std::unique_ptr<actor> body)
132+
object_t::object_t(constactor_thread thread_opt, std::unique_ptr<actor> body)
151133
: impl(body.release())
152134
, references(1)
153-
, binded(bool(options & acto::aoBindToThread))
154-
, exclusive(bool(options & acto::aoExclusive))
135+
, binded(thread_opt == actor_thread::bind)
136+
, exclusive(thread_opt == actor_thread::exclusive)
155137
, deleting(false)
156138
, scheduled(false)
157139
, unimpl(false) {
@@ -184,5 +166,11 @@ msg_t::~msg_t() {
184166
}
185167
}
186168

169+
object_t*make_instance(
170+
actor_ref context,const actor_thread opt, std::unique_ptr<actor> body) {
171+
returnruntime_t::instance()->make_instance(
172+
std::move(context), opt,std::move(body));
173+
}
174+
187175
}// namespace core
188176
}// namespace acto

‎lib/acto.h

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@ namespace acto {
1717

1818
classactor;
1919

20-
/// Use dedicated thread for an actor.
21-
staticconstexpruint32_t aoExclusive =0x01;
20+
enumclassactor_thread {
21+
/// Use shared pool of threads for the actor.
22+
shared,
2223

23-
/// Привязать актера к текущему системному потоку.
24-
/// Не имеет эффекта, если используется в контексте потока,
25-
/// созданного самой библиотекой.
26-
staticconstexpruint32_t aoBindToThread =0x02;
24+
/// Use dedicated thread for the actor.
25+
exclusive,
26+
27+
/// Bind actor to the current thread.
28+
/// The option will be ignored if used inside the thread created by the
29+
/// library.
30+
bind,
31+
};
2732

2833
namespacecore {
2934

@@ -64,7 +69,7 @@ struct object_t : public generics::intrusive_t<object_t> {
6469
uint32_t unimpl :1;
6570

6671
public:
67-
object_t(constuint32_t options, std::unique_ptr<actor> body);
72+
object_t(constactor_thread thread_opt, std::unique_ptr<actor> body);
6873

6974
/// Pushes a message into the mailbox.
7075
voidenqueue(std::unique_ptr<msg_t> msg)noexcept;
@@ -171,10 +176,6 @@ inline void sleep(const D duration) {
171176
std::this_thread::sleep_for(duration);
172177
}
173178

174-
inlinevoidyield() {
175-
std::this_thread::yield();
176-
}
177-
178179
}// namespace core
179180

180181
/**
@@ -410,76 +411,74 @@ class actor {
410411
bool terminating_{false};
411412
};
412413

413-
/** Destroys the given object.*/
414-
ACTO_APIvoiddestroy(actor_ref& object);
415-
416-
//
417-
ACTO_APIvoidfinalize_thread();
418-
419-
// Включить в текущем потоке возможность взаимодействия
420-
// с ядром библиотеки acto
421-
ACTO_APIvoidinitialize_thread();
414+
/**
415+
* Destroys the given object.
416+
*/
417+
voiddestroy(actor_ref& object);
422418

423-
/** Waits until the actor will finish.*/
424-
ACTO_APIvoidjoin(actor_ref& obj);
419+
/**
420+
* Waits until the actor will finish.
421+
*/
422+
voidjoin(actor_ref& obj);
425423

426424
/**
427425
* Processes all messages for objects
428426
* binded to the current thread (with aoBindToThread option).
429427
*/
430-
ACTO_APIvoidprocess_messages();
431-
432-
/** Library shutdown.*/
433-
ACTO_APIvoidshutdown();
428+
voidprocess_messages();
434429

435-
/** Library initialization.*/
436-
ACTO_APIvoidstartup();
430+
/**
431+
* Stops all actors.
432+
*/
433+
voidshutdown();
437434

438435
namespacecore {
439436

440-
object_t*make_instance(
441-
actor_ref context,constuint32_t options, std::unique_ptr<actor> body);
437+
object_t*make_instance(actor_ref context,
438+
const actor_thread thread_opt,
439+
std::unique_ptr<actor> body);
442440

443441
}// namespace core
444442

445443
namespacedetail {
446444

447445
template<typename Impl>
448-
inline core::object_t*make_instance(
449-
actor_ref context,constuint32_t options, std::unique_ptr<Impl> impl) {
446+
inline core::object_t*make_instance(actor_ref context,
447+
const actor_thread thread_opt,
448+
std::unique_ptr<Impl> impl) {
450449
static_assert(std::is_base_of<::acto::actor, Impl>::value,
451450
"implementation should be derived from the acto::actor class");
452451

453-
returncore::make_instance(std::move(context),options,std::move(impl));
452+
returncore::make_instance(std::move(context),thread_opt,std::move(impl));
454453
}
455454

456455
}// namespace detail
457456

458457
template<typename T,typename... P>
459458
inline actor_refspawn(P&&... p) {
460-
returnactor_ref(detail::make_instance<T>(actor_ref(),0,
459+
returnactor_ref(detail::make_instance<T>(actor_ref(),actor_thread::shared,
461460
std::make_unique<T>(std::forward<P>(p)...)),
462461
false);
463462
}
464463

465464
template<typename T>
466465
inline actor_refspawn(actor_ref context) {
467-
returnactor_ref(
468-
detail::make_instance<T>(std::move(context),0, std::make_unique<T>()),
466+
returnactor_ref(detail::make_instance<T>(std::move(context),
467+
actor_thread::shared, std::make_unique<T>()),
469468
false);
470469
}
471470

472471
template<typename T>
473-
inline actor_refspawn(constuint32_t options) {
472+
inline actor_refspawn(constactor_thread thread_opt) {
474473
returnactor_ref(
475-
detail::make_instance<T>(actor_ref(),options, std::make_unique<T>()),
474+
detail::make_instance<T>(actor_ref(),thread_opt, std::make_unique<T>()),
476475
false);
477476
}
478477

479478
template<typename T>
480-
inline actor_refspawn(actor_ref context,constuint32_t options) {
479+
inline actor_refspawn(actor_ref context,constactor_thread thread_opt) {
481480
returnactor_ref(detail::make_instance<T>(
482-
std::move(context),options, std::make_unique<T>()),
481+
std::move(context),thread_opt, std::make_unique<T>()),
483482
false);
484483
}
485484

‎lib/platform.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,4 @@
1717
# if !defined(_MT)
1818
# error "Multithreaded mode not defined. Use /MD or /MT compiler options."
1919
# endif
20-
21-
# ifdefACTO_EXPORT
22-
# defineACTO_API __declspec(dllexport)
23-
# elifACTO_IMPORT
24-
# defineACTO_API __declspec(dllimport)
25-
# else
26-
# defineACTO_API
27-
# endif
28-
#else
29-
# defineACTO_API
3020
#endif

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp