⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 event.c

📁 这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易于我们学习和理解
💻 C
字号:
#include <u.h>#include <libc.h>#include <draw.h>#include <event.h>#include "plumb.h"typedef struct EQueue EQueue;struct EQueue{	int		id;	char		*buf;	int		nbuf;	EQueue	*next;};static	EQueue	*equeue;static	Lock		eqlock;staticintpartial(int id, Event *e, uchar *b, int n){	EQueue *eq, *p;	int nmore;	lock(&eqlock);	for(eq = equeue; eq != nil; eq = eq->next)		if(eq->id == id)			break;	unlock(&eqlock);	if(eq == nil)		return 0;	/* partial message exists for this id */	eq->buf = realloc(eq->buf, eq->nbuf+n);	if(eq->buf == nil)		drawerror(display, "eplumb: cannot allocate buffer");	memmove(eq->buf+eq->nbuf, b, n);	eq->nbuf += n;	e->v = plumbunpackpartial((char*)eq->buf, eq->nbuf, &nmore);	if(nmore == 0){	/* no more to read in this message */		lock(&eqlock);		if(eq == equeue)			equeue = eq->next;		else{			for(p = equeue; p!=nil && p->next!=eq; p = p->next)				;			if(p == nil)				drawerror(display, "eplumb: bad event queue");			p->next = eq->next;		}		unlock(&eqlock);		free(eq->buf);		free(eq);	}	return 1;}staticvoidaddpartial(int id, char *b, int n){	EQueue *eq;	eq = malloc(sizeof(EQueue));	if(eq == nil)		return;	eq->id = id;	eq->nbuf = n;	eq->buf = malloc(n);	if(eq->buf == nil){		free(eq);		return;	}	memmove(eq->buf, b, n);	lock(&eqlock);	eq->next = equeue;	equeue = eq;	unlock(&eqlock);}staticintplumbevent(int id, Event *e, uchar *b, int n){	int nmore;	if(partial(id, e, b, n) == 0){		/* no partial message already waiting for this id */		e->v = plumbunpackpartial((char*)b, n, &nmore);		if(nmore > 0)	/* incomplete message */			addpartial(id, (char*)b, n);	}	if(e->v == nil)		return 0;	return id;}inteplumb(int key, char *port){	int fd;	fd = plumbopen(port, OREAD|OCEXEC);	if(fd < 0)		return -1;	return estartfn(key, fd, 8192, plumbevent);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -