Exemple #1
0
    void thread_routine() {
        threadid = ff_getThreadID();
        if (barrier) barrier->doBarrier(tid);
        void * ret;
        do {
            init_error=false;
            if (svc_init()<0) {
                error("ff_thread, svc_init failed, thread exit!!!\n");
                init_error=true;
                break;
            } else  {
                ret = svc(NULL);
            }
            svc_end();
            
            if (disable_cancelability()) {
                error("ff_thread, thread_routine, could not change thread cancelability");
                return;
            }

            // acquire lock. While freezing is true,
            // freeze and wait. 
            pthread_mutex_lock(&mutex);
            if (ret != EOS_NOFREEZE && !stp) {
                if ((freezing == 0) && (ret == EOS)) stp = true;
                while(freezing==1) { // NOTE: freezing can change to 2
                    frozen=true; 
                    pthread_cond_signal(&cond_frozen);
                    pthread_cond_wait(&cond,&mutex);
                }
            }
            
            //thawed=true;
            //pthread_cond_signal(&cond);
            //frozen=false; 
            if (freezing != 0) freezing = 1; // freeze again next time 
            pthread_mutex_unlock(&mutex);

            if (enable_cancelability()) {
                error("ff_thread, thread_routine, could not change thread cancelability");
                return;
            }
        } while(!stp);
        
        if (freezing) {
            pthread_mutex_lock(&mutex);
            frozen=true;
            pthread_cond_signal(&cond_frozen);
            pthread_mutex_unlock(&mutex);
        }
        isdone = true;
    }
Exemple #2
0
int main(){
	int xxx;
	peer_self = NULL;
	printf("initiating...\n");
	svc_init(tst_callback);
	printf("joining peer...\n");
	peer_self = svc_peer_join();
	printf("running...\n");
	printf("You should be able to hear yourslef in the speakers\n");
	scanf("%d", &xxx);
	svc_close();
	return 0;
}
Exemple #3
0
    /**
     * \brief The life cycle of FastFlow thread
     *
     * It defines the life cycle of the FastFlow thread.
     *
     */
    void thread_routine() {
        if (barrier) barrier->doBarrier(tid);
        void * ret;
        do {
            init_error=false;
            if (svc_init()<0) {
                error("ff_thread, svc_init failed, thread exit!!!\n");
                init_error=true;
                break;
            } else  {
                ret = svc(NULL);
                svc_releaseOCL();
            }
            svc_end();

            if (disable_cancelability()) {
                error("ff_thread, thread_routine, could not change thread cancelability");
                return;
            }

            // acquire lock. While freezing is true,
            // freeze and wait.
            pthread_mutex_lock(&mutex);
            if (ret != (void*)FF_EOS_NOFREEZE && !stp) {  // <-------------- CONTROLLARE aggiunto !stp
                while(freezing==1) { // NOTE: freezing can change to 2
                    frozen=true;
                    pthread_cond_signal(&cond_frozen);
                    pthread_cond_wait(&cond,&mutex);
                }
            }

            thawed=true;     // <----------------- CONTROLLARE !!!! era thawed = frozen;
            pthread_cond_signal(&cond);
            frozen=false;
            if (freezing != 0) freezing = 1; // freeze again next time
            pthread_mutex_unlock(&mutex);

            if (enable_cancelability()) {
                error("ff_thread, thread_routine, could not change thread cancelability");
                return;
            }
        } while(!stp);

        if (init_error && freezing) {
            pthread_mutex_lock(&mutex);
            frozen=true;
            pthread_cond_signal(&cond_frozen);
            pthread_mutex_unlock(&mutex);
        }
    }
