Exemplo n.º 1
0
void
_start_worker(guint thread_num)
{
#define LEELA_EXTRA_THREADNUM 2
    GThread *all_threads[thread_num+LEELA_EXTRA_THREADNUM];

    struct monitor *m = g_malloc0(sizeof(*m));
    g_mutex_init(&m->mutex);
    g_cond_init(&m->cond);
    m->count = thread_num;
    m->sleep = 0;

    m->m = g_malloc0(thread_num * sizeof(struct leela_monitor *));
    int i = 0;
    for(i=0;i<thread_num;i++)
    {
        m->m[i] = leela_monitor_new();
    }

    all_threads[0] = g_thread_new("monitor",_monitor,m);
    all_threads[1] = g_thread_new("timer",_timer,m);

    static int weight[] = {
        -1, -1, -1, -1, 0, 0, 0, 0,
        1, 1, 1, 1, 1, 1, 1, 1,
        2, 2, 2, 2, 2, 2, 2, 2,
        3, 3, 3, 3, 3, 3, 3, 3, };
    struct worker_parm wp[thread_num];

    for(i=0;i<thread_num;i++)
    {
        wp[i].id = i;
        wp[i].m = m;
        if (i < sizeof(weight)/sizeof(weight[0])) {
            wp[i].weight= weight[i];
        } else {
            wp[i].weight = 0;
        }

        all_threads[i+LEELA_EXTRA_THREADNUM] = g_thread_new("worker",_worker,&wp[i]);
    }

    for(i=0;i<thread_num+LEELA_EXTRA_THREADNUM;i++)
    {
        g_thread_join(all_threads[i]);
    }

    free_monitor(m);
}
Exemplo n.º 2
0
static void
_start(int thread) {
	pthread_t pid[thread+2];

	struct monitor *m = skynet_malloc(sizeof(*m));
	M = m;
	memset(m, 0, sizeof(*m));
	m->count = thread;
	m->sleep = 0;

	m->m = skynet_malloc(thread * sizeof(struct skynet_monitor *));
	int i;
	for (i=0;i<thread;i++) {
		m->m[i] = skynet_monitor_new();
	}
	if (pthread_mutex_init(&m->mutex, NULL)) {
		fprintf(stderr, "Init mutex error");
		exit(1);
	}
	if (pthread_cond_init(&m->cond, NULL)) {
		fprintf(stderr, "Init cond error");
		exit(1);
	}

	create_thread(&pid[0], _monitor, m);
	create_thread(&pid[1], _timer, m);
	//create_thread(&pid[2], _socket, m);
	skynet_socket_start_thread();

	struct worker_parm wp[thread];
	for (i=0;i<thread;i++) {
		wp[i].m = m;
		wp[i].id = i;
		create_thread(&pid[i+2], _worker, &wp[i]);
	}

	for (i=0;i<thread+2;i++) {
		pthread_join(pid[i], NULL); 
	}

	free_monitor(m);
}