typedef struct _queue    // 睡眠队列
{
	struct tcb *tsk;
	struct _queue *next;
}queue;

typedef struct _semaphore{
    int value;
	queue *head;   
	queue *tail;
}semaphore;

semaphore* sys_sem_create(int value);
int sys_sem_destroy(semaphore* hsem);
int sys_sem_wait(semaphore* hsem);
int sys_sem_signal(semaphore* hsem);
semaphore *sys_sem_create(int value)
{
    semaphore *hsem = (semaphore *)kmalloc(sizeof(semaphore));
	if(hsem == NULL)
		return NULL;
	else
	{
		hsem->head = NULL;
		hsem->tail = NULL;
		hsem->value = value;
		return hsem;
	}
}

int sys_sem_destroy(semaphore *hsem)
{
	if(hsem == NULL)
		return -1;
	else
	{
		hsem = (semaphore *)hsem;
		if(hsem->head==NULL)
		{
			kfree(hsem);
		}else{
			queue *temp = hsem->head;
			while(hsem->head != NULL)
			{
				temp = hsem->head->next;
				kfree(hsem->head);
				hsem->head = temp;
			}
			hsem->tail = NULL;
			kfree(hsem);
		}
	}
    return 0;
}

int sys_sem_wait(semaphore *hsem)
{
	if(hsem == NULL)
		return -1;
	hsem = (semaphore *)hsem;
	uint32_t flags;
	save_flags_cli(flags);
	hsem->value--;
	if(hsem->value<0)
	{
		queue *temp = (queue *)kmalloc(sizeof(queue));
		temp->next = NULL;
		if(hsem->head==NULL)
		{
			hsem->head = temp;
			hsem->tail = temp;
		}else
		{
			hsem->tail->next = temp;
			hsem->tail = temp;
		}
		sleep_on(&hsem->head);
	}
	restore_flags(flags);
    return 0;
}

int sys_sem_signal(semaphore *hsem)
{
	if(hsem == NULL)
		return -1;
	hsem = (semaphore *)hsem;
	uint32_t flags;
	save_flags_cli(flags);
	hsem->value++;
	if(hsem->value<=0)
	{
		if(hsem->head != NULL)
		{
			queue *temp = hsem->head;
			wake_up(&hsem->head,1);
			hsem->head = hsem->head->next;
			kfree(temp);
		}
	}
	restore_flags(flags);
    return 0;
}
//如何转换成
int sys_sem_create(int)
int sys_sem_destroy(int semid)
int sys_sem_wait(int semid)
int sys_sem_signal(int semid)