Fawkes API Fawkes Development Version
aspect_provider.cpp
1
2/***************************************************************************
3 * aspect_provider.cpp - Fawkes Aspect Provider initializer/finalizer
4 *
5 * Created: Thu Nov 25 12:20:43 2010 (Thanksgiving, Pittsburgh)
6 * Copyright 2006-2010 Tim Niemueller [www.niemueller.de]
7 *
8 ****************************************************************************/
9
10/* This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version. A runtime exception applies to
14 * this software (see LICENSE.GPL_WRE file mentioned below for details).
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Library General Public License for more details.
20 *
21 * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22 */
23
24#include <aspect/aspect_provider.h>
25#include <aspect/inifins/aspect_provider.h>
26#include <aspect/manager.h>
27
28namespace fawkes {
29
30/** @class AspectProviderAspectIniFin <aspect/inifins/aspect_provider.h>
31 * Initializer/finalizer for the AspectProviderAspect.
32 * This initializer/finalizer will register the AspectIniFin instance with
33 * the main aspect manager on init, and unregister it on finalization. it
34 * will deny unloading if there are still threads using the provided aspect.
35 * @author Tim Niemueller
36 */
37
38/** Constructor.
39 * @param manager aspect manager to register new aspects to
40 */
42: AspectIniFin("AspectProviderAspect")
43{
44 aspect_manager_ = manager;
45}
46
47void
49{
50 AspectProviderAspect *provider_thread;
51 provider_thread = dynamic_cast<AspectProviderAspect *>(thread);
52
53 if (provider_thread == NULL) {
54 throw CannotInitializeThreadException("Thread '%s' claims to have the "
55 "AspectProviderAspect, but RTTI says it "
56 "has not. ",
57 thread->name());
58 }
59
60 const std::list<AspectIniFin *> & aspects = provider_thread->aspect_provider_aspects();
61 std::list<AspectIniFin *>::const_iterator a;
62 for (a = aspects.begin(); a != aspects.end(); ++a) {
63 aspect_manager_->register_inifin(*a);
64 }
65}
66
67bool
69{
71 p_thr = dynamic_cast<AspectProviderAspect *>(thread);
72
73 if (p_thr == NULL)
74 return true;
75
76 const std::list<AspectIniFin *> & aspects = p_thr->aspect_provider_aspects();
77 std::list<AspectIniFin *>::const_iterator a;
78 for (a = aspects.begin(); a != aspects.end(); ++a) {
79 if (aspect_manager_->has_threads_for_aspect((*a)->get_aspect_name())) {
80 return false;
81 }
82 }
83
84 return true;
85}
86
87void
89{
90 AspectProviderAspect *provider_thread;
91 provider_thread = dynamic_cast<AspectProviderAspect *>(thread);
92
93 if (provider_thread == NULL) {
94 throw CannotFinalizeThreadException("Thread '%s' claims to have the "
95 "AspectProviderAspect, but RTTI says it "
96 "has not. ",
97 thread->name());
98 }
99
100 const std::list<AspectIniFin *> & aspects = provider_thread->aspect_provider_aspects();
101 std::list<AspectIniFin *>::const_iterator a;
102 for (a = aspects.begin(); a != aspects.end(); ++a) {
103 aspect_manager_->unregister_inifin(*a);
104 }
105}
106
107} // end namespace fawkes
Aspect initializer/finalizer base class.
Definition: inifin.h:34
Aspect and aspect initializer/finalizer manager.
Definition: manager.h:58
void register_inifin(AspectIniFin *inifin)
Register initializer/finalizer.
Definition: manager.cpp:77
void unregister_inifin(AspectIniFin *inifin)
Unregister initializer/finalizer.
Definition: manager.cpp:89
bool has_threads_for_aspect(const char *aspect_name)
Check if threads for a particular aspect still exist.
Definition: manager.cpp:109
virtual void finalize(Thread *thread)
Finalize thread.
AspectProviderAspectIniFin(AspectManager *manager)
Constructor.
virtual void init(Thread *thread)
Initialize thread.
virtual bool prepare_finalize(Thread *thread)
Default finalize preparation.
Thread aspect provide a new aspect.
const std::list< AspectIniFin * > & aspect_provider_aspects() const
Get name of the provided aspect.
Thread cannot be finalized.
Thread class encapsulation of pthreads.
Definition: thread.h:46
const char * name() const
Get name of thread.
Definition: thread.h:100
Fawkes library namespace.