1
1

Update the thread protection in the ring_buffer class

This commit was SVN r23532.
Этот коммит содержится в:
Ralph Castain 2010-07-29 02:12:44 +00:00
родитель 3d9b05ba2b
Коммит 0ed98967ed
2 изменённых файлов: 10 добавлений и 5 удалений

Просмотреть файл

@ -41,6 +41,8 @@ OBJ_CLASS_INSTANCE(opal_ring_buffer_t, opal_object_t,
static void opal_ring_buffer_construct(opal_ring_buffer_t *ring)
{
OBJ_CONSTRUCT(&ring->lock, opal_mutex_t);
OBJ_CONSTRUCT(&ring->cond, opal_condition_t);
ring->in_use = false;
ring->head = NULL;
ring->tail = NULL;
ring->size = 0;
@ -60,6 +62,7 @@ static void opal_ring_buffer_destruct(opal_ring_buffer_t *ring)
ring->size = 0;
OBJ_DESTRUCT(&ring->lock);
OBJ_DESTRUCT(&ring->cond);
}
/**

Просмотреть файл

@ -26,7 +26,7 @@
#include "opal_config.h"
#include "opal/threads/mutex.h"
#include "opal/threads/threads.h"
#include "opal/class/opal_object.h"
#include "opal/util/output.h"
@ -40,6 +40,8 @@ struct opal_ring_buffer_t {
opal_object_t super;
/** synchronization object */
opal_mutex_t lock;
opal_condition_t cond;
bool in_use;
/* head/tail indices */
char **head;
char **tail;
@ -81,7 +83,7 @@ static inline void* opal_ring_buffer_push(opal_ring_buffer_t *ring, void *ptr)
{
char *p=NULL;
OPAL_THREAD_LOCK(&(ring->lock));
OPAL_ACQUIRE_THREAD(&(ring->lock), &(ring->cond), &(ring->in_use));
if (NULL != *ring->head) {
p = *ring->head;
if (ring->head == ring->tail) {
@ -99,7 +101,7 @@ static inline void* opal_ring_buffer_push(opal_ring_buffer_t *ring, void *ptr)
} else {
ring->head++;
}
OPAL_THREAD_UNLOCK(&(ring->lock));
OPAL_RELEASE_THREAD(&(ring->lock), &(ring->cond), &(ring->in_use));
return (void*)p;
}
@ -117,7 +119,7 @@ static inline void* opal_ring_buffer_pop(opal_ring_buffer_t *ring)
{
char *p;
OPAL_THREAD_LOCK(&(ring->lock));
OPAL_ACQUIRE_THREAD(&(ring->lock), &(ring->cond), &(ring->in_use));
if (NULL == ring->tail || ring->head == ring->tail) {
p = NULL;
} else {
@ -129,7 +131,7 @@ static inline void* opal_ring_buffer_pop(opal_ring_buffer_t *ring)
ring->tail++;
}
}
OPAL_THREAD_UNLOCK(&(ring->lock));
OPAL_RELEASE_THREAD(&(ring->lock), &(ring->cond), &(ring->in_use));
return (void*)p;
}