예제 #1
0
int main(int argc, char **argv)
{
	int checks;
	
	printf("Testing sthread_mutex_*, impl: %s\n",
		   (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");
	
	sthread_init();
	
	mutex = sthread_mutex_init();
	sthread_mutex_lock(mutex);
	
	if (sthread_create(thread_start, (void*)1, 1) == NULL) {
		printf("sthread_create failed\n");
		exit(1);
	}
	
	/* Wait until the other thread has at least started,
	 * to give it a chance at getting through the mutex incorrectly. */
	while (ran_thread == 0) {
		sthread_yield();
	}

   sthread_dump();
	
	/* The other thread has run, but shouldn't have been
	 * able to affect counter (note that this is not a great test
	 * for preemptive scheduler, since the other thread's sequence
	 * is not atomic). */   
	assert(counter == 0);
	
	/* This should let the other thread run at some point. */
	sthread_mutex_unlock(mutex);
	
	/* Allow up to 100 checks in case the scheduler doesn't
	 * decide to run the other thread for a really long time. */
	checks = 100;
	while (checks > 0) {
		sthread_mutex_lock(mutex);
		if (counter != 0) {
			/* The other thread ran, got the lock,
			 * and incrmented the counter: test passes. */
			checks = -1;
		} else {
			checks--;
		}
		sthread_mutex_unlock(mutex);
		
		/* Nudge the scheduler to run the other thread: */
		sthread_yield();
	}
	
	if (checks == -1) {
		printf("sthread_mutex passed\n");
	} else {
		printf("*** sthread_mutex failed\n");
	}
	sthread_mutex_free(mutex);
	return 0;
}
예제 #2
0
파일: main.c 프로젝트: shuvojitNITW/sthread
int main(void)
{
        printf("executing main function yeasss!!!!!!!\n");
        sthread_t p,q,r;
        sthread_mutex_init(&m);
        sthread_create(&p,f1,NULL);
        sthread_create(&q,f2,NULL);
        sthread_create(&r,f3,NULL);
        sthread_join(-1);
        printf("\nmain finished ans: %d \n",sum);
        return 0;
}
예제 #3
0
void Test1(){
    sthread_mutex_init(mut_ptr);
    sthread_t thr1,thr2,thr3;

    if (sthread_init() == -1){}

    printf("Creating Thread 1\n");

    if (sthread_create(&thr1, threadmain, (void *)1) == -1){}

    sleep(2);
    printf("Creating Thread 2\n");
    if (sthread_create(&thr2, threadmain, (void *)2) == -1){}

    sleep(2);
    printf("Creating Thread 3\n");
    if (sthread_create(&thr3, threadmain, (void *)3) == -1){}

}
예제 #4
0
int main(int argc, char **argv) {
  sthread_mutex_t mutex = NULL;
  int i = 0;

  printf("Testing sthread_mutex_*, impl: %s\n",
     (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");
 
  sthread_init();

  mutex = sthread_mutex_init();
  while (i < 10) {
    sthread_mutex_lock(mutex);
    printf("i=%i\n",i);
    i++;
    sthread_yield();
    sthread_mutex_unlock(mutex);
  }
  printf("\nSUCESSO!\n");
  return 1;
}
예제 #5
0
파일: test-cond.c 프로젝트: kunw/P2
int main(int argc, char **argv) {
	int sent, checks, i;
	sthread_t child[MAXTHREADS];


	printf("Testing sthread_cond_*, impl: %s\n",
			(sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");
	assert(num_threads <= MAXTHREADS);

	sthread_init();

	mutex = sthread_mutex_init();
	avail_cond = sthread_cond_init();

	sthread_mutex_lock(mutex);

	for (i = 0; i < num_threads; i++) {
		child[i] = sthread_create(thread_start, NULL, 1);
		if (child[i] == NULL) {
			printf("sthread_create %d failed\n", i);
			exit(1);
		}
	}

	assert(transfered == 0);

	/* This should let the other thread run at some point. */
	sthread_mutex_unlock(mutex);

	/* Send a bunch of things for the other threads to take */
	sent = 0;
	while (sent < max_transfer) {
		sthread_mutex_lock(mutex);
		waiting++;
		sent++;
		sthread_cond_signal(avail_cond);
		sthread_mutex_unlock(mutex);
		sthread_yield();
	}

	printf("Sent %d\n", sent);

	/* Now give the other threads 100 tries to get
	 * them all across. We assume that's enough
	 * for the sake of not running this test forever. */
	checks = 10000;  //arbitrary??
	while (checks > 0) {
		sthread_mutex_lock(mutex);
		if (transfered != max_transfer)
			checks--;
		else {
			/* broadcast to let the consumers know we've
			 * finished, so they can exit
			 * (othrewise, they may still be holding the lock
			 * when we try to free it below) */
			sthread_cond_broadcast(avail_cond);
			checks = -1;
		}
		sthread_mutex_unlock(mutex);
		sthread_yield();
	}

	if (checks == -1) {
		/* Wait for child threads to finish, otherwise we could try to
		 * free the mutex before they've unlocked it! */
		printf("joining on children\n");
		for (i = 0; i < num_threads; i++) {
			sthread_join(child[i]);
			printf("joined with child %d\n", i);
		}
		printf("sthread_cond passed\n");
	} else {
		printf("*** sthread_cond failed\n");
		/* If we failed, don't bother joining on threads. */
	}

	sthread_mutex_free(mutex);
	sthread_cond_free(avail_cond);
	return 0;
}