Nife version Beta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

230 lines
7.2 KiB

  1. /* Copyright (C) 2011-2022 Patrick H. E. Foubet - S.E.R.I.A.N.E.
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or any
  5. later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>
  12. *******************************************************************/
  13. /* mth.c : for multi-threading implementation */
  14. #include "conf.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include "nife.h"
  18. #include "mth.h"
  19. #include "err.h"
  20. #ifdef _MULTI_THREADING_
  21. struct TH_Base {
  22. void * stk_N;
  23. void * Fct_Inst;
  24. uint32_t netKEY;
  25. int i_stkL;
  26. int i_stkC;
  27. int Fd_In;
  28. int i_Ts;
  29. char * stk_C[LSTACKC];
  30. short NbLig;
  31. short NbTab;
  32. short Vars;
  33. short FcType;
  34. short ModePr;
  35. bool stk_L[LSTACKL];
  36. bool Double;
  37. bool EchoOff;
  38. bool Run;
  39. bool WaitPid;
  40. bool fctEnC;
  41. bool strEnC;
  42. bool inSonP;
  43. char BufC[MAXSTRING];
  44. char BufP[LBUF];
  45. char BufP2[LBUF];
  46. PFC trSuite[NBTRSUITE];
  47. jmp_buf Env_Int;
  48. };
  49. pthread_key_t
  50. k_Base, /* the base structure */
  51. k_StkN, /* Numerical stack */
  52. k_StkL, /* Logical stack */
  53. k_iStL, /* index of Logical stack */
  54. k_StkC, /* Character stack */
  55. k_iStC, /* index of Character stack */
  56. k_FdIn, /* File Descriptor input */
  57. k_iTs, /* index of TraiteSuite */
  58. k_bufC, /* buffer for Character stack */
  59. k_bufP, /* buffer for Principal Input */
  60. k_bufP2, /* buffer for Secondary Input */
  61. k_trSu, /* table PFC traiteSuite */
  62. k_FIns, /* Fct_Inst Fct to be install Lib or User */
  63. k_EnvI, /* Env_Int */
  64. k_NetK, /* NetKey */
  65. k_NLig, /* NbLig */
  66. k_NTab, /* NbTab */
  67. k_Vars, /* VARS */
  68. k_FTyp, /* FCT_TYP */
  69. k_Doub, /* Double On/Off */
  70. k_modPr, /* Mode Print Integer (Dec=0, Hex=1, Oct=2, Bin=3) */
  71. k_Run, /* RUN On/Off */
  72. k_WPid, /* WAITPID On/Off */
  73. k_fEnC, /* fctEnCours On/Off */
  74. k_sEnC, /* stringEnCours On/Off */
  75. k_inSP, /* inSonProc On/Off */
  76. k_Echo; /* EchoOff On/Off */
  77. static pthread_once_t k_Init = PTHREAD_ONCE_INIT;
  78. static void make_keys()
  79. {
  80. pthread_key_create(&k_Base, free);
  81. pthread_key_create(&k_StkN, NULL);
  82. pthread_key_create(&k_StkL, NULL);
  83. pthread_key_create(&k_iStL, NULL);
  84. pthread_key_create(&k_StkC, NULL);
  85. pthread_key_create(&k_iStC, NULL);
  86. pthread_key_create(&k_FdIn, NULL);
  87. pthread_key_create(&k_iTs, NULL);
  88. pthread_key_create(&k_bufC, NULL);
  89. pthread_key_create(&k_bufP, NULL);
  90. pthread_key_create(&k_bufP2, NULL);
  91. pthread_key_create(&k_trSu, NULL);
  92. pthread_key_create(&k_FIns, NULL);
  93. pthread_key_create(&k_EnvI, NULL);
  94. pthread_key_create(&k_NetK, NULL);
  95. pthread_key_create(&k_NLig, NULL);
  96. pthread_key_create(&k_NTab, NULL);
  97. pthread_key_create(&k_Vars, NULL);
  98. pthread_key_create(&k_FTyp, NULL);
  99. pthread_key_create(&k_Doub, NULL);
  100. pthread_key_create(&k_modPr, NULL);
  101. pthread_key_create(&k_Run, NULL);
  102. pthread_key_create(&k_WPid, NULL);
  103. pthread_key_create(&k_fEnC, NULL);
  104. pthread_key_create(&k_sEnC, NULL);
  105. pthread_key_create(&k_inSP, NULL);
  106. pthread_key_create(&k_Echo, NULL);
  107. }
  108. void TH_create(void) /* create current thread variables */
  109. {
  110. void * M;
  111. struct TH_Base * A;
  112. pthread_once(&k_Init, make_keys);
  113. if ((M = pthread_getspecific(k_Base)) == NULL) {
  114. if ((M=malloc(sizeof(struct TH_Base)))==NULL)
  115. stopErr("TH_create","malloc");
  116. pthread_setspecific(k_Base, M);
  117. /* initialisation */
  118. A = (struct TH_Base *)M;
  119. A->stk_N = VIDE;
  120. A->Fct_Inst = VIDE;
  121. A->netKEY = 0;
  122. A->NbLig = 10;
  123. A->NbTab = 6;
  124. A->Vars = 1;
  125. A->FcType = 0;
  126. A->EchoOff = 0;
  127. A->Double = 0;
  128. A->ModePr = 0;
  129. A->Run = 1;
  130. A->WaitPid = 0;
  131. A->fctEnC = 0;
  132. A->strEnC = 0;
  133. A->inSonP = 0;
  134. A->i_stkL = 0;
  135. A->i_stkC = 0;
  136. A->Fd_In = 0;
  137. A->i_Ts = 0;
  138. pthread_setspecific(k_StkN, (void*)&(A->stk_N));
  139. pthread_setspecific(k_FIns, (void*)&(A->Fct_Inst));
  140. pthread_setspecific(k_NetK, (void*)&(A->netKEY));
  141. pthread_setspecific(k_NLig, (void*)&(A->NbLig));
  142. pthread_setspecific(k_NTab, (void*)&(A->NbTab));
  143. pthread_setspecific(k_Vars, (void*)&(A->Vars));
  144. pthread_setspecific(k_FTyp, (void*)&(A->FcType));
  145. pthread_setspecific(k_Echo, (void*)&(A->EchoOff));
  146. pthread_setspecific(k_Doub, (void*)&(A->Double));
  147. pthread_setspecific(k_modPr, (void*)&(A->ModePr));
  148. pthread_setspecific(k_StkL, (void*)(A->stk_L));
  149. pthread_setspecific(k_iStL, (void*)&(A->i_stkL));
  150. pthread_setspecific(k_StkC, (void*)(A->stk_C));
  151. pthread_setspecific(k_iStC, (void*)&(A->i_stkC));
  152. pthread_setspecific(k_FdIn, (void*)&(A->Fd_In));
  153. pthread_setspecific(k_iTs, (void*)&(A->i_Ts));
  154. pthread_setspecific(k_bufC, (void*)(A->BufC));
  155. pthread_setspecific(k_bufP, (void*)(A->BufP));
  156. pthread_setspecific(k_bufP2, (void*)(A->BufP2));
  157. pthread_setspecific(k_trSu, (void*)(A->trSuite));
  158. pthread_setspecific(k_Run, (void*)&(A->Run));
  159. pthread_setspecific(k_WPid, (void*)&(A->WaitPid));
  160. pthread_setspecific(k_fEnC, (void*)&(A->fctEnC));
  161. pthread_setspecific(k_sEnC, (void*)&(A->strEnC));
  162. pthread_setspecific(k_inSP, (void*)&(A->inSonP));
  163. pthread_setspecific(k_EnvI, (void*)&(A->Env_Int));
  164. }
  165. }
  166. void TH_init(void)
  167. {
  168. TH_create();
  169. }
  170. #else /* NOT _MULTI_THREADING_ */
  171. /* meta-stacks */
  172. void * G_TStackN[LSTACKS];
  173. char ** G_TStackC[LSTACKS];
  174. int G_TiStackC[LSTACKS];
  175. bool * G_TStackL[LSTACKS];
  176. int G_TiStackL[LSTACKS];
  177. int G_i_TStackN=0;
  178. int G_i_TStackC=0;
  179. int G_i_TStackL=0;
  180. void * G_StackN = VIDE;
  181. int G_Double=0; /* 0 si LONG, 1 si DOUBLE */
  182. int G_EchoOff=0; /* 0 si echo on, 1 si echo off */
  183. int G_NBTAB=6; /* nb d'elements de tableau affiches */
  184. int G_NBLIG=10; /* nb de lignes du stack affichees */
  185. short G_VARS=1, /* 0 VAR_OFF , 1 VAR_DOWN (default), 2 VAR_UP */
  186. G_ModePr=0, /* 0 DEC(default), 1 HEX, 2 OCT, 3 BIN */
  187. G_FCT_TYP=0; /* 0 None (default) , 1 Lib Fct , 2 User Fct */
  188. void * G_F_INS=VIDE; /* fct lib ou usr a installer */
  189. uint32_t G_NetKey=0;
  190. bool G_stackL0[LSTACKL];
  191. bool * G_stackL = G_stackL0;
  192. int G_i_stackL=0;
  193. char * G_stackC0[LSTACKC];
  194. char ** G_stackC = G_stackC0;;
  195. int G_i_stackC=0;
  196. char G_bufC[MAXSTRING];
  197. short G_Run=1;
  198. short G_WAITPID=0;
  199. short G_strEnCours=0;
  200. short G_fctEnCours=0;
  201. short G_inSonProc=0;
  202. int G_FD_IN = 0; /* lecture sur stdin par defaut */
  203. int G_iTS=0;
  204. char G_bufP[LBUF];
  205. char G_bufP2[LBUF];
  206. PFC G_traiteSuite [NBTRSUITE];
  207. jmp_buf G_ENV_INT;
  208. void TH_init(void) /* do nothing */
  209. {
  210. return;
  211. }
  212. #endif /* _MULTI_THREADING_ */