4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <sys/syscall.h>
30 static inline int gettid() { return syscall(SYS_gettid, 0, 0, 0); }
32 // The thread does not autodelete by default.
33 // If autodelete is 1 the thread autodeletes.
34 // If it's synchronous the deletion occurs in join().
35 // If it's asynchronous the deletion occurs in entrypoint.
40 static void* entrypoint(void *parameters);
43 virtual void run() = 0;
45 Thread(int synchronous = 0, int realtime = 0, int autodelete = 0);
48 int cancel(); // end this thread
49 int join(); // join this thread
50 int suspend_thread(); // suspend this thread
51 int continue_thread(); // continue this thread
52 void exit_thread(); // exit this thread
55 int get_cancel_enabled();
56 bool exists() { return tid != ((pthread_t)-1); }
57 bool running() { return exists() && !finished; }
59 int get_synchronous();
60 int set_synchronous(int value);
62 int set_realtime(int value = 1);
64 int set_autodelete(int value);
65 // Return realtime variable
66 // Return 1 if querying the kernel returned a realtime policy
67 static bool calculate_realtime();
68 unsigned long get_tid();
69 static pthread_t get_self() { return pthread_self(); }
70 static void yield() { sched_yield(); }
71 static void dump_threads(FILE *fp=stdout);
74 bool synchronous; // force join() to end
75 bool realtime; // schedule realtime
76 bool autodelete; // autodelete when run() finishes
78 bool cancel_enabled, cancelled;