int mythread_join(mythread_t targetthread, void **status) { int isinqueue; int jointargetcheck=1; queue_info->head->thread->join_target=targetthread.myaddress; isinqueue=findthread(targetthread.myaddress); if(isinqueue==0)// target thread is not present in the execution queue return 0; else // if the target thread is in the execution queue the yield the current thread { queue_info->head->thread->join_target=targetthread.myaddress; // add the target thread to the target thread field in the current thread and yield it while(jointargetcheck==1) { if(queue_info->head->thread->tid==0) { mythread_yield(); //futex_down(&(queue_info->head->thread->thread_futex));//locking its own futex rather than that of the previous ones //futex_down(&yield_futex); jointargetcheck=findthread(queue_info->head->thread->join_target); } else { mythread_yield();// check the condition if this is has target yet to be done and keep it in loop jointargetcheck=findthread(queue_info->head->thread->join_target); //break; } } return 0; } }
void MyExampleThread2(int* arg) { std::cout << "D" << std::endl; mythread_yield(); std::cout << "H" << std::endl; mythread_exit(); }int main() {
void MyExampleThread1(int* arg) { std::cout << "B" << std::endl; mythread_yield(); std::cout << "E" << std::endl; mythread_exit(); }
void *thread_function(void *arg) { int rc, i; mythread_t thread,childthread ; struct threadValues *value; value = (struct threadValues *)arg; thread=mythread_self(); printf("This is thread %d before yielding\n",thread.tid); for(i=0; i < MAX_KEYS; i++) { printf("\nSettig values %d %d for key %d by thread %d", value->v1, value->v2, i, thread.tid); rc = mythread_setspecific(keys[i], value); if(rc != 0) { printf("\nmythread_setspecific call error for thread = %d", keys[i]); } } rc=mythread_create(&childthread,NULL,&printingfunction,NULL); rc=mythread_yield(); printf("this is thread %d after yield\n",thread.tid); //rc = mythread_yield(); getData(); return NULL; }
/* Idle thread implementation. * The thread checks whether it is the only one alive, if yes, exit() * else keep scheduling someone. */ void *mythread_idle(void *phony) { mythread_private_t *traverse_tcb; pid_t idle_tcb_tid; while (1) { DEBUG_PRINTF("I am idle\n"); traverse_tcb = __mythread_selfptr(); idle_tcb_tid = traverse_tcb->tid; traverse_tcb = traverse_tcb->next; /* See whether there is a NON-DEFUNCT process in the list. * If there is, idle doesn't need to kill the process just yet */ while (traverse_tcb->tid != idle_tcb_tid) { if (traverse_tcb->state != DEFUNCT) { break; } traverse_tcb = traverse_tcb->next; } /* Idle is the only one alive, kill the process */ if (traverse_tcb->tid == idle_tcb_tid) exit(0); /* Some thread still awaits execution, yield ourselves */ mythread_yield(); } }
int main() { int i = 0; mythread_t tid[10]; void *status; // initialize key mythread_key_create(&name_key, free); mythread_key_create(&id_key, free); for (i = 0; i < 10; i++) { int *temp = (int *) (malloc) (sizeof(int)); *temp = i; mythread_create(&tid[i], NULL, hello, (void *) temp); mythread_yield(); } printf("main thread is joining with child threads\n"); for (i = 0; i < 10; i++) { mythread_join(tid[i], &status); printf("joined on thread:%d\n", tid[i]); } mythread_key_delete(id_key); mythread_key_delete(name_key); printf("main thread is exiting\n"); fflush(NULL); mythread_exit(NULL); return 0; }
void *hello(void *t) { ///int *reps = (int*)t; int i = 0; // initialize thread specific data for each thread int* id = (int*)malloc(sizeof(int)); assert(id); *id = mythread_self(); mythread_setspecific(id_key, id); //set name char* name = (char*)malloc(sizeof(char)*MAX_NAME_LENGTH); assert(name); memset(name, '\0', MAX_NAME_LENGTH); sprintf(name, "worker-%d", mythread_self()); mythread_setspecific(name_key,name); // user specific data for (; i < mythread_self(); i++) { printf("Thread-%d:Hello thread:%d\n", mythread_self(), i); sleep(1); mythread_yield(); } printf("Thread specific data for %d thread is %d and worker name is %s\n", mythread_self(), *((int *)mythread_getspecific(id_key)), (char*)mythread_getspecific(name_key)); return NULL; }
void thread(void* p) { fputs("Entered\n", stderr); int x = (int)p; int y = 2; fprintf(stderr, "Entered thread %d with %d\n", (int)mythread_myid(), x); mythread_yield(); fprintf(stderr, "back in thread %d with %d and %d\n", (int)mythread_myid(), x, y++); mythread_yield(); fprintf(stderr, "back in thread %d with %d and %d\n", (int)mythread_myid(), x, y++); if (left) { ThreadId tid3; left--; mythread_create(&tid3, NULL, thread, (void*)left); } mythread_exit(); }
void * idlethreadfunction(void *a) { struct mythread_t * idlethread=(struct mythread_t *) malloc(sizeof(mythread_t)); int i=0; while(1) { i=mythread_yield(); } }
void checkcollect(void* p) { int nt = (int)p; spawned = 0; collected = 0; mythread_seminit(&mutex, 1); /* mutex = 1 */ int i; for (i=0; i<nt; i++) { ThreadId tid1; mythread_create(&tid1, NULL, worker1, 0); spawned++; mythread_yield(); } while ((collected < spawned)) { printf("%d spawned, %d collected\n", spawned, collected); mythread_yield(); } printf("%d spawned, %d collected\n", spawned, collected); mythread_exit(); }
// function we are going to run as a thread void *thread_test(void *arg) { long count = (long)arg; while (--count) { printf("Counting down: %ld\n", count); mythread_yield(); } printf("Thread is exiting!\n"); exit(0); return NULL; }
int main() { mythread_init(); mythread_create(thread_test, (void *)5); mythread_create(thread_test, (void *)10); while (1) { mythread_yield(); } printf("main is going away\n"); return 0; }
void mythread_exit(void *retval) { int status; node * tempnode = (node *) malloc (sizeof(node)); if(queue_info->head->thread->tid==0) // if the exit is called by main then yield the main thread until the all the other threads are done { while(total_threads>1) { mythread_yield(); } exit(0); // when all the threads are done then exit out of the while loop and end the program } else // if mythread_exit was called by a child thread then just put calling thread out of the queue without modifying the other threads { tempnode = queue_info->head; queue_info->head=queue_info->head->right; futex_up(&(queue_info->head->thread->thread_futex)); futex_up(&yield_futex); total_threads=total_threads-1; futex_down(&block_exitthread_futex); } }