threadpool::threadpool(int numworkers_) { numworkers = numworkers_; freethreads = numworkers; if(pthread_cond_init(&TOMAIN,NULL)) ERR_QUIT(1,"pthread_cond_init #1 failed"); if(pthread_cond_init(&TOWORKER,NULL)) ERR_QUIT(1,"pthread_cond_init #2 failed"); // lock while I create the threads M.lock(); for(unsigned int i=0;i<numworkers;i++){ class worker *w = new worker(this,i); push_back(w); pthread_create(&w->thread,NULL,worker::start_worker,(void *)w); } M.unlock(); }
/** * work is delivered in sbufs. * This blocks the caller if there are no free workers. */ void threadpool::schedule_work(file_data_hasher_t *fdht) { M.lock(); while(freethreads==0){ // wait until a thread is free (doesn't matter which) if(pthread_cond_wait(&TOMAIN,&M.mutex)){ ERR_QUIT(1,"threadpool::schedule_work pthread_cond_wait failed"); } } work_queue.push(fdht); freethreads--; pthread_cond_signal(&TOWORKER); M.unlock(); }
ssize_t Readline(int fd, void *ptr, size_t maxlen) { ssize_t n; if ( (n = readline (fd, ptr, maxlen)) < 0) ERR_QUIT ("readline error"); return (n); }
int main(int argc, char *argv[]) { byte keypress; client_init(); if( argc > 1 ) /* Command line arguments */ handle_cl_args(argc, argv); srand( time(NULL) ); calc_lookup_tables(DEGREES); win_init(); entity_init(); if( (ent_img_loaded = (char *)malloc(num_entity_types)) == NULL ) { perror("Malloc"); ERR_QUIT("Error allocating ent_img_loaded array", 1); } else { memset(ent_img_loaded, 0, num_entity_types); } /* Load client preferences from file */ read_config_file( &client ); /* Set view dimensions, which are calculated by tile width & heights */ client.view_w = client.x_tiles * TILE_W; client.view_h = client.y_tiles * TILE_H; /* What the client WISHES their dimensions were (this may not be what it actually is, ie in demo's etc */ client.desired_w = client.view_w; client.desired_h = client.view_h; weapon_type_init(); particle_init(); menu_init(); cl_network_init(); client.state = MENU; if( client.demo == DEMO_PLAY ) { cl_network_connect(NULL, 42); client.state = GAME_LOAD; } for( ; ; ) { switch( client.state ) { case MENU: win_ungrab_pointer(); menu_driver(); break; case GAME_LOAD: win_set_properties(VERSION, client.view_w, client.view_h+STATUS_H); wait_till_expose(5); /* Finished loading, we are ready to play */ cl_change_map(client.map, client.gamemode); break; case GAME_RESUME: /* Resume game from menu */ win_set_properties(VERSION, client.view_w, client.view_h+STATUS_H); wait_till_expose(5); input_clear(); draw_status_bar(); cl_netmsg_send_ready(); /* Tell server new details */ cl_net_finish(); /* Send the NETMSG_READY to the server */ client.state = GAME_JOIN; break; case GAME_JOIN: win_set_cursor( client.mousemode!=MOUSEMODE_ROTATE ); win_set_cursor( 1 ); if( client.mousemode ) { win_grab_pointer(); } text_buf_clear(); particle_clear(); case GAME_PLAY: cl_net_update(); if( client.state == GAME_PLAY ) { /* Maybe changed in net_update */ draw_crosshair( my_entity ); if( client.map_target_active ) draw_map_target( my_entity ); text_buf_update(); if( client.netstats ) cl_net_stats(); win_update(); /* Keyboard Input */ client.dir = my_entity.dir; keypress = get_input(); cl_netmsg_send_cl_update(keypress, client.dir); } cl_net_finish(); /* Send things that need sending */ cap_fps(client.fps); break; case QUIT: game_close(); image_close(); /* Free all the images */ win_close(); write_config_file(&client); if( ent_img_loaded != NULL ) free(ent_img_loaded); exit(EXIT_SUCCESS); break; } } return EXIT_SUCCESS; }