File: globus_thread_common.c

package info (click to toggle)
globus-common 18.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,684 kB
  • sloc: ansic: 36,506; sh: 4,534; makefile: 354; perl: 151
file content (479 lines) | stat: -rw-r--r-- 10,626 bytes parent folder | download | duplicates (8)
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
 * Copyright 1999-2006 University of Chicago
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef GLOBUS_DONT_DOCUMENT_INTERNAL

/**
 * @file globus_thread_common.c
 * @brief Thread Model-Indepent Functions
 */

/******************************************************************************
			     Include header files
******************************************************************************/
#include "globus_i_common_config.h"
#include "globus_common_include.h"
#include "globus_thread_common.h"
#include "globus_i_thread.h"
#include "version.h"
#include "globus_libc.h"
#include "globus_callback.h"
#include "globus_libc.h"
#include "globus_print.h"
#include "globus_common.h"

#define THREAD_STACK_INIT_SIZE 32

typedef struct globus_l_thread_stack_node_s
{
    globus_thread_blocking_func_t  func;
    void *                         user_args;
    globus_callback_space_t        space;
    globus_bool_t                  enabled;

} globus_l_thread_stack_node_t;

typedef struct globus_l_thread_stack_manager
{
    globus_l_thread_stack_node_t *        stack;
    int                                   max;
    int                                   top;

} globus_l_thread_stack_manager_t;


static int
globus_l_thread_common_activate(void);

static int
globus_l_thread_common_deactivate(void);

static void 
globus_l_thread_blocking_callback_destroy(void* p);

static globus_thread_key_t              l_thread_stack_key;
static globus_bool_t                    globus_l_mod_active = GLOBUS_FALSE;

globus_module_descriptor_t              globus_i_thread_common_module =
{
   "globus_thread_common",
    globus_l_thread_common_activate,
    globus_l_thread_common_deactivate,
    GLOBUS_NULL,
    GLOBUS_NULL,
    &local_version
};       

static globus_l_thread_stack_manager_t *
globus_l_thread_blocking_callback_init()
{
   globus_l_thread_stack_manager_t *         manager; 

   manager = (globus_l_thread_stack_manager_t *)
		globus_malloc(sizeof(globus_l_thread_stack_manager_t));

   manager->top = -1;
   manager->max = THREAD_STACK_INIT_SIZE;
   manager->stack = (globus_l_thread_stack_node_t *)
		      globus_malloc(sizeof(globus_l_thread_stack_node_t) *
		      THREAD_STACK_INIT_SIZE);

   return manager;
}



void
globus_i_thread_report_bad_rc(int rc,
			      char *message )
{
    char achMessHead[] = "[Thread System]";
    char achDesc[GLOBUS_L_LIBC_MAX_ERR_SIZE];
    
    if(rc != GLOBUS_SUCCESS)
    {
	switch( rc )
	{
	case EAGAIN:
	    strcpy(achDesc, _GCSL("system out of resources (EAGAIN)"));
	    break;
	case ENOMEM:
	    strcpy(achDesc, _GCSL("insufficient memory (ENOMEM)"));
	    break;
	case EINVAL:
	    strcpy(achDesc, _GCSL("invalid value passed to thread interface (EINVAL)"));
	    break;
	case EPERM:
	    strcpy(achDesc, _GCSL("user does not have adequate permission (EPERM)"));
	    break;
	case ERANGE:
	    strcpy(achDesc, _GCSL("a parameter has an invalid value (ERANGE)"));
	    break;
	case EBUSY:
	    strcpy(achDesc, _GCSL("mutex is locked (EBUSY)"));
	    break;
	case EDEADLK:
	    strcpy(achDesc, _GCSL("deadlock detected (EDEADLK)"));
	    break;
	case ESRCH:
	    strcpy(achDesc, _GCSL("could not find specified thread (ESRCH)"));
	    break;
	default:
	    globus_fatal(_GCSL("%s %s\n%s unknown error number: %d\n"),
				  achMessHead,
				  message,
				  achMessHead,
				  rc);
	    break;
	}
	globus_fatal("%s %s\n%s %s",
			      achMessHead,
			      message,
			      achMessHead,
			      achDesc);
    }
} /* globus_i_thread_report_bad_rc() */