Exemple #4
0
int main(int argc, char* argv[]) {
	dstring* input;
	int input_done;
	int code;
	ENetEvent event;

	if (argc > 1) {
		fprintf(stderr, "svcc doesn't accept any command line arguments, use stdin instead.\n");
		return 1;
	}

	/* initialize enet */
	if (enet_initialize()) {
		fprintf(stderr, "Failed to initialize ENet\n");
		return 1;
	}

	/* initialize client state */
	init_client_state();

	/* init SVC */
	svc_init(send_callback);


	/* main loop */
	input = dnew();
	client.main_done = 0;
	signal(SIGINT, sigint);
	while(!client.main_done) {
		/* enet event handler */
		if (client.state == SVCECLIENT_STATE_CONNECTED) {
			/* handle connection state */

			while (1) {
				mutex_lock(&client.network_lock);
				code = enet_host_service(client.host, &event, 0);
				mutex_unlock(&client.network_lock);
				if (code <= 0) break;

				/* handle event */
				switch(event.type) {
				case ENET_EVENT_TYPE_DISCONNECT:
					/* we got disconnected */
					mutex_lock(&client.network_lock);
					client.state = SVCECLIENT_STATE_DISCONNECTED;
					free_server_info();
					mutex_unlock(&client.network_lock);
					msg_state();
					break;

				case ENET_EVENT_TYPE_RECEIVE:
					/* some data just arrived */
					if (event.channelID == 0) {
						handle_server_msg(event.packet);
					} else if (event.channelID == 1) {
						handle_server_audio(event.packet);
					} else {
						fprintf(stderr, "Received message from server on invalid channel %i.\n", event.channelID);
					}
					mutex_lock(&client.network_lock);
					enet_packet_destroy(event.packet);
					mutex_unlock(&client.network_lock);
					break;

				case ENET_EVENT_TYPE_CONNECT:
					/* no one will connect, ignore the bastard */
				default:
					break;
				}

			}
		} else if (client.state == SVCECLIENT_STATE_CONNECTING) {
			/* handle connecting state */
			mutex_lock(&client.network_lock);
			code = enet_host_service(client.host, &event, 0);
			mutex_unlock(&client.network_lock);

			if (code > 0) {
				if (event.type == ENET_EVENT_TYPE_CONNECT) {
					client.state = SVCECLIENT_STATE_CONNECTED;
					client.server_info = dsdict_new();
					msg_state();
				} else {
					mutex_lock(&client.network_lock);
					client.state = SVCECLIENT_STATE_DISCONNECTED;
					enet_peer_disconnect(client.client, 0);
					client.client = NULL;
					enet_host_destroy(client.host);
					client.host = NULL;
					msg_state();
					mutex_unlock(&client.network_lock);
				}
			}
		} else {
			/* sleep for 8ms */
			usleep(8000);
		}

		/* play with the input queue */
		if (!client.main_done) {
			input_done = 0;
			while (!input_done) {
				/* get next input command */
				mutex_lock(&client.input_lock);
				if (client.input_queue->size) {
					dcpy(input, client.input_queue->front->string);
					dlist_erase(client.input_queue, client.input_queue->front);
				} else {
					input_done = 1;
				}
				mutex_unlock(&client.input_lock);

				/* quit input loop if queue is empty */
				if (input_done) {
					break;
				}

				/* handle commands */
				input_done = client.main_done = handle_input_command(input);
			}
		}

		/* check if input died for some reasons */
		mutex_lock(&client.input_lock);
		if (client.input_error) {
			/* if so, leave main loop */
			client.main_done = 1;
		}
		mutex_unlock(&client.input_lock);

		fflush(stdout);
		fflush(stderr);

		/* sleep for 8ms */
		usleep(8000);
	}

	fprintf(stdout, ":STATE exiting\n");
	dfree(input);

	svc_close();

	quit_client_state();

	enet_deinitialize();

	return 0;
}
Exemple #5
0
int main(int argc, char** argv) {
	int wstat, ret;
	pid_t pid;
	struct timeval tv;
	service_t *svc;
	
	// initialize wake structure, which is owned by main
	memset(wake, 0, sizeof(*wake));
	// just in case, but probably redundant
	FD_ZERO(&wake->fd_read);
	FD_ZERO(&wake->fd_write);
	FD_ZERO(&wake->fd_err);
	FD_ZERO(&wake->fd_ready_read);
	FD_ZERO(&wake->fd_ready_write);
	FD_ZERO(&wake->fd_ready_err);
	// wake structure holds current time so we don't keep calling clock_gettime()
	wake->now= gettime_mon_frac();
	
	log_init();
	svc_init();
	fd_init();
	ctl_init();

	// Special defaults when running as init
	if (getpid() == 1) {
		opt_config_file= CONFIG_FILE_DEFAULT_PATH;
		opt_terminate_guard= 1;
	}
	
	umask(077);

	// parse arguments, overriding default values
	parse_opts(argv+1);
	
	// Check for required options
	if (!opt_interactive && !opt_config_file && !opt_socket_path)
		fatal(EXIT_BAD_OPTIONS, "require -i or -c or -S");
	
	// Initialize file descriptor object pool
	if (opt_fd_pool_count > 0 && opt_fd_pool_size_each > 0)
		if (!fd_preallocate(opt_fd_pool_count, opt_fd_pool_size_each))
			fatal(EXIT_INVALID_ENVIRONMENT, "Unable to preallocate file descriptor objects");
	if (!fd_init_special_handles())
		fatal(EXIT_BROKEN_PROGRAM_STATE, "Can't initialize all special handles");

	if (!register_open_fds())
		fatal(EXIT_BAD_OPTIONS, "Not enough FD objects to register all open FDs");

	// Set up signal handlers and signal mask and signal self-pipe
	// Do this AFTER registering all open FDs, because it creates a pipe
	sig_init();
	
	// Initialize service object pool
	if (opt_svc_pool_count > 0 && opt_svc_pool_size_each > 0)
		if (!svc_preallocate(opt_svc_pool_count, opt_svc_pool_size_each))
			fatal(EXIT_INVALID_ENVIRONMENT, "Unable to preallocate service objects");

	// Initialize controller object pool
	control_socket_init();

	if (opt_socket_path && !control_socket_start(STRSEG(opt_socket_path)))
		fatal(EXIT_INVALID_ENVIRONMENT, "Can't create controller socket");
	
	if (opt_interactive)
		if (!setup_interactive_mode())
			fatal(EXIT_INVALID_ENVIRONMENT, "stdin/stdout are not usable!");

	if (opt_config_file)
		if (!setup_config_file(opt_config_file))
			fatal(EXIT_INVALID_ENVIRONMENT, "Unable to process config file");

	if (opt_mlockall) {
		// Lock all memory into ram. init should never be "swapped out".
		if (mlockall(MCL_CURRENT | MCL_FUTURE))
			log_error("mlockall: %s", strerror(errno));
	}
	
	// fork and setsid if requested, but not if PID 1 or interactive
	if (opt_daemonize) {
		if (getpid() == 1 || opt_interactive)
			log_warn("Ignoring --daemonize (see manual)");
		else
			daemonize();
	}

	// terminate is disabled when running as init, so this is an infinite loop
	// (except when debugging)
	wake->now= gettime_mon_frac();
	while (!main_terminate) {
		// set our wait parameters so other methods can inject new wake reasons
		wake->next= wake->now + (200LL<<32); // wake at least every 200 seconds
		wake->max_fd= -1;
		
		log_run();

		// collect new signals since last iteration and set read-wake on signal fd
		sig_run();
		
		// reap all zombies, possibly waking services
		while ((pid= waitpid(-1, &wstat, WNOHANG)) > 0) {
			log_trace("waitpid found pid = %d", (int)pid);
			if ((svc= svc_by_pid(pid)))
				svc_handle_reaped(svc, wstat);
			else
				log_trace("pid does not belong to any service");
		}
		if (pid < 0)
			log_trace("waitpid: %s", strerror(errno));
		
		// run state machine of each service that is active.
		svc_run_active();
		
		// possibly accept new controller connections
		control_socket_run();
		
		// run controller state machines
		ctl_run();
		
		log_run();
		
		// Wait until an event or the next time a state machine needs to run
		// (state machines edit wake.next)
		wake->now= gettime_mon_frac();
		if (wake->next - wake->now > 0) {
			tv.tv_sec= (long)((wake->next - wake->now) >> 32);
			tv.tv_usec= (long)((((wake->next - wake->now)&0xFFFFFFFFLL) * 1000000) >> 32);
			log_trace("wait up to %d.%d sec", tv.tv_sec, tv.tv_usec);
		}
		else