我不再有以下代码的注释,也没有任何示例 - 这是一个可移植(伪)线程库,实现为预处理器宏
typedef struct
{
unsigned int magic;
unsigned short ctx;
unsigned char is_destroyed;
}
_run;
typedef struct
{
unsigned int magic;
unsigned int cnt;
}
_sem;
#define aa_RUNNER_WAITING 0
#define aa_RUNNER_YIELDED 1
#define aa_RUNNER_EXITED 2
#define aa_RUNNER_ENDED 3
#define aaRunnerCreate(rp) (rp)->magic='runr'; (rp)->ctx=0; (rp)->is_destroyed=NO
#define aaRunnerDestroy(rp) (rp)->is_destroyed=YES
#define aaRunnerThread(args) C args
#define aaRunnerBegin(rp) { C yflag=YES; if(yflag) {} switch((rp)->ctx) { case 0:
#define aaRunnerEnd(rp) } yflag=NO; if(yflag) {} aaRunnerCreate(rp); return aa_RUNNER_ENDED; }
#define aaRunnerWaitUntil(rp,condx) do { (rp)->ctx=__LINE__; case __LINE__: if(!(condx)) { return aa_RUNNER_WAITING; } } while(0)
#define aaRunnerWaitWhile(rp,condi) aaRunnerWaitUntil((rp),!(condi))
#define aaRunnerWaitThread(rp,thr) aaRunnerWaitWhile((rp),aaRunnerSchedule(thr))
#define aaRunnerWaitSpawn(rp,chl,thr) do { aaRunnerCreate((chl)); aaRunnerWaitThread((rp),(thr)); } while(0)
#define aaRunnerRestart(rp) do { aaRunnerCreate(rp); return aa_RUNNER_WAITING; } while(0)
#define aaRunnerExit(rp) do { aaRunnerCreate(rp); (rp)->magic=0; return aa_RUNNER_EXITED; } while(0)
#define aaRunnerSchedule(f) ((f)<aa_RUNNER_EXITED)
#define aaRunnerYield(rp) do { yflag=NO; (rp)->ctx=__LINE__; case __LINE__: if(!yflag||!((rp)->is_destroyed)) { return aa_RUNNER_YIELDED; } } while(0)
#define aaRunnerYieldUntil(rp,condi) do { yflag=NO; (rp)->ctx=__LINE__; case __LINE__: if(!yflag||!(condi)) { return aa_RUNNER_YIELDED; } } while(0)
#define aaRunnerSemInit(sp,c) (sp)->magic='runs'; (sp)->cnt=c
#define aaRunnerSemWait(rp,sp) do { aaRunnerWaitUntil(rp,(sp)->cnt>0); --(sp)->cnt; } while(0)
#define aaRunnerSemSignal(rp,sp) ++(sp)->cnt