for (t = all_thread; t < all_thread + MAX_THREAD; t++) { if (t->state == FREE) break; } t->state = RUNNABLE; // YOUR CODE HERE // user ra return func in switch t->context.ra = (uint64)func; // point to stack top(highest addr) t->context.sp = (uint64)t->stack + STACK_SIZE; }
利用ra在 switch 到 thread 后,返回到函数的位置,将sp指向该thread的栈顶。
最后是thread_schedule:
1 2 3 4 5 6 7 8 9 10
if (current_thread != next_thread) { /* switch threads? */ next_thread->state = RUNNING; t = current_thread; current_thread = next_thread; /* 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]); // 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]); }