File: globus_module.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 (1194 lines) | stat: -rw-r--r-- 31,370 bytes parent folder | download | duplicates (4)
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
/*
 * 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_module.c
 * @brief Module Activation and Deactivation
 */
#endif /* GLOBUS_DONT_DOCUMENT_INTERNAL */

/******************************************************************************
			     Include header files
******************************************************************************/
#include "globus_i_common_config.h"
#include "globus_common_include.h"
#include "globus_common.h"
#include "globus_module.h"
#include "globus_list.h"
#include "globus_thread_common.h"
#include "globus_memory.h"
#include "globus_hashtable.h"
#include "globus_libc.h"
#include "globus_thread.h"

/******************************************************************************
			       Type definitions
******************************************************************************/

/*
 * global vars
 */

static int *                            globus_l_module_argc = NULL;
static char ***                         globus_l_module_argv = NULL;
static globus_thread_key_t              globus_l_activate_parent_key;
static globus_thread_key_t              globus_l_deactivate_parent_key;
/*
 * data structure needed to implement a recursive mutex
 */
typedef struct
{
    globus_mutex_t			mutex;
    globus_cond_t			cond;
    globus_thread_t			thread_id;
    int					level;
} globus_l_module_mutex_t;

/*
 * data structures for a hash table entry and the associated key
 */
typedef globus_module_activation_func_t globus_l_module_key_t;
typedef struct
{
    globus_module_descriptor_t *	descriptor;
    globus_list_t *			clients;
    int					reference_count;
    globus_module_deactivate_proxy_cb_t deactivate_cb;
    void *                              user_arg;
} globus_l_module_entry_t;

/******************************************************************************
		       Define module specific variables
******************************************************************************/

globus_bool_t
globus_i_module_initialized = GLOBUS_FALSE;

static globus_bool_t
globus_l_environ_initialized = GLOBUS_FALSE;
static globus_bool_t
globus_l_environ_mutex_initialized = GLOBUS_FALSE;

/* Recursive mutex to protect internal data structures */
static globus_l_module_mutex_t		globus_l_module_mutex;

/* Hash table and list to maintain a table of registered modules */
const int GLOBUS_L_MODULE_TABLE_SIZE = 13;
static globus_hashtable_t		globus_l_module_table;
static globus_list_t *			globus_l_module_list;

/* Hash table for globus_environ*/
const int GLOBUS_L_ENVIRON_TABLE_SIZE = 13;
static globus_mutex_t		globus_l_environ_hashtable_mutex;
static globus_hashtable_t		globus_l_environ_table;

globus_list_t *globus_l_module_atexit_funcs = GLOBUS_NULL;

/******************************************************************************
		      Module specific function prototypes
******************************************************************************/
static void
globus_l_module_initialize();

static globus_bool_t
globus_l_module_increment(
    globus_module_descriptor_t *	module_descriptor,
    globus_l_module_key_t		parent_key,
    globus_module_deactivate_proxy_cb_t deactivate_cb,
    void *                              user_arg);

static globus_l_module_entry_t *
globus_l_module_decrement(
    globus_module_descriptor_t *	module_descriptor,
    globus_l_module_key_t		parent_key);

static
int
globus_l_module_reference_count(
    globus_module_descriptor_t *	module_descriptor);
/******************************************************************************
		      Recursive mutex function prototypes
******************************************************************************/
static void
globus_l_module_mutex_init(
    globus_l_module_mutex_t *		mutex);

static void
globus_l_module_mutex_lock(
    globus_l_module_mutex_t *		mutex);

static void
globus_l_module_mutex_unlock(
    globus_l_module_mutex_t *		mutex);
    
/******************************************************************************
			   API function definitions
******************************************************************************/

#if USE_SYMBOL_LABELS
__asm__(".symver globus_module_activate_proxy_compat,"
        "globus_module_activate_proxy@GLOBUS_COMMON_11");
__asm__(".symver globus_module_activate_proxy_new,"
        "globus_module_activate_proxy@@GLOBUS_COMMON_14");
__asm__(".symver globus_module_activate_compat,"
        "globus_module_activate@GLOBUS_COMMON_11");
__asm__(".symver globus_module_activate_new,"
        "globus_module_activate@@GLOBUS_COMMON_14");
__asm__(".symver globus_module_activate_array_compat,"
        "globus_module_activate_array@GLOBUS_COMMON_11");
__asm__(".symver globus_module_activate_array_new,"
        "globus_module_activate_array@@GLOBUS_COMMON_14");

#define globus_module_activate_proxy globus_module_activate_proxy_new
#define globus_module_activate globus_module_activate_new
#define globus_module_activate_array globus_module_activate_array_new
int WARNING_USING_MIXED_THREAD_MODELS = 0;
#endif

/*
 * globus_module_activate()
 */
#ifdef __GNUC__
__attribute__ ((externally_visible))
#endif
int
globus_module_activate_proxy(
    globus_module_descriptor_t *	module_descriptor,
    globus_module_deactivate_proxy_cb_t deactivate_cb,
    void *                              user_arg)
{
    globus_l_module_key_t               parent_key;
    int                                 ret_val;
    globus_l_module_key_t               parent_key_save;
    
    /*
     * If this is the first time this routine has been called, then we need to
     * initialize the internal data structures and activate the threads
     * packages if the system has been configured to use threads.
     */
    if (globus_i_module_initialized == GLOBUS_FALSE)
    {
	globus_i_module_initialized = GLOBUS_TRUE;
	globus_l_module_initialize();
    }
    
    parent_key = (globus_l_module_key_t)
        globus_thread_getspecific(globus_l_activate_parent_key);
    /*
     * Once the recursive mutex has been acquired, increment the reference
     * counter for this module, and call it's activation function if it is not
     * currently active.
     */
    globus_l_module_mutex_lock(&globus_l_module_mutex);
    {
	ret_val = GLOBUS_SUCCESS;

	if (module_descriptor->activation_func != GLOBUS_NULL)
	{
	    if (globus_l_module_increment(module_descriptor,
					  parent_key,
					  deactivate_cb,
					  user_arg) == GLOBUS_TRUE)
	    {
		parent_key_save = parent_key;
		globus_thread_setspecific(
		    globus_l_activate_parent_key,
		    module_descriptor->activation_func);
		
		ret_val = module_descriptor->activation_func();
                
                if(ret_val != GLOBUS_SUCCESS)
                {
                    globus_l_module_decrement(
                        module_descriptor, parent_key_save);
                }
                else
                {
		/*
		 * Set up the exit handler
		 */
                    if(module_descriptor->atexit_func != GLOBUS_NULL)
                    {
                        /* only call the atexit function once */
                        if(!globus_list_search(
                            globus_l_module_atexit_funcs,
                            (void *) module_descriptor->atexit_func))
                        {
                            globus_list_insert(
                                &globus_l_module_atexit_funcs,
                                (void *) module_descriptor->atexit_func);

                            atexit(module_descriptor->atexit_func);
                        }
                    }
                }
                
                globus_thread_setspecific(
		    globus_l_activate_parent_key, parent_key_save);
	    }
	}
    }
    globus_l_module_mutex_unlock(&globus_l_module_mutex);

    return ret_val;
}
/* globus_module_activate() */

/**
 * @brief Activate a module
 * @ingroup globus_module
 * @details
 * Add a reference to the module named by module_descriptor to the list of
 * activated modules. If this is the first reference to this module, its
 * activation function will be called.
 *
 * @param module_descriptor
 *     Module to activate
 */
#ifdef __GNUC__
__attribute__ ((externally_visible))
#endif
int
globus_module_activate(
    globus_module_descriptor_t *	module_descriptor)
{
    return globus_module_activate_proxy(module_descriptor, NULL, NULL);
}

/**
 * @brief Activate a group of modules
 * @ingroup globus_module
 * @details
 * Activate an NULL-terminated array of modules. If any fail to activate, all
 * are deactivated and the error from the failed activation is returned. If
 * nonzero is returned, and failed_module is non-NULL, it will be set to point
 * to the 1st module which failed to activate. 
 * @param module_array
 *     NULL-terminated array of module descriptors to activate.
 * @param failed_module
 *     Pointer to set to the first module whose activation function failed.
 */
#ifdef __GNUC__
__attribute__ ((externally_visible))
#endif
int
globus_module_activate_array(
    globus_module_descriptor_t *        module_array[],
    globus_module_descriptor_t **       failed_module)
{
    int                                 i;
    int                                 rc = GLOBUS_SUCCESS;

    if (failed_module)
    {
        *failed_module = NULL;
    }
    for (i = 0; module_array[i] != NULL; i++)
    {
        rc = globus_module_activate(module_array[i]);

        if (rc != GLOBUS_SUCCESS)
        {
            if (failed_module)
            {
                *failed_module = module_array[i];
            }
            goto deactivate_out;
        }
    }

deactivate_out:
    if (rc != GLOBUS_SUCCESS)
    {
        for (--i; i >=0; i--)
        {
            globus_module_deactivate(module_array[i]);
        }
    }
    return rc;
}
/* globus_module_activate_array() */

#if USE_SYMBOL_LABELS
#ifdef __GNUC__
__attribute__ ((externally_visible))
#endif
int
globus_module_activate_proxy_compat(
    globus_module_descriptor_t *	module_descriptor,
    globus_module_deactivate_proxy_cb_t deactivate_cb,
    void *                              user_arg)
{
    int rc;
    rc = globus_thread_set_model("pthread");

    if (rc != GLOBUS_SUCCESS)
    {
        WARNING_USING_MIXED_THREAD_MODELS = 1;
    }
    return globus_module_activate_proxy(
        module_descriptor, deactivate_cb, user_arg);
}
/* globus_module_activate_proxy_compat() */

#ifdef __GNUC__
__attribute__ ((externally_visible))
#endif
int
globus_module_activate_compat(
    globus_module_descriptor_t *	module_descriptor)
{
    int rc;
    rc = globus_thread_set_model("pthread");

    if (rc != GLOBUS_SUCCESS)
    {
        WARNING_USING_MIXED_THREAD_MODELS = 1;
    }
    return globus_module_activate(module_descriptor);
}
/* globus_module_activate_compat() */

#ifdef __GNUC__
__attribute__ ((externally_visible))
#endif
int
globus_module_activate_array_compat(
    globus_module_descriptor_t *        module_array[],
    globus_module_descriptor_t **       failed_module)
{
    int rc;
    rc = globus_thread_set_model("pthread");

    if (rc != GLOBUS_SUCCESS)
    {
        WARNING_USING_MIXED_THREAD_MODELS = 1;

        abort();
    }

    return globus_module_activate_array(module_array, failed_module);
}
/* globus_module_activate_array_compat() */

#endif

/**
 * @brief Deactivate a module
 * @ingroup globus_module
 */
