summaryrefslogtreecommitdiff
path: root/Source/DirectFB/lib/direct/thread.h
blob: 18205c4047fd6da7bfe143bb727b5ea26b1f8996 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
   (c) Copyright 2001-2009  The world wide DirectFB Open Source Community (directfb.org)
   (c) Copyright 2000-2004  Convergence (integrated media) GmbH

   All rights reserved.

   Written by Denis Oliver Kropp <dok@directfb.org>,
              Andreas Hundt <andi@fischlustig.de>,
              Sven Neumann <neo@directfb.org>,
              Ville Syrjälä <syrjala@sci.fi> and
              Claudio Ciccani <klan@users.sf.net>.

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

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#ifndef __DIRECT__THREAD_H__
#define __DIRECT__THREAD_H__

#include <sys/types.h>

#include <direct/types.h>
#include <direct/conf.h>

typedef enum {
     DTT_DEFAULT    =   0,
     DTT_CLEANUP    =  -5,
     DTT_INPUT      = -10,
     DTT_OUTPUT     = -12,
     DTT_MESSAGING  = -15,
     DTT_CRITICAL   = -20
} DirectThreadType;

typedef void * (*DirectThreadMainFunc)( DirectThread *thread, void *arg );

typedef void   (*DirectThreadInitFunc)( DirectThread *thread, void *arg );


/*
 * Add a handler being called at the beginning of new threads.
 */
DirectThreadInitHandler *direct_thread_add_init_handler   ( DirectThreadInitFunc     func,
                                                            void                    *arg );

/*
 * Remove the specified handler.
 */
void                     direct_thread_remove_init_handler( DirectThreadInitHandler *handler );

/*
 * Create a new thread and start it.
 * The thread type is relevant for the scheduling priority.
 */
DirectThread *direct_thread_create     ( DirectThreadType      thread_type,
                                         DirectThreadMainFunc  thread_main,
                                         void                 *arg,
                                         const char           *name );

/*
 * Returns the thread of the caller.
 */
DirectThread *direct_thread_self       ( void );

/*
 * Returns the name of the specified thread.
 */
const char   *direct_thread_get_name   ( DirectThread *thread );

/*
 * Returns the thread ID of the specified thread.
 */
pid_t         direct_thread_get_tid    ( DirectThread *thread );

/*
 * Returns the name of the calling thread.
 */
const char   *direct_thread_self_name  ( void );

/*
 * Changes the name of the calling thread.
 */
void          direct_thread_set_name   ( const char   *name );

/*
 * Wait on the thread object to be notified via direct_thread_notify().
 */
DirectResult  direct_thread_wait       ( DirectThread *thread,
                                         int           timeout_ms );

/*
 * Notify the thread object waking up callers of direct_thread_wait().
 */
void direct_thread_notify     ( DirectThread *thread );

void direct_thread_lock       ( DirectThread *thread );
void direct_thread_unlock     ( DirectThread *thread );

/*
 * Kindly ask the thread to terminate (for joining without thread cancellation).
 */
void direct_thread_terminate  ( DirectThread *thread );

/*
 * Cancel a running thread.
 */
void direct_thread_cancel     ( DirectThread *thread );

/*
 * Returns true if the specified thread has been canceled.
 */
bool direct_thread_is_canceled( DirectThread *thread );

/*
 * Detach a thread.
 */
void direct_thread_detach     ( DirectThread *thread );

/*
 * Returns true if the specified thread has been detached.
 */
bool direct_thread_is_detached( DirectThread *thread );

/*
 * Check if the calling thread is canceled.
 * Must not be called by other threads than 'thread'.
 * This function won't return if the thread is canceled.
 */
void direct_thread_testcancel ( DirectThread *thread );

/*
 * Wait until a running thread is terminated.
 */
void direct_thread_join       ( DirectThread *thread );

/*
 * Returns true if the specified thread has been join.
 */
bool direct_thread_is_joined  ( DirectThread *thread );

/*
 * Free resources allocated by direct_thread_create.
 * If the thread is still running it will be killed.
 */
void direct_thread_destroy    ( DirectThread *thread );

/*
 * Utilities for stringification.
 */
#if DIRECT_BUILD_TEXT
const char *direct_thread_type_name     ( DirectThreadType type );
const char *direct_thread_scheduler_name( DirectConfigThreadScheduler scheduler );
const char *direct_thread_policy_name   ( int policy );
#endif

#endif