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

📄 microthreads.h

📁 Simple microthreads and fsm for microcontrollers using only preprocessor/inline code
💻 H
字号:
//------------------------------------------------------------------------------
/*!	
   \file microthreads.h
   \brief Include file to simplify finite state machines (FSM) implementation.

   \version rel. 1.2 gen 2009
   \date 27/01/2009
   \author Massimo Manca, Micron Engineering di M. Manca 2006.
   \author www.micronengineering.it
   \author massimo.manca@micronengineering.it
   \author This is completely free software.
*/
//------------------------------------------------------------------------------
#ifndef MicroThreadsH
#define MicroThreadsH


#define THREAD_EXITED   0
#define THREAD_WAITING  1
#define THREAD_ENDED    -1

#define FIRST_STATE     -127


#define THREAD_END      return(0); } return(0);
#define THREAD_SET(s)   s = (int)__LINE__; case __LINE__:

#define MicroThread(name_args)   int name_args

#define ThreadAbort()            \
   do {                          \
      eStateCnt = THREAD_EXITED; \
      return(THREAD_ENDED);      \
   } while(0)

#define ThreadExit()             \
   do {                          \
      eStateCnt = FIRST_STATE;   \
      return(FSM_EXITED);        \
   } while(0)

#define ThreadBegin()            \
   static enum eFsmState {IDLE=FIRST_STATE }eStateCnt=FIRST_STATE;   \
   char cSuspend=1;        \
   while(1) {              \
      switch(eStateCnt) {  \
         case IDLE:
      
#define ThreadEnd()     \
     break;             \
     default: ; }       \
     ThreadRestart();   \
     THREAD_END
     
#define ThreadRestart() eStateCnt=FIRST_STATE
#define ThreadReturn()  return(THREAD_WAITING)
#define ThreadRun(f)    (f != THREAD_EXITED)

#define ThreadSuspend()          \
   do {                          \
      cSuspend = 0;              \
      ThreadWaitUntil(cSuspend); \
   } while(0)

#define ThreadWaitUntil(condition)  \
  do {				                  \
    THREAD_SET(eStateCnt);		      \
    if(!(condition)) 		         \
      return(THREAD_WAITING);       \
  } while(0)

#define ThreadWaitWhile(condition)  ThreadWaitUntil(!(condition))
#define ThreadWaitExit(t)           while(ThreadRun(t))
#define ThreadSchedule(f)           ((f) == THREAD_WAITING)

// SEMAPHORES
struct mt_semaphore {
  unsigned short wCount;
};

typedef struct mt_semaphores THREAD_SEMAPHORE;

#define ThreadSemaphoreSignal(s) ++(s)->wCount

#define ThreadSemaphoreWait(s)         \
  do {						               \
    ThreadWaitUntil((s)->count > 0);	\
    --(s)->count;				            \
  } while(0)



//---------------------------------------------------------------------------
#endif
 

⌨️ 快捷键说明

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