`
mryufeng
  • 浏览: 968465 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Adding my own BIF

阅读更多
原文地址:http://www.trapexit.org/Adding_my_own_BIF


Adding my own BIF
From Erlang Community
caveat

unless you really know what you're doing, you'll be better off using a linked-in driver or a port.
steps

    1. run configure
    2. add your bifs to erts/emulator/beam/bif.tab

bif re:grep/2 bif re:compile/1

    3. create a C file

erts/emulator/beam/erl_bif_re.c

    4. add your C file to erts/emulator/<arch>/Makefile

RUN_OBJS = $(OBJDIR)/erl_bif_re.o \

    5. implement your bifs by stealing bits from existing erl_bif_*.c files

BIF_RETTYPE re_grep_2(BIF_ALIST_2){
  Eterm result;
  result = magic_function();
  BIF_RET(result);
}

    6. run make; make install

notes

    * steps 0-3 need only be done once.

    * note that if you add

bif re:grep/2

to bif.tab there should be a erl_bif_re.c that implements

BIF_RETTYPE re_grep_2(BIF_ALIST_2);

为什么要用bif呢? bif比驱动或者port的好处是 bif支持trap, 所以能够让cpu计算平均在各个进程分配,这个网络程序很重要的一个要求. 没有这个特性一个费时的操作会把整个调度器拖死,其他的进程就谈不上什么响应了。下篇文章教你如何写带trap功能的bif。

分享到:
评论
1 楼 mryufeng 2009-07-28  
trap某种意义上讲就是erlang的coroutine, 因为erlang的调度是抢占式的, 同时又给程序员提供了协助式的, 真是贴心哦。。。

相关推荐

Global site tag (gtag.js) - Google Analytics