Exemple #1
0
/* --------------- threadpool API ------------------ */
threadpool_t *threadpool_create(int init, int max, int stack_size) {
    threadpool_t *pool;
    int i;
    assert(init > 0 && max >= init && stack_size >= 0);

    /* Allocate memory and zero all them. */
    pool = (threadpool_t *)calloc(1, sizeof(*pool));
    if (!pool) {
        return NULL;
    }

    Pthread_mutex_init(&pool->mutex, NULL);
    Pthread_cond_init(&pool->cond, NULL);
    Pthread_cond_init(&pool->exit_cond, NULL);
    Pthread_cond_init(&pool->task_over_cond, NULL);

    heap_init(&pool->task_queue);
    heap_set_less(&pool->task_queue, priority_less);
    pool->thread_stack_size = (stack_size == 0) ? THREAD_STACK_SIZE :
        stack_size;

    for (i = 0; i < init; ++i) {
        threadpool_thread_create(pool);
    }

    pool->threads_idle = init;
    pool->threads_num = init;
    pool->threads_max = max;
    return pool;
}
Exemple #2
0
DiskInfo::DiskInfo(int _diskid) 
{
	diskid = _diskid;
	waitingThread = 0;
	readingThread = 0;
	Pthread_mutex_init(&mutex, NULL);
	Pthread_cond_init(&cond, NULL);
}
Exemple #3
0
int crew_create(crew_p crew,int crew_size){
    int worker_index;
    pthread_t tid;
    if(crew_size>CREWSIZE)
        return EINVAL;
    Pthread_mutex_init(&crew->mutex,NULL);
    Pthread_cond_init(&crew->done,NULL);
    Pthread_cond_init(&crew->go,NULL);
    crew->crew_size = crew_size;
    crew->work_cnt =0;
    crew->first_work=crew->last_work= NULL;
    for(worker_index=0;worker_index<crew_size;worker_index++){
        crew->workers[worker_index].index = worker_index;
        crew->workers[worker_index].crew =crew;
        Pthread_create(&tid,NULL,work_loop,&crew->workers[worker_index]);
        crew->workers[worker_index].tid = tid;
    }
    return 0;
}
Exemple #4
0
void shm_init_cond()
{
	pthread_cond_t *cptr;
	pthread_condattr_t cattr;

	cptr = &x264_ring->cond;
	Pthread_condattr_init(&cattr);
#ifdef _POSIX_THREAD_PROCESS_SHARED
	Pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED);
#else
	CAP_DBG_EXIT("_POSIX_THREAD_PROCESS_SHARED not define");
#endif
	Pthread_cond_init(cptr, &cattr);
}
Exemple #5
0
	MyThread(THREAD_START_FUNC t) {
		Pthread_cond_init(&c, NULL);
		Pthread_create(&me, NULL, t, (void *) this);
	}
