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
36namespace fawkes {
37
38class ThreadList;
39class Mutex;
40class Barrier;
41class InterruptibleBarrier;
42
44{
45public:
46 ThreadListSealedException(const char *operation);
47};
48
50{
51public:
52 ThreadListNotSealedException(const char *format, ...);
53};
54
55class ThreadList : private LockList<Thread *>
56{
57public:
58 ThreadList(const char *tlname = "");
59 ThreadList(bool maintain_barrier, const char *tlname = "");
60 ThreadList(const ThreadList &tl);
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
102 using LockList<Thread *>::begin;
103 using LockList<Thread *>::end;
104 using LockList<Thread *>::iterator;
105 using LockList<Thread *>::lock;
106 using LockList<Thread *>::try_lock;
107 using LockList<Thread *>::unlock;
108 using LockList<Thread *>::size;
109 using LockList<Thread *>::empty;
110 using LockList<Thread *>::front;
111 using LockList<Thread *>::back;
112
113 ThreadList &operator=(const ThreadList &tl);
114
115private:
116 void notify_of_failed_init();
117 void update_barrier();
118
119private:
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
A barrier is a synchronization tool which blocks until a given number of threads have reached the bar...
Definition: barrier.h:32
Base class for exceptions in Fawkes.
Definition: exception.h:36
A barrier is a synchronization tool which blocks until a given number of threads have reached the bar...
List with a lock.
Definition: lock_list.h:45
virtual void unlock() const
Unlock list.
Definition: lock_list.h:138
virtual void lock() const
Lock list.
Definition: lock_list.h:124
virtual bool try_lock() const
Try to lock list.
Definition: lock_list.h:131
Mutex mutual exclusion lock.
Definition: mutex.h:33
Thread finalizer interface.
Thread initializer interface.
Thread list not sealed exception.
Definition: thread_list.h:50
ThreadListNotSealedException(const char *format,...)
Constructor.
Definition: thread_list.cpp:71
Thread list sealed exception.
Definition: thread_list.h:44
ThreadListSealedException(const char *operation)
Constructor.
Definition: thread_list.cpp:53
List of threads.
Definition: thread_list.h:56
void wakeup_unlocked()
Wakeup all threads in list.
void remove_locked(Thread *thread)
Remove with lock protection.
ThreadList::iterator erase(iterator pos)
Erase element at given position.
void push_front_locked(Thread *thread)
Add thread to the front with lock protection.
void clear()
Clear the list.
bool prepare_finalize(ThreadFinalizer *finalizer)
Prepare finalize.
void set_prepfin_hold(bool hold)
Set prepfin hold on all threads.
void init(ThreadInitializer *initializer, ThreadFinalizer *finalizer)
Initialize threads.
ThreadList & operator=(const ThreadList &tl)
Assignment operator.
void finalize(ThreadFinalizer *finalizer)
Finalize Threads.
void set_name(const char *format,...)
Set name of thread.
void cancel()
Cancel threads.
~ThreadList()
Destructor.
void stop()
Stop threads.
void wakeup()
Wakeup all threads in list.
void pop_front()
Remove first element.
void try_recover(std::list< std::string > &recovered_threads)
Check if any of the bad barriers recovered.
void push_back_locked(Thread *thread)
Add thread to the end with lock protection.
void remove(Thread *thread)
Remove with lock protection.
const char * name()
Name of the thread list.
void force_stop(ThreadFinalizer *finalizer)
Force stop of all threads.
void cancel_finalize()
Cancel finalization on all threads.
void pop_back()
Remove last element.
void push_front(Thread *thread)
Add thread to the front.
ThreadList(const char *tlname="")
Constructor.
Definition: thread_list.cpp:92
void set_maintain_barrier(bool maintain_barrier)
Set if this thread list should maintain a barrier.
bool sealed()
Check if list is sealed.
void join()
Join threads.
void start()
Start threads.
void seal()
Seal the list.
void wakeup_and_wait(unsigned int timeout_sec=0, unsigned int timeout_nanosec=0)
Wakeup threads and wait for them to finish.
void push_back(Thread *thread)
Add thread to the end.
Thread class encapsulation of pthreads.
Definition: thread.h:46
Fawkes library namespace.