/*
 *
 */
int
globus_thread_blocking_space_callback_push(
    globus_thread_blocking_func_t       func,
    void *                              user_args,
    globus_callback_space_t             space,
    globus_thread_callback_index_t *    i)
{
   globus_l_thread_stack_node_t *            n;
   globus_l_thread_stack_manager_t *         manager; 

   if(!globus_l_mod_active)
   {
       return GLOBUS_FAILURE;
   }

   /*
    *  If module not yet activated return -1
    */

   manager = (globus_l_thread_stack_manager_t *)
		       globus_thread_getspecific(l_thread_stack_key);
   /*
    *  If first time push is called on this thread create a new 
    *  manager structure.
    */
   if(manager == NULL)
   {
       manager = globus_l_thread_blocking_callback_init();
   }

   manager->top = manager->top + 1;
   n = &(manager->stack[manager->top]);

   n->func = func;
   n->user_args = user_args;
   n->space = space;
   n->enabled = GLOBUS_TRUE;

   if(i != NULL)
   {
       *i = manager->top;
   }
   if(manager->top >= manager->max - 1)
   {
       manager->max += THREAD_STACK_INIT_SIZE;
       manager->stack = (globus_l_thread_stack_node_t *)
       realloc((void*)manager->stack, 
                    sizeof(globus_l_thread_stack_node_t) * manager->max);
	   
   }
   globus_thread_setspecific(l_thread_stack_key,
			         (void *)manager);

   return GLOBUS_SUCCESS;
}

/*
 *
 */
int
globus_thread_blocking_callback_pop(
    globus_thread_callback_index_t * i)
{
   globus_l_thread_stack_manager_t *       manager; 
   
   /*
    *  If module not yet activated return -1
    */
   if(!globus_l_mod_active)
   {
       return GLOBUS_FAILURE;
   }
   
       manager = (globus_l_thread_stack_manager_t *)
  		          globus_thread_getspecific(l_thread_stack_key);

       if(manager == NULL ||
          manager->top < 0)
       {
           return GLOBUS_FAILURE;
       }

       if(i != NULL)
       {
           *i = manager->top;
       }
       manager->top--;

   return GLOBUS_SUCCESS;
}

/*
 *
 */
int
globus_thread_blocking_callback_enable(
    globus_thread_callback_index_t * i)
{
    globus_l_thread_stack_manager_t *       manager; 
   
    /*
     *  If module not yet activated return -1
     */
    if(!globus_l_mod_active)
    {
        return GLOBUS_FAILURE;
    }
   
    manager = (globus_l_thread_stack_manager_t *)
		       globus_thread_getspecific(l_thread_stack_key);

    if(manager == NULL)
    {
	return GLOBUS_FAILURE;
    }

    if(*i <= manager->top)
    {
        manager->stack[*i].enabled = GLOBUS_TRUE;
    }

    return GLOBUS_SUCCESS;
}

void
globus_thread_blocking_reset()
{
    globus_l_thread_stack_manager_t *       manager; 

    manager = (globus_l_thread_stack_manager_t *)
		       globus_thread_getspecific(l_thread_stack_key);
    globus_l_thread_blocking_callback_destroy(manager);
}

/*
 *
 */
int
globus_thread_blocking_callback_disable(
					globus_thread_callback_index_t * i)
{
    globus_l_thread_stack_manager_t *  manager; 
   
    /*
     *  If module not yet activated return -1
     */
    if(!globus_l_mod_active)
    {
        return GLOBUS_FAILURE;
    }
   
    manager = (globus_l_thread_stack_manager_t *)
		       globus_thread_getspecific(l_thread_stack_key);

    if(manager == NULL)
    {
	return GLOBUS_FAILURE;
    }

    if(*i <= manager->top)
    {
        manager->stack[*i].enabled = GLOBUS_FALSE;
    }

    return GLOBUS_TRUE;
}

