cmtports.c
来自「简单实现多任务的C语言库 可以实现各个任务间的简单的消息通讯」· C语言 代码 · 共 173 行
C
173 行
#include "cmtlib.h"
/* ----------------------------------------------------------------
** Ports.
** ----------------------------------------------------------------
*/
/*man**************************************************************
NAME
cmt_pcreate - create a message port
SYNOPSIS
#include "cmtlib.h"
CMT_PORT far * far cdecl cmt_pcreate(int size)
DESCRIPTION
this function creates a message port for a given number
of messages. in our multitasker, messages are always
void far pointers, the given size indicates how many pointers
this port can hold.
DIAGNOSTICS
returns pointer to newly created port or NULL
******************************************************************/
CMT_PORT far * far cdecl cmt_pcreate(int size)
{
int i;
CMT_PORT far *np=NULL;
if (size>0) {
np=cmt_malloc(sizeof(CMT_PORT)+size*sizeof(void far *));
if (np) {
for (i=0;i<size;i++)
np->port[i]=NULL;
np->size=size;
np->nmsgs=np->head=np->tail=0;
}
}
return np;
}
/*man**************************************************************
NAME
cmt_pdelete - discards a message port
SYNOPSIS
#include "cmtlib.h"
CMT_PORT far * far cdecl cmt_pdelete(CMT_PORT far *port)
DESCRIPTION
this function discards a message port.
DIAGNOSTICS
always returns NULL
NOTES
this function does not check if the port has any
pending messages (which would be lost after the port
is discarded).
******************************************************************/
CMT_PORT far * far cdecl cmt_pdelete(CMT_PORT far *port)
{
if (port)
cmt_free(port);
return NULL;
}
/*man**************************************************************
NAME
cmt_psend - sends a message to port
SYNOPSIS
#include "cmtlib.h"
void far cdecl cmt_psend(CMT_PORT far *port,void far *message)
DESCRIPTION
send a message to message port. if the port is full, this
function blocks until there is room for another message.
******************************************************************/
void far cdecl cmt_psend(CMT_PORT far *port,void far *message)
{
if (port) {
while (port->nmsgs>=port->size)
cmt_pause();
port->port[port->head++]=message;
if (port->head>=port->size)
port->head=0;
port->nmsgs++;
}
}
/*man**************************************************************
NAME
cmt_psendnobl - sends a message to port, not blocking
SYNOPSIS
#include "cmtlib.h"
int far cdecl cmt_psendnobl(CMT_PORT far *port,void far *message)
DESCRIPTION
tries to send a message to message port
DIAGNOSTICS
returns 1 if the message is successfully delivered or 0 if the
port is full
******************************************************************/
int far cdecl cmt_psendnobl(CMT_PORT far *port,void far *message)
{
if (port) {
if (port->nmsgs>=port->size)
return 0;
port->port[port->head++]=message;
if (port->head>=port->size)
port->head=0;
port->nmsgs++;
return 1;
}
return 0;
}
/*man**************************************************************
NAME
cmt_preceive - receive a message from port
SYNOPSIS
#include "cmtlib.h"
void far * far cdecl cmt_preceive(CMT_PORT far *port)
DESCRIPTION
receives a message from a port. blocks until a message
becomes avaiable
DIAGNOSTICS
returns a message
******************************************************************/
void far * far cdecl cmt_preceive(CMT_PORT far *port)
{
void *m=NULL;
if (port) {
while (!port->nmsgs)
cmt_pause();
m=port->port[port->tail++];
if (port->tail>=port->size)
port->tail=0;
port->nmsgs--;
}
return m;
}
/*man**************************************************************
NAME
cmt_pcount - returns number of messages in the port
SYNOPSIS
#include "cmtlib.h"
int far cdecl cmt_pcount(CMT_PORT far *port)
DESCRIPTION
this function simply returns a number of messages
that are waiting delivery in the port.
DIAGNOSTICS
returns number of pending messages
******************************************************************/
int far cdecl cmt_pcount(CMT_PORT far *port)
{
return port->nmsgs;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?