int
globus_module_deactivate(
    globus_module_descriptor_t *	module_descriptor)
{
    globus_l_module_key_t               parent_key;
    int					ret_val;
    globus_l_module_key_t		parent_key_save;


    /*
     * If module activation hasn't been initialized then return an error
     */
    if (!globus_i_module_initialized)
    {
	return GLOBUS_FAILURE;
    }
    
    parent_key = (globus_l_module_key_t)
        globus_thread_getspecific(globus_l_deactivate_parent_key);
    /*
     * Once the recursive mutex has been acquired, decrement the reference
     * counter for this module, and call it's deactivation function if it is
     * no longer being used.
     */
    ret_val = GLOBUS_SUCCESS;
    if (module_descriptor->activation_func != GLOBUS_NULL)
    {
        globus_l_module_entry_t *       entry;
        
        globus_l_module_mutex_lock(&globus_l_module_mutex);
        
        entry = globus_l_module_decrement(module_descriptor, parent_key);
        if (entry && entry->reference_count == 0)
        {
            globus_l_module_mutex_unlock(&globus_l_module_mutex);
            
            parent_key_save = parent_key;
            globus_thread_setspecific(
                globus_l_deactivate_parent_key,
                module_descriptor->activation_func);
            
            if(entry->deactivate_cb)
            {
                ret_val = entry->deactivate_cb(
                    module_descriptor, entry->user_arg);
            }
            else if(module_descriptor->deactivation_func != NULL)
            {
                ret_val = module_descriptor->deactivation_func();
            }
            
            globus_thread_setspecific(
                globus_l_deactivate_parent_key, parent_key_save);
        }
        else
        {
            if(globus_l_module_reference_count(module_descriptor) == 0)
            {
                ret_val = GLOBUS_FAILURE;
            }
            globus_l_module_mutex_unlock(&globus_l_module_mutex);
        }
    }

    return ret_val;
}
/* globus_module_deactivate() */

/*
 * globus_module_deactivate_all()
 */
int
globus_module_deactivate_all(void)
{
    /*
     * If module activation hasn't been initialized then return an error
     */
    if (!globus_i_module_initialized)
    {
	return GLOBUS_FAILURE;
    }
    
    globus_l_module_mutex_lock(&globus_l_module_mutex);
    {
	globus_bool_t			 deactivated_one;

	deactivated_one = GLOBUS_TRUE;

	while(deactivated_one)
	{
	    globus_list_t *		module_list;

	    module_list = globus_l_module_list;
	    deactivated_one = GLOBUS_FALSE;

	    while(!globus_list_empty(module_list))
	    {
		globus_l_module_entry_t *module_entry;

		module_entry = globus_list_first(module_list);
		module_list = globus_list_rest(module_list);
	    
		if(globus_list_empty(module_entry->clients) &&
		   module_entry->reference_count > 0)
		{
		    globus_l_module_mutex_unlock(&globus_l_module_mutex);
		    globus_module_deactivate(module_entry->descriptor);
		    globus_l_module_mutex_lock(&globus_l_module_mutex);
		    deactivated_one = GLOBUS_TRUE;
		}
	    }
	}
    }
    globus_l_module_mutex_unlock(&globus_l_module_mutex);

    return GLOBUS_SUCCESS;
}
/* globus_module_deactivate_all() */

/*
 * globus_module_get_module_pointer()
 *
 */

void *
globus_module_get_module_pointer(
    globus_module_descriptor_t *	structptr)
{
    void * retptr;
    void * (*module_func)();

    module_func=structptr->get_pointer_func;

    if (module_func!=NULL)
    {	
        retptr=(*module_func)();
    }
    else
    {
        retptr=GLOBUS_NULL;
    }

    return(retptr);
} 
/*globus_module_get_module_pointer();*/


/*
 * globus_module_setenv();
 */ 

