Changeset 520492a in mainline
- Timestamp:
- 2006-03-24T13:57:43Z (19 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- c4c5de5
- Parents:
- 4f2c821
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
init/init.c
r4f2c821 r520492a 256 256 static int ptest(void *arg) 257 257 { 258 printf("Pseudo thread -1\n");259 ps _preempt();260 printf("Pseudo thread -2\n");261 ps _preempt();262 printf("Pseudo thread -3\n");263 ps _preempt();264 printf("Pseudo thread -4\n");265 ps _preempt();266 printf("Pseudo finish\n");258 printf("Pseudo thread stage1.\n"); 259 psthread_schedule_next(); 260 printf("Pseudo thread stage2.\n"); 261 psthread_schedule_next(); 262 printf("Pseudo thread stage3.\n"); 263 psthread_schedule_next(); 264 printf("Pseudo thread stage4.\n"); 265 psthread_schedule_next(); 266 printf("Pseudo thread exiting.\n"); 267 267 return 0; 268 268 } … … 309 309 310 310 ptid = psthread_create(ptest, NULL); 311 printf("main thread-1\n"); 312 ps_preempt(); 313 printf("main thread-2\n"); 314 ps_preempt(); 315 printf("main thread-3\n"); 316 317 ps_join(ptid); 318 printf("Main exiting\n"); 311 printf("Main thread stage1.\n"); 312 psthread_schedule_next();; 313 printf("Main thread stage2.\n"); 314 psthread_schedule_next();; 315 printf("Main thread stage3.\n"); 316 317 psthread_join(ptid); 319 318 320 319 printf("Main thread exiting.\n"); -
libc/generic/psthread.c
r4f2c821 r520492a 37 37 static LIST_INITIALIZE(ready_list); 38 38 39 static void ps_exit(void) __attribute__ ((noinline)); 39 static void psthread_exit(void) __attribute__ ((noinline)); 40 static void psthread_main(void); 40 41 41 /** Function to preempt to other thread without adding42 * currently running thread to runqueue42 /** Function to preempt to other pseudo thread without adding 43 * currently running pseudo thread to ready_list. 43 44 */ 44 void ps _exit(void)45 void psthread_exit(void) 45 46 { 46 47 psthread_data_t *pt; … … 51 52 _exit(0); 52 53 } 53 pt = list_get_instance(ready_list.next, psthread_data_t, li st);54 list_remove(&pt->li st);54 pt = list_get_instance(ready_list.next, psthread_data_t, link); 55 list_remove(&pt->link); 55 56 context_restore(&pt->ctx); 56 57 } 57 58 58 59 /** Function that is called on entry to new uspace thread */ 59 static intpsthread_main(void)60 void psthread_main(void) 60 61 { 61 62 psthread_data_t *pt = __tls_get(); … … 64 65 pt->finished = 1; 65 66 if (pt->waiter) 66 list_append(&pt->waiter->li st, &ready_list);67 list_append(&pt->waiter->link, &ready_list); 67 68 68 ps _exit();69 psthread_exit(); 69 70 } 70 71 71 /** Do a preemption of userpace threads */ 72 int ps_preempt(void) 72 /** Schedule next userspace pseudo thread. 73 * 74 * @return 0 if there is no ready pseudo thread, 1 otherwise. 75 */ 76 int psthread_schedule_next(void) 73 77 { 74 78 psthread_data_t *pt; … … 78 82 79 83 pt = __tls_get(); 80 if (! 84 if (!context_save(&pt->ctx)) 81 85 return 1; 82 86 83 list_append(&pt->li st, &ready_list);84 pt = list_get_instance(ready_list.next, psthread_data_t, li st);85 list_remove(&pt->li st);87 list_append(&pt->link, &ready_list); 88 pt = list_get_instance(ready_list.next, psthread_data_t, link); 89 list_remove(&pt->link); 86 90 87 91 context_restore(&pt->ctx); 88 92 } 89 93 90 /** Wait for uspace thread to finish */ 91 int ps_join(pstid_t psthrid) 94 /** Wait for uspace pseudo thread to finish. 95 * 96 * @param psthrid Pseudo thread to wait for. 97 * 98 * @return Value returned by the finished thread. 99 */ 100 int psthread_join(pstid_t psthrid) 92 101 { 93 102 volatile psthread_data_t *pt, *mypt; … … 102 111 if (context_save(&((psthread_data_t *) mypt)->ctx)) { 103 112 pt->waiter = (psthread_data_t *) mypt; 104 ps _exit();113 psthread_exit(); 105 114 } 106 115 } … … 114 123 115 124 /** 116 * Create a userspace thread 125 * Create a userspace thread and append it to ready list. 117 126 * 127 * @param func Pseudo thread function. 128 * @param arg Argument to pass to func. 129 * 130 * @return 0 on failure, TLS of the new pseudo thread. 118 131 */ 119 132 pstid_t psthread_create(int (*func)(void *), void *arg) … … 136 149 context_set(&pt->ctx, FADDR(psthread_main), pt->stack, getpagesize(), pt); 137 150 138 list_append(&pt->li st, &ready_list);151 list_append(&pt->link, &ready_list); 139 152 140 153 return (pstid_t )pt; 141 154 } 142 -
libc/include/psthread.h
r4f2c821 r520492a 45 45 struct psthread_data *self; /* ia32, amd64 needs to get self address */ 46 46 47 link_t li st;47 link_t link; 48 48 context_t ctx; 49 49 void *stack; … … 62 62 63 63 pstid_t psthread_create(int (*func)(void *), void *arg); 64 int ps _preempt(void);65 int ps _join(pstid_t psthrid);64 int psthread_schedule_next(void); 65 int psthread_join(pstid_t psthrid); 66 66 67 67 #endif
Note:
See TracChangeset
for help on using the changeset viewer.