2004-06-15 23:46:26 +04:00
|
|
|
/*
|
2005-11-05 22:57:48 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
|
|
|
|
* University Research and Technology
|
|
|
|
* Corporation. All rights reserved.
|
2006-08-23 04:29:35 +04:00
|
|
|
* Copyright (c) 2004-2006 The University of Tennessee and The University
|
2005-11-05 22:57:48 +03:00
|
|
|
* of Tennessee Research Foundation. 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-06-15 23:46:26 +04:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2005-07-03 04:52:18 +04:00
|
|
|
#ifndef OPAL_FREE_LIST_H
|
|
|
|
#define OPAL_FREE_LIST_H
|
2004-06-15 23:46:26 +04:00
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal_config.h"
|
2005-07-02 20:46:27 +04:00
|
|
|
|
2005-07-03 20:22:16 +04:00
|
|
|
#include "opal/class/opal_list.h"
|
2005-07-02 20:46:27 +04:00
|
|
|
#include "opal/threads/condition.h"
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal/constants.h"
|
2007-04-12 10:18:01 +04:00
|
|
|
#include "opal/prefetch.h"
|
2004-06-15 23:46:26 +04:00
|
|
|
|
2009-03-17 18:11:48 +03:00
|
|
|
BEGIN_C_DECLS
|
2004-06-15 23:46:26 +04:00
|
|
|
|
2005-07-02 19:42:29 +04:00
|
|
|
struct opal_free_list_t
|
2004-06-15 23:46:26 +04:00
|
|
|
{
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_t super;
|
2004-10-20 03:58:12 +04: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 23:46:26 +04:00
|
|
|
size_t fl_elem_size;
|
2005-07-03 20:06:07 +04:00
|
|
|
opal_class_t* fl_elem_class;
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_t fl_lock;
|
|
|
|
opal_condition_t fl_condition;
|
2005-09-03 23:46:44 +04:00
|
|
|
opal_list_t fl_allocations;
|
2004-06-15 23:46:26 +04:00
|
|
|
};
|
2005-07-02 19:42:29 +04:00
|
|
|
typedef struct opal_free_list_t opal_free_list_t;
|
2006-08-23 04:29:35 +04:00
|
|
|
OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_free_list_t);
|
2004-06-15 23:46:26 +04:00
|
|
|
|
2005-07-02 19:42:29 +04:00
|
|
|
struct opal_free_list_item_t
|
2005-06-07 00:20:47 +04:00
|
|
|
{
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_item_t super;
|
2005-06-07 00:20:47 +04:00
|
|
|
};
|
2005-07-02 19:42:29 +04:00
|
|
|
typedef struct opal_free_list_item_t opal_free_list_item_t;
|
2006-08-23 04:29:35 +04:00
|
|
|
OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_free_list_item_t);
|
2004-06-15 23:46:26 +04:00
|
|
|
|
2004-08-03 01:24:00 +04:00
|
|
|
/**
|
|
|
|
* Initialize a free list.
|
|
|
|
*
|
|
|
|
* @param free_list (IN) Free list.
|
|
|
|
* @param element_size (IN) Size of each element.
|
2005-07-03 20:06:07 +04:00
|
|
|
* @param element_class (IN) opal_class_t of element - used to initialize allocated elements.
|
2004-08-03 01:24:00 +04: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.
|
|
|
|
*/
|
|
|
|
|
2006-08-20 19:54:04 +04:00
|
|
|
OPAL_DECLSPEC int opal_free_list_init(
|
2005-07-02 19:51:22 +04:00
|
|
|
opal_free_list_t *free_list,
|
2004-06-15 23:46:26 +04:00
|
|
|
size_t element_size,
|
2005-07-03 20:06:07 +04:00
|
|
|
opal_class_t* element_class,
|
2004-06-15 23:46:26 +04:00
|
|
|
int num_elements_to_alloc,
|
|
|
|
int max_elements_to_alloc,
|
2005-07-02 19:42:29 +04:00
|
|
|
int num_elements_per_alloc);
|
2004-06-15 23:46:26 +04:00
|
|
|
|
2007-03-20 16:01:32 +03:00
|
|
|
OPAL_DECLSPEC int opal_free_list_grow(opal_free_list_t* flist, size_t num_elements) __opal_attribute_nonnull__(1);
|
2004-06-15 23:46:26 +04:00
|
|
|
|
2004-08-03 01:24:00 +04:00
|
|
|
/**
|
|
|
|
* Attemp to obtain an item from a free list.
|
|
|
|
*
|
|
|
|
* @param fl (IN) Free list.
|
|
|
|
* @param item (OUT) Allocated item.
|
2006-02-12 04:33:29 +03:00
|
|
|
* @param rc (OUT) OPAL_SUCCESS or error status on failure.
|
2004-08-03 01:24:00 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2006-07-18 01:51:50 +04:00
|
|
|
#define OPAL_FREE_LIST_GET(fl, item, rc) \
|
|
|
|
{ \
|
|
|
|
if(opal_using_threads()) { \
|
|
|
|
opal_mutex_lock(&((fl)->fl_lock)); \
|
|
|
|
item = (opal_free_list_item_t*) \
|
|
|
|
opal_list_remove_first(&((fl)->super)); \
|
2007-04-12 10:18:01 +04:00
|
|
|
if( OPAL_UNLIKELY(NULL == item) ) { \
|
2006-07-18 01:51:50 +04:00
|
|
|
opal_free_list_grow((fl), (fl)->fl_num_per_alloc); \
|
|
|
|
item = (opal_free_list_item_t*) \
|
|
|
|
opal_list_remove_first(&((fl)->super)); \
|
|
|
|
} \
|
|
|
|
opal_mutex_unlock(&((fl)->fl_lock)); \
|
|
|
|
} else { \
|
|
|
|
item = (opal_free_list_item_t*) \
|
|
|
|
opal_list_remove_first(&((fl)->super)); \
|
2007-04-12 10:18:01 +04:00
|
|
|
if( OPAL_UNLIKELY(NULL == item) ) { \
|
2006-07-18 01:51:50 +04:00
|
|
|
opal_free_list_grow((fl), (fl)->fl_num_per_alloc); \
|
|
|
|
item = (opal_free_list_item_t*) \
|
|
|
|
opal_list_remove_first(&((fl)->super)); \
|
|
|
|
} \
|
|
|
|
} \
|
2006-02-12 04:33:29 +03:00
|
|
|
rc = (NULL == item) ? OPAL_ERR_TEMP_OUT_OF_RESOURCE : OPAL_SUCCESS; \
|
2006-07-18 01:51:50 +04:00
|
|
|
}
|
2004-06-15 23:46:26 +04:00
|
|
|
|
2004-08-03 01:24:00 +04:00
|
|
|
/**
|
|
|
|
* Blocking call to obtain an item from a free list.
|
|
|
|
*
|
|
|
|
* @param fl (IN) Free list.
|
|
|
|
* @param item (OUT) Allocated item.
|
2006-02-12 04:33:29 +03:00
|
|
|
* @param rc (OUT) OPAL_SUCCESS or error status on failure.
|
2004-08-03 01:24:00 +04:00
|
|
|
*
|
|
|
|
* 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 23:46:26 +04:00
|
|
|
|
2007-04-12 10:18:01 +04:00
|
|
|
#define OPAL_FREE_LIST_WAIT(fl, item, rc) \
|
|
|
|
do { \
|
|
|
|
OPAL_THREAD_LOCK(&((fl)->fl_lock)); \
|
|
|
|
while( NULL == (item = (opal_free_list_item_t*) opal_list_remove_first(&((fl)->super))) ) { \
|
|
|
|
if( OPAL_LIKELY((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 { \
|
|
|
|
opal_free_list_grow((fl), (fl)->fl_num_per_alloc); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
OPAL_THREAD_UNLOCK(&((fl)->fl_lock)); \
|
|
|
|
rc = OPAL_SUCCESS; \
|
|
|
|
} while(0)
|
2004-07-15 22:08:20 +04:00
|
|
|
|
|
|
|
|
2004-08-03 01:24:00 +04:00
|
|
|
/**
|
|
|
|
* Return an item to a free list.
|
|
|
|
*
|
|
|
|
* @param fl (IN) Free list.
|
|
|
|
* @param item (OUT) Allocated item.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-04-12 10:18:01 +04:00
|
|
|
#define OPAL_FREE_LIST_RETURN(fl, item) \
|
|
|
|
do { \
|
|
|
|
OPAL_THREAD_LOCK(&(fl)->fl_lock); \
|
|
|
|
opal_list_prepend(&((fl)->super), ((opal_list_item_t*) item)); \
|
|
|
|
if( OPAL_UNLIKELY((fl)->fl_num_waiting > 0) ) { \
|
|
|
|
opal_condition_signal(&((fl)->fl_condition)); \
|
|
|
|
} \
|
|
|
|
OPAL_THREAD_UNLOCK(&(fl)->fl_lock); \
|
|
|
|
} while(0)
|
|
|
|
|
2009-03-17 18:11:48 +03:00
|
|
|
END_C_DECLS
|
|
|
|
|
2004-06-15 23:46:26 +04:00
|
|
|
#endif
|
|
|
|
|