]> git.mdlowis.com Git - proto/cerise-os.git/commitdiff
fixed crashing context switch
authorMichael D. Lowis <mike@mdlowis.com>
Wed, 7 Sep 2022 01:28:30 +0000 (21:28 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Wed, 7 Sep 2022 01:28:30 +0000 (21:28 -0400)
experiments/ctxswitch.c
experiments/task_switch.s [new file with mode: 0644]

index 6a4ebd96ff5057b0ad78a5eda380e4e562e7aef1..d8dbc6c5a0059dc682d269fbbe91baf0a085848a 100644 (file)
@@ -46,32 +46,11 @@ static __thread int ThreadID;
 static Thread_T* Threads;
 static struct {
     pthread_cond_t cond;
-    pthread_cond_t mutex;
+    pthread_mutex_t mutex;
     long int value;
 } TaskCount;
 
-extern void Task_Switch(Task_T* prev, Task_T* next);
-asm (
-".global Task_Switch\n"
-"Task_Switch:\n"
-"    push %rdi\n"
-"    push %rbp\n"
-"    push %rbx\n"
-"    push %r12\n"
-"    push %r13\n"
-"    push %r14\n"
-"    push %r15\n"
-"    mov %rsp,(%rdi)\n"
-"    mov (%rsi),%rsp\n"
-"    pop %r15\n"
-"    pop %r14\n"
-"    pop %r13\n"
-"    pop %r12\n"
-"    pop %rbx\n"
-"    pop %rbp\n"
-"    pop %rdi\n"
-"    ret\n"
- );
+extern void ContextSwitch(Task_T* curr, Task_T* next);
 
 static void Enqueue(int id, Task_T* task)
 {
@@ -119,8 +98,9 @@ void Task_Yield(void)
             Threads[ThreadID].tail = NULL;
         }
 
-        printf("thread %d switching to task\n", ThreadID);
-        Task_Switch(curr, Threads[ThreadID].curr);
+        printf("thread %d switching from task %d to task %d\n",
+            ThreadID, curr->id,  Threads[ThreadID].curr->id);
+        ContextSwitch(curr, Threads[ThreadID].curr);
     }
 }
 
@@ -130,11 +110,10 @@ void Task_Exit(void)
 
     TRY( pthread_mutex_lock(&TaskCount.mutex) );
     TaskCount.value--;
-    printf("TASK COUNT %d\n", TaskCount.value);
+    printf("TASK COUNT(-) %ld\n", TaskCount.value);
     TRY( pthread_cond_signal(&TaskCount.cond) );
     TRY( pthread_mutex_unlock(&TaskCount.mutex) );
 
-
     while(true)
     {
         Task_Yield();
@@ -182,6 +161,7 @@ static void* ThreadMain(void* arg)
         /* let's wait for there to be work to do */
         TRY_ELSE( pthread_cond_wait(&Threads[ThreadID].cond, &Threads[ThreadID].mutex) )
         {
+            perror("pthread_cond_wait:");
             /* ignore error */
         }
 
@@ -204,7 +184,7 @@ void Task_Spawn(void (*task_fn)(void*), void *arg, int stacksize)
     TRY( pthread_mutex_lock(&TaskCount.mutex) );
     Task_T* task = CreateTask(task_fn, arg, stacksize);
     TaskCount.value++;
-    printf("TASK COUNT %d\n", TaskCount.value);
+    printf("TASK COUNT(+) %ld\n", TaskCount.value);
     TRY( pthread_cond_signal(&TaskCount.cond) );
     TRY( pthread_mutex_unlock(&TaskCount.mutex) );
 
@@ -266,13 +246,14 @@ int main(int argc, char** argv) {
         TRY_ELSE( pthread_cond_wait(&TaskCount.cond, &TaskCount.mutex) )
         {
             /* ignore error */
+            perror("pthread_cond_wait:");
         }
-        TRY( pthread_mutex_lock(&TaskCount.mutex) );
+//        TRY( pthread_mutex_lock(&TaskCount.mutex) );
         if(TaskCount.value == 0)
         {
             break;
         }
-        TRY( pthread_mutex_unlock(&TaskCount.mutex) );
+//        TRY( pthread_mutex_unlock(&TaskCount.mutex) );
     }
 
 //    int count = atoi(argv[1]);
diff --git a/experiments/task_switch.s b/experiments/task_switch.s
new file mode 100644 (file)
index 0000000..e133eba
--- /dev/null
@@ -0,0 +1,20 @@
+.global ContextSwitch
+ContextSwitch:
+    push %rdi
+    push %rbp
+    push %rbx
+    push %r12
+    push %r13
+    push %r14
+    push %r15
+    mov %rsp,(%rdi)
+    mov (%rsi),%rsp
+    pop %r15
+    pop %r14
+    pop %r13
+    pop %r12
+    pop %rbx
+    pop %rbp
+    pop %rdi
+    ret
+