Fawkes API  Fawkes Development Version
thread_list.h
1 
2 /***************************************************************************
3  * thread_list.h - Thread list
4  *
5  * Created: Tue Oct 31 18:04:31 2006
6  * Copyright 2006-2009 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 #ifndef _CORE_THREADING_THREAD_LIST_H_
25 #define _CORE_THREADING_THREAD_LIST_H_
26 
27 #include <core/exception.h>
28 #include <core/threading/thread.h>
29 #include <core/threading/thread_finalizer.h>
30 #include <core/threading/thread_initializer.h>
31 #include <core/utils/lock_list.h>
32 
33 #include <string>
34 #include <utility>
35 
36 namespace fawkes {
37 
38 class ThreadList;
39 class Mutex;
40 class Barrier;
41 class InterruptibleBarrier;
42 
43 class ThreadListSealedException : public Exception
44 {
45 public:
46  ThreadListSealedException(const char *operation);
47 };
48 
50 {
51 public:
52  ThreadListNotSealedException(const char *format, ...);
53 };
54 
55 class ThreadList : private LockList<Thread *>
56 {
57 public:
58  ThreadList(const char *tlname = "");
59  ThreadList(bool maintain_barrier, const char *tlname = "");
60  ThreadList(const ThreadList &tl);
61  ~ThreadList();
62 
63  const char *name();
64  void set_name(const char *format, ...);
65 
66  void seal();
67  bool sealed();
68 
69  void init(ThreadInitializer *initializer, ThreadFinalizer *finalizer);
70  bool prepare_finalize(ThreadFinalizer *finalizer);
71  void finalize(ThreadFinalizer *finalizer);
72  void cancel_finalize();
73  void set_prepfin_hold(bool hold);
74 
75  void wakeup();
76  void wakeup(Barrier *barrier);
77  void wakeup_unlocked();
78  void wakeup_unlocked(Barrier *barrier);
79  void wakeup_and_wait(unsigned int timeout_sec = 0, unsigned int timeout_nanosec = 0);
80  void start();
81  void stop();
82  void cancel();
83  void join();
84 
85  void try_recover(std::list<std::string> &recovered_threads);
86  void set_maintain_barrier(bool maintain_barrier);
87 
88  void force_stop(ThreadFinalizer *finalizer);
89 
90  void push_front(Thread *thread);
91  void push_front_locked(Thread *thread);
92  void push_back(Thread *thread);
93  void push_back_locked(Thread *thread);
94  void clear();
95  void pop_back();
96  void pop_front();
97  ThreadList::iterator erase(iterator pos);
98 
99  void remove(Thread *thread);
100  void remove_locked(Thread *thread);
101 
112 
113  ThreadList &operator=(const ThreadList &tl);
114 
115 private:
116  void notify_of_failed_init();
117  void update_barrier();
118 
119 private:
120  char * name_;
121  bool sealed_;
122  Mutex * finalize_mutex_;
123  InterruptibleBarrier *wnw_barrier_;
124 
125  std::list<std::pair<InterruptibleBarrier *, ThreadList>> wnw_bad_barriers_;
126  std::list<std::pair<InterruptibleBarrier *, ThreadList>>::iterator wnw_bbit_;
127 };
128 
129 } // end namespace fawkes
130 
131 #endif
fawkes::ThreadList::sealed
bool sealed()
Check if list is sealed.
Definition: thread_list.cpp:725
fawkes::ThreadList::force_stop
void force_stop(ThreadFinalizer *finalizer)
Force stop of all threads.
Definition: thread_list.cpp:659
fawkes::ThreadList::wakeup
void wakeup()
Wakeup all threads in list.
Definition: thread_list.cpp:163
fawkes::ThreadList::clear
void clear()
Clear the list.
Definition: thread_list.cpp:811
fawkes::ThreadList::set_name
void set_name(const char *format,...)
Set name of thread.
Definition: thread_list.cpp:704
fawkes::ThreadList::remove
void remove(Thread *thread)
Remove with lock protection.
Definition: thread_list.cpp:825
fawkes::Mutex
Definition: mutex.h:36
fawkes::ThreadList::set_maintain_barrier
void set_maintain_barrier(bool maintain_barrier)
Set if this thread list should maintain a barrier.
Definition: thread_list.cpp:309
fawkes::ThreadList::~ThreadList
~ThreadList()
Destructor.
Definition: thread_list.cpp:136
fawkes::ThreadList::erase
ThreadList::iterator erase(iterator pos)
Erase element at given position.
Definition: thread_list.cpp:879
fawkes::ThreadList::push_back
void push_back(Thread *thread)
Add thread to the end.
Definition: thread_list.cpp:777
fawkes::ThreadListSealedException::ThreadListSealedException
ThreadListSealedException(const char *operation)
Constructor.
Definition: thread_list.cpp:57
fawkes::ThreadListNotSealedException::ThreadListNotSealedException
ThreadListNotSealedException(const char *format,...)
Constructor.
Definition: thread_list.cpp:75
fawkes::LockList
Definition: thread.h:42
fawkes::ThreadListNotSealedException
Definition: thread_list.h:53
fawkes::ThreadList::set_prepfin_hold
void set_prepfin_hold(bool hold)
Set prepfin hold on all threads.
Definition: thread_list.cpp:636
fawkes::ThreadList::cancel
void cancel()
Cancel threads.
Definition: thread_list.cpp:476
fawkes::ThreadList::push_front_locked
void push_front_locked(Thread *thread)
Add thread to the front with lock protection.
Definition: thread_list.cpp:761
fawkes::ThreadList::name
const char * name()
Name of the thread list.
Definition: thread_list.cpp:694
fawkes
fawkes::ThreadList::operator=
ThreadList & operator=(const ThreadList &tl)
Assignment operator.
Definition: thread_list.cpp:148
fawkes::ThreadList::finalize
void finalize(ThreadFinalizer *finalizer)
Finalize Threads.
Definition: thread_list.cpp:584
fawkes::ThreadList::pop_front
void pop_front()
Remove first element.
Definition: thread_list.cpp:852
fawkes::ThreadList::prepare_finalize
bool prepare_finalize(ThreadFinalizer *finalizer)
Prepare finalize.
Definition: thread_list.cpp:536
fawkes::ThreadList::remove_locked
void remove_locked(Thread *thread)
Remove with lock protection.
Definition: thread_list.cpp:839
fawkes::ThreadList::join
void join()
Join threads.
Definition: thread_list.cpp:501
fawkes::ThreadFinalizer
Definition: thread_finalizer.h:44
fawkes::Thread
Definition: thread.h:44
fawkes::ThreadList::pop_back
void pop_back()
Remove last element.
Definition: thread_list.cpp:864
fawkes::ThreadList::stop
void stop()
Stop threads.
Definition: thread_list.cpp:515
fawkes::ThreadList::init
void init(ThreadInitializer *initializer, ThreadFinalizer *finalizer)
Initialize threads.
Definition: thread_list.cpp:378
fawkes::Barrier
Definition: barrier.h:35
fawkes::ThreadInitializer
Definition: thread_initializer.h:45
fawkes::ThreadList::try_recover
void try_recover(std::list< std::string > &recovered_threads)
Check if any of the bad barriers recovered.
Definition: thread_list.cpp:331
fawkes::ThreadList::start
void start()
Start threads.
Definition: thread_list.cpp:451
fawkes::ThreadList::push_front
void push_front(Thread *thread)
Add thread to the front.
Definition: thread_list.cpp:742
fawkes::ThreadList::ThreadList
ThreadList(const char *tlname="")
Constructor.
Definition: thread_list.cpp:96
fawkes::ThreadList::push_back_locked
void push_back_locked(Thread *thread)
Add thread to the end with lock protection.
Definition: thread_list.cpp:796
fawkes::InterruptibleBarrier
Definition: interruptible_barrier.h:39
fawkes::ThreadList
Definition: thread_list.h:59
fawkes::ThreadList::wakeup_unlocked
void wakeup_unlocked()
Wakeup all threads in list.
Definition: thread_list.cpp:177
fawkes::ThreadList::cancel_finalize
void cancel_finalize()
Cancel finalization on all threads.
Definition: thread_list.cpp:619
fawkes::ThreadList::seal
void seal()
Seal the list.
Definition: thread_list.cpp:732
fawkes::ThreadList::wakeup_and_wait
void wakeup_and_wait(unsigned int timeout_sec=0, unsigned int timeout_nanosec=0)
Wakeup threads and wait for them to finish.
Definition: thread_list.cpp:245
fawkes::Exception
Definition: exception.h:39