2003-12-22 19:29:21 +03:00
|
|
|
/*
|
2004-11-22 04:38:40 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University.
|
|
|
|
* All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The Trustees of the University of Tennessee.
|
|
|
|
* All rights reserved.
|
2004-11-28 23:09:25 +03:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
|
|
|
* University of Stuttgart. All rights reserved.
|
2005-03-24 15:43:37 +03:00
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
2004-11-22 04:38:40 +03:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-01-07 10:57:00 +03:00
|
|
|
* $HEADER$
|
2003-12-22 19:29:21 +03:00
|
|
|
*/
|
2004-01-07 10:57:00 +03:00
|
|
|
|
2004-02-10 17:04:27 +03:00
|
|
|
/**
|
2004-02-13 00:30:06 +03:00
|
|
|
* @file
|
|
|
|
*
|
2004-06-07 19:33:53 +04:00
|
|
|
* Implementation of ompi_object_t, the base ompi foundation class
|
2004-02-10 17:04:27 +03:00
|
|
|
*/
|
2003-12-22 19:29:21 +03:00
|
|
|
|
2004-08-19 03:24:27 +04:00
|
|
|
#include "ompi_config.h"
|
|
|
|
|
2004-02-13 00:30:06 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2004-03-19 00:35:28 +03:00
|
|
|
#include "include/constants.h"
|
2005-03-25 01:13:05 +03:00
|
|
|
#include "include/sys/atomic.h"
|
2004-06-07 19:33:53 +04:00
|
|
|
#include "class/ompi_object.h"
|
2003-12-22 19:29:21 +03:00
|
|
|
|
2004-02-13 00:30:06 +03:00
|
|
|
/*
|
|
|
|
* Instantiation of class descriptor for the base class. This is
|
|
|
|
* special, since be mark it as already initialized, with no parent
|
|
|
|
* and no constructor or desctructor
|
|
|
|
*/
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_class_t ompi_object_t_class = {
|
|
|
|
"ompi_object_t", /* name */
|
2004-02-13 00:30:06 +03:00
|
|
|
NULL, /* parent class */
|
|
|
|
NULL, /* constructor */
|
|
|
|
NULL, /* destructor */
|
|
|
|
1, /* initialized -- this class is preinitialized */
|
|
|
|
0, /* class hierarchy depth */
|
|
|
|
NULL, /* array of constructors */
|
|
|
|
NULL /* array of destructors */
|
|
|
|
};
|
2003-12-22 19:29:21 +03:00
|
|
|
|
2005-03-25 01:13:05 +03:00
|
|
|
/*
|
|
|
|
* Local variables
|
|
|
|
*/
|
|
|
|
static ompi_lock_t class_lock = { { OMPI_ATOMIC_UNLOCKED } };
|
|
|
|
static void** classes = NULL;
|
|
|
|
static int num_classes = 0;
|
|
|
|
static int max_classes = 0;
|
|
|
|
static const int increment = 10;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local functions
|
|
|
|
*/
|
|
|
|
static void save_class(ompi_class_t *cls);
|
|
|
|
static void expand_array(void);
|
|
|
|
|
2004-02-10 17:04:27 +03:00
|
|
|
|
2004-02-13 00:30:06 +03:00
|
|
|
/*
|
|
|
|
* Lazy initialization of class descriptor.
|
|
|
|
*/
|
2004-10-18 20:55:20 +04:00
|
|
|
void ompi_class_initialize(ompi_class_t *cls)
|
2003-12-22 19:29:21 +03:00
|
|
|
{
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_class_t *c;
|
2004-02-13 00:30:06 +03:00
|
|
|
int i;
|
2003-12-22 19:29:21 +03:00
|
|
|
|
2004-10-18 20:55:20 +04:00
|
|
|
assert(cls);
|
2005-03-25 01:13:05 +03:00
|
|
|
|
|
|
|
/* Check to see if any other thread got in here and initialized
|
|
|
|
this class before we got a chance to */
|
|
|
|
|
|
|
|
if (1 == cls->cls_initialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ompi_atomic_lock(&class_lock);
|
|
|
|
|
|
|
|
/* If another thread initializing this same class came in at
|
|
|
|
roughly the same time, it may have gotten the lock and
|
|
|
|
initialized. So check again. */
|
|
|
|
|
|
|
|
if (1 == cls->cls_initialized) {
|
|
|
|
ompi_atomic_unlock(&class_lock);
|
|
|
|
return;
|
|
|
|
}
|
2004-02-10 17:04:27 +03:00
|
|
|
|
2004-02-13 00:30:06 +03:00
|
|
|
/*
|
|
|
|
* First calculate depth of class hierarchy
|
|
|
|
*/
|
|
|
|
|
2004-10-18 20:55:20 +04:00
|
|
|
cls->cls_depth = 0;
|
|
|
|
for (c = cls; c; c = c->cls_parent) {
|
|
|
|
cls->cls_depth += 1;
|
2004-02-13 00:30:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate arrays for hierarchy of constructors and destructors
|
|
|
|
*/
|
2004-02-10 17:04:27 +03:00
|
|
|
|
2005-03-25 01:13:05 +03:00
|
|
|
cls->cls_construct_array =
|
|
|
|
(void (**)(ompi_object_t*))malloc(cls->cls_depth *
|
|
|
|
sizeof(ompi_construct_t) * 2);
|
2004-10-18 20:55:20 +04:00
|
|
|
if (NULL == cls->cls_construct_array) {
|
2004-02-13 00:30:06 +03:00
|
|
|
perror("Out of memory");
|
2005-03-25 01:13:05 +03:00
|
|
|
exit(-1);
|
2004-02-13 00:30:06 +03:00
|
|
|
}
|
2005-03-25 01:13:05 +03:00
|
|
|
cls->cls_destruct_array = cls->cls_construct_array + cls->cls_depth;
|
2004-02-13 00:30:06 +03:00
|
|
|
|
2004-10-18 20:55:20 +04:00
|
|
|
c = cls;
|
|
|
|
for (i = 0; i < cls->cls_depth; i++) {
|
|
|
|
cls->cls_construct_array[i] = c->cls_construct;
|
|
|
|
cls->cls_destruct_array[i] = c->cls_destruct;
|
2004-02-13 00:30:06 +03:00
|
|
|
c = c->cls_parent;
|
|
|
|
}
|
2004-02-10 17:04:27 +03:00
|
|
|
|
2004-10-18 20:55:20 +04:00
|
|
|
cls->cls_initialized = 1;
|
2005-03-25 01:13:05 +03:00
|
|
|
save_class(cls);
|
|
|
|
|
|
|
|
/* All done */
|
|
|
|
|
|
|
|
ompi_atomic_unlock(&class_lock);
|
2004-02-13 00:30:06 +03:00
|
|
|
}
|
2005-03-25 01:13:05 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that this is finalize for *all* classes.
|
|
|
|
*/
|
|
|
|
int ompi_class_finalize(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (NULL != classes) {
|
|
|
|
for (i = 0; i < num_classes; ++i) {
|
|
|
|
if (NULL != classes[i]) {
|
|
|
|
free(classes[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(classes);
|
|
|
|
classes = NULL;
|
|
|
|
num_classes = 0;
|
|
|
|
max_classes = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OMPI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void save_class(ompi_class_t *cls)
|
|
|
|
{
|
|
|
|
if (num_classes >= max_classes) {
|
|
|
|
expand_array();
|
|
|
|
}
|
|
|
|
|
|
|
|
classes[num_classes] = cls->cls_construct_array;
|
|
|
|
++num_classes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void expand_array(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
max_classes += increment;
|
|
|
|
classes = realloc(classes, sizeof(void *) * max_classes);
|
|
|
|
if (NULL == classes) {
|
|
|
|
perror("class malloc failed");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
for (i = num_classes; i < max_classes; ++i) {
|
|
|
|
classes[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|