2019-12-01 01:02:08 -07:00
|
|
|
#include <stdlib.h>
|
2019-12-01 01:07:37 -07:00
|
|
|
#include <string.h>
|
2019-12-01 01:02:08 -07:00
|
|
|
#include <pthread.h>
|
|
|
|
#include "wintype.h"
|
|
|
|
#include "time_func.h"
|
|
|
|
#include "msg_queue.h"
|
|
|
|
|
2019-12-05 18:21:37 -07:00
|
|
|
PMSG_QUEUE Mq_alloc(UINT32 nentries)
|
2019-12-01 01:02:08 -07:00
|
|
|
{
|
|
|
|
int sz = sizeof(MSG_QUEUE) + (nentries * sizeof(MSG));
|
|
|
|
PMSG_QUEUE rc;
|
|
|
|
|
|
|
|
rc = (PMSG_QUEUE)malloc(sz);
|
|
|
|
if (!rc)
|
|
|
|
return NULL;
|
2019-12-01 01:07:37 -07:00
|
|
|
memset(rc, 0, sz);
|
|
|
|
rc->startbound = &(rc->messagestore[0]);
|
2019-12-01 01:02:08 -07:00
|
|
|
rc->endbound = rc->startbound + nentries;
|
|
|
|
rc->head = rc->tail = rc->startbound;
|
|
|
|
rc->nentries = nentries;
|
|
|
|
pthread_mutex_init(&(rc->mutex), NULL);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-12-01 15:43:05 -07:00
|
|
|
void Mq_destroy(PMSG_QUEUE queue)
|
2019-12-01 01:02:08 -07:00
|
|
|
{
|
|
|
|
pthread_mutex_destroy(&(queue->mutex));
|
|
|
|
free(queue);
|
|
|
|
}
|
|
|
|
|
2019-12-01 01:36:07 -07:00
|
|
|
static void post_internal(PMSG_QUEUE queue, PMSG msg)
|
2019-12-01 01:02:08 -07:00
|
|
|
{
|
|
|
|
PMSG nexttail;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&(queue->mutex));
|
|
|
|
nexttail = queue->tail + 1;
|
|
|
|
if (nexttail == queue->endbound)
|
|
|
|
nexttail = queue->startbound;
|
|
|
|
if (nexttail != queue->head)
|
|
|
|
{
|
|
|
|
memcpy(queue->tail, msg, sizeof(MSG));
|
|
|
|
queue->tail = nexttail;
|
|
|
|
}
|
|
|
|
/* else drop the message silently */
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&(queue->mutex));
|
|
|
|
}
|
|
|
|
|
2019-12-05 18:21:37 -07:00
|
|
|
void Mq_post(PMSG_QUEUE queue, HANDLE target, UINT32 message, const UINT_PTR *attrs, int nattrs)
|
2019-12-01 01:02:08 -07:00
|
|
|
{
|
|
|
|
MSG tmpmsg;
|
|
|
|
|
|
|
|
memset(&tmpmsg, 0, sizeof(MSG));
|
|
|
|
tmpmsg.target = target;
|
|
|
|
tmpmsg.message = message;
|
|
|
|
if (nattrs > MSG_ATTRCOUNT)
|
|
|
|
nattrs = MSG_ATTRCOUNT;
|
|
|
|
if (nattrs > 0)
|
|
|
|
memcpy(&(tmpmsg.attrs), attrs, sizeof(uintptr_t) * nattrs);
|
2019-12-01 15:43:05 -07:00
|
|
|
tmpmsg.timestamp = Time_since_start();
|
2019-12-01 01:02:08 -07:00
|
|
|
post_internal(queue, &tmpmsg);
|
|
|
|
}
|
|
|
|
|
2019-12-05 18:21:37 -07:00
|
|
|
void Mq_post1(PMSG_QUEUE queue, HANDLE target, UINT32 message, UINT_PTR attr1)
|
2019-12-01 01:02:08 -07:00
|
|
|
{
|
|
|
|
MSG tmpmsg;
|
|
|
|
|
|
|
|
memset(&tmpmsg, 0, sizeof(MSG));
|
|
|
|
tmpmsg.target = target;
|
|
|
|
tmpmsg.message = message;
|
|
|
|
tmpmsg.attrs[0] = attr1;
|
2019-12-01 15:43:05 -07:00
|
|
|
tmpmsg.timestamp = Time_since_start();
|
2019-12-01 01:02:08 -07:00
|
|
|
post_internal(queue, &tmpmsg);
|
|
|
|
}
|
|
|
|
|
2019-12-05 18:21:37 -07:00
|
|
|
BOOL Mq_peek(PMSG_QUEUE queue, PMSG msg, UINT32 flags)
|
2019-12-01 01:02:08 -07:00
|
|
|
{
|
2019-12-05 18:21:37 -07:00
|
|
|
BOOL rc = FALSE;
|
2019-12-01 01:02:08 -07:00
|
|
|
PMSG nexthead;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&(queue->mutex));
|
|
|
|
if (queue->head != queue->tail)
|
|
|
|
{
|
|
|
|
nexthead = queue->head + 1;
|
|
|
|
if (nexthead == queue->endbound)
|
|
|
|
nexthead = queue->startbound;
|
|
|
|
memcpy(msg, queue->head, sizeof(MSG));
|
|
|
|
if (flags & PEEK_REMOVE)
|
|
|
|
queue->head = nexthead;
|
2019-12-05 18:21:37 -07:00
|
|
|
rc = TRUE;
|
2019-12-01 01:02:08 -07:00
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&(queue->mutex));
|
|
|
|
return rc;
|
|
|
|
}
|