Skip to content

Instantly share code, notes, and snippets.

@scalone
Created April 17, 2018 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scalone/7185e6146198de8ea480260ea97335e3 to your computer and use it in GitHub Desktop.
Save scalone/7185e6146198de8ea480260ea97335e3 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct message {
int len;
char data[4096];
struct message *front;
struct message *rear;
} message ;
typedef struct queueMessage {
struct message *first;
struct message *last;
int size;
} queueMessage;
static struct queueMessage *connThreadQueueRecv;
queueMessage *wthread_new_recv(void)
{
queueMessage *queue = (queueMessage*) malloc(sizeof (queueMessage));
queue->size = 0;
return queue;
}
int wthread_enqueue_recv(char *buf, int len)
{
struct message *newMessage;
if (len > 0) {
newMessage = (message *)malloc(sizeof(message));
/*Copy message*/
strcpy(newMessage->data, buf);
newMessage->len = len;
if (connThreadQueueRecv->size == 0) {
connThreadQueueRecv->first = newMessage;
connThreadQueueRecv->last = newMessage;
connThreadQueueRecv->size = 1;
} else {
/*populate last, queue and node*/
connThreadQueueRecv->last->rear = newMessage;
newMessage->front = connThreadQueueRecv->last;
connThreadQueueRecv->last = newMessage;
connThreadQueueRecv->size++;
}
}
return len;
}
int wthread_dequeue_recv(int timeout, char *buf)
{
int len = 0;
int try = 0;
struct message *first;
struct message *local;
do {
/*Pprintf(true, "wthread_dequeue_recv [%d]", connThreadQueueRecv->size);*/
if (connThreadQueueRecv->size > 0) {
local = connThreadQueueRecv->first;
memcpy(buf, local->data, local->len);
/*Pprintf(true, "DATA [%s]", connThreadQueueRecv->first->data);*/
len = connThreadQueueRecv->first->len;
first = connThreadQueueRecv->first->rear;
free(local);
connThreadQueueRecv->first = first;
return len;
}
/*SVC_WAIT(1000);*/
} while(try++ < timeout);
return len;
}
int main(void) {
char buf[1024];
printf("START\n\n");
connThreadQueueRecv = wthread_new_recv();
wthread_enqueue_recv("1234123412341234" , 16);
wthread_enqueue_recv("12341234123412345" , 17);
wthread_enqueue_recv("123412341234123456", 18);
printf("B4\n");
printf("connThreadQueueRecv [%d]\n", connThreadQueueRecv->size);
/*printf("[%d][%s]\n", connThreadQueueRecv->firs->rear->len, connThreadQueueRecv->first->rear->data);*/
/*printf("[%d][%s]\n", connThreadQueueRecv.first->len, connThreadQueueRecv.first->data);*/
wthread_dequeue_recv(0, buf);
printf("BUF1 [%s]\n", buf);
wthread_dequeue_recv(0, buf);
printf("BUF2 [%s]\n", buf);
wthread_dequeue_recv(0, buf);
printf("BUF3 [%s]\n", buf);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment