Advertisement
STEPAN_KODER

Untitled

Sep 27th, 2023
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. I see the issue. It seems that `current_thread` is not properly initialized, which is leading to a segmentation fault in `mythread_exit`.
  2.  
  3. Let's fix the code. I'll provide a modified version with the necessary corrections.
  4.  
  5. ```c
  6. #define _GNU_SOURCE
  7. #include "mythread.h"
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <sched.h>
  11. #include <setjmp.h>
  12. #include <unistd.h>
  13. #include <signal.h>
  14. #include <sys/wait.h>
  15. #include <sys/mman.h>
  16.  
  17. #define MAX_THREADS 10 // Define the maximum number of threads
  18. #define STACK_SIZE (1 << 20) // 1MB stack size, adjust as needed
  19.  
  20. int NUM_ALIVE_THREADS = 1;
  21. int THREAD_COMPLETED = 0;
  22. int current_thread = 0;
  23.  
  24. mythread_t* threads[MAX_THREADS]; // Array to hold thread pointers
  25.  
  26. void* routine_wrapper(void *arg){
  27. mythread_t* t = (mythread_t *)arg;
  28. setjmp(t->env); // Save the thread's context
  29.  
  30. t->res = t->start_routine(t->arg);
  31. t->completed = 1;
  32. mythread_exit();
  33. if (__sync_fetch_and_add(&THREAD_COMPLETED, 1) == NUM_ALIVE_THREADS - 1) {
  34. // All threads completed, resume the main thread
  35. __sync_synchronize();
  36. longjmp(t->env, 1); // Jump back to the main thread
  37. }
  38. else {
  39. pause(); // Suspend the current thread until all other threads complete
  40. }
  41. }
  42.  
  43. void schedule() {
  44. int start_thread = current_thread;
  45. do {
  46. current_thread = (current_thread + 1) % MAX_THREADS;
  47. if (threads[current_thread] != NULL && threads[current_thread]->joined && threads[current_thread]->active && !threads[current_thread]->completed) {
  48. // Switch to this thread
  49. longjmp(threads[current_thread]->env, 1);
  50. }
  51. } while (current_thread != start_thread);
  52. }
  53.  
  54. void mythread_yield() {
  55. threads[current_thread]->active = 0; // Mark current thread as inactive
  56. schedule(); // Find the next thread to execute
  57. threads[current_thread]->active = 1; // Mark next thread as active
  58. }
  59.  
  60. int mythread_create(mythread_t *thread, void *(*start_routine)(void *), void *arg) {
  61. thread->arg = arg;
  62. thread->stack = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  63. if (thread->stack == MAP_FAILED) {
  64. perror("mmap");
  65. return -1;
  66. }
  67. thread->start_routine = start_routine;
  68.  
  69. if (clone((int (*)(void *)) routine_wrapper, (char*)thread->stack + STACK_SIZE, CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_THREAD, thread) == -1) {
  70. perror("clone");
  71. return -1;
  72. }
  73.  
  74. thread->joined = 1;
  75. thread->completed = 0;
  76. thread->active = 1; // Mark the thread as active
  77. threads[NUM_ALIVE_THREADS - 1] = thread; // Add the thread to the array
  78. return 0;
  79. }
  80.  
  81. void mythread_detach(mythread_t *t) {
  82. t->joined = 0;
  83. }
  84.  
  85. int mythread_join(mythread_t *thread, void **ret_val) {
  86. if (thread->joined == 0) {
  87. return -1; // Thread is not joinable
  88. }
  89.  
  90. while (thread->completed == 0) {
  91. sched_yield(); // Suspend the current thread until the thread completes
  92. }
  93.  
  94. if (ret_val != NULL) {
  95. *ret_val = thread->res;
  96. }
  97.  
  98. munmap(thread->stack, STACK_SIZE); // Free the stack memory
  99.  
  100. return 0;
  101. }
  102.  
  103. void mythread_exit() {
  104. threads[current_thread]->active = 0; // Mark current thread as inactive
  105. if (__sync_fetch_and_add(&NUM_ALIVE_THREADS, -1) != 1) {
  106. // There are other threads, switch to another thread
  107. mythread_yield();
  108. }
  109. else {
  110. exit(0);
  111. }
  112. }
  113. ```
  114.  
  115. In this code, I have ensured that `current_thread` starts at 0, and I've corrected the placement of the thread in the `threads` array. Additionally, I've adjusted the initialization of `NUM_ALIVE_THREADS`. Please test this code and let me know if you encounter any further issues.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement