Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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_initializer.h>
30 #include <core/threading/thread_finalizer.h>
31 #include <core/utils/lock_list.h>
32 
33 #include <utility>
34 #include <string>
35 
36 namespace fawkes {
37 
38 
39 class ThreadList;
40 class Mutex;
41 class Barrier;
42 class InterruptibleBarrier;
43 
45 {
46  public:
47  ThreadListSealedException(const char *operation);
48 };
49 
51 {
52  public:
53  ThreadListNotSealedException(const char *format, ...);
54 };
55 
56 
57 class ThreadList : private LockList<Thread *>
58 {
59  public:
60  ThreadList(const char *tlname = "");
61  ThreadList(bool maintain_barrier, const char *tlname = "");
62  ThreadList(const ThreadList &tl);
63  ~ThreadList();
64 
65  const char * name();
66  void set_name(const char *format, ...);
67 
68  void seal();
69  bool sealed();
70 
71  void init(ThreadInitializer *initializer, ThreadFinalizer *finalizer);
72  bool prepare_finalize(ThreadFinalizer *finalizer);
73  void finalize(ThreadFinalizer *finalizer);
74  void cancel_finalize();
75  void set_prepfin_hold(bool hold);
76 
77  void wakeup();
78  void wakeup(Barrier *barrier);
79  void wakeup_unlocked();
80  void wakeup_unlocked(Barrier *barrier);
81  void wakeup_and_wait(unsigned int timeout_sec = 0,
82  unsigned int timeout_nanosec = 0);
83  void start();
84  void stop();
85  void cancel();
86  void join();
87 
88  void try_recover(std::list<std::string> &recovered_threads);
89  void set_maintain_barrier(bool maintain_barrier);
90 
91  void force_stop(ThreadFinalizer *finalizer);
92 
93  void push_front(Thread *thread);
94  void push_front_locked(Thread *thread);
95  void push_back(Thread *thread);
96  void push_back_locked(Thread *thread);
97  void clear();
98  void pop_back();
99  void pop_front();
100  ThreadList::iterator erase(iterator pos);
101 
102  void remove(Thread *thread);
103  void remove_locked(Thread *thread);
104 
115 
116  ThreadList & operator= (const ThreadList & tl);
117 
118 
119  private:
120  void notify_of_failed_init();
121  void update_barrier();
122 
123  private:
124  char *__name;
125  bool __sealed;
126  Mutex *__finalize_mutex;
127  InterruptibleBarrier *__wnw_barrier;
128 
129  std::list<std::pair<InterruptibleBarrier *, ThreadList> > __wnw_bad_barriers;
130  std::list<std::pair<InterruptibleBarrier *, ThreadList> >::iterator __wnw_bbit;
131 };
132 
133 
134 } // end namespace fawkes
135 
136 #endif
bool sealed()
Check if list is sealed.
ThreadListSealedException(const char *operation)
Constructor.
Definition: thread_list.cpp:53
ThreadListNotSealedException(const char *format,...)
Constructor.
Definition: thread_list.cpp:72
const char * name()
Name of the thread list.
void clear()
Clear the list.
void start()
Start threads.
void cancel_finalize()
Cancel finalization on all threads.
void push_back_locked(Thread *thread)
Add thread to the end with lock protection.
void seal()
Seal the list.
void set_prepfin_hold(bool hold)
Set prepfin hold on all threads.
void force_stop(ThreadFinalizer *finalizer)
Force stop of all threads.
void join()
Join threads.
Thread class encapsulation of pthreads.
Definition: thread.h:42
void pop_back()
Remove last element.
bool prepare_finalize(ThreadFinalizer *finalizer)
Prepare finalize.
A barrier is a synchronization tool which blocks until a given number of threads have reached the bar...
Thread list sealed exception.
Definition: thread_list.h:44
void push_front_locked(Thread *thread)
Add thread to the front with lock protection.
List of threads.
Definition: thread_list.h:57
ThreadList & operator=(const ThreadList &tl)
Assignment operator.
ThreadList(const char *tlname="")
Constructor.
Definition: thread_list.cpp:94
Base class for exceptions in Fawkes.
Definition: exception.h:36
void remove_locked(Thread *thread)
Remove with lock protection.
ThreadList::iterator erase(iterator pos)
Erase element at given position.
void init(ThreadInitializer *initializer, ThreadFinalizer *finalizer)
Initialize threads.
void stop()
Stop threads.
Thread initializer interface.
List with a lock.
Definition: thread.h:40
void wakeup()
Wakeup all threads in list.
void finalize(ThreadFinalizer *finalizer)
Finalize Threads.
void pop_front()
Remove first element.
~ThreadList()
Destructor.
void push_back(Thread *thread)
Add thread to the end.
Thread list not sealed exception.
Definition: thread_list.h:50
void wakeup_unlocked()
Wakeup all threads in list.
void set_name(const char *format,...)
Set name of thread.
void try_recover(std::list< std::string > &recovered_threads)
Check if any of the bad barriers recovered.
void set_maintain_barrier(bool maintain_barrier)
Set if this thread list should maintain a barrier.
Mutex mutual exclusion lock.
Definition: mutex.h:32
void push_front(Thread *thread)
Add thread to the front.
void wakeup_and_wait(unsigned int timeout_sec=0, unsigned int timeout_nanosec=0)
Wakeup threads and wait for them to finish.
A barrier is a synchronization tool which blocks until a given number of threads have reached the bar...
Definition: barrier.h:32
void cancel()
Cancel threads.
Thread finalizer interface.