⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pth_mctx.c

📁 Linux下的中文输入法
💻 C
📖 第 1 页 / 共 2 页
字号:
/***  GNU Pth - The GNU Portable Threads**  Copyright (c) 1999-2004 Ralf S. Engelschall <rse@engelschall.com>****  This file is part of GNU Pth, a non-preemptive thread scheduling**  library which can be found at http://www.gnu.org/software/pth/.****  This library is free software; you can redistribute it and/or**  modify it under the terms of the GNU Lesser General Public**  License as published by the Free Software Foundation; either**  version 2.1 of the License, or (at your option) any later version.****  This library is distributed in the hope that it will be useful,**  but WITHOUT ANY WARRANTY; without even the implied warranty of**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU**  Lesser General Public License for more details.****  You should have received a copy of the GNU Lesser General Public**  License along with this library; if not, write to the Free Software**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307**  USA, or contact Ralf S. Engelschall <rse@engelschall.com>.****  pth_mctx.c: Pth machine context handling*/                             /* ``If you can't do it in                                  ANSI C, it isn't worth doing.''                                                 -- Unknown        */#include "pth_p.h"#if cpp/* * machine context state structure * * In `jb' the CPU registers, the program counter, the stack * pointer and (usually) the signals mask is stored. When the * signal mask cannot be implicitly stored in `jb', it's * alternatively stored explicitly in `sigs'. The `error' stores * the value of `errno'. */#if PTH_MCTX_MTH(mcsc)#include <ucontext.h>#endiftypedef struct pth_mctx_st pth_mctx_t;struct pth_mctx_st {#if PTH_MCTX_MTH(mcsc)    ucontext_t uc;    int restored;#elif PTH_MCTX_MTH(sjlj)    pth_sigjmpbuf jb;#else#error "unknown mctx method"#endif    sigset_t sigs;#if PTH_MCTX_DSP(sjlje)    sigset_t block;#endif    int error;};/*** ____ MACHINE STATE SWITCHING ______________________________________*//* * save the current machine context */#if PTH_MCTX_MTH(mcsc)#define pth_mctx_save(mctx) \        ( (mctx)->error = errno, \          (mctx)->restored = 0, \          getcontext(&(mctx)->uc), \          (mctx)->restored )#elif PTH_MCTX_MTH(sjlj) && PTH_MCTX_DSP(sjlje)#define pth_mctx_save(mctx) \        ( (mctx)->error = errno, \          pth_sc(sigprocmask)(SIG_SETMASK, &((mctx)->block), NULL), \          pth_sigsetjmp((mctx)->jb) )#elif PTH_MCTX_MTH(sjlj)#define pth_mctx_save(mctx) \        ( (mctx)->error = errno, \          pth_sigsetjmp((mctx)->jb) )#else#error "unknown mctx method"#endif/* * restore the current machine context * (at the location of the old context) */#if PTH_MCTX_MTH(mcsc)#define pth_mctx_restore(mctx) \        ( errno = (mctx)->error, \          (mctx)->restored = 1, \          (void)setcontext(&(mctx)->uc) )#elif PTH_MCTX_MTH(sjlj)#define pth_mctx_restore(mctx) \        ( errno = (mctx)->error, \          (void)pth_siglongjmp((mctx)->jb, 1) )#else#error "unknown mctx method"#endif/* * restore the current machine context * (at the location of the new context) */#if PTH_MCTX_MTH(sjlj) && PTH_MCTX_DSP(sjlje)#define pth_mctx_restored(mctx) \        pth_sc(sigprocmask)(SIG_SETMASK, &((mctx)->sigs), NULL)#else#define pth_mctx_restored(mctx) \        /*nop*/#endif/* * switch from one machine context to another */#define SWITCH_DEBUG_LINE \        "==== THREAD CONTEXT SWITCH ==========================================="#ifdef PTH_DEBUG#define  _pth_mctx_switch_debug pth_debug(NULL, 0, 1, SWITCH_DEBUG_LINE);#else#define  _pth_mctx_switch_debug /*NOP*/#endif#if PTH_MCTX_MTH(mcsc)#define pth_mctx_switch(old,new) \    _pth_mctx_switch_debug \    swapcontext(&((old)->uc), &((new)->uc));#elif PTH_MCTX_MTH(sjlj)#define pth_mctx_switch(old,new) \    _pth_mctx_switch_debug \    if (pth_mctx_save(old) == 0) \        pth_mctx_restore(new); \    pth_mctx_restored(old);#else#error "unknown mctx method"#endif#endif /* cpp *//*** ____ MACHINE STATE INITIALIZATION ________________________________*/#if PTH_MCTX_MTH(mcsc)/* * VARIANT 1: THE STANDARDIZED SVR4/SUSv2 APPROACH * * This is the preferred variant, because it uses the standardized * SVR4/SUSv2 makecontext(2) and friends which is a facility intended * for user-space context switching. The thread creation therefore is * straight-foreward. */intern int pth_mctx_set(    pth_mctx_t *mctx, void (*func)(void), char *sk_addr_lo, char *sk_addr_hi){    /* fetch current context */    if (getcontext(&(mctx->uc)) != 0)        return FALSE;    /* remove parent link */    mctx->uc.uc_link           = NULL;    /* configure new stack */    mctx->uc.uc_stack.ss_sp    = pth_skaddr(makecontext, sk_addr_lo, sk_addr_hi-sk_addr_lo);    mctx->uc.uc_stack.ss_size  = pth_sksize(makecontext, sk_addr_lo, sk_addr_hi-sk_addr_lo);    mctx->uc.uc_stack.ss_flags = 0;    /* configure startup function (with no arguments) */    makecontext(&(mctx->uc), func, 0+1);    return TRUE;}#elif PTH_MCTX_MTH(sjlj)     &&\      !PTH_MCTX_DSP(sjljlx)  &&\      !PTH_MCTX_DSP(sjljisc) &&\      !PTH_MCTX_DSP(sjljw32)/* * VARIANT 2: THE SIGNAL STACK TRICK * * This uses sigstack/sigaltstack() and friends and is really the * most tricky part of Pth. When you understand the following * stuff you're a good Unix hacker and then you've already * understood the gory ingredients of Pth.  So, either welcome to * the club of hackers, or do yourself a favor and skip this ;) * * The ingenious fact is that this variant runs really on _all_ POSIX * compliant systems without special platform kludges.  But be _VERY_ * carefully when you change something in the following code. The slightest * change or reordering can lead to horribly broken code.  Really every * function call in the following case is intended to be how it is, doubt * me... * * For more details we strongly recommend you to read the companion * paper ``Portable Multithreading -- The Signal Stack Trick for * User-Space Thread Creation'' from Ralf S. Engelschall. A copy of the * draft of this paper you can find in the file rse-pmt.ps inside the * GNU Pth distribution. */#if !defined(SA_ONSTACK) && defined(SV_ONSTACK)#define SA_ONSTACK SV_ONSTACK#endif#if !defined(SS_DISABLE) && defined(SA_DISABLE)#define SS_DISABLE SA_DISABLE#endif#if PTH_MCTX_STK(sas) && !defined(HAVE_SS_SP) && defined(HAVE_SS_BASE)#define ss_sp ss_base#endifstatic volatile jmp_buf      mctx_trampoline;static volatile pth_mctx_t   mctx_caller;static volatile sig_atomic_t mctx_called;static pth_mctx_t * volatile mctx_creating;static      void (* volatile mctx_creating_func)(void);static volatile sigset_t     mctx_creating_sigs;static void pth_mctx_set_trampoline(int);static void pth_mctx_set_bootstrap(void);/* initialize a machine state */intern int pth_mctx_set(    pth_mctx_t *mctx, void (*func)(void), char *sk_addr_lo, char *sk_addr_hi){    struct sigaction sa;    struct sigaction osa;#if PTH_MCTX_STK(sas) && defined(HAVE_STACK_T)    stack_t ss;    stack_t oss;#elif PTH_MCTX_STK(sas)    struct sigaltstack ss;    struct sigaltstack oss;#elif PTH_MCTX_STK(ss)    struct sigstack ss;    struct sigstack oss;#else#error "unknown mctx stack setup"#endif    sigset_t osigs;    sigset_t sigs;    pth_debug1("pth_mctx_set: enter");    /*     * Preserve the SIGUSR1 signal state, block SIGUSR1,     * and establish our signal handler. The signal will     * later transfer control onto the signal stack.     */    sigemptyset(&sigs);    sigaddset(&sigs, SIGUSR1);    pth_sc(sigprocmask)(SIG_BLOCK, &sigs, &osigs);    sa.sa_handler = pth_mctx_set_trampoline;    sigemptyset(&sa.sa_mask);    sa.sa_flags = SA_ONSTACK;    if (sigaction(SIGUSR1, &sa, &osa) != 0)        return FALSE;    /*     * Set the new stack.     *     * For sigaltstack we're lucky [from sigaltstack(2) on     * FreeBSD 3.1]: ``Signal stacks are automatically adjusted     * for the direction of stack growth and alignment     * requirements''     *     * For sigstack we have to decide ourself [from sigstack(2)     * on Solaris 2.6]: ``The direction of stack growth is not     * indicated in the historical definition of struct sigstack.     * The only way to portably establish a stack pointer is for     * the application to determine stack growth direction.''     */#if PTH_MCTX_STK(sas)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -