diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-09-03 03:53:36 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-09-03 03:53:36 -0400 |
| commit | 5feecff9d93522002c74a1423d138c2aa8bc150d (patch) | |
| tree | ff64b7695be5fd8716cfe6ed640ed556dc203587 /builtins | |
| parent | 5f0b099e1444d462be5aa8df49f87c9b324e1b85 (diff) | |
Deprecate `Where` and change channel API to use a boolean `front` value
Diffstat (limited to 'builtins')
| -rw-r--r-- | builtins/channel.c | 23 | ||||
| -rw-r--r-- | builtins/channel.h | 19 | ||||
| -rw-r--r-- | builtins/text.h | 1 | ||||
| -rw-r--r-- | builtins/where.c | 37 | ||||
| -rw-r--r-- | builtins/where.h | 37 |
5 files changed, 20 insertions, 97 deletions
diff --git a/builtins/channel.c b/builtins/channel.c index a0a0ddc5..08d6424f 100644 --- a/builtins/channel.c +++ b/builtins/channel.c @@ -18,11 +18,10 @@ #include "text.h" #include "types.h" #include "util.h" -#include "where.h" public channel_t *Channel$new(Int_t max_size) { - if (Int$compare_value(max_size, I(0)) <= 0) + if (Int$compare_value(max_size, I_small(0)) <= 0) fail("Cannot create a channel with a size less than one: %ld", max_size); channel_t *channel = new(channel_t); channel->items = (array_t){}; @@ -32,22 +31,22 @@ public channel_t *Channel$new(Int_t max_size) return channel; } -public void Channel$give(channel_t *channel, const void *item, Where_t where, int64_t padded_item_size) +public void Channel$give(channel_t *channel, const void *item, bool front, int64_t padded_item_size) { (void)pthread_mutex_lock(&channel->mutex); while (channel->items.length >= channel->max_size) pthread_cond_wait(&channel->cond, &channel->mutex); - Int_t index = (where.tag == $tag$Where$Start) ? I(1) : I(0); + Int_t index = front ? I_small(1) : I_small(0); Array$insert(&channel->items, item, index, padded_item_size); (void)pthread_mutex_unlock(&channel->mutex); (void)pthread_cond_signal(&channel->cond); } -public void Channel$give_all(channel_t *channel, array_t to_give, Where_t where, int64_t padded_item_size) +public void Channel$give_all(channel_t *channel, array_t to_give, bool front, int64_t padded_item_size) { if (to_give.length == 0) return; (void)pthread_mutex_lock(&channel->mutex); - Int_t index = (where.tag == $tag$Where$Start) ? I(1) : I(0); + Int_t index = front ? I_small(1) : I_small(0); if (channel->items.length + to_give.length >= channel->max_size) { for (int64_t i = 0; i < to_give.length; i++) { while (channel->items.length >= channel->max_size) @@ -61,24 +60,24 @@ public void Channel$give_all(channel_t *channel, array_t to_give, Where_t where, (void)pthread_cond_signal(&channel->cond); } -public void Channel$get(channel_t *channel, void *out, Where_t where, int64_t item_size, int64_t padded_item_size) +public void Channel$get(channel_t *channel, void *out, bool front, int64_t item_size, int64_t padded_item_size) { (void)pthread_mutex_lock(&channel->mutex); while (channel->items.length == 0) pthread_cond_wait(&channel->cond, &channel->mutex); - memcpy(out, channel->items.data, item_size); - Int_t index = (where.tag == $tag$Where$End) ? I(0) : I(1); - Array$remove_at(&channel->items, index, I(1), padded_item_size); + memcpy(out, channel->items.data + channel->items.stride * (front ? 0 : channel->items.length-1), item_size); + Int_t index = front ? I_small(1) : Int64_to_Int(channel->items.length); + Array$remove_at(&channel->items, index, I_small(1), padded_item_size); (void)pthread_mutex_unlock(&channel->mutex); (void)pthread_cond_signal(&channel->cond); } -public void Channel$peek(channel_t *channel, void *out, Where_t where, int64_t item_size) +public void Channel$peek(channel_t *channel, void *out, bool front, int64_t item_size) { (void)pthread_mutex_lock(&channel->mutex); while (channel->items.length == 0) pthread_cond_wait(&channel->cond, &channel->mutex); - int64_t index = (where.tag == $tag$Where$End) ? channel->items.length-1 : 0; + int64_t index = front ? 0 : channel->items.length-1; memcpy(out, channel->items.data + channel->items.stride*index, item_size); (void)pthread_mutex_unlock(&channel->mutex); (void)pthread_cond_signal(&channel->cond); diff --git a/builtins/channel.h b/builtins/channel.h index bf24f806..debbc457 100644 --- a/builtins/channel.h +++ b/builtins/channel.h @@ -8,18 +8,17 @@ #include "datatypes.h" #include "types.h" #include "util.h" -#include "where.h" channel_t *Channel$new(Int_t max_size); -void Channel$give(channel_t *channel, const void *item, Where_t where, int64_t padded_item_size); -#define Channel$give_value(channel, item, where, padded_item_size) \ - ({ __typeof(item) _item = item; Channel$give(channel, &_item, where, padded_item_size); }) -void Channel$give_all(channel_t *channel, array_t to_give, Where_t where, int64_t padded_item_size); -void Channel$get(channel_t *channel, void *out, Where_t where, int64_t item_size, int64_t padded_item_size); -#define Channel$get_value(channel, where, t, padded_item_size) \ - ({ t _val; Channel$get(channel, &_val, where, sizeof(t), padded_item_size); _val; }) -void Channel$peek(channel_t *channel, void *out, Where_t where, int64_t item_size); -#define Channel$peek_value(channel, where, t) ({ t _val; Channel$peek(channel, &_val, where, sizeof(t)); _val; }) +void Channel$give(channel_t *channel, const void *item, bool front, int64_t padded_item_size); +#define Channel$give_value(channel, item, front, padded_item_size) \ + ({ __typeof(item) _item = item; Channel$give(channel, &_item, front, padded_item_size); }) +void Channel$give_all(channel_t *channel, array_t to_give, bool front, int64_t padded_item_size); +void Channel$get(channel_t *channel, void *out, bool front, int64_t item_size, int64_t padded_item_size); +#define Channel$get_value(channel, front, t, padded_item_size) \ + ({ t _val; Channel$get(channel, &_val, front, sizeof(t), padded_item_size); _val; }) +void Channel$peek(channel_t *channel, void *out, bool front, int64_t item_size); +#define Channel$peek_value(channel, front, t) ({ t _val; Channel$peek(channel, &_val, front, sizeof(t)); _val; }) void Channel$clear(channel_t *channel); array_t Channel$view(channel_t *channel); uint64_t Channel$hash(const channel_t **channel, const TypeInfo *type); diff --git a/builtins/text.h b/builtins/text.h index 20ea9a25..b3cb6d79 100644 --- a/builtins/text.h +++ b/builtins/text.h @@ -10,7 +10,6 @@ #include "datatypes.h" #include "integers.h" #include "types.h" -#include "where.h" int printf_text(FILE *stream, const struct printf_info *info, const void *const args[]); int printf_text_size(const struct printf_info *info, size_t n, int argtypes[n], int sizes[n]); diff --git a/builtins/where.c b/builtins/where.c deleted file mode 100644 index d57f532e..00000000 --- a/builtins/where.c +++ /dev/null @@ -1,37 +0,0 @@ -// A type called "Where" that is an enum for "Anywhere", "Start", or "End" -// Mainly used for text methods - -#include <stdbool.h> -#include <stdint.h> - -#include "text.h" -#include "types.h" -#include "util.h" -#include "where.h" - -static Text_t Where$as_text(Where_t *obj, bool use_color) -{ - if (!obj) - return Text$from_str("Where"); - switch (obj->tag) { - case $tag$Where$Anywhere: - return Text$from_str(use_color ? "\x1b[36;1mWhere.Anywhere\x1b[m" : "Where.Anywhere"); - case $tag$Where$Start: - return Text$from_str(use_color ? "\x1b[36;1mWhere.Start\x1b[m" : "Where.Start"); - case $tag$Where$End: - return Text$from_str(use_color ? "\x1b[36;1mWhere.End\x1b[m" : "Where.End"); - default: - return (Text_t){.length=0}; - } -} - -public const Where_t Where$tagged$Anywhere = {$tag$Where$Anywhere}; -public const Where_t Where$tagged$Start = {$tag$Where$Start}; -public const Where_t Where$tagged$End = {$tag$Where$End}; -public const TypeInfo Where$Anywhere = {0, 0, {.tag=EmptyStruct, .EmptyStruct.name="Anywhere"}}; -public const TypeInfo Where$Start = {0, 0, {.tag=EmptyStruct, .EmptyStruct.name="Start"}}; -public const TypeInfo Where$End = {0, 0, {.tag=EmptyStruct, .EmptyStruct.name="End"}}; -public const TypeInfo Where = {sizeof(Where_t), __alignof__(Where_t), - {.tag=CustomInfo, .CustomInfo={.as_text=(void*)Where$as_text}}}; - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/where.h b/builtins/where.h deleted file mode 100644 index cfb45c62..00000000 --- a/builtins/where.h +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -// Type info and methods for Where datatype (Anywhere, Start, or End enum) -// Mainly used for text methods. - -#include <gc/cord.h> -#include <stdbool.h> -#include <stdint.h> - -#include "types.h" - -typedef struct Where_s Where_t; -extern const TypeInfo Where; -typedef struct Where$Anywhere_s Where$Anywhere_t; -extern const TypeInfo Where$Anywhere; -typedef struct Where$Start_s Where$Start_t; -extern const TypeInfo Where$Start; -typedef struct Where$End_s Where$End_t; -extern const TypeInfo Where$End; - -struct Where$Anywhere_s {}; -struct Where$Start_s {}; -struct Where$End_s {}; -struct Where_s { - enum { $tag$Where$Anywhere = 0, $tag$Where$Start = 1, $tag$Where$End = 2 } tag; - union { - Where$Anywhere_t Anywhere; - Where$Start_t Start; - Where$End_t End; - }; -}; - -extern const Where_t Where$tagged$Anywhere; -extern const Where_t Where$tagged$Start; -extern const Where_t Where$tagged$End; - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 |
