[go: up one dir, main page]

File: thread.c

package info (click to toggle)
autodir 0.99.8-1
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 1,920 kB
  • ctags: 775
  • sloc: sh: 8,544; ansic: 7,297; xml: 431; makefile: 104
file content (197 lines) | stat: -rw-r--r-- 5,972 bytes parent folder | download
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197

/*

Copyright (C) (2004 - 2006) (Venkata Ramana Enaganti) <ramana@intraperson.com>

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.

*/

/* for creating threads*/

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include "msg.h"
#include "thread.h"

/*If it is 32bit machine you might want to optimize this value.
This value given is not portable. Better to do own tuning and testing.*/

#if 1
#define THREAD_STACKSZ (0) /*OPTIMIZE*/
#else
#define THREAD_STACKSZ	(400000) /*Tested value for Fedora 3 IA32*/
#endif

/*common attributes for all threads*/
static pthread_attr_t cmn_attr;
static pthread_attr_t join_attr;

pthread_mutexattr_t common_mutex_attr;
pthread_condattr_t common_cond_attr;

static void thread_attribute_init( pthread_attr_t *attr,
			int joinable, size_t stacksz )
{
	int j = ( joinable ? PTHREAD_CREATE_JOINABLE
				: PTHREAD_CREATE_DETACHED );

	if( ( errno = pthread_attr_init( attr ) ) )
		msglog( MSG_FATAL|LOG_ERRNO, "thread_attribute_init: " \
			"pthread_attr_init" );

	if( ( errno = pthread_attr_setdetachstate( attr, j ) ) )
		msglog( MSG_FATAL|LOG_ERRNO, "thread_attribute_init: " \
			"pthread_attr_setdetachstate" );

	if( stacksz && ( errno = pthread_attr_setstacksize( attr, stacksz ) ) )
		msglog( MSG_FATAL|LOG_ERRNO, "thread_attribute_init: " \
			"pthread_attr_setstacksize %d", stacksz );

        if( ( errno = pthread_attr_setscope( attr, PTHREAD_SCOPE_SYSTEM ) ) )
                msglog( MSG_FATAL|LOG_ERRNO, "thread_attribute_init: " \
                       "pthread_attr_setscope PTHREAD_SCOPE_SYSTEM" );
}

static void mutexattr_init(void)
{
        const char *myname = "mutex_init";

        if( ( errno = pthread_mutexattr_init( &common_mutex_attr ) ) )
                msglog( MSG_FATAL|LOG_ERRNO, "%s: pthread_mutexattr_init",
                        myname );

        if( ( errno = pthread_mutexattr_setpshared( &common_mutex_attr,
                                                PTHREAD_PROCESS_PRIVATE ) ) )
                msglog( MSG_FATAL|LOG_ERRNO, "%s: pthread_mutexattr_setpshared " \
                        "PTHREAD_PROCESS_PRIVATE", myname );

        if( ( errno = pthread_mutexattr_settype( &common_mutex_attr,
                                                PTHREAD_MUTEX_NORMAL ) ) )
                msglog( MSG_FATAL|LOG_ERRNO, "%s: pthread_mutexattr_settype " \
                        "PTHREAD_MUTEX_NORMAL: %m", myname );
}

static void condattr_init(void)
{
        const char        *myname = "condattr_init";

        if ( ( errno = pthread_condattr_init( &common_cond_attr ) ) )
                msglog(MSG_FATAL|LOG_ERRNO, "%s: pthread_condattr_init",
                        myname);

        if ( ( errno = pthread_condattr_setpshared( &common_cond_attr,
                                                PTHREAD_PROCESS_PRIVATE ) ) )
                msglog( MSG_FATAL|LOG_ERRNO, "%s: pthread_condattr_setpshared: " \
                        "PTHREAD_PROCESS_PRIVATE", myname);

#if defined _POSIX_CLOCK_SELECTION && _POSIX_CLOCK_SELECTION >= 0
#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) 
#if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0)
        if ( ( errno = pthread_condattr_setclock(&common_cond_attr,
                                                CLOCK_MONOTONIC ) ) )
                msglog( MSG_FATAL|LOG_ERRNO, "%s: pthread_condattr_setclock",
                                                myname );
#else
#warning "CLOCK_MONOTONIC not available. using CLOCK_REALTIME"
        if ( ( errno = pthread_condattr_setclock( &common_cond_attr,
                                                CLOCK_REALTIME ) ) )
                msglog( MSG_FATAL|LOG_ERRNO, "%s: pthread_condattr_setclock",
                                                myname );
#endif
#endif
#endif
}

void thread_init( void )
{
	/*common attributes*/
	thread_attribute_init( &cmn_attr, 0, THREAD_STACKSZ );

	/*joinable attributes*/
	thread_attribute_init( &join_attr, 1, THREAD_STACKSZ );

        mutexattr_init();
        condattr_init();
}

#define DEFAULT_RETRY_WAIT	5

/*We can not simply exit or report error back
  if there is failure starting a thread
  because of resource scarcity.
*/

pthread_t thread_new_wait( void *( *th_func )( void * ),
		void *data, time_t retry )
{
	pthread_t pt;

	if( retry <= 0 )
		retry = DEFAULT_RETRY_WAIT;

	while( ( errno = pthread_create( &pt, &cmn_attr, th_func,data ) ) )
	{
		msglog( MSG_ERR|LOG_ERRNO, "thread_new_wait: " \
				"pthread_create" );

		/*real BOTTLE-NECK if you are not careful*/
		sleep( retry );
	}

	return pt;
}

int thread_new( void *( *th_func )( void * ),
				void *data, pthread_t *pt )
{
	pthread_t p;

	if( ( errno = pthread_create( &p, &cmn_attr, th_func, data) ) )
	{
		msglog( MSG_ERR|LOG_ERRNO, "thread_new: pthread_create" );
		if( pt ) *pt = 0;
		return 0;
	}

	if( pt ) *pt = p;
	return 1;
}

/* these threads can be joined by others. Watch out for memory leaks.*/
int thread_new_joinable( void *( *th_func )( void * ),
				void *data, pthread_t *pt )
{
	pthread_t p;

	if( ( errno = pthread_create( &p, &join_attr, th_func, data ) ) )
	{
		msglog( MSG_ERR|LOG_ERRNO, "thread_new_joinable: " \
				"pthread_create" );
		if( pt ) *pt = 0;
		return 0;
	}

	if( pt ) *pt = p;
	return 1;
}