Exemple #6
0
int
main(int argc, char **argv)
{
	int n, k, e;
	unsigned prod_sum, cons_sum;
	pthread_attr_t attr;
	struct packet *p;
	int packets_per_producer;

 	if (argc > 1 && !strcmp("-", argv[1]))
 		Error("usage: %s num_producers num_consumers "
			"num_items buflen busy-loops", argv[0]);

	if (argc > 1) {
		num_producers = atoi(argv[1]);
		if (num_producers < 0)
			num_producers = 1;
		if (num_producers > MAXPRODUCERS)
			num_producers = MAXPRODUCERS;
	}
	if (argc > 2) {
		num_consumers = atoi(argv[2]);
		if (num_consumers < 0)
			num_consumers = 1;
		if (num_consumers > MAXCONSUMERS)
			num_consumers = MAXCONSUMERS;
	}
	if (argc > 3) {
		num_items = atoi(argv[3]);
		if (num_items < 0)
			num_items = 0;
	}
	if (argc > 4) {
		buflen = atoi(argv[4]);
		if (buflen < 1)
			buflen = 1;
	}
	if (argc > 5)
		busy_loops = atoi(argv[5]);
	printf("num_producers %d num_consumers %d num_items %d "
		"buflen %d busy-loops %d\n",
		num_producers, num_consumers, num_items, buflen, busy_loops);
	num_items /= num_producers; /* items/producer */

	Pthread_attr_init(&attr);

	Pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

	/* Producer Datenstruktur */
	packets_per_producer = (num_producers + num_consumers) * 2;
	for (n = 0; n < num_producers; ++n) {
		Pthread_mutex_init(&producers[n].queue_mutex, NULL);
		Pthread_cond_init(&producers[n].empty_cond, NULL);
		producers[n].seed = n;
		producers[n].no = n;
		producers[n].emptyPacketQueue = NULL;
		for (k = 0; k < packets_per_producer; ++k) {
			p = malloc(sizeof(struct packet) + 
				(buflen - 1) * sizeof(unsigned));
			if (!p)
				Error("malloc");

			p->owner = n;
			p->next = producers[n].emptyPacketQueue;
			producers[n].emptyPacketQueue = p;
		}
	}

	/* Consumer Datenstruktur */
	for (n = 0; n < num_consumers; ++n) {
		Pthread_mutex_init(&consumers[n].queue_mutex, NULL);
		Pthread_cond_init(&consumers[n].empty_cond, NULL);
		consumers[n].no = n;
		consumers[n].eof = 0;
		consumers[n].fullPacketQueue = NULL;
	}

	/* Starte die Producer */
	for (n = 0; n < num_producers; ++n)
     		Pthread_create(&producers[n].tid, &attr, 
						Producer, &producers[n]);

	/* Starte die Consumer */
	for (n = 0; n < num_consumers; ++n)
     		Pthread_create(&consumers[n].tid, &attr, 
						Consumer, &consumers[n]);

	/* Warte auf alle Producer */
	prod_sum = 0;
	for (n = 0; n < num_producers; ++n) {
		Pthread_join(producers[n].tid, NULL);

		prod_sum += producers[n].sum;
	}

	/* setze eof und wecke consumer auf */
	for (n = 0; n < num_consumers; ++n) {
		Pthread_mutex_lock(&consumers[n].queue_mutex);
		consumers[n].eof = 1;
		Pthread_mutex_unlock(&consumers[n].queue_mutex);
		Pthread_cond_broadcast(&consumers[n].empty_cond);
	}

	/* Warte auf alle Consumer */
	cons_sum = 0;
	for (n = 0; n < num_consumers; ++n) {
		Pthread_join(consumers[n].tid, NULL);

		cons_sum += consumers[n].sum;
	}

	printf("prot_sum %u cons_sum %u\n", prod_sum, cons_sum);
	if (cons_sum != prod_sum)
		printf("ERROR\n");

	return 0;
}
int client_test()
{
	int i, status;
	int count = 0;
	int normal = 0;
	pid_t pid[NUM_THREADS], wpid;
	unsigned long addr, len;
	unsigned long addr_to_map, addr_to_brk;
	void *ret[NUM_THREADS];
	cat_t ar = create_category(CAT_S);
        cat_t aw = create_category(CAT_I);       
        label_t L1 = {ar, aw};
        label_t L2 = {ar};
	own_t O = {};
	label_t L_self, L_test;
	own_t O_self;
	
	addr = (unsigned long)ab_calloc(256, 4, L1);
	printf("child A malloc: %lx\n", addr);
	//printf("addr = %lx\n", *(unsigned long *)addr);
	//*(unsigned long *)addr = 0x12345678;

	// test code for ab_realloc()
	addr = (unsigned long)ab_malloc(11*4096, L2);
	*(unsigned long *)addr = 0xdeadbeef;
	printf("child A malloc: %lx, %lx\n", addr, *(unsigned long *)addr);
	addr = (unsigned long)ab_realloc((void *)addr, 12*4096);
	printf("child A realloc: %lx, %lx\n", addr, *(unsigned long *)addr);

	// test code for get_ownership()
/*	get_ownership(O_self);
	print_label(O_self);
*/
	// test code for pthread mutex and condition variable
	//mutex = (pthread_mutex_t *)ab_malloc(sizeof(pthread_mutex_t), L1);
	pthread_mutexattr_t attr;
	Pthread_mutexattr_init(&attr);
	Pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
	Pthread_mutex_init(mutex, &attr);

	cond = (pthread_cond_t *)ab_malloc(sizeof(pthread_cond_t), L1);
	pthread_condattr_t condattr;
	Pthread_condattr_init(&condattr);
	Pthread_condattr_setpshared(&condattr, PTHREAD_PROCESS_SHARED);
	Pthread_cond_init(cond, &condattr);

	// test code for ab_pthread_create() and ab_pthread_join()
	pthread_t tid[NUM_THREADS];
	struct child_arg tdata[NUM_THREADS];
	
	for (i = 0; i < NUM_THREADS; ++i) {
		if (i == 0) {
			tdata[i]._addr = addr;
			tdata[i].addr_to_map = (void *)(mutex);
			tdata[i].i = i;
			ab_pthread_create(&tid[i], NULL, child_func, &tdata[i], L1, O);
		}
		if (i == 1) {
			tdata[i].i = i;
			ab_pthread_create(&tid[i], NULL, child_func, &tdata[i], L2, O);
		}
		if (i == 2) {
			tdata[i].i = i;
			ab_pthread_create(&tid[i], NULL, child_func, &tdata[i], (label_t){}, (own_t){});
		}
		if (i == 3) {
			tdata[i].i = i;
			ab_pthread_create(&tid[i], NULL, child_func, &tdata[i], (label_t){}, O);
		}
	}
	//wait some time for all the child to start
	//sleep(10);
	//printf("child A: addr = %lx\n", *(unsigned long *)addr);
	//*(unsigned long *)addr = 0xdeadbeef;
	// test code for pthread mutex
	AB_DBG("child A: mutex = %p\n", mutex);
	AB_DBG("child A: ");
	debug_mutex(mutex);
	Pthread_mutex_lock(&mmutex);
	//Pthread_cond_wait(cond, mutex);
	AB_DBG("child A: ");
	debug_mutex(mutex);
	printf("child A: doing somthing\n");
	printf("child A: global_uninit=%lx\n", global_uninit);
	printf("child A: global_init=%lx\n", global_init);
	printf("child A: &global_uninit=%p\n", &global_uninit);
	printf("child A: &global_init=%p\n", &global_init);
	global_init = 0xdead0000;
	global_uninit = 0xdead0000;
	printf("child A: updated global_uninit=%lx\n", global_uninit);
	printf("child A: updated global_init=%lx\n", global_init);
	sleep(10);	
	Pthread_mutex_unlock(&mmutex);	
	AB_DBG("child A: ");
	debug_mutex(mutex);
	// test code for ablib_brk()
//	addr = (unsigned long)ablib_sbrk(pid[0], 0);
//	printf("child 0 sbrk: %lx\n", addr);
	
	// test code for ab_malloc()
/*	addr = (unsigned long)ab_malloc(41000, L2);
	printf("child A malloc: %lx\n", addr);
	//ab_free((void *)addr);
	addr = (unsigned long)ab_malloc(11*1024*4, L2);
	printf("child A malloc: %lx\n", addr);
*/
	// test code for ab_malloc() and ab_free() exhaustively
/*	unsigned long addr_list[100];
	for (i = 0; i < 100; i = i + 1) {
		addr_list[i] = (unsigned long)ab_malloc(i*1000, L2);
		printf("child A malloc: %lx, size = %d\n", addr_list[i], i*1000);
	}
	for (i = 0; i < 100; i = i + 1) {
		ab_free((void *)addr_list[i]);
		printf("child A free: %lx\n", addr_list[i]);
	}
	for (i = 0; i < 100; i = i + 1) {
		addr_list[i] = (unsigned long)ab_malloc(i*1000, L2);
		printf("child A malloc: %lx, size = %d\n", addr_list[i], i*1000);
	}
*/
	// test code for get_mem_label()
/*	addr = (unsigned long)ab_malloc(4, L2);
	printf("child A malloc: %lx\n", addr);
	print_label(L2);
	get_mem_label((void *)addr, L_test);
	print_label(L_test);
*/

	// test code for get_label() & get_mem_label();
/*	get_label(L_self);
	print_label(L_self);
	addr = (unsigned long)ab_malloc(4, L_self);
	printf("child A malloc: %lx\n", addr);

	get_mem_label((void *)addr, L_test);
	print_label(L_test);
*/	

	
/*	while(1) {
		wpid = waitpid(-1, &status, 0);
		if(wpid <= 0)
			break;
		count++;
		if(WIFEXITED(status)) {
			printf("child %lu exited normally. count = %d\n", 
			       (unsigned long)wpid, count);
			normal++;
		}
		if(WIFSIGNALED(status)) {
			printf("child %lu terminated by a signal. count = %d\n",
			       (unsigned long)wpid, count);
		}
		else
			printf("wait pid returns with status %d\n", status);
	}
*/	
	// test code for ab_pthread_create() and ab_pthread_join()
	for (i = 0; i < NUM_THREADS; ++i) {
		AB_DBG("main: tid[%d] = %d\n", i, (int)tid[i]);
		if (ab_pthread_join(tid[i], NULL) == 0) {
			normal++;
		}
	}
	
	if (normal == NUM_THREADS) {
		printf("mapping test successful.\n");
		return 0;
	}


test_failed:
	printf("test failed!!\n");
	return -1;			

}