Advertisement
STEPAN_KODER

Untitled

Sep 27th, 2023
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. Here's the code with the requested changes:
  2.  
  3. ```c
  4. #define _GNU_SOURCE
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <sched.h>
  8. #include <setjmp.h>
  9. #include <unistd.h>
  10. #include <signal.h>
  11. #include <sys/wait.h>
  12. #include <sys/mman.h>
  13.  
  14. #define MAX_THREADS 10 // Define the maximum number of threads
  15. #define STACK_SIZE (1 << 20) // 1MB stack size, adjust as needed
  16.  
  17. int NUM_ALIVE_THREADS = 1;
  18. int THREAD_COMPLETED = 0;
  19. int current_thread = 0;
  20.  
  21. mythread_t* threads[MAX_THREADS]; // Array to hold thread pointers
  22.  
  23. void* routine_wrapper(void *arg) {
  24. mythread_t* t = (mythread_t *)arg;
  25. if (setjmp(t->env) == 0) { // Only enter here on initial setup, not on longjmp
  26. t->res = t->start_routine(t->arg);
  27. t->completed = 1;
  28. schedule();
  29. } else {
  30. if (__sync_fetch_and_add(&THREAD_COMPLETED, 1) == NUM_ALIVE_THREADS - 1) {
  31. // All threads completed, resume the main thread
  32. __sync_synchronize();
  33. schedule();
  34. } else {
  35. pause(); // Suspend the current thread until all other threads complete
  36. }
  37. }
  38. }
  39.  
  40. void schedule() {
  41. int start_thread = current_thread;
  42. do {
  43. current_thread = (current_thread + 1) % NUM_ALIVE_THREADS;
  44. if (threads[current_thread] != NULL && threads[current_thread]->joined && threads[current_thread]->active && !threads[current_thread]->completed) {
  45. longjmp(threads[current_thread]->env, 1);
  46. }
  47. } while (current_thread != start_thread);
  48. }
  49.  
  50. void mythread_yield() {
  51. threads[current_thread]->active = 0; // Mark current thread as inactive
  52. schedule(); // Find the next thread to execute
  53. threads[current_thread]->active = 1; // Mark next thread as active
  54. }
  55.  
  56. int mythread_create(mythread_t *thread, void *(*start_routine)(void *), void *arg) {
  57. thread->arg = arg;
  58. thread->stack = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  59. if (thread->stack == MAP_FAILED) {
  60. perror("mmap");
  61. return -1;
  62. }
  63. thread->start_routine = start_routine;
  64.  
  65. if (clone((int (*)(void *)) routine_wrapper, (char*)thread->stack + STACK_SIZE, CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_THREAD, thread) == -1) {
  66. perror("clone");
  67. return -1;
  68. }
  69.  
  70. thread->joined = 1;
  71. thread->completed = 0;
  72. thread->active = 1; // Mark the thread as active
  73. threads[NUM_ALIVE_THREADS - 1] = thread; // Add the thread to the array
  74. __sync_fetch_and_add(&NUM_ALIVE_THREADS, 1);
  75. return 0;
  76. }
  77.  
  78. void mythread_detach(mythread_t *t) {
  79. t->joined = 0;
  80. }
  81.  
  82. int mythread_join(mythread_t *thread, void **ret_val) {
  83. if (thread->joined == 0) {
  84. return -1; // Thread is not joinable
  85. }
  86.  
  87. while (thread->completed == 0) {
  88. sched_yield(); // Suspend the current thread until the thread completes
  89. }
  90.  
  91. if (ret_val != NULL) {
  92. *ret_val = thread->res;
  93. }
  94.  
  95. munmap(thread->stack, STACK_SIZE); // Free the stack memory
  96.  
  97. return 0;
  98. }
  99.  
  100. void mythread_exit() {
  101. threads[current_thread]->active = 0; // Mark current thread as inactive
  102. if (__sync_fetch_and_add(&NUM_ALIVE_THREADS, -1) != 1) {
  103. // There are other threads, switch to another thread
  104. mythread_yield();
  105. } else {
  106. exit(0);
  107. }
  108. }
  109.  
  110. void *work(void *arg) {
  111. for(int i = 0; i < 100; i++){
  112. printf("hello world %d\n", i);
  113. }
  114. return NULL;
  115. }
  116.  
  117. void *work2(void *arg) {
  118. for(int i = 0; i < 10; i++){
  119. printf("ADS %d\n", i);
  120. }
  121. return (void *) 10;
  122. }
  123.  
  124. int main(int argc, char *argv[]) {
  125. mythread_t t, t2;
  126. mythread_create(&t, work, NULL);
  127. mythread_create(&t2, work2, NULL);
  128. mythread_detach(&t);
  129. int *res = malloc(sizeof(int));
  130. mythread_join(&t2, (void **) res);
  131. mythread_exit();
  132. return 0;
  133. }
  134. ```
  135.  
  136. In this code, I've made the changes according to your request. The threads should switch automatically after a thread completes its execution without manually calling `mythread_yield()`.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement