2004-06-15 19:46:26 +00:00
|
|
|
/*
|
2005-11-05 19:57:48 +00:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
|
|
|
|
* University Research and Technology
|
|
|
|
* Corporation. All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The University of Tennessee and The University
|
|
|
|
* of Tennessee Research Foundation. All rights
|
|
|
|
* reserved.
|
2004-11-28 20:09:25 +00:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
|
|
|
* University of Stuttgart. All rights reserved.
|
2005-03-24 12:43:37 +00:00
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
2004-11-22 01:38:40 +00:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-06-15 19:46:26 +00:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OMPI_FREE_LIST_H
|
|
|
|
#define OMPI_FREE_LIST_H
|
|
|
|
|
|
|
|
#include "ompi_config.h"
|
2005-07-03 16:22:16 +00:00
|
|
|
#include "opal/class/opal_list.h"
|
2005-08-15 11:02:01 +00:00
|
|
|
#include "opal/threads/threads.h"
|
2005-07-03 22:45:48 +00:00
|
|
|
#include "opal/threads/condition.h"
|
2006-02-12 01:33:29 +00:00
|
|
|
#include "ompi/constants.h"
|
|
|
|
#include "ompi/mca/mpool/mpool.h"
|
2004-06-15 19:46:26 +00:00
|
|
|
|
2004-10-20 22:31:03 +00:00
|
|
|
#if defined(c_plusplus) || defined(__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2005-07-03 16:06:07 +00:00
|
|
|
OMPI_DECLSPEC extern opal_class_t ompi_free_list_t_class;
|
2004-06-15 19:46:26 +00:00
|
|
|
struct mca_mem_pool_t;
|
|
|
|
|
|
|
|
|
|
|
|
struct ompi_free_list_t
|
|
|
|
{
|
2005-07-03 16:22:16 +00:00
|
|
|
opal_list_t super;
|
2004-10-19 23:58:12 +00:00
|
|
|
size_t fl_max_to_alloc;
|
|
|
|
size_t fl_num_allocated;
|
|
|
|
size_t fl_num_per_alloc;
|
|
|
|
size_t fl_num_waiting;
|
2004-06-15 19:46:26 +00:00
|
|
|
size_t fl_elem_size;
|
2005-07-03 16:06:07 +00:00
|
|
|
opal_class_t* fl_elem_class;
|
2004-08-02 00:24:22 +00:00
|
|
|
mca_mpool_base_module_t* fl_mpool;
|
2005-07-03 22:45:48 +00:00
|
|
|
opal_mutex_t fl_lock;
|
|
|
|
opal_condition_t fl_condition;
|
2005-09-03 19:46:44 +00:00
|
|
|
opal_list_t fl_allocations;
|
2004-06-15 19:46:26 +00:00
|
|
|
};
|
|
|
|
typedef struct ompi_free_list_t ompi_free_list_t;
|
|
|
|
|
2005-06-06 20:20:47 +00:00
|
|
|
struct ompi_free_list_item_t
|
|
|
|
{
|
2005-07-03 16:22:16 +00:00
|
|
|
opal_list_item_t super;
|
2005-06-06 20:20:47 +00:00
|
|
|
void* user_data;
|
|
|
|
};
|
|
|
|
typedef struct ompi_free_list_item_t ompi_free_list_item_t;
|
2004-06-15 19:46:26 +00:00
|
|
|
|
2004-08-02 21:24:00 +00:00
|
|
|
/**
|
|
|
|
* Initialize a free list.
|
|
|
|
*
|
|
|
|
* @param free_list (IN) Free list.
|
|
|
|
* @param element_size (IN) Size of each element.
|
2005-07-03 16:06:07 +00:00
|
|
|
* @param element_class (IN) opal_class_t of element - used to initialize allocated elements.
|
2004-08-02 21:24:00 +00:00
|
|
|
* @param num_elements_to_alloc Initial number of elements to allocate.
|
|
|
|
* @param max_elements_to_alloc Maximum number of elements to allocate.
|
|
|
|
* @param num_elements_per_alloc Number of elements to grow by per allocation.
|
|
|
|
* @param mpool Optional memory pool for allocation.s
|
|
|
|
*/
|
|
|
|
|
2004-10-28 18:13:43 +00:00
|
|
|
OMPI_DECLSPEC int ompi_free_list_init(
|
2004-08-02 21:24:00 +00:00
|
|
|
ompi_free_list_t *free_list,
|
2004-06-15 19:46:26 +00:00
|
|
|
size_t element_size,
|
2005-07-03 16:06:07 +00:00
|
|
|
opal_class_t* element_class,
|
2004-06-15 19:46:26 +00:00
|
|
|
int num_elements_to_alloc,
|
|
|
|
int max_elements_to_alloc,
|
|
|
|
int num_elements_per_alloc,
|
2004-08-02 00:24:22 +00:00
|
|
|
mca_mpool_base_module_t*);
|
2004-06-15 19:46:26 +00:00
|
|
|
|
2004-10-28 18:13:43 +00:00
|
|
|
OMPI_DECLSPEC int ompi_free_list_grow(ompi_free_list_t* flist, size_t num_elements);
|
2004-06-15 19:46:26 +00:00
|
|
|
|
2004-08-02 21:24:00 +00:00
|
|
|
/**
|
|
|
|
* Attemp to obtain an item from a free list.
|
|
|
|
*
|
|
|
|
* @param fl (IN) Free list.
|
|
|
|
* @param item (OUT) Allocated item.
|
|
|
|
* @param rc (OUT) OMPI_SUCCESS or error status on failure.
|
|
|
|
*
|
|
|
|
* If the requested item is not available the free list is grown to
|
|
|
|
* accomodate the request - unless the max number of allocations has
|
|
|
|
* been reached. If this is the case - an out of resource error is
|
|
|
|
* returned to the caller.
|
|
|
|
*/
|
|
|
|
|
2004-06-15 19:46:26 +00:00
|
|
|
#define OMPI_FREE_LIST_GET(fl, item, rc) \
|
|
|
|
{ \
|
2005-07-03 22:45:48 +00:00
|
|
|
if(opal_using_threads()) { \
|
|
|
|
opal_mutex_lock(&((fl)->fl_lock)); \
|
2005-07-03 16:22:16 +00:00
|
|
|
item = opal_list_remove_first(&((fl)->super)); \
|
2004-06-15 19:46:26 +00:00
|
|
|
if(NULL == item) { \
|
|
|
|
ompi_free_list_grow((fl), (fl)->fl_num_per_alloc); \
|
2005-07-03 16:22:16 +00:00
|
|
|
item = opal_list_remove_first(&((fl)->super)); \
|
2004-06-15 19:46:26 +00:00
|
|
|
} \
|
2005-07-03 22:45:48 +00:00
|
|
|
opal_mutex_unlock(&((fl)->fl_lock)); \
|
2004-06-15 19:46:26 +00:00
|
|
|
} else { \
|
2005-07-03 16:22:16 +00:00
|
|
|
item = opal_list_remove_first(&((fl)->super)); \
|
2004-06-15 19:46:26 +00:00
|
|
|
if(NULL == item) { \
|
|
|
|
ompi_free_list_grow((fl), (fl)->fl_num_per_alloc); \
|
2005-07-03 16:22:16 +00:00
|
|
|
item = opal_list_remove_first(&((fl)->super)); \
|
2004-06-15 19:46:26 +00:00
|
|
|
} \
|
|
|
|
} \
|
|
|
|
rc = (NULL == item) ? OMPI_ERR_TEMP_OUT_OF_RESOURCE : OMPI_SUCCESS; \
|
|
|
|
}
|
|
|
|
|
2004-08-02 21:24:00 +00:00
|
|
|
/**
|
|
|
|
* Blocking call to obtain an item from a free list.
|
|
|
|
*
|
|
|
|
* @param fl (IN) Free list.
|
|
|
|
* @param item (OUT) Allocated item.
|
|
|
|
* @param rc (OUT) OMPI_SUCCESS or error status on failure.
|
|
|
|
*
|
|
|
|
* If the requested item is not available the free list is grown to
|
|
|
|
* accomodate the request - unless the max number of allocations has
|
|
|
|
* been reached. In this case the caller is blocked until an item
|
|
|
|
* is returned to the list.
|
|
|
|
*/
|
|
|
|
|
2004-06-15 19:46:26 +00:00
|
|
|
|
2006-01-13 22:02:40 +00:00
|
|
|
#define OMPI_FREE_LIST_WAIT(fl, item, rc) \
|
|
|
|
{ \
|
|
|
|
OPAL_THREAD_LOCK(&((fl)->fl_lock)); \
|
|
|
|
item = opal_list_remove_first(&((fl)->super)); \
|
|
|
|
while(NULL == item) { \
|
|
|
|
if((fl)->fl_max_to_alloc <= (fl)->fl_num_allocated) { \
|
|
|
|
(fl)->fl_num_waiting++; \
|
|
|
|
opal_condition_wait(&((fl)->fl_condition), &((fl)->fl_lock)); \
|
|
|
|
(fl)->fl_num_waiting--; \
|
|
|
|
} else { \
|
|
|
|
ompi_free_list_grow((fl), (fl)->fl_num_per_alloc); \
|
|
|
|
} \
|
|
|
|
item = opal_list_remove_first(&((fl)->super)); \
|
|
|
|
} \
|
|
|
|
OPAL_THREAD_UNLOCK(&((fl)->fl_lock)); \
|
|
|
|
rc = (NULL == item) ? OMPI_ERR_OUT_OF_RESOURCE : OMPI_SUCCESS; \
|
2004-07-15 18:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-02 21:24:00 +00:00
|
|
|
/**
|
|
|
|
* Return an item to a free list.
|
|
|
|
*
|
|
|
|
* @param fl (IN) Free list.
|
|
|
|
* @param item (OUT) Allocated item.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define OMPI_FREE_LIST_RETURN(fl, item) \
|
|
|
|
{ \
|
2005-07-03 22:45:48 +00:00
|
|
|
OPAL_THREAD_LOCK(&(fl)->fl_lock); \
|
2005-07-03 16:22:16 +00:00
|
|
|
opal_list_prepend(&((fl)->super), (item)); \
|
2004-08-02 21:24:00 +00:00
|
|
|
if((fl)->fl_num_waiting > 0) { \
|
2005-07-03 22:45:48 +00:00
|
|
|
opal_condition_signal(&((fl)->fl_condition)); \
|
2004-08-02 21:24:00 +00:00
|
|
|
} \
|
2005-07-03 22:45:48 +00:00
|
|
|
OPAL_THREAD_UNLOCK(&(fl)->fl_lock); \
|
2004-08-02 21:24:00 +00:00
|
|
|
}
|
2004-10-20 22:31:03 +00:00
|
|
|
#if defined(c_plusplus) || defined(__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
2004-06-15 19:46:26 +00:00
|
|
|
#endif
|
|
|
|
|