LAL  7.5.0.1-08ee4f4
qthread.c
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2007 Bernd Machenschalk, Duncan Brown
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with with program; see the file COPYING. If not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 */
19 
20 /*-----------------------------------------------------------------------
21  *
22  * File Name: qthread.c
23  *
24  * Author: Brown, D. A.
25  *
26  *
27  *-----------------------------------------------------------------------
28  */
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <pthread.h>
34 #include <bits/local_lim.h>
35 
36 /* lal debug flag for verbosity level */
37 
38 /* dummy symbol to ensure that we link this to the fft routines */
40 
41 /* the following functions are needed to resolve symbols in */
42 /* executables linked to the Intel Math Kernel Library DFT */
43 /* routines. The function have an implementation that should */
44 /* work as long as they are not actually run in threadded */
45 /* code. These routines are called by the DFT routines */
46 /* during execution, even if the MKL environment variable */
47 /* MKL_SERIAL=YES */
48 
49 /* once-only execution */
50 enum { NEVER = 0, IN_PROGRESS = 1, DONE = 2 };
51 
52 int pthread_once(pthread_once_t * once_control, void (*init_routine)(void))
53 {
54  /* call the function init_routine() if once_control is not set to done */
55  if ( lalDebugLevel & 4 )
56  fprintf( stdout, "calling pthread_once(%p,%p)\n",
57  once_control, init_routine );
58 
59  if ( *once_control == DONE ) return 0;
60  init_routine();
61  *once_control = DONE;
62  return 0;
63 }
64 
65 /* thread specific keys */
66 typedef void (*destr_function)(void *);
67 
69 {
70  int in_use; /* already allocated? */
71  destr_function destr; /* destruction routine */
72 };
73 
74 /* table of keys */
75 static struct pthread_key_struct pthread_keys[PTHREAD_KEYS_MAX] =
76 { { 0, NULL } };
77 
78 /* value in keys */
79 static void * pthread_key_values[PTHREAD_KEYS_MAX];
80 
81 /* create a new key */
82 int pthread_key_create (pthread_key_t * key, destr_function destr)
83 {
84  int i;
85 
86  if ( lalDebugLevel & 4 )
87  fprintf( stdout, "calling pthread_key_create(%p,%p)\n", key, destr );
88 
89  for ( i = 0; i < PTHREAD_KEYS_MAX; i++ )
90  {
91  if (! pthread_keys[i].in_use)
92  {
93  /* Mark key in use */
94  pthread_keys[i].in_use = 1;
96  *key = i;
97 
98  /* set the value of the key to null */
99  pthread_key_values[i] = NULL;
100 
101  return 0;
102  }
103  }
104  return EAGAIN;
105 }
106 
107 /* delete a key */
108 int pthread_key_delete(pthread_key_t key)
109 {
110  if ( lalDebugLevel & 4 )
111  fprintf( stdout, "calling pthread_key_delete(%d)\n", key );
112 
113  if ( key >= PTHREAD_KEYS_MAX || ! pthread_keys[key].in_use )
114  {
115  return EINVAL;
116  }
117 
118  pthread_keys[key].in_use = 0;
119  pthread_keys[key].destr = NULL;
120 
121  /* set the value of the key to null */
122  pthread_key_values[key] = NULL;
123 
124  return 0;
125 }
126 
127 /* set key value */
128 int pthread_setspecific(pthread_key_t key, const void * pointer)
129 {
130  if ( lalDebugLevel & 4 )
131  fprintf( stdout, "calling pthread_setspecific(%d,%p)\n", key, pointer );
132 
133  if ( key >= PTHREAD_KEYS_MAX || ! pthread_keys[key].in_use )
134  {
135  return EINVAL;
136  }
137  pthread_key_values[key] = (void *) pointer;
138 
139  return 0;
140 }
141 
142 /* get key value */
143 void * pthread_getspecific(pthread_key_t key)
144 {
145  if ( lalDebugLevel & 4 )
146  fprintf( stdout, "calling pthread_getspecific(%d)\n", key );
147 
148  if ( key >= PTHREAD_KEYS_MAX || ! pthread_keys[key].in_use )
149  {
150  return NULL;
151  }
152 
153  return pthread_key_values[key];
154 }
155 
156 
157 /* these functions are needed to resolve the symbols when */
158 /* linking in the DFT routines from the Intel Math Kernel */
159 /* Library but are not actually called during execution if */
160 /* the environment variable MKL_SERIAL=YES. lalAbortHook() */
161 /* is called since there is no implementation defined here. */
162 
163 int pthread_cancel(pthread_t thread)
164 {
165  lalAbortHook( "attempt to call pthread_cancel(%ld)\n", thread );
166  return 0;
167 }
168 
169 int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
170  void * (*start_routine)(void *), void *arg)
171 {
172  lalAbortHook( "attempt to call pthread_create(%p,%p,%p,%p)\n",
173  thread, attr, start_routine, arg );
174  return 0;
175 }
176 
177 int pthread_join(pthread_t thread_id, void ** thread_return)
178 {
179  lalAbortHook( "attempt to call pthread_join(%ld,%p)\n",
180  thread_id, thread_return );
181  return 0;
182 }
183 
184 int pthread_sigmask(int how, const sigset_t * newmask, sigset_t * oldmask)
185 {
186  lalAbortHook( "attempt to call pthread_sigmask(%d,%p,%p)\n",
187  how, newmask, oldmask );
188  return 0;
189 }
void(* lalAbortHook)(const char *,...)
Definition: LALError.c:75
#define fprintf
#define lalDebugLevel
Definition: LALDebugLevel.h:58
int pthread_once(pthread_once_t *once_control, void(*init_routine)(void))
Definition: qthread.c:52
int pthread_key_create(pthread_key_t *key, destr_function destr)
Definition: qthread.c:82
void * pthread_getspecific(pthread_key_t key)
Definition: qthread.c:143
int dummy_have_qthread
Definition: qthread.c:39
int pthread_join(pthread_t thread_id, void **thread_return)
Definition: qthread.c:177
@ NEVER
Definition: qthread.c:50
@ IN_PROGRESS
Definition: qthread.c:50
@ DONE
Definition: qthread.c:50
int pthread_sigmask(int how, const sigset_t *newmask, sigset_t *oldmask)
Definition: qthread.c:184
int pthread_cancel(pthread_t thread)
Definition: qthread.c:163
static void * pthread_key_values[PTHREAD_KEYS_MAX]
Definition: qthread.c:79
static struct pthread_key_struct pthread_keys[PTHREAD_KEYS_MAX]
Definition: qthread.c:75
int pthread_setspecific(pthread_key_t key, const void *pointer)
Definition: qthread.c:128
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Definition: qthread.c:169
int pthread_key_delete(pthread_key_t key)
Definition: qthread.c:108
void(* destr_function)(void *)
Definition: qthread.c:66
destr_function destr
Definition: qthread.c:71