Skip to content

Commit 1d29a76

Browse files
committed
Update docs of changes, change thread_local setup
- start the replacement of `thread_local` with macro `thrd_local` this macro will do emulation if feature not available in compiler or can be force to emulate if necessary. The macro create functions based off variable name.
1 parent 702fd17 commit 1d29a76

File tree

5 files changed

+41
-31
lines changed

5 files changed

+41
-31
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ To be clear, this is a programming paradigm on structuring your code. Which can
1414
1515
You can read [Fibers, Oh My!](https://graphitemaster.github.io/fibers/) for a breakdown on how the actual context switch here is achieved by assembly. This library incorporates [libuv](http://docs.libuv.org) in a way that make providing callbacks unnecessary, same as in [Using C++ Resumable Functions with Libuv](https://devblogs.microsoft.com/cppblog/using-ibuv-with-c-resumable-functions/). **Libuv** is handling any hardware or multi-threading CPU access. This not necessary for library usage, the setup can be replaced with some other Event Loop library, or just disabled. There is a unmaintained [libasync](https://github.com/btrask/libasync) package tried combining **libco**, with **libuv** too, Linux only.
1616

17-
Two videos covering things to keep in mind about concurrency, [Building Scalable Deployments with Multiple Goroutines](https://www.youtube.com/watch?v=LNNaxHYFhw8) and [Detecting and Fixing Unbound Concurrency Problems](https://www.youtube.com/watch?v=gggi4GIvgrg).
18-
1917
## Table of Contents
2018

2119
* [Introduction](#introduction)
@@ -55,9 +53,13 @@ All internal functions that needs memory allocation is using these routines.
5553
There will be at least one coroutine always present, the initial, required `co_main()`.
5654
When a coroutine finish execution either by returning or exceptions, memory is released/freed.
5755

56+
> Note: This _resources management system_ outlined above has been _decoupled_ to _external libraries_ and now brought in as _dependencies_. Where the above is just wrapper calls to: [c-raii](https://github.com/zelang-dev/c-raii) for complete **Defer**, plus **C++ RAII** behavior, with an custom **malloc** replacement [rpmalloc](https://github.com/zelang-dev/rpmalloc), and emulated **C11 Threads and thread Pool** [cthread](https://github.com/zelang-dev/cthread).
57+
58+
- As such, the listed external libraries allow _smart auto memory management_ behaviors in any application, or any other **coroutine** library for that matter.
59+
5860
The other problem with **C** is the low level usage view. I initially started out with the concept of creating ***Yet Another Programming language***.
59-
But after discovering [Cello High Level C](https://libcello.org/), and the general issues and need to still integrate with exiting C libraries.
60-
This repo is now staging area the missing **C** runtime, [ZeLang](https://docs.zelang.dev). The documentation **WIP**.
61+
62+
But after discovering [Cello High Level C](https://libcello.org/), realizing the general issues and need to still integrate with exiting C libraries. This repo is now the staging area for the missing **C** runtime, [ZeLang](https://docs.zelang.dev). The documentation **WIP**, and source code hasn't been updated to recent changes in this library.
6163

6264
This **page**, `coroutine.h` and _examples folder_ files is the only current docs, but basic usage should be apparent.
6365
The _coroutine execution_ part here is _completed_, but how it operates/behaves with other system resources is what still being developed and tested.

docs/index.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ To be clear, this is a programming paradigm on structuring your code. Which can
1414
1515
You can read [Fibers, Oh My!](https://graphitemaster.github.io/fibers/) for a breakdown on how the actual context switch here is achieved by assembly. This library incorporates [libuv](http://docs.libuv.org) in a way that make providing callbacks unnecessary, same as in [Using C++ Resumable Functions with Libuv](https://devblogs.microsoft.com/cppblog/using-ibuv-with-c-resumable-functions/). **Libuv** is handling any hardware or multi-threading CPU access. This not necessary for library usage, the setup can be replaced with some other Event Loop library, or just disabled. There is a unmaintained [libasync](https://github.com/btrask/libasync) package tried combining **libco**, with **libuv** too, Linux only.
1616

17-
Two videos covering things to keep in mind about concurrency, [Building Scalable Deployments with Multiple Goroutines](https://www.youtube.com/watch?v=LNNaxHYFhw8) and [Detecting and Fixing Unbound Concurrency Problems](https://www.youtube.com/watch?v=gggi4GIvgrg).
18-
1917
## Table of Contents
2018

2119
* [Introduction](#introduction)
@@ -55,9 +53,13 @@ All internal functions that needs memory allocation is using these routines.
5553
There will be at least one coroutine always present, the initial, required `co_main()`.
5654
When a coroutine finish execution either by returning or exceptions, memory is released/freed.
5755

56+
> Note: This _resources management system_ outlined above has been _decoupled_ to _external libraries_ and now brought in as _dependencies_. Where the above is just wrapper calls to: [c-raii](https://github.com/zelang-dev/c-raii) for complete **Defer**, plus **C++ RAII** behavior, with an custom **malloc** replacement [rpmalloc](https://github.com/zelang-dev/rpmalloc), and emulated **C11 Threads and thread Pool** [cthread](https://github.com/zelang-dev/cthread).
57+
58+
- As such, the listed external libraries allow _smart auto memory management_ behaviors in any application, or any other **coroutine** library for that matter.
59+
5860
The other problem with **C** is the low level usage view. I initially started out with the concept of creating ***Yet Another Programming language***.
59-
But after discovering [Cello High Level C](https://libcello.org/), and the general issues and need to still integrate with exiting C libraries.
60-
This repo is now staging area the missing **C** runtime, [ZeLang](https://docs.zelang.dev). The documentation **WIP**.
61+
62+
But after discovering [Cello High Level C](https://libcello.org/), realizing the general issues and need to still integrate with exiting C libraries. This repo is now the staging area for the missing **C** runtime, [ZeLang](https://docs.zelang.dev). The documentation **WIP**, and source code hasn't been updated to recent changes in this library.
6163

6264
This **page**, `coroutine.h` and _examples folder_ files is the only current docs, but basic usage should be apparent.
6365
The _coroutine execution_ part here is _completed_, but how it operates/behaves with other system resources is what still being developed and tested.

include/coroutine.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#endif
77

88
#define time_Second 1000
9+
#define time_Minute 60000
10+
#define time_Hour 3600000
911

1012
#include "uv_routine.h"
1113
#include "raii.h"
@@ -740,6 +742,17 @@ C_API void coroutine_dec_count(void);
740742
C_API void coroutine_log_reset(void);
741743
C_API uv_args_t *coroutine_event_args(void);
742744

745+
/* Collect coroutines with references preventing immediate cleanup. */
746+
C_API void gc_coroutine(routine_t *);
747+
748+
/* Collect channels with references preventing immediate cleanup. */
749+
C_API void gc_channel(channel_t *);
750+
751+
C_API gc_channel_t *gc_channel_list(void);
752+
C_API gc_coroutine_t *gc_coroutine_list(void);
753+
C_API void gc_coroutine_free(void);
754+
C_API void gc_channel_free(void);
755+
743756
C_API void channel_print(channel_t *);
744757
C_API channel_t *channel_create(int, int);
745758
C_API void channel_free(channel_t *);
@@ -889,13 +902,6 @@ C_API string_t co_itoa(int64_t number);
889902
C_API int co_strpos(string_t text, string pattern);
890903
C_API void co_strcpy(string dest, string_t src, size_t len);
891904

892-
C_API void gc_coroutine(routine_t *);
893-
C_API void gc_channel(channel_t *);
894-
C_API gc_channel_t *gc_channel_list(void);
895-
C_API gc_coroutine_t *gc_coroutine_list(void);
896-
C_API void gc_coroutine_free(void);
897-
C_API void gc_channel_free(void);
898-
899905
/* Check if validated by json type */
900906
C_API bool is_json(json_t *);
901907

src/channel.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
#include "coroutine.h"
22

33
static thread_local int channel_id_generate = 0;
4-
static thread_local gc_channel_t *channel_list = NULL;
4+
thrd_local_static(gc_channel_t *, channel_list, NULL)
55
static char error_message[ERROR_SCRAPE_SIZE] = {0};
66

77
void gc_channel(channel_t *ch) {
8-
if (!channel_list)
9-
channel_list = ht_channel_init();
8+
if (is_channel_list_empty())
9+
channel_list_update(ht_channel_init());
1010

1111
if (is_type(ch, CO_CHANNEL))
12-
hash_put(channel_list, co_itoa(ch->id), ch);
12+
hash_put(channel_list(), co_itoa(ch->id), ch);
1313
}
1414

15-
CO_FORCE_INLINE gc_channel_t *gc_channel_list(void) {
16-
return channel_list;
15+
void gc_channel_free(void) {
16+
if (!is_channel_list_empty())
17+
hash_free(channel_list());
1718
}
1819

19-
void gc_channel_free(void) {
20-
if (channel_list)
21-
hash_free(channel_list);
20+
CO_FORCE_INLINE gc_channel_t *gc_channel_list(void) {
21+
return channel_list();
2222
}
2323

2424
channel_t *channel_create(int elem_size, int bufsize) {

src/coroutine.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
#include "coroutine.h"
22

3-
static thread_local gc_coroutine_t *coroutine_list = NULL;
3+
thrd_local_static(gc_coroutine_t *, coroutine_list, NULL)
44

55
void gc_coroutine(routine_t *co) {
6-
if (!coroutine_list)
7-
coroutine_list = (gc_coroutine_t *)ht_group_init();
6+
if (is_coroutine_list_empty())
7+
coroutine_list_update(ht_group_init());
88

99
if (co->magic_number == CO_MAGIC_NUMBER)
10-
hash_put(coroutine_list, co_itoa(co->cid), co);
10+
hash_put(coroutine_list(), co_itoa(co->cid), co);
1111
}
1212

1313
void gc_coroutine_free() {
14-
if (coroutine_list)
15-
hash_free(coroutine_list);
14+
if (!is_coroutine_list_empty())
15+
hash_free(coroutine_list());
1616
}
1717

1818
CO_FORCE_INLINE gc_coroutine_t *gc_coroutine_list() {
19-
return coroutine_list;
19+
return coroutine_list();
2020
}
2121

2222
values_type args_get(void_t params, int item) {

0 commit comments

Comments
 (0)