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/inifins/aspect_provider.h>
25 #include <aspect/aspect_provider.h>
26 #include <aspect/manager.h>
27 
28 namespace fawkes {
29 #if 0 /* just to make Emacs auto-indent happy */
30 }
31 #endif
32 
33 /** @class AspectProviderAspectIniFin <aspect/inifins/aspect_provider.h>
34  * Initializer/finalizer for the AspectProviderAspect.
35  * This initializer/finalizer will register the AspectIniFin instance with
36  * the main aspect manager on init, and unregister it on finalization. it
37  * will deny unloading if there are still threads using the provided aspect.
38  * @author Tim Niemueller
39  */
40 
41 /** Constructor.
42  * @param manager aspect manager to register new aspects to
43  */
45  : AspectIniFin("AspectProviderAspect")
46 {
47  __aspect_manager = manager;
48 }
49 
50 
51 void
53 {
54  AspectProviderAspect *provider_thread;
55  provider_thread = dynamic_cast<AspectProviderAspect *>(thread);
56 
57  if (provider_thread == NULL) {
58  throw CannotInitializeThreadException("Thread '%s' claims to have the "
59  "AspectProviderAspect, but RTTI says it "
60  "has not. ", thread->name());
61  }
62 
63  const std::list<AspectIniFin *> &aspects =
64  provider_thread->aspect_provider_aspects();
65  std::list<AspectIniFin *>::const_iterator a;
66  for (a = aspects.begin(); a != aspects.end(); ++a) {
67  __aspect_manager->register_inifin(*a);
68  }
69 }
70 
71 
72 bool
74 {
75  AspectProviderAspect *p_thr;
76  p_thr = dynamic_cast<AspectProviderAspect *>(thread);
77 
78  if (p_thr == NULL) return true;
79 
80  const std::list<AspectIniFin *> &aspects = p_thr->aspect_provider_aspects();
81  std::list<AspectIniFin *>::const_iterator a;
82  for (a = aspects.begin(); a != aspects.end(); ++a) {
83  if (__aspect_manager->has_threads_for_aspect((*a)->get_aspect_name())) {
84  return false;
85  }
86  }
87 
88  return true;
89 }
90 
91 
92 void
94 {
95  AspectProviderAspect *provider_thread;
96  provider_thread = dynamic_cast<AspectProviderAspect *>(thread);
97 
98  if (provider_thread == NULL) {
99  throw CannotFinalizeThreadException("Thread '%s' claims to have the "
100  "AspectProviderAspect, but RTTI says it "
101  "has not. ", thread->name());
102  }
103 
104  const std::list<AspectIniFin *> &aspects =
105  provider_thread->aspect_provider_aspects();
106  std::list<AspectIniFin *>::const_iterator a;
107  for (a = aspects.begin(); a != aspects.end(); ++a) {
108  __aspect_manager->unregister_inifin(*a);
109  }
110 }
111 
112 
113 } // end namespace fawkes
AspectProviderAspectIniFin(AspectManager *manager)
Constructor.
Fawkes library namespace.
virtual void finalize(Thread *thread)
Finalize thread.
Thread class encapsulation of pthreads.
Definition: thread.h:42
void register_inifin(AspectIniFin *inifin)
Register initializer/finalizer.
Definition: manager.cpp:78
void unregister_inifin(AspectIniFin *inifin)
Unregister initializer/finalizer.
Definition: manager.cpp:91
Thread cannot be initialized.
virtual void init(Thread *thread)
Initialize thread.
Aspect and aspect initializer/finalizer manager.
Definition: manager.h:59
const char * name() const
Get name of thread.
Definition: thread.h:95
const std::list< AspectIniFin * > & aspect_provider_aspects() const
Get name of the provided aspect.
bool has_threads_for_aspect(const char *aspect_name)
Check if threads for a particular aspect still exist.
Definition: manager.cpp:111
Thread aspect provide a new aspect.
Thread cannot be finalized.
virtual bool prepare_finalize(Thread *thread)
Default finalize preparation.
Aspect initializer/finalizer base class.
Definition: inifin.h:36