void
globus_module_setenv(
    const char *                        name,
    const char *                        value)
{
    int				rc;

    /*
     *  First, check to see if the environment mutex has been initialized
     */

    if(globus_l_environ_mutex_initialized == GLOBUS_FALSE)
    {
	if(globus_i_module_initialized == GLOBUS_TRUE)
	{
	    rc = globus_mutex_init(&globus_l_environ_hashtable_mutex,
                           (globus_mutexattr_t *) GLOBUS_NULL);
            globus_assert (rc == 0);
	    globus_l_environ_mutex_initialized = GLOBUS_TRUE;
	}
    }
   
    /*
     *  then, check to see if the environment hash table has been initialized
     */
 

    if(globus_l_environ_initialized == GLOBUS_FALSE)
    {
	if(globus_i_module_initialized==GLOBUS_TRUE)
	{
	    globus_mutex_lock(&globus_l_environ_hashtable_mutex);
	}

        globus_hashtable_init(&globus_l_environ_table,
                          GLOBUS_L_ENVIRON_TABLE_SIZE,
                          globus_hashtable_string_hash,
                          globus_hashtable_string_keyeq);

	globus_l_environ_initialized = GLOBUS_TRUE;

	if(globus_i_module_initialized == GLOBUS_TRUE)
	{
	    globus_mutex_unlock(&globus_l_environ_hashtable_mutex);
	}
    }

    /*
     *  Then actually put the name and value into the hash table
     */

    if(globus_i_module_initialized == GLOBUS_TRUE)
    {
	globus_mutex_lock(&globus_l_environ_hashtable_mutex);
    }

    globus_hashtable_remove(
	&globus_l_environ_table,
	(void *) name);
    globus_hashtable_insert(
         &globus_l_environ_table,
         (void *) name,
         (void *) value);

    if(globus_i_module_initialized == GLOBUS_TRUE)
    {
	globus_mutex_unlock(&globus_l_environ_hashtable_mutex);
    }

}
/*globus_module_setenv();*/

/*
 * globus_module_getenv();
 */

char * 
globus_module_getenv(
    const char *                        name)
{
    char * 			entry;

    if(globus_l_environ_initialized == GLOBUS_TRUE)
    {
	if((globus_i_module_initialized == GLOBUS_TRUE)
	    &&(globus_l_environ_mutex_initialized == GLOBUS_TRUE))
	{
	    globus_mutex_lock(&globus_l_environ_hashtable_mutex);
	}

        entry =
           globus_hashtable_lookup(
               &globus_l_environ_table,
               (void *) name); 


	if((globus_i_module_initialized == GLOBUS_TRUE)
	    &&(globus_l_environ_mutex_initialized == GLOBUS_TRUE))
	{
	    globus_mutex_unlock(&globus_l_environ_hashtable_mutex);
	}
    }
    else
    {
        entry=GLOBUS_NULL;
    }

    /*
     *  If we have found an entry, return it
     */

    if (entry!=GLOBUS_NULL)
    {
	return(entry);
    }

    /*
     *  otherwise check system environment
     */

    entry=getenv(name);

    if (entry!=NULL)
    {
	return(entry);
    }

    return(GLOBUS_NULL);
}
/*globus_module_getenv();*/


/**
 * get version associated with module
 *
 * This function copies the version structure associated with the module
 * into 'version'.
 *
 * @param module_descriptor
 *        pointer to a module descriptor (module does NOT need to be activated)
 *
 * @param version
 *        pointer to storage for a globus_version_t.  The version will be
 *        copied into this
 *
 * @return
 *        - GLOBUS_SUCCESS
 *        - GLOBUS_FAILURE if there is no version associated with this module
 *          (module->version == null)
 */

int
globus_module_get_version(
    globus_module_descriptor_t *	module_descriptor,
    globus_version_t *                  version)
{
    globus_version_t *                  module_version;
    
    module_version = module_descriptor->version;
    
    if(!module_version)
    {
        return GLOBUS_FAILURE;
    }
    
    version->major      = module_version->major;       
    version->minor      = module_version->minor;       
    version->timestamp  = module_version->timestamp;   
    version->branch_id  = module_version->branch_id;   

    return GLOBUS_SUCCESS;
}


/**
 * print module's version
 *
 * This function prints a modules version info using the standard form 
 * provided by globus_version_print
 *
 * @param module_descriptor
 *        pointer to a module descriptor (module does NOT need to be activated)
 *
 * @param stream
 *        stream to print on (stdout, stderr, etc)
 *
 * @param verbose
 *        If GLOBUS_TRUE, then all available version info is printed 
 *        (ex: globus_module: 1.1 (1013708618-5))
 *        else, only the major.minor is printed (ex: globus_module: 1.1)
 *
 * @return
 *        - void
 */

