Skip to content

Commit

Permalink
nit: deque: avoid typedef hell
Browse files Browse the repository at this point in the history
Prefer to define the structures in vanilla C without relying on any
custom typedefs, and move the typedef to the bottom of the file for
consumers to use, if they want.
  • Loading branch information
aws-nslick committed Sep 4, 2024
1 parent 07d8b1c commit 16bcbc1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
27 changes: 14 additions & 13 deletions include/nccl_ofi_deque.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,20 @@ extern "C" {
* this structure as a black box. Critically, the caller must ensure the
* contents of this structure are not modified while it is in the deque.
*/
struct nccl_ofi_deque_elem_t {
struct nccl_ofi_deque_elem {
/* Pointer to previous element */
struct nccl_ofi_deque_elem_t *prev;
struct nccl_ofi_deque_elem *prev;
/* Pointer to next element */
struct nccl_ofi_deque_elem_t *next;
struct nccl_ofi_deque_elem *next;
};
typedef struct nccl_ofi_deque_elem_t nccl_ofi_deque_elem_t;

/*
* Deque (doubly-ended queue) structure
*
* Core deque structure. This should be considered opaque to users
* of the deque interface
*/
struct nccl_ofi_deque_t {
struct nccl_ofi_deque {
/* "Head" of queue.
* The queue is circular. An empty queue has only this element. In an empty
* queue, head.prev and head.next point to head.
Expand All @@ -46,18 +45,17 @@ struct nccl_ofi_deque_t {
* front. insert_back and insert_front add elements to these respective
* locations.
*/
nccl_ofi_deque_elem_t head;
struct nccl_ofi_deque_elem head;
/* Lock for deque operations */
pthread_mutex_t lock;
};
typedef struct nccl_ofi_deque_t nccl_ofi_deque_t;

/*
* Initialize deque structure.
*
* @return zero on success, non-zero on non-success.
*/
int nccl_ofi_deque_init(nccl_ofi_deque_t **deque_p);
extern int nccl_ofi_deque_init(struct nccl_ofi_deque **deque_p);

/*
* Finalize a deque
Expand All @@ -66,15 +64,15 @@ int nccl_ofi_deque_init(nccl_ofi_deque_t **deque_p);
*
* @return zero on success, non-zero on non-success.
*/
int nccl_ofi_deque_finalize(nccl_ofi_deque_t *deque);
extern int nccl_ofi_deque_finalize(struct nccl_ofi_deque *deque);

/*
* Insert an element to the back of the deque
*
* @param deque_elem user-allocated storage space for list entry
* @return zero on success, non-zero on error
*/
static inline int nccl_ofi_deque_insert_back(nccl_ofi_deque_t *deque, nccl_ofi_deque_elem_t *deque_elem)
static inline int nccl_ofi_deque_insert_back(struct nccl_ofi_deque *deque, struct nccl_ofi_deque_elem *deque_elem)
{
assert(deque);
assert(deque_elem);
Expand All @@ -99,7 +97,7 @@ static inline int nccl_ofi_deque_insert_back(nccl_ofi_deque_t *deque, nccl_ofi_d
* @param deque_elem user-allocated storage space for list entry
* @return zero on success, non-zero on error
*/
static inline int nccl_ofi_deque_insert_front(nccl_ofi_deque_t *deque, nccl_ofi_deque_elem_t *deque_elem)
static inline int nccl_ofi_deque_insert_front(struct nccl_ofi_deque *deque, struct nccl_ofi_deque_elem *deque_elem)
{
assert(deque);
assert(deque_elem);
Expand All @@ -123,7 +121,7 @@ static inline int nccl_ofi_deque_insert_front(nccl_ofi_deque_t *deque, nccl_ofi_
*
* @return true if empty, false if not
*/
static inline bool nccl_ofi_deque_isempty(nccl_ofi_deque_t *deque)
static inline bool nccl_ofi_deque_isempty(struct nccl_ofi_deque *deque)
{
return deque->head.next == &deque->head;
}
Expand All @@ -133,7 +131,7 @@ static inline bool nccl_ofi_deque_isempty(nccl_ofi_deque_t *deque)
* @param deque_elem returned element; NULL if deque is empty or an error occurred
* @return zero on success, non-zero on non-success
*/
static inline int nccl_ofi_deque_remove_front(nccl_ofi_deque_t *deque, nccl_ofi_deque_elem_t **deque_elem)
static inline int nccl_ofi_deque_remove_front(struct nccl_ofi_deque *deque, struct nccl_ofi_deque_elem **deque_elem)
{
assert(deque);
assert(deque_elem);
Expand Down Expand Up @@ -163,6 +161,9 @@ static inline int nccl_ofi_deque_remove_front(nccl_ofi_deque_t *deque, nccl_ofi_
return 0;
}

typedef struct nccl_ofi_deque_elem nccl_ofi_deque_elem_t;
typedef struct nccl_ofi_deque nccl_ofi_deque_t;

#ifdef __cplusplus
} // End extern "C"
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/nccl_ofi_deque.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "nccl_ofi_deque.h"
#include "nccl_ofi_log.h"

int nccl_ofi_deque_init(nccl_ofi_deque_t **deque_p)
int nccl_ofi_deque_init(struct nccl_ofi_deque **deque_p)
{
nccl_ofi_deque_t *deque = (nccl_ofi_deque_t *)malloc(sizeof(nccl_ofi_deque_t));

Expand All @@ -36,7 +36,7 @@ int nccl_ofi_deque_init(nccl_ofi_deque_t **deque_p)
return 0;
}

int nccl_ofi_deque_finalize(nccl_ofi_deque_t *deque)
int nccl_ofi_deque_finalize(struct nccl_ofi_deque *deque)
{
assert(deque);

Expand Down

0 comments on commit 16bcbc1

Please sign in to comment.