home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / text_cla / mos.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-12  |  8.1 KB  |  346 lines

  1. /***********************************************************************
  2. **+
  3. **  Module Name:  mos.h
  4. **
  5. **  Description:  mos defines
  6. **
  7. **  Written by:  John Tal
  8. **
  9. **
  10. **  Modification history:
  11. **
  12. **  Date         Engineer     Mod #          Modification Description
  13. **
  14. **  12-FEB-1992  Tal          v 1.0-000      Initial release
  15. **  25-APR-1992  Tal          v 1.0-001      Event Handling
  16. **-
  17. ***********************************************************************/
  18.  
  19.  
  20. #ifdef MOS_H
  21. #else
  22. #define MOS_H
  23.  
  24. #define MOS_ENVIRONMENT
  25.  
  26. #include <memlib.h>
  27.  
  28.  
  29.  
  30. /*
  31. **  Mos Include/define file
  32. */
  33.  
  34.  
  35.  
  36. #define  PROC_NAME_LEN     16
  37.  
  38.  
  39. /*
  40. **  An interprocess message
  41. */
  42.  
  43. struct MSG_S
  44. {
  45.    SHORT   sSendPid;
  46.    SHORT   sDestPid;
  47.    SHORT   sLen;
  48.    PCHAR   pcData;
  49. };
  50.  
  51. typedef struct MSG_S MSG_T;
  52. typedef MSG_T * MSG_P;
  53.  
  54. #define MOS_NO_MSG -1
  55.  
  56.  
  57.  
  58. #define SEM_NAME_LEN 16
  59.  
  60. /*
  61. **  A semaphore
  62. */
  63.  
  64. struct SEM_S
  65. {
  66.    SHORT   sVal;
  67.    CHAR    szSemName[SEM_NAME_LEN + 1];
  68.    CHAR    fInUse;     /* being used, TRUE/FALSE */
  69.    LLIST_P  pstQueHead;  /* processes waiting for this semaphore in queue */
  70.    LLIST_P  pstQueTail;
  71. };
  72.  
  73. typedef struct SEM_S SEM_T;
  74. typedef SEM_T * SEM_P;
  75.  
  76. #define MOS_MAX_SEMS 16
  77.  
  78.  
  79.  
  80.  
  81. /*
  82. **  A Process
  83. */
  84.  
  85. struct PROC_S
  86. {
  87.    CHAR   szName[PROC_NAME_LEN + 1];   /* the process name */
  88.    SHORT  sProcId;                     /* the process id */
  89.    SHORT  sPriority;                   /* the process priority */
  90.    SHORT  sState;                      /* the process state */
  91.    SHORT  sPrevState;                  /* the previous state, for function */
  92.    LLIST_P  pstMsgQueHead;             /* the process msg queue head */
  93.    LLIST_P  pstMsgQueTail;             /* the process msg queue tail */
  94.    LONG   lWakeTime;                   /* time to wakeup process */
  95.    BOOL   fCritical;                   /* flag to prevent swapping */ 
  96. #ifdef C_ANSI
  97.    SHORT  (*pProc)(struct PROC_S *);  /* address of main function which is the process */
  98.    SHORT  (*pBlockedFunc)(PVOID);     /* address of current function which is in process */
  99. #else
  100.    SHORT  (*pProc)();            /* address of main function which is the process */
  101.    SHORT  (*pBlockedFunc)();     /* address of current function which is in process */
  102. #endif
  103.    PVOID  pvWorkArea;
  104. };
  105.  
  106. typedef struct PROC_S PROC_T;
  107. typedef PROC_T * PROC_P;
  108.  
  109.  
  110. /*
  111. **  Process States
  112. */
  113.  
  114. #define  PROC_STATE_NO_EXIST  -1
  115. #define  PROC_STATE_RUNNING   1
  116. #define  PROC_STATE_READY     2
  117. #define  PROC_STATE_SLEEPING  3
  118. #define  PROC_STATE_SEM_WAIT  4
  119. #define  PROC_STATE_MSG_WAIT  5
  120. #define  PROC_STATE_EVT_WAIT  6
  121.  
  122.  
  123.  
  124. /*
  125. **  An event
  126. **  The scheduler will check the event list by calling each check func
  127. **  when the return value from an event check func = TRUE, the
  128. **  EventFunc() is called.   The EventFunc() is a limited-scope MOS
  129. **  process.  All it should do send the appropriate message to whatever
  130. **  regular process(es) are waiting for its messag(es). 
  131. **  When the pCheckFunc is true, the pstEvent proc is placed on the
  132. **  ready queue (event procs should be higher priority than regular
  133. **  procs).  The event remains in the event list so the event proc should
  134. **  set its sState back to PROC_STATE_EVT_WAIT before exiting so it will not
  135. **  be placed back on the ready queue.
  136. **
  137. **  (Event procs are like VMS AST (asynchronus system traps)).  
  138. */
  139.  
  140. struct EVENT_S
  141. {
  142. #ifdef C_ANSI
  143.    SHORT  (*pCheckFunc)(VOID);   /* function to check for event */
  144. #else
  145.    SHORT  (*pCheckFunc)();       /* function to check for event */
  146. #endif
  147.    PROC_P pstEventProc;         /* process awaiting event */
  148. };
  149.  
  150. typedef struct EVENT_S EVENT_T;
  151. typedef EVENT_T * EVENT_P;
  152.  
  153.  
  154.  
  155. #define MOS_EVENT_PROC_EXIT(pstProc) { (pstProc)->sState = PROC_STATE_EVT_WAIT; return (C_OK); }
  156.  
  157.  
  158.  
  159. #define MOS_MAX_SEMS 16
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168. /*
  169. **  BLOCK_FUNC
  170. **
  171. **  The PROC_T member pBlockedFunc is provided for the scheduler to be able
  172. **  to restore a MOS process to a specific function within its call tree.
  173. **
  174. **  The scheduler will normally begin execution of a MOS process with the
  175. **  function which was initialized into pProc by MosProcCreate 
  176. */
  177.  
  178. #define  MOS_SET_BLOCK_FUNC(Proc,Func)  (Proc -> pBlockedFunc = Func)
  179. #define  MOS_CLEAR_BLOCK_FUNC(Proc)     (Proc -> pBlockedFunc = NULL)
  180.  
  181.  
  182. /*
  183. **  PROC WORK AREA
  184. **
  185. **  The proc work area is provided as a utility to each process.
  186. **  It is a void pointer that can point to anything which has meaning
  187. **  within functions of a process.
  188. */
  189.  
  190. #define  MOS_SET_PROC_WORK_AREA(Proc,WA)  (Proc -> pvWorkArea = (PVOID) WA)
  191. #define  MOS_GET_PROC_WORK_AREA(Proc)     (Proc -> pvWorkArea)
  192.  
  193. /* access to other MOS process members */
  194.  
  195. #define  MOS_SET_WAKE_TIME(Proc,time)   (Proc -> lWakeTime = time)
  196. #define  MOS_MSG_WAITING(Proc)         ((Proc -> pstMsgQueHead != NULL) ? C_TRUE : C_FALSE)
  197.  
  198.  
  199. #define  MOS_NUM_PROCS    4   /* for MosGenericProc, not normally used */
  200. #define  MOS_NULL_PROC_PRIO  0  /* priority of Mos Null process */
  201. #define  MOS_PROC_NULL   -1   /* proc not running/found */
  202.  
  203.  
  204. /*
  205. **  SUSPENDED
  206. **  
  207. **  Many MOS functions may not immediately complete for the current
  208. **  process (including MosSemWait, MosSleep, MosMsgRead).  If any of
  209. **  these functions which can block return a suspended return code,
  210. **  your process must immediately do a C_LEAVE(C_OK) to return
  211. **  execution to the MOS scheduler 
  212. */
  213.  
  214. #define  MOS_PROC_SUSPEND  -2  /* signal for you to exit */
  215.  
  216.  
  217.  
  218. #ifdef C_ANSI
  219. SHORT MosInit(VOID);
  220. SHORT MosTerm(VOID);
  221. SHORT MosScheduler(VOID);
  222. SHORT MosProcCreate(SHORT (*)(),PCHAR, SHORT);
  223. SHORT MosProcInit(SHORT (*)(),PCHAR, SHORT, PROC_P *);
  224. SHORT MosProcTerm(PROC_P);
  225. SHORT MosCheckSleepQueue(VOID);
  226. SHORT MosReschedReady(PROC_P);
  227. SHORT MosGetReadyProc(PROC_P *);
  228. SHORT MosGetPidByName(PCHAR,PSHORT);
  229. SHORT MosSemInit(PROC_P,PCHAR,PSHORT);
  230. SHORT MosSemTerm(PROC_P,PCHAR,SHORT);
  231. SHORT MosSemWait(PROC_P,SHORT);
  232. SHORT MosSemClear(PROC_P,SHORT);
  233. SHORT MosSemSet(PROC_P,SHORT);
  234. SHORT MosSemSignal(PROC_P,SHORT);
  235. SHORT MosEventCreate(SHORT (*)(VOID),SHORT (*)(PROC_P),PCHAR, SHORT);
  236. SHORT MosEventTerm(PROC_P);
  237. SHORT MosEventCheckList(VOID);
  238.  
  239. #else
  240. SHORT MosInit();
  241. SHORT MosTerm();
  242. SHORT MosScheduler();
  243. SHORT MosProcCreate();
  244. SHORT MosProcInit();
  245. SHORT MosProcTerm();
  246. SHORT MosCheckSleepQueue();
  247. SHORT MosReschedReady();
  248. SHORT MosGetReadyProc();
  249. SHORT MosGetPidByName();
  250. SHORT MosSemInit();
  251. SHORT MosSemTerm();
  252. SHORT MosSemWait();
  253. SHORT MosSemClear();
  254. SHORT MosSemSet();
  255. SHORT MosSemSignal();
  256. SHORT MosEventCreate();
  257. SHORT MosEventTerm();
  258. SHORT MosEventCheckList();
  259. #endif
  260. /*
  261. **  Mos Function Prototypes
  262. */
  263.  
  264.  
  265. /*
  266. **  The infamous NULL process
  267. */
  268.  
  269. #ifdef C_ANSI
  270. SHORT  MosNullProc(PROC_P pstProc);
  271. #else
  272. SHORT  MosNullProc();
  273. #endif
  274.  
  275.  
  276. /*
  277. **  A user notification function
  278. */
  279.  
  280. #ifdef C_ANSI
  281. SHORT  MosProcAnnounce(PROC_P pstProc);
  282. #else
  283. SHORT  MosProcAnnounce();
  284. #endif
  285.  
  286. /*
  287. **  Semaphore handlers
  288. */
  289.  
  290. #ifdef C_ANSI
  291. SHORT  MosSemWait(PROC_P pstProc,SHORT sSemHandle);
  292. SHORT  MosSemSignal(PROC_P pstProc,SHORT sSemHandle);
  293. #else
  294. SHORT  MosSemWait();
  295. SHORT  MosSemSignal();
  296. #endif
  297.  
  298. /*
  299. **  Time functions for process sleeping
  300. */
  301.  
  302. #ifdef C_ANSI
  303. LONG   MosCurTime(VOID);
  304. SHORT  MosSleep(PROC_P pstProc);
  305. #else
  306. LONG   MosCurTime();
  307. SHORT  MosSleep();
  308. #endif
  309.  
  310. /*
  311. **  Compare functions, called by memlib tree and linked list routines
  312. **  for searching/deleting
  313. */
  314.  
  315. #ifdef C_ANSI
  316. SHORT  MosCompareTime(PVOID pvData1,PVOID pvData2);
  317. SHORT  MosCompareTreProcId(PVOID pvData1,PVOID pvData2);
  318. SHORT  MosCompareIdNode(PVOID pvData1,PVOID pvData2);
  319. SHORT  MosCompareTreProcName(PVOID pvData1,PVOID pvData2);
  320. #else
  321. SHORT  MosCompareTime();
  322. SHORT  MosCompareTreProcId();
  323. SHORT  MosCompareIdNode();
  324. SHORT  MosCompareIdNode();
  325. #endif
  326.  
  327. /*
  328. **   Inter-process messaging
  329. */
  330.  
  331. #ifdef C_ANSI
  332. SHORT  MosMsgWrite(PROC_P pstProc,PCHAR pcData, SHORT sLen, SHORT sTargetProcId);
  333. SHORT  MosMsgRead(PROC_P pstProc, MSG_P * ppcMsgBuf);
  334. SHORT  MosTellMsgRead(PROC_P pstProc,PCHAR pcMsgBuf);
  335. #else
  336. SHORT  MosMsgWrite();
  337. SHORT  MosMsgRead();
  338. SHORT  MosTellMsgRead();
  339. #endif
  340.  
  341.  
  342.  
  343. #endif
  344.  
  345.  
  346.