void
globus_module_print_version(
    globus_module_descriptor_t *	module_descriptor,
    FILE *                              stream,
    globus_bool_t                       verbose)
{
    globus_version_print(
        module_descriptor->module_name,
        module_descriptor->version,
        stream,
        verbose);
}

/**
 * print all activated modules' versions
 *
 * This function prints all activated modules' version info using the standard 
 * form provided by globus_version_print
 *
 * @param stream
 *        stream to print on (stdout, stderr, etc)
 *
 * @param verbose
 *        If GLOBUS_TRUE, then all available version info is printed 
 *        (ex: globus_module: 1.1 (1013708618-5))
 *        else, only the major.minor is printed (ex: globus_module: 1.1)
 *
 * @return
 *        - void
 */

void
globus_module_print_activated_versions(
    FILE *                              stream,
    globus_bool_t                       verbose)
{
    /*
     * If module activation hasn't been initialized then there are no
     * activated modules
     */
    if(!globus_i_module_initialized)
    {
        return;
    }
    
    globus_l_module_mutex_lock(&globus_l_module_mutex);
    {
        globus_list_t *		        module_list;
        
        module_list = globus_l_module_list;
        while(!globus_list_empty(module_list))
        {
            globus_l_module_entry_t *       module_entry;
    
            module_entry = globus_list_first(module_list);
            module_list = globus_list_rest(module_list);
            
            if(module_entry->reference_count > 0)
            {
                globus_version_print(
                    module_entry->descriptor->module_name,
                    module_entry->descriptor->version,
                    stream,
                    verbose);
            }
        }
    }
    globus_l_module_mutex_unlock(&globus_l_module_mutex);

    return;
}


/**
 * print version structure
 *
 * This function provides a stand way of printing version information
 * The version is printed to stream with the following form:
 * name: major.minor                        if verbose = false
 * name: major.minor.timestamp-branch_id    if verbose = true
 *
 * In either case, if name is NULL, then only the numerical version will be 
 * printed.
 *
 * @param name
 *        A string to prefix the version.  May be NULL
 *
 * @param version
 *        The version structure containing the info to print.
 *        (May be NULL, although pointless to do so)
 *
 * @param stream
 *        stream to print on (stdout, stderr, etc)
 *
 * @param verbose
 *        If GLOBUS_TRUE, then all available version info is printed 
 *        (ex: globus_module: 1.1 (1013708618-5))
 *        else, only the major.minor is printed (ex: globus_module: 1.1)
 *
 * @return
 *        - void
 */

void
globus_version_print(
    const char *                        name,
    const globus_version_t *            version,
    FILE *                              stream,
    globus_bool_t                       verbose)
{
    if(name)
    {
        globus_libc_fprintf(stream, "%s: ", name);
    }
    
    if(version)
    {
        if(verbose)
        {
            globus_libc_fprintf(
                stream, 
                "%d.%d (%lu-%d)\n", 
                version->major,
                version->minor,
                version->timestamp,
                version->branch_id);
        }
        else
        {
            globus_libc_fprintf(
                stream, 
                "%d.%d\n", 
                version->major,
                version->minor);
        }
    }
    else
    {
        globus_libc_fprintf(stream, "%s", _GCSL("<no version>\n"));
    }
}


/******************************************************************************
		     Module specific function definitions
******************************************************************************/

/*
 * globus_l_module_initialize()
 */
