GNU Pth for User-Space Context


User-Space Context

The following functions provide a stand-alone sub-API for user-space context switching. It internally is based on the same underlying machine context switching mechanism the threads in GNU Pth are based on. Hence these functions you can use for implementing your own simple user-space threads. The pth_uctx_t context is somewhat modeled after POSIX ucontext(3).
The time required to create (via pth_uctx_make(3)) a user-space context can range from just a few microseconds up to a more dramatical time (depending on the machine context switching method which is available on the platform). On the other hand, the raw performance in switching the user-space contexts is always very good (nearly independent of the used machine context switching method). For instance, on an Intel Pentium-III CPU with 800Mhz running under FreeBSD 4 one usually achieves about 260,000 user-space context switches (via pth_uctx_switch(3)) per second.

int pth_uctx_create(pth_uctx_t *uctx);
This function creates a user-space context and stores it into uctx. There is still no underlying user-space context configured. You still have to do this with pth_uctx_make(3). On success, this function returns TRUE, else FALSE.

int pth_uctx_make(pth_uctx_t uctx, char *sk_addr, size_t sk_size, const sigset_t *sigmask, void (*start_func)(void *), void *start_arg, pth_uctx_t uctx_after);
This function makes a new user-space context in uctx which will operate on the run-time stack sk_addr (which is of maximum size sk_size), with the signals in sigmask blocked (if sigmask is not NULL) and starting to execute with the call start_func(start_arg). If sk_addr is NULL, a stack is dynamically allocated. The stack size sk_size has to be at least 16384 (16KB). If the start function start_func returns and uctx_after is not NULL, an implicit user-space context switch to this context is performed. Else (if uctx_after is NULL) the process is terminated with exit(3). This function is somewhat modeled after POSIX makecontext(3). On success, this function returns TRUE, else FALSE.

int pth_uctx_switch(pth_uctx_t uctx_from, pth_uctx_t uctx_to);
This function saves the current user-space context in uctx_from for later restoring by another call to pth_uctx_switch(3) and restores the new user-space context from uctx_to, which previously had to be set with either a previous call to pth_uctx_switch(3) or initially by pth_uctx_make(3). This function is somewhat modeled after POSIX swapcontext(3). If uctx_from or uctx_to are NULL or if uctx_to contains no valid user-space context, FALSE is returned instead of TRUE. These are the only errors possible.

int pth_uctx_destroy(pth_uctx_t uctx);

This function destroys the user-space context in uctx. The run-time stack associated with the user-space context is deallocated only if it was not given by the application (see sk_addr of pth_uctx_create(3)). If uctx is NULL, FALSE is returned instead of TRUE. This is the only error possible.


来源: http://www.gnu.org/software/pth/pth-manual.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值