/*
 *
 */
int
globus_thread_blocking_space_will_block(
    int                                 blocking_space)
{
    int                                       ctr;
    globus_thread_blocking_func_t             func;
    globus_l_thread_stack_manager_t *         manager; 

    
    /*
     *  If module not yet activated return -1
     */
    if(!globus_l_mod_active)
    {
        return GLOBUS_FAILURE;
    }

    manager = (globus_l_thread_stack_manager_t *)
		       globus_thread_getspecific(l_thread_stack_key);

    if(manager == NULL)
    {
        return GLOBUS_FAILURE;
    }

    for(ctr = manager->top;  ctr >= 0; ctr--)
    {
       if(manager->stack[ctr].enabled && 
        (manager->stack[ctr].space == GLOBUS_CALLBACK_GLOBAL_SPACE ||
            manager->stack[ctr].space == blocking_space))
       {
           func =  (manager->stack[ctr].func);
	   func(ctr, blocking_space, manager->stack[ctr].user_args);
       }
    }

    return GLOBUS_SUCCESS;
}

int
globus_l_thread_common_activate(void)
{
    int rc;

    /* 
     * safely initialize ICU library w/ threads
     */

    rc = globus_thread_key_create(&l_thread_stack_key,
			          globus_l_thread_blocking_callback_destroy);

    if(rc == 0)
    {
        globus_l_mod_active  = GLOBUS_TRUE;
    }
    return rc;
}

int
globus_l_thread_common_deactivate(void)
{
    return GLOBUS_SUCCESS;
}

static void 
globus_l_thread_blocking_callback_destroy(void* p)
{
     globus_l_thread_stack_manager_t * manager = 
             (globus_l_thread_stack_manager_t*)p;
 
     if(!manager)
       return;
     free(manager->stack);
     free(manager);
    globus_thread_setspecific(l_thread_stack_key,
                                 (void *)GLOBUS_NULL);
}

/* This function contains non-portable code and won't compile
 * directly on Windows.
 *
 * Michael Lebman
 * 5-28-02
 */
#ifndef TARGET_ARCH_WIN32

void thread_print(char * s, ...)
{
    char tmp[1023];
    int x;
    va_list ap;
    pid_t pid = getpid();
    globus_thread_t tid = globus_thread_self();
    
    va_start(ap, s);

    sprintf(tmp, "p#%dt#%ld::", pid, (long) tid.dummy);
    x = strlen(tmp);
    vsprintf(&tmp[x], s, ap);

    globus_libc_printf("%s", tmp);
    globus_thread_yield();
   
    fflush(stdin);
}

#endif

/*
 *  not found in win32
 */
#ifndef TARGET_ARCH_WIN32

int
globus_i_thread_ignore_sigpipe(void)
{
#ifdef HAVE_SIGACTION
    struct sigaction act;
    struct sigaction oldact;
    int rc;
    int save_errno;

    memset(&oldact, '\0', sizeof(struct sigaction));

    do
    {
        rc = sigaction(SIGPIPE, GLOBUS_NULL, &oldact);
	save_errno = errno;
    } while(rc < 0 && save_errno == EINTR);

    if(rc != GLOBUS_SUCCESS)
    {
	return rc;
    }
    else if(oldact.sa_handler != SIG_DFL)
    {
	return GLOBUS_SUCCESS;
    }
    else
    {
        memset(&act, '\0', sizeof(struct sigaction));
        sigemptyset(&(act.sa_mask));
        act.sa_handler = SIG_IGN;
        act.sa_flags = 0;

        return sigaction(SIGPIPE, &act, GLOBUS_NULL);
    }
#else
    return GLOBUS_SUCCESS;
#endif
}
/* globus_i_thread_ignore_sigpipe() */

#endif

#endif /* GLOBUS_DONT_DOCUMENT_INTERNAL */