Changeset c28413a9 in mainline


Ignore:
Timestamp:
2012-11-20T18:50:04Z (11 years ago)
Author:
Adam Hraska <adam.hraska+hos@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
ba368a6
Parents:
669f3d32
Message:

cht: Added cht_create_simple(). cht_create() can now block until memory is available.

Location:
kernel
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/include/adt/cht.h

    r669f3d32 rc28413a9  
    132132#define cht_read_unlock()   rcu_read_unlock()
    133133
     134extern bool cht_create_simple(cht_t *h, cht_ops_t *op);
    134135extern bool cht_create(cht_t *h, size_t init_size, size_t min_size,
    135         size_t max_load, cht_ops_t *op);
     136        size_t max_load, bool can_block, cht_ops_t *op);
    136137extern void cht_destroy(cht_t *h);
    137138
  • kernel/generic/src/adt/cht.c

    r669f3d32 rc28413a9  
    408408
    409409static size_t size_to_order(size_t bucket_cnt, size_t min_order);
    410 static cht_buckets_t *alloc_buckets(size_t order, bool set_invalid);
     410static cht_buckets_t *alloc_buckets(size_t order, bool set_invalid,
     411        bool can_block);
    411412static inline cht_link_t *find_lazy(cht_t *h, void *key);
    412413static cht_link_t *search_bucket(cht_t *h, marked_ptr_t head, void *key,
     
    478479static void cas_order_barrier(void);
    479480
     481static void dummy_remove_callback(cht_link_t *item)
     482{
     483        /* empty */
     484}
     485
     486/** Creates a concurrent hash table.
     487 *
     488 * @param h         Valid pointer to a cht_t instance.
     489 * @param op        Item specific operations. All operations are compulsory.
     490 * @return True if successfully created the table. False otherwise.
     491 */
     492bool cht_create_simple(cht_t *h, cht_ops_t *op)
     493{
     494        return cht_create(h, 0, 0, 0, false, op);
     495}
     496
    480497/** Creates a concurrent hash table.
    481498 *
     
    490507 * @param max_load  Maximum average number of items per bucket that allowed
    491508 *                  before the table grows.
     509 * @param can_block If true creating the table blocks until enough memory
     510 *                  is available (possibly indefinitely). Otherwise,
     511 *                  table creation does not block and returns immediately
     512 *                  even if not enough memory is available.
    492513 * @param op        Item specific operations. All operations are compulsory.
    493514 * @return True if successfully created the table. False otherwise.
    494515 */
    495516bool cht_create(cht_t *h, size_t init_size, size_t min_size, size_t max_load,
    496         cht_ops_t *op)
     517        bool can_block, cht_ops_t *op)
    497518{
    498519        ASSERT(h);
     
    509530        size_t order = size_to_order(init_size, min_order);
    510531       
    511         h->b = alloc_buckets(order, false);
     532        h->b = alloc_buckets(order, false, can_block);
    512533       
    513534        if (!h->b)
     
    520541        atomic_set(&h->item_cnt, 0);
    521542        atomic_set(&h->resize_reqs, 0);
     543       
     544        if (NULL == op->remove_callback) {
     545                h->op->remove_callback = dummy_remove_callback;
     546        }
     547       
    522548        /*
    523549         * Cached item hashes are stored in item->rcu_link.func. Once the item
     
    539565 * @param set_invalid Bucket heads are marked invalid if true; otherwise
    540566 *                    they are marked N_NORMAL.
     567 * @param can_block   If true memory allocation blocks until enough memory
     568 *                    is available (possibly indefinitely). Otherwise,
     569 *                    memory allocation does not block.
    541570 * @return Newly allocated and initialized buckets or NULL if not enough memory.
    542571 */
    543 static cht_buckets_t *alloc_buckets(size_t order, bool set_invalid)
     572static cht_buckets_t *alloc_buckets(size_t order, bool set_invalid, bool can_block)
    544573{
    545574        size_t bucket_cnt = (1 << order);
    546575        size_t bytes =
    547576                sizeof(cht_buckets_t) + (bucket_cnt - 1) * sizeof(marked_ptr_t);
    548         cht_buckets_t *b = malloc(bytes, FRAME_ATOMIC);
     577        cht_buckets_t *b = malloc(bytes, can_block ? 0 : FRAME_ATOMIC);
    549578       
    550579        if (!b)
     
    21452174                return;
    21462175       
    2147         h->new_b = alloc_buckets(h->b->order + 1, true);
     2176        h->new_b = alloc_buckets(h->b->order + 1, true, false);
    21482177
    21492178        /* Failed to alloc a new table - try next time the resizer is run. */
     
    22312260                return;
    22322261       
    2233         h->new_b = alloc_buckets(h->b->order - 1, true);
     2262        h->new_b = alloc_buckets(h->b->order - 1, true, false);
    22342263
    22352264        /* Failed to alloc a new table - try next time the resizer is run. */
  • kernel/generic/src/synch/futex.c

    r669f3d32 rc28413a9  
    133133        task->futexes = malloc(sizeof(struct futex_cache), 0);
    134134       
    135         /* todo: make it block until mem is available */
    136         bool ret = cht_create(&task->futexes->ht, 0, 0, 0, &task_futex_ht_ops);
    137         ASSERT(ret);
     135        cht_create(&task->futexes->ht, 0, 0, 0, true, &task_futex_ht_ops);
    138136}
    139137
  • kernel/test/cht/cht1.c

    r669f3d32 rc28413a9  
    237237{
    238238        cht_t h;
    239         if (!cht_create(&h, 5, 0, 0, &val_ops))
     239        if (!cht_create_simple(&h, &val_ops))
    240240                return "Could not create the table.";
    241241       
     
    453453        cht_t h;
    454454       
    455         if (!cht_create(&h, 0, 0, 0, &stress_ops)) {
     455        if (!cht_create_simple(&h, &stress_ops)) {
    456456                TPRINTF("Failed to create the table\n");
    457457                return false;
Note: See TracChangeset for help on using the changeset viewer.