📄 v.c
字号:
#include"kernel.h"
#define WRONG_THREAD_ID -1
#define WRONG_SEMAPHORE_ID -2
#define WRONG_THREAD_STATE -3
#define WRONG_CAPABILITY -4
static int v(int semaphore_id,int *steps,struct capability *cap)
{
struct semaphore *s;
struct thread *t;
struct return_stack *rt;
int i,thread_id,fr_th,ba_th,my_step;
if((semaphore_id<0)||(semaphore_id>=SEMAPHORE_NUMBER))
return WRONG_SEMAPHORE_ID;
s=&(os->semaphore[semaphore_id]);
if(s->process<0)
return WRONG_SEMAPHORE_ID;
if(!KERNEL_COMPARE_CAPABILITY(s->capability,(*cap)))
return WRONG_CAPABILITY;
if((my_step=(*steps))==0){
*steps=s->value;
return 0;
};
for(i=0;;i++){
thread_id=s->thread_ring;
if(my_step>0){
if((thread_id<0)||(i>=my_step)){
s->value=s->value+my_step-i;
break;
}
}else{
if(thread_id<0)
break;
};
s->value++;
t=&(os->thread[thread_id]);
fr_th=t->sem_front;ba_th=t->sem_back;
if(fr_th==thread_id)
s->thread_ring=-1;
else{
s->thread_ring=fr_th;
os->thread[fr_th].sem_back=ba_th;
os->thread[ba_th].sem_front=fr_th;
}
t->sleep_semaphore=(-1);
if(t->set_v_operation_result_flag){
rt=&(t->return_stack[t->return_stack_top-1]);
if(t->return_stack_top<RETURN_BLOCK_NUMBER)
if(t->return_stack_top>0)
rt->environment.system_call=i+2;
}
insert_into_heap(thread_id);
}
*steps=s->value;
return i;
}
int v_thread(int thread_id)
{
int value;
struct thread *t;
struct semaphore *s;
if((thread_id<0)||(thread_id>=THREAD_NUMBER))
return(WRONG_THREAD_ID);
t=&(os->thread[thread_id]);
if(t->state!=SLEEP)
return(WRONG_THREAD_STATE);
s=&(os->semaphore[t->sleep_semaphore]);
if(s->process<0)
return WRONG_SEMAPHORE_ID;
s->thread_ring=thread_id;
value=1;
return(v(t->sleep_semaphore,&value,&(os->system_capability)));
}
int system_call_v(int semaphore_id,int *steps,struct capability *cap)
{
if(semaphore_id==0)
return WRONG_SEMAPHORE_ID;
else
return v(semaphore_id,steps,cap);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -