Example #1
0
static int   thread_pool_create(struct thread_pool **pool, int nthreads, void (*process)(int s), int s, int ssize) {
    int i, r;
    int rc = -1;

    struct thread_pool *p = NULL;
    pthread_attr_t *attr  = NULL;;
    if ((p = calloc(1, sizeof(struct thread_pool))) == NULL)
        goto nomem;

    if ((r = pthread_mutex_init(&p->mutex, NULL)) != 0)
        goto failed;

    if (nthreads <= 0 || nthreads > MAX_THREADS)
        nthreads = MAX_THREADS;

    if (STACKSIZE(ssize)) {
        if ((attr = calloc(1, sizeof(pthread_attr_t))) == NULL)
            goto failed;

        if ((r = pthread_attr_init(attr)) != 0)
            goto failed;

        if ((r = pthread_attr_setstacksize(attr, ssize * 1000000)) != 0)
            goto failed;
    }

    p->s       = s;
    p->nthreads= nthreads;
    p->ssize   = ssize;
    p->process = process;

    for (i = 0; i < nthreads; i++) {
        /*if ssize == -1 attr == NULL*/
        r = pthread_create(&p->tids[i], attr, thread_worker, p);
    }

    *pool = p;
    rc = 0;

failed:
    if (attr) {
        pthread_attr_destroy(attr);
        free(attr);
    }
    if (rc == 0)
        return 0;

    pthread_mutex_destroy(&p->mutex);
    free(p);

nomem:
    fprintf(stderr, "error:%s:%s\n", strerror(r), strerror(errno));
    return -1;
}
Example #2
0
int main(int argc, char **argv) {

    int s;
    int nthread = -1;
    int ssize   = -1;
    char *ip, *port;
    char c;

    ip = port = NULL;

    while ((c = getopt(argc, argv, "p:i:n:s:h")) != -1) {
        switch(c) {
            case 'p':
                port = optarg;
                break;
            case 'i':
                ip   = optarg;
                break;
            case 'n':
                nthread = atoi(optarg);
                break;
            case 's':
                ssize   = atoi(optarg);
                if (!STACKSIZE(ssize))
                    ssize = 10;
                break;
            case 'h':
                usage();
                exit(0);
            default:
                usage();
                exit(1);
        }
    }
    if (port == NULL) {
        usage();
        return 1;
    }

    s = tcp_listen(ip, port);
    if (s == -1)
        return 1;

    struct thread_pool *pool;

    int r = thread_pool_create(&pool, nthread, process_msg, s, ssize);
    printf("thread pool create = %d\n", r);
    thread_pool_destroy(pool);
    close(s);

    return 0;
}
Example #3
0
File: hud.c Project: broese/mcbuild
int huddraw_build() {
    build_info * bi = get_build_info(hud_build_plan);
    int pages = C(bi->mat)/MATS_PER_PAGE + ((C(bi->mat)%MATS_PER_PAGE) > 0);
    if (pages < 1) pages = 1;
    if (hud_build_page > pages) hud_build_page = pages;
    int poff = (hud_build_page-1)*MATS_PER_PAGE;

    char buf[256];

    // placed/total blocks
    if (hud_build_plan)
        sprintf(buf, "Plan:%d", bi->total);
    else
        sprintf(buf, "%d/%d", bi->placed, bi->total);
    fg_color = B3(COLOR_BLACK);
    draw_text(1, 1, buf);

    // available total material
    sprintf(buf, "%d", bi->available);
    int remain = bi->total-bi->placed;
    if (hud_build_plan) {
        fg_color = B0(COLOR_BLACK);
    }
    else {
        if (bi->available >= remain)
            fg_color = B3(COLOR_EMERALD_GREEN);
        else if (bi->available >= remain/2)
            fg_color = B3(COLOR_GOLD_YELLOW);
        else if (bi->available >= remain/4)
            fg_color = B3(COLOR_ORANGE);
        else
            fg_color = B3(COLOR_REDSTONE_RED);
    }
    draw_text(48, 1, buf);

    // build limit
    if (!hud_build_plan && bi->limit > 0) {
        sprintf(buf, "%3d", bi->limit);
        fg_color = B3(COLOR_YELLOW);
        bg_color = B2(COLOR_BLUE);
        draw_text(72, 1, buf);
        bg_color = 0;
    }

    // current page
    sprintf(buf, "%d/%d", hud_build_page, pages);
    bg_color = B0(COLOR_NETHER_RED);
    fg_color = B3(COLOR_YELLOW);
    draw_text(115, 1, buf);
    bg_color = 0;

    int i;
    for(i=0; i<MATS_PER_PAGE; i++) {
        int mi = i+poff;
        if (mi>=C(bi->mat)) break;
        build_info_material * m = P(bi->mat)+mi;

        char name[256];
        get_bid_name(name, m->material);
        name[22] = 0;

        int toplace = m->total-m->placed;
        int stacksize = STACKSIZE(m->material.bid);
        if (hud_build_plan) {
            int stacks = toplace/stacksize + ((toplace%stacksize)>0);
            sprintf(buf, "%4d $%-3d %s", toplace, stacks, name);
            fg_color = B0(COLOR_BLACK);
        }
        else {
            sprintf(buf, "%4d/%4d %s", toplace, m->available, name);
            if (m->available >= toplace)
                fg_color = B3(COLOR_EMERALD_GREEN);
            else if (m->available >= toplace/2)
                fg_color = B3(COLOR_GOLD_YELLOW);
            else if (m->available >= toplace/4)
                fg_color = B3(COLOR_ORANGE);
            else
                fg_color = B3(COLOR_REDSTONE_RED);
        }
        draw_text(0, 6*i+8, buf);
    }

    return 1;
}