Skip to content
Snippets Groups Projects
Commit 018d6187 authored by rswindell's avatar rswindell
Browse files

If RINGBUF_MUTEX is defined, ringbuffers are mutex-protected (thread-safe).

Experimenting with this in sbbs to see if there are any race conditions between
threads that manipulate the I/O buffers (e.g. Ctrl-C in input_thread clears
output buffer).
parent 044003ad
Branches
Tags
No related merge requests found
......@@ -93,6 +93,9 @@ int RINGBUFCALL RingBufInit( RingBuf* rb, DWORD size
rb->pHead=rb->pTail=rb->pStart;
rb->pEnd=rb->pStart+size;
rb->size=size;
#ifdef RINGBUF_MUTEX
pthread_mutex_init(&rb->mutex,NULL);
#endif
return(0);
}
......@@ -100,6 +103,9 @@ void RINGBUFCALL RingBufDispose( RingBuf* rb)
{
if(rb->pStart!=NULL)
os_free(rb->pStart);
#ifdef RINGBUF_MUTEX
pthread_mutex_destroy(&rb->mutex);
#endif
memset(rb,0,sizeof(RingBuf));
}
......@@ -107,6 +113,10 @@ DWORD RINGBUFCALL RingBufFull( RingBuf* rb )
{
DWORD head,tail,retval;
#ifdef RINGBUF_MUTEX
pthread_mutex_lock(&rb->mutex);
#endif
head = (DWORD) rb->pHead;
tail = (DWORD) rb->pTail;
......@@ -115,6 +125,10 @@ DWORD RINGBUFCALL RingBufFull( RingBuf* rb )
else
retval = rb->size - (tail - head);
#ifdef RINGBUF_MUTEX
pthread_mutex_unlock(&rb->mutex);
#endif
return(retval);
}
......@@ -134,6 +148,10 @@ DWORD RINGBUFCALL RingBufWrite( RingBuf* rb, BYTE* src, DWORD cnt )
if(rb->pStart==NULL)
return(0);
#ifdef RINGBUF_MUTEX
pthread_mutex_lock(&rb->mutex);
#endif
/* allowed to write at pEnd */
max = (((DWORD) rb->pEnd) - ((DWORD) rb->pHead)) + 1;
......@@ -164,6 +182,10 @@ DWORD RINGBUFCALL RingBufWrite( RingBuf* rb, BYTE* src, DWORD cnt )
if(rb->pHead > rb->pEnd)
rb->pHead = rb->pStart;
#ifdef RINGBUF_MUTEX
pthread_mutex_unlock(&rb->mutex);
#endif
return(cnt);
}
......@@ -176,6 +198,10 @@ DWORD RINGBUFCALL RingBufRead( RingBuf* rb, BYTE* dst, DWORD cnt )
if( len == 0 )
return(0);
#ifdef RINGBUF_MUTEX
pthread_mutex_lock(&rb->mutex);
#endif
if( len < cnt )
cnt = len;
......@@ -207,6 +233,10 @@ DWORD RINGBUFCALL RingBufRead( RingBuf* rb, BYTE* dst, DWORD cnt )
if(rb->pTail > rb->pEnd)
rb->pTail = rb->pStart;
#ifdef RINGBUF_MUTEX
pthread_mutex_unlock(&rb->mutex);
#endif
return(cnt);
}
......@@ -218,6 +248,10 @@ DWORD RINGBUFCALL RingBufPeek( RingBuf* rb, BYTE* dst, DWORD cnt)
if( len == 0 )
return(0);
#ifdef RINGBUF_MUTEX
pthread_mutex_lock(&rb->mutex);
#endif
if( len < cnt )
cnt = len;
......@@ -239,13 +273,23 @@ DWORD RINGBUFCALL RingBufPeek( RingBuf* rb, BYTE* dst, DWORD cnt)
rb_memcpy( dst, rb->pStart, remain );
}
#ifdef RINGBUF_MUTEX
pthread_mutex_unlock(&rb->mutex);
#endif
return(cnt);
}
/* Reset head and tail pointers */
void RINGBUFCALL RingBufReInit(RingBuf* rb)
{
#ifdef RINGBUF_MUTEX
pthread_mutex_lock(&rb->mutex);
#endif
rb->pHead = rb->pTail = rb->pStart;
#ifdef RINGBUF_MUTEX
pthread_mutex_unlock(&rb->mutex);
#endif
}
/* End of RINGBUF.C */
......@@ -42,6 +42,10 @@
#ifndef _RINGBUF_H_
#define _RINGBUF_H_
#ifdef RINGBUF_MUTEX
#include "threadwrap.h"
#endif
#ifndef DWORD
#define DWORD unsigned long
#endif
......@@ -73,6 +77,9 @@ typedef struct {
BYTE* pTail; /* next byte to be consumed */
BYTE* pEnd; /* end of the buffer, used for wrap around */
DWORD size;
#ifdef RINGBUF_MUTEX
pthread_mutex_t mutex;
#endif
} RingBuf;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment