您的位置:首页 > 其它

GNU Pth for User-Space Context

2014-02-23 18:55 387 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: