summaryrefslogtreecommitdiff
path: root/libs/pbd3/pbd/thread.h
blob: c1d5c3c78df5c38fea0459f2d841dba355be013b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
    Copyright (C) 1998-99 Paul Barton-Davis 

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    $Id$
*/

#ifndef __qm_thread_h__
#define __qm_thread_h__

#include <pthread.h>

/* A generic base class for Quasimodo objects requiring their own
   thread to do work.
*/

class QMThread 

{
 public:
	QMThread (const char *name, 
		  void *(start)(void *), void *, 
		  bool realtime = false, int rt_priority = 10);

	virtual ~QMThread();

	int   run ();
	void  poke ();
	void  pause ();
	void  stop ();
	void  *wait ();

	/* This doesn't guarantee anything about the state of
	   the thread, but if you do things the right way, and
	   make sure that the do_work() routine checks 
	   work_no_more() at the right times, and that the
	   thread is awake, then calling this will cause
	   the thread to exit fairly quickly.
	*/

	void  halt() { _must_exit = true ; }

	void  exit (void *status);
	pthread_t thread_id() { return _thread; }

	bool thread_ok () { return _have_thread; }
	bool thread_active() { return _thread_active; }

	bool thread_running () {
		/* XXX not atomic */
		return _running && _thread_active;
	}

	bool thread_waiting () { return _thread_waiting; }

	static void try_to_kill_all_threads() {
		all_threads_must_die = true;
	}

 protected:
	void *main ();

	bool  work_no_more () { return (!_running || _must_exit || all_threads_must_die); }

	bool  myself () { 
		return pthread_equal (_thread, pthread_self());
	}

	void suspend() {
		_running = false;
	}

	void lock (pthread_mutex_t *lock) {
		pthread_mutex_lock (lock);
	}
	
	void unlock (pthread_mutex_t *lock) {
		pthread_mutex_unlock (lock);
	}

	virtual void *do_work () = 0;

  private:
	const char *_name;
	bool _must_exit;
	bool _running;
	bool _thread_active;
	bool _thread_waiting;
	bool _have_thread;

	size_t work_cnt;

	pthread_mutex_t status_lock;
	pthread_cond_t wake_up; /* protected by status_lock */
	pthread_cond_t asleep;  /* protected by status_lock */
	pthread_cond_t running; /* protected by status_lock */
	pthread_cond_t exited;  /* protected by status_lock */
	pthread_t _thread;
	
	void lock () { 
		pthread_mutex_lock (&status_lock);
	}

	void unlock () { 
		pthread_mutex_unlock (&status_lock);
	}

	static bool all_threads_must_die;
	
	static void signal_catcher (int sig);
	void setup_signals ();
};

#endif // __qm_thread_h__