static void
globus_l_module_initialize()
{
    /*
     * Initialize the threads package (can't use the standard interface since
     * it depends on threads)
     */
    globus_i_thread_pre_activate();
    globus_i_memory_pre_activate();
    /*
     * Initialize the registered module table and list
     */
    globus_hashtable_init(&globus_l_module_table,
			  GLOBUS_L_MODULE_TABLE_SIZE,
			  globus_hashtable_voidp_hash,
			  globus_hashtable_voidp_keyeq);

    globus_l_module_list = GLOBUS_NULL;
    
    /*
     * Initialize the recursive mutex
     */
    globus_l_module_mutex_init(&globus_l_module_mutex);
    
    globus_thread_key_create(&globus_l_activate_parent_key, NULL);
    globus_thread_key_create(&globus_l_deactivate_parent_key, NULL);
    
    /*
     * Now finish initializing the threads package
     */
    globus_module_activate(GLOBUS_THREAD_MODULE);
}
/* globus_l_module_initialize() */


/*
 * globus_l_module_increment()
 */
static globus_bool_t
globus_l_module_increment(
    globus_module_descriptor_t *	module_descriptor,
    globus_l_module_key_t		parent_key,
    globus_module_deactivate_proxy_cb_t deactivate_cb,
    void *                              user_arg)
{
    globus_l_module_entry_t *		entry;
    
    entry =
	globus_hashtable_lookup(
	    &globus_l_module_table,
	    (void *) module_descriptor->activation_func);

    if (entry != GLOBUS_NULL)
    {
	/*
	 * The module has already been registered.  Increment its reference
	 * counter and add any new clients to the dependency list
	 */
	entry->reference_count++;
	if (parent_key != GLOBUS_NULL
	    && globus_list_search(entry->clients,
				  (void *) parent_key) == GLOBUS_NULL)
	{
	    globus_list_insert(&entry->clients, (void *) parent_key);
	}

	if(entry->reference_count == 1)
	{
	    entry->deactivate_cb = deactivate_cb;
	    entry->user_arg = user_arg;
	    return GLOBUS_TRUE;
	}
	else
	{
    	    return GLOBUS_FALSE;
	}
    }
    else
    {
	/*
	 * This is the first time this module has been registered.  Create a
	 * new entry in the modules table.
	 */
	entry = (globus_l_module_entry_t *)
	    globus_malloc(sizeof(globus_l_module_entry_t));
	globus_assert(entry != GLOBUS_NULL);

	entry->descriptor = module_descriptor;
	entry->reference_count = 1;
	entry->clients = GLOBUS_NULL;
	entry->deactivate_cb = deactivate_cb;
	entry->user_arg = user_arg;
	if (parent_key != GLOBUS_NULL)
	{
	    globus_list_insert(&entry->clients, (void *) parent_key);
	}
	
	globus_hashtable_insert(
	    &globus_l_module_table,
	    (void *) module_descriptor->activation_func,
	    entry);

	globus_list_insert(&globus_l_module_list, entry);
	
	return GLOBUS_TRUE;
    }
}
/* globus_l_module_increment() */

static
int
globus_l_module_reference_count(
    globus_module_descriptor_t *	module_descriptor)
{
    globus_l_module_entry_t *		entry;
    
    entry =
	globus_hashtable_lookup(
	    &globus_l_module_table,
	    (void *) module_descriptor->activation_func);
    if (entry == GLOBUS_NULL || entry->reference_count <= 0)
    {
	return 0;
    }
    else
    {
        return entry->reference_count;
    }
}

/*
 * globus_l_module_decrement()
 */
static globus_l_module_entry_t *
globus_l_module_decrement(
    globus_module_descriptor_t *	module_descriptor,
    globus_l_module_key_t		parent_key)
{
    globus_l_module_entry_t *		entry;
    
    entry =
	globus_hashtable_lookup(
	    &globus_l_module_table,
	    (void *) module_descriptor->activation_func);
    if (entry == GLOBUS_NULL || entry->reference_count <= 0)
    {
	return NULL;
    }

    entry->reference_count--;
    
    if (parent_key != GLOBUS_NULL)
    {
	globus_list_t *			client_entry;

	
	client_entry = globus_list_search(entry->clients,
					  (void *) parent_key);
	if(client_entry != GLOBUS_NULL)
        {
	    globus_list_remove(&entry->clients, client_entry);
	}
	/* else module was activated outside this parent */
    }

    return entry;
}
/* globus_l_module_decrement() */


