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 
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
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:71
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.
Fawkes library namespace.
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:45
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:43
void push_front_locked(Thread *thread)
Add thread to the front with lock protection.
List of threads.
Definition: thread_list.h:55
ThreadList & operator=(const ThreadList &tl)
Assignment operator.
ThreadList(const char *tlname="")
Constructor.
Definition: thread_list.cpp:92
Base class for exceptions in Fawkes.
Definition: exception.h:35
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:43
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:49
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:31
void cancel()
Cancel threads.
void remove(Thread *thread)
Remove with lock protection.
Thread finalizer interface.