Skip to content

Commit

Permalink
inherit some macros from ww.h and change function names
Browse files Browse the repository at this point in the history
  • Loading branch information
radkesvat committed Aug 6, 2024
1 parent 6cb034c commit c69626c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
29 changes: 13 additions & 16 deletions ww/eventloop/base/hchan.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#include "hchan.h"
#include "hmutex.h"
#include "ww.h"

#define MUSTALIGN2(n, w) assert(((w) & ((w) - 1)) == 0); /* alignment w is not a power of two */

#define align2(n, w) (((n) + ((w) - 1)) & ~((w) - 1))

// DEBUG_CHAN_LOG: define to enable debug logging of send and recv
// #define DEBUG_CHAN_LOG
Expand All @@ -17,7 +15,6 @@
// TODO: set value depending on target preprocessor information.
#define LINE_CACHE_SIZE 64

#define ATTR_ALIGNED_LINE_CACHE __attribute__((aligned(LINE_CACHE_SIZE)))

typedef _Atomic(unsigned int) atomic_uint32_t;
typedef _Atomic(unsigned long long) atomic_uint64_t;
Expand Down Expand Up @@ -548,12 +545,12 @@ static bool chan_recv_direct(hchan_t* c, void* dstelemptr, Thr* sendert) {
return ok;
}

hchan_t* hchan_Open(size_t elemsize, uint32_t bufcap) {
hchan_t* hchanOpen(size_t elemsize, uint32_t bufcap) {
int64_t memsize = (int64_t)sizeof(hchan_t) + ((int64_t)bufcap * (int64_t)elemsize);

// ensure we have enough space to offset the allocation by line cache (for alignment)
MUSTALIGN2(memsize + ((LINE_CACHE_SIZE + 1) / 2), LINE_CACHE_SIZE);
memsize = align2(memsize + ((LINE_CACHE_SIZE + 1) / 2), LINE_CACHE_SIZE);
memsize = ALIGN2(memsize + ((LINE_CACHE_SIZE + 1) / 2), LINE_CACHE_SIZE);

// check for overflow
if (memsize < (int64_t)sizeof(hchan_t)) {
Expand All @@ -562,11 +559,11 @@ hchan_t* hchan_Open(size_t elemsize, uint32_t bufcap) {
}

// allocate memory, placing hchan_t at a line cache address boundary
uintptr_t ptr = (uintptr_t)malloc(memsize);
uintptr_t ptr = (uintptr_t)globalMalloc(memsize);

// align c to line cache boundary
MUSTALIGN2(ptr, LINE_CACHE_SIZE);
hchan_t* c = (hchan_t*)align2(ptr, LINE_CACHE_SIZE);
hchan_t* c = (hchan_t*)ALIGN2(ptr, LINE_CACHE_SIZE);

c->memptr = ptr;
c->elemsize = elemsize;
Expand All @@ -581,7 +578,7 @@ hchan_t* hchan_Open(size_t elemsize, uint32_t bufcap) {
return c;
}

void hchan_Close(hchan_t* c) {
void hchanClose(hchan_t* c) {
// dlog_chan("--- close ---");

chan_lock(&c->lock);
Expand Down Expand Up @@ -615,24 +612,24 @@ void hchan_Close(hchan_t* c) {
// dlog_chan("close: done");
}

void hchan_Free(hchan_t* c) {
void hchanFree(hchan_t* c) {
assert(atomic_load_explicit(&c->closed, memory_order_acquire)); // must close channel before freeing its memory
chan_lock_destroy(&c->lock);
free((void*)c->memptr);
globalFree((void*)c->memptr);
}

uint32_t hchan_Cap(const hchan_t* c) {
uint32_t hchanCap(const hchan_t* c) {
return c->qcap;
}
bool hchan_Send(hchan_t* c, void* elemptr) {
bool hchanSend(hchan_t* c, void* elemptr) {
return chan_send(c, elemptr, NULL);
}
bool hchan_Recv(hchan_t* c, void* elemptr) {
bool hchanRecv(hchan_t* c, void* elemptr) {
return chan_recv(c, elemptr, NULL);
}
bool hchan_TrySend(hchan_t* c, void* elemptr, bool* closed) {
bool hchanTrySend(hchan_t* c, void* elemptr, bool* closed) {
return chan_send(c, elemptr, closed);
}
bool hchan_TryRecv(hchan_t* c, void* elemptr, bool* closed) {
bool hchanTryRecv(hchan_t* c, void* elemptr, bool* closed) {
return chan_recv(c, elemptr, closed);
}
16 changes: 8 additions & 8 deletions ww/eventloop/base/hchan.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,38 @@ typedef struct hchan_s hchan_t; // opaque

// hchan_open creates and initializes a new channel which holds elements of elemsize byte.
// If bufcap>0 then a buffered channel with the capacity to hold bufcap elements is created.
hchan_t* hchan_Open(size_t elemsize, uint32_t bufcap);
hchan_t* hchanOpen(size_t elemsize, uint32_t bufcap);

// hchan_close cancels any waiting senders and receivers.
// Messages sent before this call are guaranteed to be delivered, assuming there are
// active receivers. Once a channel is closed it can not be reopened nor sent to.
// hchan_close must only be called once per channel.
void hchan_Close(hchan_t*);
void hchanClose(hchan_t*);

// hchan_free frees memory of a channel
void hchan_Free(hchan_t*);
void hchanFree(hchan_t*);

// hchan_cap returns the channel's buffer capacity
uint32_t hchan_Cap(const hchan_t* c);
uint32_t hchanCap(const hchan_t* c);

// hchan_send enqueues a message to a channel by copying the value at elemptr to the channel.
// Blocks until the message is sent or the channel is closed.
// Returns false if the channel closed.
bool hchan_Send(hchan_t*, void* elemptr);
bool hchanSend(hchan_t*, void* elemptr);

// hchan_recv dequeues a message from a channel by copying a received value to elemptr.
// Blocks until there's a message available or the channel is closed.
// Returns true if a message was received, false if the channel is closed.
bool hchan_Recv(hchan_t*, void* elemptr);
bool hchanRecv(hchan_t*, void* elemptr);

// hchan_trysend attempts to sends a message without blocking.
// It returns true if the message was sent, false if not.
// Unlike hchan_send, this function does not return false to indicate that the channel
// is closed, but instead it returns false if the message was not sent and sets *closed
// to false if the reason for the failure was a closed channel.
bool hchan_TrySend(hchan_t*, void* elemptr, bool* closed);
bool hchanTrySend(hchan_t*, void* elemptr, bool* closed);

// hchan_tryrecv works like hchan_recv but does not block.
// Returns true if a message was received.
// This function does not block/wait.
bool hchan_TryRecv(hchan_t* ch, void* elemptr, bool* closed);
bool hchanTryRecv(hchan_t* ch, void* elemptr, bool* closed);

0 comments on commit c69626c

Please sign in to comment.