void
globus_i_module_dump(
    FILE *				out_f)
{
    globus_list_t *			module_list;

    globus_libc_fprintf(out_f, "==========\nModule List\n----------\n");
    
    module_list = globus_l_module_list;
    while(!globus_list_empty(module_list))
    {
	globus_list_t *			client_list;
	globus_l_module_entry_t *	module_entry;

	module_entry = globus_list_first(module_list);
	module_list = globus_list_rest(module_list);

	globus_libc_fprintf(out_f, "%s; cnt=%d",
		module_entry->descriptor->module_name,
		module_entry->reference_count);

	client_list = module_entry->clients;

	if (!globus_list_empty(client_list))
	{
	    void *			client_entry;
	    globus_l_module_entry_t *	client_module_entry;
	    
	    client_entry = globus_list_first(client_list);
	    client_list = globus_list_rest(client_list);
	    client_module_entry =
		globus_hashtable_lookup(&globus_l_module_table, client_entry);
	    globus_libc_fprintf(out_f, "; clients=%s",
		    client_module_entry->descriptor->module_name);
	    
	    while(!globus_list_empty(client_list))
	    {
		client_entry = globus_list_first(client_list);
		client_list = globus_list_rest(client_list);
		client_module_entry =
		    globus_hashtable_lookup(&globus_l_module_table,
					    client_entry);
		globus_libc_fprintf(out_f, ",%s",
			client_module_entry->descriptor->module_name);
	    }
	}

	globus_libc_fprintf(out_f, "\n");
    }

    globus_libc_fprintf(out_f, "==========\n");
}


/******************************************************************************
		     Recursive mutex function definitions
******************************************************************************/

/*
 * globus_l_module_mutex_init()
 */
static void
globus_l_module_mutex_init(
	globus_l_module_mutex_t *		mutex)
{
    globus_mutex_init(&mutex->mutex, (globus_mutexattr_t *) GLOBUS_NULL);
    globus_cond_init(&mutex->cond, (globus_condattr_t *) GLOBUS_NULL);

    mutex->level = 0;
}
/* globus_l_module_mutex_init() */

/*
 * globus_l_module_mutex_lock()
 */
static void
globus_l_module_mutex_lock(
	globus_l_module_mutex_t *		mutex)
{
    globus_mutex_lock(&mutex->mutex);
    {
	globus_assert(mutex->level >= 0);
	while (mutex->level > 0
		   && !globus_thread_equal( mutex->thread_id, globus_thread_self()) )
	{
	    globus_cond_wait(&mutex->cond, &mutex->mutex);
	}

	mutex->level++;
	mutex->thread_id = globus_thread_self();
	
    }
    globus_mutex_unlock(&mutex->mutex);
}
/* globus_l_module_mutex_lock() */

/*
 * globus_l_module_mutex_unlock()
 */
static void
globus_l_module_mutex_unlock(
	globus_l_module_mutex_t *		mutex)
{
    globus_mutex_lock(&mutex->mutex);
    {
	globus_assert(mutex->level > 0);
	globus_assert( globus_thread_equal( mutex->thread_id, globus_thread_self() ) );

	mutex->level--;
	if (mutex->level == 0)
	{
	    globus_cond_signal(&mutex->cond);
	}
    }
    globus_mutex_unlock(&mutex->mutex);
}
/* globus_l_module_mutex_unlock() */

void
globus_module_set_args(
    int *                               argc,
    char ***                            argv)
{
    globus_l_module_argc = argc;
    globus_l_module_argv = argv;
}

void
globus_module_get_args(
    int **                               argc,
    char ****                            argv)
{
    *argc = globus_l_module_argc;
    *argv = globus_l_module_argv;
}