source: trunk/src/sh_pthread.c@ 223

Last change on this file since 223 was 170, checked in by katerina, 17 years ago

Plenty of compiler warnings fixed, SQL query length fixed, doc update.

File size: 6.8 KB
RevLine 
[136]1#include "config_xor.h"
2
3#include "sh_pthread.h"
4
5#ifdef HAVE_PTHREAD
[140]6
7#include <signal.h>
8#include "sh_calls.h"
[141]9#include "sh_modules.h"
[143]10extern volatile int sh_thread_pause_flag;
[140]11
[143]12SH_MUTEX_INIT(mutex_skey, PTHREAD_MUTEX_INITIALIZER);
13SH_MUTEX_INIT(mutex_resolv, PTHREAD_MUTEX_INITIALIZER);
14SH_MUTEX_INIT(mutex_pwent, PTHREAD_MUTEX_INITIALIZER);
15SH_MUTEX_INIT(mutex_readdir, PTHREAD_MUTEX_INITIALIZER);
16SH_MUTEX_INIT(mutex_thread_nolog, PTHREAD_MUTEX_INITIALIZER);
[136]17
18void sh_pthread_mutex_unlock (void *arg)
19{
20 (void) pthread_mutex_unlock ((pthread_mutex_t *)arg);
21 return;
22}
[138]23
[170]24int sh_pthread_init_threadspecific(void)
[140]25{
26 int rc = 0;
27#ifdef SH_STEALTH
[170]28 extern int sh_g_thread(void);
[140]29
30 rc = sh_g_thread();
31 if (rc != 0)
32 return rc;
33#endif
34
35 return rc;
36}
37
38
39/*
40 * ---- Utilities for modules ----
41 */
42
43/* MODULES: init()
44 *
[142]45 * #ifdef HAVE_PTHREAD
46 * if (arg != NULL)
47 * {
48 * if (0 == sh_pthread_create(sh_threaded_module_run, (void *)arg))
49 * return SH_MOD_THREAD;
50 * else
51 * return SH_MOD_FAILED;
52 * }
53 * #else
54 * return sh_utmp_init_internal();
55 * #endif
[140]56 *
[142]57 *
58 * sh_threaded_module_run(module_struct)
[140]59 * -- calls internal init,
60 * -- polls timer,
61 * -- runs module check,
62 * -- runs sh_pthread_testcancel()
63 * -- returns (return == exit)
64 */
65
66#define SH_NUM_THREADS 16
67static pthread_t threads[SH_NUM_THREADS];
68static int ithread[SH_NUM_THREADS];
69static pthread_mutex_t create_mutex = PTHREAD_MUTEX_INITIALIZER;
70
71int sh_pthread_create(void *(*start_routine)(void*), void *arg)
72{
73 int rc, nthread = 1;
74 sigset_t signal_set;
75 int retval = 0;
76
77 pthread_mutex_lock(&create_mutex);
78
79 /* block all signals
80 */
81 sigfillset( &signal_set );
82 pthread_sigmask( SIG_BLOCK, &signal_set, NULL );
83
84 /* find a free slot in threads[]
85 */
86 while (nthread < SH_NUM_THREADS)
87 {
88 if (ithread[nthread] == 0)
89 break;
90 ++nthread;
91 if (nthread == SH_NUM_THREADS)
92 {
93 retval = -1;
94 goto err_out;
95 }
96 }
97
98 rc = pthread_create(&threads[nthread], NULL, start_routine, arg);
99 if (rc != 0)
100 {
101 retval = -1;
102 goto err_out;
103 }
104
105 ithread[nthread] = 1;
106
107 err_out:
108 pthread_sigmask( SIG_UNBLOCK, &signal_set, NULL );
109 pthread_mutex_unlock(&create_mutex);
110 return retval;
111}
112
113int sh_pthread_cancel_all()
114{
115 int i;
116 int ret = 0;
117
118 SH_MUTEX_LOCK(create_mutex);
119
120 for (i = 1; i < SH_NUM_THREADS; ++i)
121 {
122 if (ithread[i] != 0)
123 if (0 != pthread_cancel(threads[i]))
124 ithread[i] = 0;
125 }
126
127 for (i = 1; i < SH_NUM_THREADS; ++i)
128 {
129 if (ithread[i] != 0)
130 pthread_join(threads[i], NULL);
131 ithread[i] = 0;
132 }
133
134 SH_MUTEX_UNLOCK(create_mutex);
135 return ret;
136}
137
138/* ---- Utility functions for modules ----
139 */
[143]140
141#undef S_TRUE
142#define S_TRUE 1
143#undef S_FALSE
144#define S_FALSE 0
145
[142]146void sh_threaded_module_cleanup(void *arg)
[140]147{
148 sh_mtype * this_module = (sh_mtype *) arg;
[142]149 this_module->mod_cleanup();
[140]150 return;
151}
152
[142]153void * sh_threaded_module_run(void *arg)
[140]154{
155 sh_mtype * this_module = (sh_mtype *) arg;
156
157 /* First we lock the module. This ensures that it cannot be
158 * run twice.
159 */
160 pthread_cleanup_push(sh_pthread_mutex_unlock, (void*) &(this_module->mod_mutex));
161 pthread_mutex_lock(&(this_module->mod_mutex));
162
163 if (0 == sh_pthread_init_threadspecific())
164 {
165
166 if (0 == this_module->mod_init(NULL))
167 {
[142]168 pthread_cleanup_push(sh_threaded_module_cleanup, arg);
[140]169
170 while (1)
171 {
[143]172 if (sh_thread_pause_flag != S_TRUE &&
173 0 != this_module->mod_timer(time(NULL)))
[140]174 {
[142]175 /* If module has been de-activated on reconfigure,
176 * mod_check() must return non-zero.
177 * The mod_cleanup() routine must then enable the
178 * module to be re-activated eventually.
179 */
180 if (0 != this_module->mod_check())
181 break;
[140]182 pthread_testcancel();
183 }
[143]184 retry_msleep(1,0);
[140]185 }
186
187 pthread_cleanup_pop(1); /* notreached,but required */
188 }
189 }
190
191 pthread_cleanup_pop(1);
192
[142]193 return NULL;
[140]194}
195
196
197/*
198 * ---- Implementation of recursive mutexes from libxml2 ----
199 */
[139]200#if !defined(HAVE_PTHREAD_MUTEX_RECURSIVE)
[138]201/**
202 * libxml2 threads.c: set of generic threading related routines
203 *
204 * Gary Pennington <Gary.Pennington@uk.sun.com>
205 * daniel@veillard.com
[140]206
207 * Except where otherwise noted in the source code (e.g. the files hash.c,
208 * list.c and the trio files, which are covered by a similar licence but
209 * with different Copyright notices) all the files are:
210 *
211 * Copyright (C) 1998-2003 Daniel Veillard. All Rights Reserved.
212 *
213 * Permission is hereby granted, free of charge, to any person obtaining a copy
214 * of this software and associated documentation files (the "Software"), to deal
215 * in the Software without restriction, including without limitation the rights
216 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
217 * copies of the Software, and to permit persons to whom the Software is fur-
218 * nished to do so, subject to the following conditions:
219 *
220 * The above copyright notice and this permission notice shall be included in
221 * all copies or substantial portions of the Software.
222 *
223 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
224 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIT-
225 * NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
226 * DANIEL VEILLARD BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
227 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CON-
228 * NECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
229 *
230 * Except as contained in this notice, the name of Daniel Veillard shall not
231 * be used in advertising or otherwise to promote the sale, use or other deal-
232 * ings in this Software without prior written authorization from him.
[138]233 */
234
[140]235/* Modified NewRMutex -> InitRMutex. We use a static structure, rather than
236 * allocating one. Also dropped code for non-POSIX OSes.
237 */
[138]238void sh_InitRMutex(struct sh_RMutex * tok)
239{
240 pthread_mutex_init(&tok->lock, NULL);
241 tok->held = 0;
242 tok->waiters = 0;
243 pthread_cond_init(&tok->cv, NULL);
244
245 return;
246}
247
248void sh_RMutexLock(struct sh_RMutex * tok)
249{
250 if (tok == NULL)
251 return;
252
253 pthread_mutex_lock(&tok->lock);
254 if (tok->held) {
255 if (pthread_equal(tok->tid, pthread_self())) {
256 tok->held++;
257 pthread_mutex_unlock(&tok->lock);
258 return;
259 } else {
260 tok->waiters++;
261 while (tok->held)
262 pthread_cond_wait(&tok->cv, &tok->lock);
263 tok->waiters--;
264 }
265 }
266 tok->tid = pthread_self();
267 tok->held = 1;
268 pthread_mutex_unlock(&tok->lock);
269}
270
271void sh_RMutexUnlock(void * arg)
272{
273 struct sh_RMutex * tok = (struct sh_RMutex *) arg;
274
275 if (tok == NULL)
276 return;
277
278 pthread_mutex_lock(&tok->lock);
279 tok->held--;
280 if (tok->held == 0) {
281 if (tok->waiters)
282 pthread_cond_signal(&tok->cv);
283 tok->tid = 0;
284 }
285 pthread_mutex_unlock(&tok->lock);
286}
[136]287#endif
[139]288
289
290#endif
Note: See TracBrowser for help on using the repository browser.