Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. /* do not use UNICODE */
  2. #undef _UNICODE
  3. #undef UNICODE
  4.  
  5. #include <stdio.h>
  6. #include <windows.h>
  7. #include "utils.h"
  8.  
  9. #define NO_THREADS 6
  10.  
  11. #define ERR_A 1
  12. #define ERR_B 2
  13. #define ERR_C 3
  14.  
  15. DWORD myErrno;
  16.  
  17. BOOL function_A(VOID);
  18. BOOL function_B(VOID);
  19. BOOL function_C(VOID);
  20.  
  21. VOID myPerror(char *description)
  22. {
  23. DWORD err = 0;
  24.  
  25. /* TODO - get error id from TLS */
  26. err = (DWORD)TlsGetValue(myErrno);
  27.  
  28. switch (err) {
  29. case ERR_A:
  30. printf("Got 'Somenthing is wrong!' error: %s\n",
  31. description);
  32. break;
  33. case ERR_B:
  34. printf("Got 'This is not my fault!' error: %s\n",
  35. description);
  36. break;
  37. case ERR_C:
  38. printf("Got 'Weird error!' error: %s\n",
  39. description);
  40. break;
  41. default:
  42. printf("Unknowned error\n");
  43. }
  44. }
  45.  
  46. /* function executed by the threads */
  47. DWORD WINAPI ThreadFunc(LPVOID lpParameter)
  48. {
  49. DWORD lpvData = 1;
  50. DWORD dwReturn;
  51.  
  52. DWORD choice = (rand() + GetCurrentThreadId()) % 3;
  53.  
  54. switch (choice) {
  55. case 0:
  56. dwReturn = function_A();
  57. if (dwReturn == FALSE)
  58. myPerror("function A failed\n");
  59. break;
  60.  
  61. case 1:
  62. dwReturn = function_B();
  63. if (dwReturn == FALSE)
  64. myPerror("function B failed\n");
  65. break;
  66.  
  67. case 2:
  68. dwReturn = function_C();
  69. if (dwReturn == FALSE)
  70. myPerror("function C failed\n");
  71. break;
  72. }
  73.  
  74. return 0;
  75. }
  76.  
  77. DWORD main(VOID)
  78. {
  79. DWORD IDThread, dwReturn;
  80. HANDLE hThread[NO_THREADS];
  81. int i;
  82.  
  83. /* TODO - allocate TLS index */
  84. myErrno = TlsAlloc();
  85.  
  86. /* create threads */
  87. for (i = 0; i < NO_THREADS; i++) {
  88. hThread[i] = CreateThread(
  89. NULL, /* default security attributes */
  90. 0, /* default stack size */
  91. (LPTHREAD_START_ROUTINE) ThreadFunc,
  92. NULL, /* no thread parameter */
  93. 0, /* immediately run the thread */
  94. &IDThread); /* thread id */
  95. DIE(hThread[i] == NULL, "CreateThread");
  96. }
  97.  
  98. /* wait for threads completion */
  99. for (i = 0; i < NO_THREADS; i++) {
  100. dwReturn = WaitForSingleObject(hThread[i], INFINITE);
  101. DIE(dwReturn == WAIT_FAILED, "WaitForSingleObject");
  102. }
  103.  
  104. /* TODO - free TLS index */
  105. TlsFree(myErrno);
  106.  
  107. return 0;
  108. }
  109.  
  110.  
  111. BOOL function_A(VOID)
  112. {
  113. DWORD dwRet;
  114.  
  115. /* function encounters ERR_A error and sets myErrno */
  116. dwRet = TlsSetValue(myErrno, (LPVOID)ERR_A);
  117. DIE(dwRet == FALSE, "TlsSet failed");
  118.  
  119. return FALSE;
  120. }
  121.  
  122. BOOL function_B(VOID)
  123. {
  124. DWORD dwRet;
  125.  
  126. /* function encounters ERR_B error and sets myErrno */
  127. dwRet = TlsSetValue(myErrno, (LPVOID)ERR_C);
  128. DIE(dwRet == FALSE, "TlsSet failed");
  129.  
  130. return FALSE;
  131. }
  132.  
  133. BOOL function_C(VOID)
  134. {
  135. DWORD dwRet;
  136.  
  137. /* function encounters ERR_C error and sets myErrno */
  138. dwRet = TlsSetValue(myErrno, (LPVOID)ERR_B);
  139. DIE(dwRet == FALSE, "TlsSet failed");
  140.  
  141. return FALSE;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement