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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
*
* The Contents of this file are made available subject to the terms of
* the Sun Industry Standards Source License Version 1.2
*
* Sun Microsystems Inc., March, 2001
*
*
* Sun Industry Standards Source License Version 1.2
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.2 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2003 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
************************************************************************/
/*___INFO__MARK_END__*/
#include "cull_state.h"
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include "uti/sge_stdlib.h"
#include "cull/pack.h"
/* struct to store ALL state information of cull lib */
typedef struct {
int lerrno; /* cull errno */
char noinit[50]; /* cull error buffer */
const lSortOrder* global_sort_order; /* qsort() by-pass argument */
const lNameSpace* name_space; /* name vector */
} cull_state_t;
static pthread_once_t cull_once = PTHREAD_ONCE_INIT;
static pthread_key_t cull_state_key;
static void cull_once_init(void);
static void cull_state_destroy(void* theState);
static cull_state_t* cull_state_getspecific(pthread_key_t aKey);
static void cull_state_init(cull_state_t *theState);
/****** cull/state/cull_state_get_????() ************************************
* NAME
* cull_state_get_????() - read access to cull state.
*
* FUNCTION
* Provides access to thread local storage.
*
******************************************************************************/
int cull_state_get_lerrno(void)
{
cull_state_t *cull_state = NULL;
pthread_once(&cull_once, cull_once_init);
cull_state = cull_state_getspecific(cull_state_key);
return cull_state->lerrno;
}
const char *cull_state_get_noinit(void)
{
cull_state_t *cull_state = NULL;
pthread_once(&cull_once, cull_once_init);
cull_state = cull_state_getspecific(cull_state_key);
return cull_state->noinit;
}
const lSortOrder *cull_state_get_global_sort_order(void)
{
cull_state_t *cull_state = NULL;
pthread_once(&cull_once, cull_once_init);
cull_state = cull_state_getspecific(cull_state_key);
return cull_state->global_sort_order;
}
const lNameSpace *cull_state_get_name_space(void)
{
cull_state_t *cull_state = NULL;
pthread_once(&cull_once, cull_once_init);
cull_state = cull_state_getspecific(cull_state_key);
return cull_state->name_space;
}
/****** cull/list/cull_state_set_????() ************************************
* NAME
* cull_state_set_????() - write access to cull state.
*
* FUNCTION
* Provides access to thread local storage.
*
******************************************************************************/
void cull_state_set_lerrno( int i)
{
cull_state_t *cull_state = NULL;
pthread_once(&cull_once, cull_once_init);
cull_state = cull_state_getspecific(cull_state_key);
cull_state->lerrno = i;
return;
}
void cull_state_set_noinit( char *s)
{
cull_state_t *cull_state = NULL;
pthread_once(&cull_once, cull_once_init);
cull_state = cull_state_getspecific(cull_state_key);
strcpy(cull_state->noinit, s);
return;
}
void cull_state_set_global_sort_order( const lSortOrder *so)
{
cull_state_t *cull_state = NULL;
pthread_once(&cull_once, cull_once_init);
cull_state = cull_state_getspecific(cull_state_key);
cull_state->global_sort_order = so;
return;
}
void cull_state_set_name_space(const lNameSpace *ns)
{
cull_state_t *cull_state = NULL;
pthread_once(&cull_once, cull_once_init);
cull_state = cull_state_getspecific(cull_state_key);
cull_state->name_space = ns;
return;
}
/****** cull/state/cull_once_init() ********************************************
* NAME
* cull_once_init() -- One-time CULL initialization.
*
* SYNOPSIS
* static cull_once_init(void)
*
* FUNCTION
* Create access key for thread local storage. Register cleanup function.
*
* This function must be called exactly once.
*
* INPUTS
* void - none
*
* RESULT
* void - none
*
* NOTES
* MT-NOTE: cull_once_init() is MT safe.
*
*******************************************************************************/
static void cull_once_init(void)
{
pthread_key_create(&cull_state_key, cull_state_destroy);
return;
} /* cull_once_init() */
/****** cull/state/cull_state_destroy() ****************************************
* NAME
* cull_state_destroy() -- Free thread local storage
*
* SYNOPSIS
* static void cull_state_destroy(void* theState)
*
* FUNCTION
* Free thread local storage.
*
* INPUTS
* void* theState - Pointer to memory which should be freed.
*
* RESULT
* static void - none
*
* NOTES
* MT-NOTE: cull_state_destroy() is MT safe.
*
*******************************************************************************/
static void cull_state_destroy(void* theState)
{
sge_free(&theState);
}
/****** cull/state/cull_state_getspecific() ************************************
* NAME
* cull_state_getspecific() -- Get thread local cull state
*
* SYNOPSIS
* static cull_state_t* cull_state_getspecific(pthread_key_t aKey)
*
* FUNCTION
* Return thread local cull state.
*
* If a given thread does call this function for the first time, no thread
* local cull state is available for this particular thread. In this case the
* thread local cull state is allocated and set.
*
* INPUTS
* pthread_key_t aKey - Key for thread local cull state
*
* RESULT
* static cull_state_t* - Pointer to thread local cull state
*
* NOTES
* MT-NOTE: cull_state_getspecific() is MT safe
*
*******************************************************************************/
static cull_state_t* cull_state_getspecific(pthread_key_t aKey)
{
cull_state_t *cull_state = pthread_getspecific(aKey);
if (cull_state == NULL) {
int res = EINVAL;
cull_state = (cull_state_t*)sge_malloc(sizeof(cull_state_t));
cull_state_init(cull_state);
res = pthread_setspecific(cull_state_key, (const void*)cull_state);
if (0 != res) {
fprintf(stderr, "pthread_set_specific(%s) failed: %s\n", "cull_state_getspecific", strerror(res));
abort();
}
}
return cull_state;
} /* cull_state_getspecific() */
/****** cull/state/cull_state_init() *******************************************
* NAME
* cull_state_init() -- Initialize CULL state.
*
* SYNOPSIS
* static void cull_state_init(cull_state_t *theState)
*
* FUNCTION
* Initialize CULL state.
*
* INPUTS
* struct cull_state_t* theState - Pointer to CULL state structure.
*
* RESULT
* static void - none
*
* NOTES
* MT-NOTE: cull_state_init() is MT safe.
*
*******************************************************************************/
static void cull_state_init(cull_state_t *theState)
{
theState->lerrno = 0;
theState->noinit[0] = '\0';
theState->global_sort_order = NULL;
theState->name_space = NULL;
}
|