Yielding C Fun
==============
Introduction
------------
Yielding C Fun (YCF) is a tool that transforms functions written in a
subset of the C programming language so that they become yieldable. A
yieldable function can be suspended/yielded/paused/trapped (either
automatically or where the user has inserted a particular statement)
and then be resumed at a later point. Yileldable functions are also
called [coroutines](https://en.wikipedia.org/wiki/Coroutine).
Difference Between Yielding C Fun and Coroutine Libraries
---------------------------------------------------------
Several libraries implement [coroutine support for the C programming
language](https://en.wikipedia.org/wiki/Coroutine#Implementations_for_C)
(e.g., \[[1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11],
[12], [13]\]). These libraries either rely on platform-specific code
or do not save call stack variables. Yielding C Fun (YCF) does not
have any of these two limitations. YCF can accomplish this as it is a
source-to-source transformation tool and not only a library.
YCF has been created to make it easier to implement yielding Erlang
[NIFs](http://erlang.org/doc/tutorial/nif.html) and
[BIFs](http://erlang.org/pipermail/erlang-questions/2009-October/046899.html)
(i.e., Erlang functions that are written in C). Below are examples of
YCF features that are useful when implementing yielding Erlang NIFs
and BIFs:
* YCF automatically generates a destroy function for each yieldable
function. The destroy function frees resources that are used by a
suspended function. The destroy function is useful when a suspended
function needs to abort (e.g., when the Erlang process that invoked
the function has died).
* YCF can automatically insert code that yields functions after a
user specifiable number of loop iterations and goto statements.
* YCF has a hook system that lets the user insert code that is
triggered when certain events happen (e.g., when a function
yields).
The main limitations of YCF are that it cannot handle all valid C code
and that it cannot make library functions without source code
yieldable. Pointers to stack-allocated data are also not allowed (YCF
has a memory allocation function called `YCF_STACK_ALLOC` to work
around this issue).
Requirements
------------
* A C99 compatible C compiler
* make (optional but useful for building)
Compile and Test
----------------
Build the executable `$YCF_ROOT/bin/yielding_c_fun.bin`:
make
Build the executable and run all tests:
make test
Getting Started
---------------
A brief introduction tutorial can be found
[here](doc/thread_tutorial.md). This tutorial is a perfect place to
start!
The "[test/examples/](test/examples/)" folder in this repository
contains many small examples that are useful when learning about
YCF. YCF's automatic tests use these examples as well. The driver for
these tests is located in `test/test.sh`.
[This Erlang NIF example](test/examples/sha256_erlang_nif/) shows how
one can use YCF to write a yielding Erlang NIF library.
Command Line Parameters
-----------------------
```
Usage: yielding_c_fun [-h]
yielding_c_fun [-use_gc [-print_gc_info]]
[-log_max_mem_usage log_file]
[(( -f | -frec | -fnoauto ) function_name)...
[-output_file_name output_file]
[-header_file_name header_file]
[-debug]
[-only_yielding_funs]
[-static_aux_funs]
input_c_file]]
```
* `-h`
Print help text
* `-use_gc`
Use garbage collection. The garbage collection system assumes that
the C call stack consists of a continuous memory block and is
therefore not enabled by default even though this assumption is
valid on all major platforms. YCF does not reclaim any allocated
memory if the `-use_gc` flag is not set.
* `-print_gc_info`
(For debugging) Print garbage collection information to `stderr`
* `-log_max_mem_usage log_file`
(For debugging) Print the peak memory usage of the tool to the file
`log_file`
* `-fnoauto function_name`
Generate a yieldable version of the function named
function_name. The user can use `YCF_YIELD()`,
`YCF_YIELD_NO_REDS()`, and `YCF_CONSUME_REDS(N)` to control
when and where the function should yield. See the section titled
"Special Statements and Macros" for more information.
* `-f function_name`
Generate a yieldable version of the function named
`function_name`. The generated function automatically decrements the
reduction counter by one at the beginning of loop bodies and before
goto statements. The function yields automatically if the reduction
counter reaches a value that is zero or smaller after it has been
decremented.
* `-frec function_name`
Same as the `-f` option with the exception that the generated function
also decrements one reduction before calls to other yieldable
functions and before returning. The function yields automatically if
the reduction counter reaches a value that is zero or smaller after
it has been decremented.
* `-fexternal function_name`
YCF expects that a yielding version of the function called
`function_name` is generated externally. Calls to the function
called `function_name` from yielding functions calls the externally
generated yielding version of the function called `function_name`.
* `-output_file_name output_file`
Output the generated code to a file named output_file. The output
is printed to standard output if this parameter is unspecified.
* `-header_file_name header_file`
Generate a header file containing only declarations for the generated
functions and write the header file to the file named header_file.
* `-debug`
Generate debug code that executes when a function yields. The debug
code searches the call stack of the yielding functions for pointers
to data that is allocated on the call stack. The program crashes
with an error message if any such pointer is found.
The generated debug code depends on that a function called
`ycf_debug_get_stack_start()` is declared somewhere in the
program. The `ycf_debug_get_stack_start()` functions should return a
value of type `void*`. Example:
static _Thread_local void* ycf_debug_global_stack_start_ptr = NULL;
void* ycf_debug_get_stack_start() {
return ycf_debug_global_stack_start_ptr;
}
If `ycf_debug_get_stack_start()` returns `NULL`, the value of the
`ycf_yield_state` parameter will be used as the start of the stack (it
is assumed that the stack grows towards lower addresses). If
`ycf_debug_get_stack_start()` returns something different than `NULL`,
that value will be used as the start of the stack. To check that
nested yielding functions do not have pointers to the call stack,
one have to make sure that `ycf_debug_get_stack_start()` returns
something different than `NULL` (otherwise, each function will just
check for pointers to its own frame). Example:
ycf_debug_global_stack_start_ptr = &wb;
ret = fun_ycf_gen_yielding(&nr_of_reductions,&wb,NULL,allocator,freer,NULL,0,NULL,1);
ycf_debug_global_stack_start_ptr = NULL;
* `-only_yielding_funs`
Print only the generated functions and struct declarations. The
default behavior is to insert the generated functions into a copy of
the input source file.
* `-static_aux_funs`
Make the generated auxiliary functions static (i.e., local to the C
compilation unit)
* `input_c_file`
The source file containing the functions that YCF shall create
yieldable versions of. YCF does not do any macro expansions. There
are several restrictions on the code that YCF can handle that are
described in the "Code Restrictions" section below.
Generated Functions
-------------------
YCF generates three functions for each function name that it is
given. These functions have
没有合适的资源?快使用搜索试试~ 我知道了~
otp_src_25.0.2.tar.gz
需积分: 15 3 下载量 165 浏览量
2022-07-01
12:14:48
上传
评论
收藏 98.42MB GZ 举报
温馨提示
共2000个文件
erl:3618个
xml:2354个
beam:1322个
otp_src_25.0.2.tar.gz
资源详情
资源评论
资源推荐
收起资源包目录
otp_src_25.0.2.tar.gz (2000个子文件)
prim_socket_nif.c 613KB
erl_process.c 425KB
inet_drv.c 391KB
pcre_jit_compile.c 357KB
pcre_compile.c 316KB
erl_proc_sig_queue.c 261KB
pcre_exec.c 232KB
erl_alloc_util.c 232KB
io.c 208KB
pcre_ucd.c 204KB
dist.c 194KB
erl_nif.c 175KB
erl_bif_info.c 172KB
external.c 170KB
bif.c 161KB
utils.c 158KB
erl_db_util.c 154KB
erl_db.c 152KB
prim_net_nif.c 139KB
ethr_atomics.c 130KB
erl_db_hash.c 125KB
pcre_dfa_exec.c 124KB
erl_db_tree.c 118KB
nif_SUITE.c 116KB
erl_gc.c 116KB
erl_alloc.c 107KB
erl_map.c 101KB
erl_check_io.c 96KB
erl_trace.c 92KB
sys.c 91KB
ycf_yield_fun.c 90KB
odbcserver.c 82KB
erl_hl_timer.c 81KB
erl_init.c 80KB
deflate.c 80KB
erl_db_catree.c 79KB
erl_unicode.c 79KB
ethr_mutex.c 77KB
erl_mmap.c 76KB
erl_poll.c 73KB
ei_connect.c 73KB
erl_bif_binary.c 72KB
socket_util.c 72KB
erl_node_tables.c 70KB
erl_time_sup.c 69KB
inet_gethost.c 68KB
win_con.c 66KB
copy.c 65KB
beam_common.c 64KB
big.c 64KB
erl_bif_trace.c 64KB
beam_bif_load.c 59KB
erl_cpu_topology.c 59KB
sys_drivers.c 56KB
erlexec.c 55KB
inflate.c 55KB
erl_bits.c 55KB
beam_file.c 54KB
ycf_parser.c 54KB
erl_port_task.c 53KB
win_prim_file.c 50KB
erl_monitor_link.c 50KB
erl_bif_ddll.c 50KB
erl_misc_utils.c 49KB
pkey.c 49KB
emu_load.c 49KB
time.c 48KB
pcre_study.c 48KB
erl_call.c 48KB
erl_bif_persistent.c 47KB
erl_ptab.c 47KB
erl_process_lock.c 47KB
ethread_tests.c 46KB
erl_message.c 46KB
erl_bif_port.c 45KB
prim_file_nif.c 45KB
trees.c 42KB
erl_bif_re.c 42KB
erl_thr_progress.c 41KB
epmd_srv.c 41KB
ttsl_drv.c 41KB
beam_bp.c 40KB
binary.c 40KB
ycf_node.c 39KB
asn1_erl_nif.c 38KB
erl_lock_check.c 38KB
run_erl.c 37KB
erl_process_dump.c 37KB
erl_mseg.c 37KB
erl_ao_firstfit_alloc.c 37KB
api_ng.c 37KB
beam_debug.c 36KB
chkio_drv.c 35KB
erl_bif_lists.c 35KB
erl_arith.c 34KB
erlsrv_interactive.c 33KB
erl_poll.c 32KB
erl_io_queue.c 32KB
zlib_nif.c 32KB
asm_load.c 31KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
fgfxsn
- 粉丝: 0
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于matlab实现文档+程序边缘计算任务卸载与资源调度的算法,是论文的源代码,具有价值.rar
- 什么是学生成绩管理系统c++以及学习学生成绩管理系统的意义
- 什么是词向量-以及学习关于了解词向量的意义
- 什么是mybatis动态sql以及学习mybatis动态sql的意义
- 华为数据治理方法论,包括:数据治理框架、数据治理组织架构、数据治理度量评估体系以及华为数据治理案例分享
- 基于matlab实现对表面肌电信号进行归一化处理,并对归一化后的图形显示 .rar
- 基于matlab实现单级倒立摆的 T-S 模型 包括 LMI 程序源码
- 图书管理系统(struts+hibernate+spring+ext).rar
- 基于matlab实现此压缩包包含语音信号处理中的语音变声代码加音频.rar
- STM32使用PWM驱动舵机并通过OLED显示
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0