|
|
| version 1.11, 2007/07/16 15:56:17 | version 1.12, 2008/09/05 04:10:01 |
|---|---|
| Line 201 cache_shake( | Line 201 cache_shake( |
| continue; | continue; |
| hash = cache->c_hash + node->cn_hashidx; | hash = cache->c_hash + node->cn_hashidx; |
| if (node->cn_count > 0 || | if (pthread_mutex_trylock(&hash->ch_mutex) != 0) { |
| pthread_mutex_trylock(&hash->ch_mutex) != 0) { | |
| pthread_mutex_unlock(&node->cn_mutex); | pthread_mutex_unlock(&node->cn_mutex); |
| continue; | continue; |
| } | } |
| ASSERT(node->cn_count == 0); | |
| ASSERT(node->cn_priority == priority); | ASSERT(node->cn_priority == priority); |
| node->cn_priority = -1; | node->cn_priority = -1; |
| Line 264 cache_node_allocate( | Line 264 cache_node_allocate( |
| return NULL; | return NULL; |
| } | } |
| pthread_mutex_init(&node->cn_mutex, NULL); | pthread_mutex_init(&node->cn_mutex, NULL); |
| list_head_init(&node->cn_mru); | |
| node->cn_count = 1; | node->cn_count = 1; |
| node->cn_priority = 0; | node->cn_priority = 0; |
| return node; | return node; |
| Line 309 cache_node_get( | Line 310 cache_node_get( |
| if (!cache->compare(node, key)) | if (!cache->compare(node, key)) |
| continue; | continue; |
| /* | /* |
| * node found, bump node's reference count, move it to the | * node found, bump node's reference count, remove it |
| * top of its MRU list, and update stats. | * from its MRU list, and update stats. |
| */ | */ |
| pthread_mutex_lock(&node->cn_mutex); | pthread_mutex_lock(&node->cn_mutex); |
| node->cn_count++; | |
| mru = &cache->c_mrus[node->cn_priority]; | if (node->cn_count == 0) { |
| pthread_mutex_lock(&mru->cm_mutex); | ASSERT(node->cn_priority >= 0); |
| list_move(&node->cn_mru, &mru->cm_list); | ASSERT(!list_empty(&node->cn_mru)); |
| pthread_mutex_unlock(&mru->cm_mutex); | mru = &cache->c_mrus[node->cn_priority]; |
| pthread_mutex_lock(&mru->cm_mutex); | |
| mru->cm_count--; | |
| list_del_init(&node->cn_mru); | |
| pthread_mutex_unlock(&mru->cm_mutex); | |
| } | |
| node->cn_count++; | |
| pthread_mutex_unlock(&node->cn_mutex); | pthread_mutex_unlock(&node->cn_mutex); |
| pthread_mutex_unlock(&hash->ch_mutex); | pthread_mutex_unlock(&hash->ch_mutex); |
| Line 342 cache_node_get( | Line 348 cache_node_get( |
| node->cn_hashidx = hashidx; | node->cn_hashidx = hashidx; |
| /* add new node to appropriate hash and lowest priority MRU */ | /* add new node to appropriate hash */ |
| mru = &cache->c_mrus[0]; | |
| pthread_mutex_lock(&mru->cm_mutex); | |
| pthread_mutex_lock(&hash->ch_mutex); | pthread_mutex_lock(&hash->ch_mutex); |
| hash->ch_count++; | hash->ch_count++; |
| mru->cm_count++; | |
| list_add(&node->cn_hash, &hash->ch_list); | list_add(&node->cn_hash, &hash->ch_list); |
| list_add(&node->cn_mru, &mru->cm_list); | |
| pthread_mutex_unlock(&hash->ch_mutex); | pthread_mutex_unlock(&hash->ch_mutex); |
| pthread_mutex_unlock(&mru->cm_mutex); | |
| *nodep = node; | *nodep = node; |
| return 1; | return 1; |
| Line 359 cache_node_get( | Line 360 cache_node_get( |
| void | void |
| cache_node_put( | cache_node_put( |
| struct cache * cache, | |
| struct cache_node * node) | struct cache_node * node) |
| { | { |
| struct cache_mru * mru; | |
| pthread_mutex_lock(&node->cn_mutex); | pthread_mutex_lock(&node->cn_mutex); |
| #ifdef CACHE_DEBUG | #ifdef CACHE_DEBUG |
| if (node->cn_count < 1) { | if (node->cn_count < 1) { |
| Line 368 cache_node_put( | Line 372 cache_node_put( |
| __FUNCTION__, node->cn_count, node); | __FUNCTION__, node->cn_count, node); |
| cache_abort(); | cache_abort(); |
| } | } |
| if (!list_empty(&node->cn_mru)) { | |
| fprintf(stderr, "%s: node put on node (%p) in MRU list\n", | |
| __FUNCTION__, node); | |
| cache_abort(); | |
| } | |
| #endif | #endif |
| node->cn_count--; | node->cn_count--; |
| if (node->cn_count == 0) { | |
| /* add unreferenced node to appropriate MRU for shaker */ | |
| mru = &cache->c_mrus[node->cn_priority]; | |
| pthread_mutex_lock(&mru->cm_mutex); | |
| mru->cm_count++; | |
| list_add(&node->cn_mru, &mru->cm_list); | |
| pthread_mutex_unlock(&mru->cm_mutex); | |
| } | |
| pthread_mutex_unlock(&node->cn_mutex); | pthread_mutex_unlock(&node->cn_mutex); |
| } | } |
| Line 379 cache_node_set_priority( | Line 398 cache_node_set_priority( |
| struct cache_node * node, | struct cache_node * node, |
| int priority) | int priority) |
| { | { |
| struct cache_mru * mru; | |
| if (priority < 0) | if (priority < 0) |
| priority = 0; | priority = 0; |
| else if (priority > CACHE_MAX_PRIORITY) | else if (priority > CACHE_MAX_PRIORITY) |
| priority = CACHE_MAX_PRIORITY; | priority = CACHE_MAX_PRIORITY; |
| pthread_mutex_lock(&node->cn_mutex); | pthread_mutex_lock(&node->cn_mutex); |
| ASSERT(node->cn_count > 0); | ASSERT(node->cn_count > 0); |
| if (priority == node->cn_priority) { | |
| pthread_mutex_unlock(&node->cn_mutex); | |
| return; | |
| } | |
| mru = &cache->c_mrus[node->cn_priority]; | |
| pthread_mutex_lock(&mru->cm_mutex); | |
| list_del_init(&node->cn_mru); | |
| mru->cm_count--; | |
| pthread_mutex_unlock(&mru->cm_mutex); | |
| mru = &cache->c_mrus[priority]; | |
| pthread_mutex_lock(&mru->cm_mutex); | |
| list_add(&node->cn_mru, &mru->cm_list); | |
| node->cn_priority = priority; | node->cn_priority = priority; |
| mru->cm_count++; | |
| pthread_mutex_unlock(&mru->cm_mutex); | |
| pthread_mutex_unlock(&node->cn_mutex); | pthread_mutex_unlock(&node->cn_mutex); |
| } | } |