/* YOUR CODE HERE * Invoke thread_switch to switch from t to next_thread: * thread_switch(??, ??); */ thread_switch((uint64)&t->context, (uint64)¤t_thread->context);
static voidput(int key, int value) { int i = key % NBUCKET;
pthread_mutex_lock(&locks[i]); /* held the mutex */ // is the key already present? structentry *e =0; for (e = table[i]; e != 0; e = e->next) { if (e->key == key) break; } if(e){ // update the existing key. e->value = value; } else { // the new is new. insert(key, value, &table[i], table[i]); } pthread_mutex_unlock(&locks[i]); /* release mutex */
staticvoid barrier() { // YOUR CODE HERE // // Block until all threads have called barrier() and // then increment bstate.round. // pthread_mutex_lock(&bstate.barrier_mutex); ++bstate.nthread; if (bstate.nthread % nthread == 0) { ++bstate.round; pthread_cond_broadcast(&bstate.barrier_cond); pthread_mutex_unlock(&bstate.barrier_mutex); return; } else { pthread_cond_wait(&bstate.barrier_cond, &bstate.barrier_mutex); } pthread_mutex_unlock(&bstate.barrier_mutex); }