10.07.2015 Views

Is Parallel Programming Hard, And, If So, What Can You Do About It?

Is Parallel Programming Hard, And, If So, What Can You Do About It?

Is Parallel Programming Hard, And, If So, What Can You Do About It?

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

62 CHAPTER 5. PARTITIONING AND SYNCHRONIZATION DESIGN1 struct hash_table2 {3 long nbuckets;4 struct node **buckets;5 };67 typedef struct node {8 unsigned long key;9 struct node *next;10 } node_t;1112 int hash_search(struct hash_table *h, long key)13 {14 struct node *cur;15 int retval;1617 rcu_read_lock();18 cur = h->buckets[key % h->nbuckets];19 while (cur != NULL) {20 if (cur->key >= key) {21 retval = (cur->key == key);22 rcu_read_unlock();23 return retval;24 }25 cur = cur->next;26 }27 rcu_read_unlock();28 return 0;29 }Figure 5.25: RCU Hash Table SearchUpdate-side code must wait for a “grace period”after removing an element before freeing it, andRCU implementations provide special primitives forthis purpose. For example, the Linux kernel providessynchronize_rcu() to block until the end ofa subsequent grace period (and other related primitivesas well), while Hart et al. [HMB06] provide afree_node_later() primitive that frees the specifieddata element after the end of a subsequent graceperiod (in contrast to their free_node() primitivethat immediately frees the specified data element).RCU’s greatest strength is its low-overhead (insome cases, zero-overhead) read-side primitives. Inmany implementations, these primitives are in factdeterministic, which is useful in realtime environments.5.4.3 Hierarchical LockingThe idea behind hierarchical locking is to have acoarse-grained lock that is held only long enough towork out which fine-grained lock to acquire. Figure5.26 shows how our hash-table search might beadapted to do hierarchical locking, but also showsthe great weakness of this approach: we have paidthe overhead of acquiring a second lock, but we onlyhold it for a short time. In this case, the simplerdata-locking approach would be simpler and likelyperform better.Quick Quiz 5.14: In what situation would hierarchicallocking work well?1 struct hash_table2 {3 long nbuckets;4 struct bucket **buckets;5 };67 struct bucket {8 spinlock_t bucket_lock;9 node_t *list_head;10 };1112 typedef struct node {13 spinlock_t node_lock;14 unsigned long key;15 struct node *next;16 } node_t;1718 int hash_search(struct hash_table *h, long key)19 {20 struct bucket *bp;21 struct node *cur;22 int retval;2324 bp = h->buckets[key % h->nbuckets];25 spin_lock(&bp->bucket_lock);26 cur = bp->list_head;27 while (cur != NULL) {28 if (cur->key >= key) {29 spin_lock(&cur->node_lock);30 spin_unlock(&bp->bucket_lock);31 retval = (cur->key == key);32 spin_unlock(&cur->node_lock);33 return retval;34 }35 cur = cur->next;36 }37 spin_unlock(&bp->bucket_lock);38 return 0;39 }Figure 5.26:SearchHierarchical-Locking Hash Table

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!