Exemple #1
0
/* public functions ========================================================= */
int speed_init(void) {
    if (running) {
        goto err_running;
    }

    if ((ostream = fopen("speed", "w")) == NULL) {
        goto err_ostream;
    }

    rt_intr_create(&intr, NULL, 81, 0);
    rt_intr_enable(&intr);
    rt_mutex_create(&mutex_instant, NULL);
    rt_mutex_create(&mutex_average, NULL);
    rt_queue_create(&queue, "irq81", QUEUE_SIZE, Q_UNLIMITED, Q_FIFO);
    rt_task_spawn(&task_soft, NULL, 0, 80, 0, task_soft_routine, NULL);
    rt_task_spawn(&task_hard, NULL, 0, 90, 0, task_hard_routine, NULL);

    instant = 0;
    average = 0;

    running = 1;

    return 0;

err_ostream:
err_running:
    return -1;
}
int main(int argc, char * argv[])
{	
	RT_TASK task;
	RTIME periode;
	int err;
	int fd;
	int zero = 0;
	fd = open("/dev/cpu_dma_latency", O_WRONLY);
	if (fd >= 0) {
		write(fd, & zero, sizeof(zero));
		// ne pas fermer le fichier avant la fin du processus
	} 
	if ((argc != 2) || (sscanf(argv[1], "%llu", & periode) != 1)) {
		fprintf(stderr, "usage: %s periode_en_us\n", argv[0]);
		exit(EXIT_FAILURE);
	}
	
	mlockall(MCL_CURRENT|MCL_FUTURE);
	rt_print_auto_init(1);

	if ((err = rt_task_spawn(& task, NULL, 0, 99,
	        T_JOINABLE, fonction_periodique, & periode)) != 0) {
		fprintf(stderr, "rt_task_spaw: %s\n", strerror(-err));
		exit(EXIT_FAILURE);
	}

	rt_task_join(& task);
	return 0;
}
Exemple #3
0
int main(int argc, char *argv[]) {
	int err, ret;
	printf("start\n");
	// install signal handler
	signal(SIGTERM, clean_exit);	
	signal(SIGINT, clean_exit);	
	// start timer -> depricated in Xeno 2.2
	/* ret = rt_timer_start(TM_ONESHOT);
	switch (ret) {
		case 0:       printf("timer started\n");
		              break;
		case -EBUSY:  printf("timer is running\n");
		              break;
		case -ENOSYS: printf("can't start timer\n");
		              return ret;
	}
	*/
	mlockall(MCL_CURRENT | MCL_FUTURE);
	err = rt_task_spawn(&test_task_ptr, "Timer", STACK_SIZE, STD_PRIO, 0, &testtask, NULL);
	if (err) {
		printf("error rt_task_spawn\n");
		return 0;
	}
	// wait for signal & return of signal handler
	pause();
	fflush(NULL);
	return 0;
}
int main(void)
{	
	int err;
	int i;
	RT_TASK task[NB_TACHES];
	char nom_tache[80];
	
	mlockall(MCL_CURRENT|MCL_FUTURE);
	rt_print_auto_init(1);

	if ((err = rt_alarm_create(& alarme, "Ex01")) != 0) {
		fprintf(stderr, "rt_alarm_create(): %s\n",
		                strerror(-err));
		exit(EXIT_FAILURE);
	}

	for (i = 0; i < NB_TACHES-1; i++) {
		snprintf(nom_tache, 80, "Alarme-%d\n", i+1);
		if ((err = rt_task_spawn(& (task[i]), nom_tache, 0,
		                         90-NB_TACHES + i, T_JOINABLE,
		                         fonction_thread, (void *) (i+1))) != 0) {
			fprintf(stderr, "rt_task_spawn: %s\n", strerror(-err));
			exit(EXIT_FAILURE);
		}
	}

	if ((err = rt_alarm_start(& alarme, TM_NOW, 1000000000)) != 0) {
		fprintf(stderr, "rt_alarm_start: %s\n", strerror(-err));
		exit(EXIT_FAILURE);
	}

	for (i = 0; i < NB_TACHES; i ++)
		rt_task_join(& task[i]);
	return 0;
}
Exemple #5
0
int main(int argc, char * argv[])
{
	RT_TASK task;
	RTIME period;
	int err;
	int fd;
	int zero = 0;

	fd = open("/dev/cpu_dma_latency", O_WRONLY);
	if (fd >= 0) {
		write(fd, &zero, sizeof(zero));
		// don't close the file before process termination.
	}

	if ((argc != 2)
	 || (sscanf(argv[1], "%llu", &period) != 1)) {
		fprintf(stderr, "usage: %s period_us\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	mlockall(MCL_CURRENT|MCL_FUTURE);

	err = rt_task_spawn(& task, NULL, 0, 99,
	        T_JOINABLE, periodic_task, & period);
	if (err != 0) {
		fprintf(stderr, "rt_task_spaw: %s\n", strerror(-err));
		exit(EXIT_FAILURE);
	}

	rt_task_join(& task);

	return 0;
}
Exemple #6
0
int root_thread_init (void)

{
    rt_timer_set_mode(1000000);	/* Forc 1ms periodic tick. */

    rt_task_spawn(&consumer_task,
		  "ConsumerTask",
		  CONSUMER_STACK_SIZE,
		  CONSUMER_TASK_PRI,
		  0,
		  &consumer,
		  NULL);

    rt_task_spawn(&producer_task,
		  "ProducerTask",
		  PRODUCER_STACK_SIZE,
		  PRODUCER_TASK_PRI,
		  0,
		  &producer,
		  NULL);
    return 0;
}
Exemple #7
0
int main(void)
{
	int err;
	RT_TASK task, task2;

	mlockall(MCL_CURRENT|MCL_FUTURE);
	rt_print_auto_init(1);

	if (((err = rt_task_spawn( & task,"HELLO",0,99,T_JOINABLE,hello,NULL)) != 0 )) {
	fprintf(stderr, "rt_task_spawn : %s\n",strerror(-err));
	exit(EXIT_FAILURE);
	}

	if (((err = rt_task_spawn( & task2,"HELLO2",0,98,T_JOINABLE,hello2,NULL)) != 0 )) {
	fprintf(stderr, "rt_task_spawn : %s\n",strerror(-err));
	exit(EXIT_FAILURE);
	}

	rt_task_join( & task );
	rt_task_join( & task2);
	return 0;
}
Exemple #8
0
int main(int argc, char* argv[])
{
	char str[10];
	int err;

	rt_print_auto_init(1);
	mlockall(MCL_CURRENT|MCL_FUTURE);

	rt_printf("start task\n");
	sprintf(str, "hello");
	//rt_task_create(&demo_task, str, 0, 50, 0);
	//rt_task_start(&demo_task, &demo, 0);
	err = rt_task_spawn(&demo_task, str, TASK_STKSZ, TASK_PRIO, TASK_MODE, &demo, NULL);
	if(!err)rt_task_delete(&demo_task);
	printf("task finished\n");
	return 0;
}
Exemple #9
0
int main(int argc, char *const argv[])
{
	int ret;

	traceobj_init(&trobj, argv[0], sizeof(tseq) / sizeof(int));

	ret = rt_alarm_create(&alrm, "ALARM", alarm_handler, &alrm);
	traceobj_check(&trobj, ret, 0);

	ret = rt_task_spawn(&t_main, "main_task", 0,  50, 0, main_task, NULL);
	traceobj_check(&trobj, ret, 0);

	traceobj_mark(&trobj, 8);

	traceobj_join(&trobj);

	traceobj_verify(&trobj, tseq, sizeof(tseq) / sizeof(int));

	exit(0);
}
Exemple #10
0
void loop(void *arg){
	Matrix outputMatrix=empty_matrix(1,1);
	RT_TASK pwm_task;

	lpt_init();

	rt_task_spawn(&pwm_task,NULL,0,gs->task_prio,0,&pwm,NULL);

	while (running) {
		read_inputs();
		
		//outputMatrix=periodic_function(io.input_result,io.input_num);

		write_outputs(outputMatrix);
	}

	rt_task_delete(&pwm_task);

	lpt_close();
}
Exemple #11
0
int main()
{
	RT_TASK task;
	
	// bloque la memoire virtuelle pour ne pas avoir de swap
	mlockall(MCL_CURRENT|MCL_FUTURE);
	
	//
	rt_print_auto_init(1);

	// spawn : cree et lance la tache : equiv rt_task_create + rt_task_start
	if (rt_task_spawn(& task, NULL, 0, 99,
	        T_JOINABLE, fonction_periodique, NULL) != 0) {
		fprintf(stderr, "Impossible de creer la tache\n");
		exit(EXIT_FAILURE);
	}

	rt_task_join(& task);
	return 0;
}
Exemple #12
0
int cond_wait_until(cond_t *cond, mutex_t *mutex, unsigned long long date)
{
	struct timespec ts = {
		.tv_sec = date / NS_PER_S,
		.tv_nsec = date % NS_PER_S,
	};

	return -pthread_cond_timedwait(cond, mutex, &ts);
}
#define cond_destroy(cond) (-pthread_cond_destroy(cond))

int thread_msleep(unsigned ms)
{
	struct timespec ts = {
		.tv_sec = (ms * NS_PER_MS) / NS_PER_S,
		.tv_nsec = (ms * NS_PER_MS) % NS_PER_S,
	};

	return -nanosleep(&ts, NULL);
}

int thread_spawn(thread_t *thread, int prio,
		 void *(*handler)(void *cookie), void *cookie)
{
	struct sched_param param;
	pthread_attr_t tattr;
	int err;

	pthread_attr_init(&tattr);
	pthread_attr_setinheritsched(&tattr, PTHREAD_EXPLICIT_SCHED);
	pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
	param.sched_priority = prio;
	pthread_attr_setschedparam(&tattr, &param);
	pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE);
	pthread_attr_setstacksize(&tattr, xeno_stacksize(0));

	err = pthread_create(thread, &tattr, handler, cookie);

	pthread_attr_destroy(&tattr);

	return -err;
}
#define thread_yield() sched_yield()
#define thread_kill(thread, sig) (-__real_pthread_kill(thread, sig))
#define thread_self() pthread_self()
#define thread_join(thread) (-pthread_join(thread, NULL))

#else /* __NATIVE_SKIN__ */
typedef RT_MUTEX mutex_t;
typedef RT_TASK *thread_t;
typedef RT_COND cond_t;

#define timer_read() rt_timer_read()

int __mutex_init(mutex_t *mutex, const char *name, int type, int pi)
{
	if (type == PTHREAD_MUTEX_ERRORCHECK)
		return -EINVAL;
	(void)(pi);

	return -rt_mutex_create(mutex, name);
}
#define mutex_init(mutex, type, pi) __mutex_init(mutex, #mutex, type, pi)
#define mutex_destroy(mutex) rt_mutex_delete(mutex)
#define mutex_lock(mutex) rt_mutex_acquire(mutex, TM_INFINITE)
#define mutex_unlock(mutex) rt_mutex_release(mutex)

int __cond_init(cond_t *cond, const char *name, int absolute)
{
	(void)(absolute);
	return rt_cond_create(cond, name);
}
#define cond_init(cond, absolute) __cond_init(cond, #cond, absolute)
#define cond_signal(cond) rt_cond_signal(cond)
#define cond_wait(cond, mutex, ns) rt_cond_wait(cond, mutex, (RTIME)ns)
#define cond_wait_until(cond, mutex, ns) \
	rt_cond_wait_until(cond, mutex, (RTIME)ns)
#define cond_destroy(cond) rt_cond_delete(cond)

#define thread_self() rt_task_self()
#define thread_msleep(ms) rt_task_sleep((RTIME)ms * NS_PER_MS)
int
thread_spawn_inner(thread_t *thread, const char *name,
		   int prio, void *(*handler)(void *), void *cookie)
{
	thread_t tcb;
	int err;

	tcb = malloc(sizeof(*tcb));
	if (!tcb)
		return -ENOSPC;

	err = rt_task_spawn(tcb, name, 0, prio, T_JOINABLE,
			    (void (*)(void *))handler, cookie);
	if (!err)
		*thread = tcb;

	return err;
}
Exemple #13
0
        INTERNAL_QUAL int rtos_task_create(RTOS_TASK* task,
                                           int priority,
                                           unsigned cpu_affinity,
                                           const char* name,
                                           int sched_type,
                                           size_t stack_size,
                                           void * (*start_routine)(void *),
                                           ThreadInterface* obj)
        {
            rtos_task_check_priority(&sched_type, &priority);
            XenoCookie* xcookie = (XenoCookie*)malloc( sizeof(XenoCookie) );
            xcookie->data = obj;
            xcookie->wrapper = start_routine;
            if ( name == 0 || strlen(name) == 0)
                name = "XenoThread";
            task->name = strncpy( (char*)malloc( (strlen(name)+1)*sizeof(char) ), name, strlen(name)+1 );
            task->sched_type = sched_type; // User requested scheduler.
            int rv;

            unsigned int aff = 0;
            if ( cpu_affinity != 0 ) {
                // calculate affinity:
                for(unsigned i = 0; i < 8*sizeof(cpu_affinity); i++) {
                    if(cpu_affinity & (1 << i)) { 
                        // RTHAL_NR_CPUS is defined in the kernel, not in user space. So we just limit up to 7, until Xenomai allows us to get the maximum.
                        if ( i > 7 ) {
                            const unsigned int all_cpus = ~0;
                            if ( cpu_affinity != all_cpus ) // suppress this warning when ~0 is provided
                                log(Warning) << "rtos_task_create: ignoring cpu_affinity for "<< name << " on CPU " << i << " since it's larger than RTHAL_NR_CPUS - 1 (="<< 7 <<")"<<endlog();
                        } else {
                            aff |= T_CPU(i); 
                        }
                    }
                }
            }
            
            if (stack_size == 0) {
                log(Debug) << "Raizing default stack size to 128kb for Xenomai threads in Orocos." <<endlog();
                stack_size = 128000;
            }

            // task, name, stack, priority, mode, fun, arg
            // UGLY, how can I check in Xenomai that a name is in use before calling rt_task_spawn ???
            rv = rt_task_spawn(&(task->xenotask), name, stack_size, priority, T_JOINABLE | (aff & T_CPUMASK), rtos_xeno_thread_wrapper, xcookie);
            if ( rv == -EEXIST ) {
                free( task->name );
                task->name = strncpy( (char*)malloc( (strlen(name)+2)*sizeof(char) ), name, strlen(name)+1 );
                task->name[ strlen(name) ] = '0';
                task->name[ strlen(name)+1 ] = 0;
                while ( rv == -EEXIST &&  task->name[ strlen(name) ] != '9') {
                    task->name[ strlen(name) ] += 1;
                    rv = rt_task_spawn(&(task->xenotask), task->name, stack_size, priority, T_JOINABLE | (aff & T_CPUMASK), rtos_xeno_thread_wrapper, xcookie);
                }
            }
            if ( rv == -EEXIST ) {
                log(Warning) << name << ": an object with that name is already existing in Xenomai." << endlog();
                rv = rt_task_spawn(&(task->xenotask), 0, stack_size, priority, T_JOINABLE | (aff & T_CPUMASK), rtos_xeno_thread_wrapper, xcookie);
            }
            if ( rv != 0) {
                log(Error) << name << " : CANNOT INIT Xeno TASK " << task->name <<" error code: "<< rv << endlog();
                return rv;
            }

            rt_task_yield();
            return 0;
        }
Exemple #14
0
int main(int argc, char **argv)
{
	
    printf("%s\n","Created Model");
    int serversock, clientsock;
    size_t clientlen;  
    struct sockaddr_in server_addr, client_addr, view_addr[2];
    unsigned int port;
    unsigned int view_port;
    char *portNum;
    mlockall(MCL_CURRENT | MCL_FUTURE);
	
    
    signal(SIGKILL, cleanup);
 
    if(argc < 1){
	usage(*argv);
	exit(0);
    }
	
    int c;
    while((c = getopt(argc, argv, "p:b:v:")) != -1){
 	switch(c){char data[1024];
	
	   case 'p':
	     port = atoi(strdup(optarg));
	   
	   break;

           case 'v':
	     view_port = atoi(strdup(optarg));
	   
	   break;

	   /* many potential super fast speeds can be achieved */
	   case 'b':    
	   ballThreadSpeed = atoi(optarg) > 0 ?
			     atoi(optarg) : BTHREAD_RATIO;	
      	   break;
	   default:
		usage(*argv);
		exit(0);
	   break;
        }
    }

	
   

    mThreadPid = getpid();
    initializePong();


   /* if(rt_task_create(&ballThread, "ball_Thread" ,4096, 99, 0))
	rt_err("rt_task_create()");
    if(rt_task_create(&recvThread[0], "Recv_Thread", 4096, 99, T_JOINABLE))
	rt_err("rt_task_create()");    
    if(rt_task_create(&recvThread[1], "Recv2_Thread", 4096, 99, T_JOINABLE))
	rt_err("rt_task_create()"); 
*/

     /* task creation */
    char temp_ball_Thread[1000];
    char temp_Recv_Thread[1000];
    char temp_Recv2_Thread[1000];
    sprintf(temp_ball_Thread,"BallThread%d",port);
    sprintf(temp_Recv_Thread,"RecvThread%d",port);
    sprintf(temp_Recv2_Thread,"RecvThread2%d",port);

    if(rt_task_create(&ballThread, temp_ball_Thread ,4096, 99, 0))
	rt_err("rt_task_create()");
    if(rt_task_create(&recvThread[0], temp_Recv_Thread, 4096, 99, T_JOINABLE))
	rt_err("rt_task_create()");    
    if(rt_task_create(&recvThread[1], temp_Recv2_Thread, 4096, 99, T_JOINABLE))
	rt_err("rt_task_create()"); 


    /* creation of mutexes for buffer */
    unsigned int i = 0; 
    for(; i < 3; i++){
       char buf[8];
       sprintf(buf, "Mutex %d %d", i, port);
       if(rt_mutex_create(&txMutex[i], buf))
          rt_err("rt_mutex_create()");
    }

    char temp_start_mutex[1000];
    sprintf(temp_start_mutex,"AutoStartMutex%d",port);
    if(rt_mutex_create(&autoStart, temp_start_mutex))
       rt_err("rt_mutex_create()");

    i = 0;

     /* socket creation and structure initialization*/ 
    if ((serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) 
	    errx(EXIT_FAILURE, "socket()", strerror(-errno));


    int yes = 1;
    if(setsockopt(serversock,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1){
	
         perror("Setsockopt");
         exit(1);
    }

    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port = htons(port);	/* server port */
    
    if (bind(serversock, (struct sockaddr *) &server_addr, 
                sizeof(server_addr)) < 0){
	    close(serversock); 
	    errx(EXIT_FAILURE, "bind():", strerror(errno));
    }
    /* listens for Game Controller only*/
    if (listen(serversock, 1) < 0)
       errx(EXIT_FAILURE, "listen():", strerror(-errno));

         int viewSocket; 
         if ((viewSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
           rt_err("socket()");

       /* connection loop */
    printf("Waiting for connections ...\n");

    while(true){ 
      char *currClient;
        
        clientlen = sizeof(struct sockaddr_in);
        clientsock = accept(serversock,
                (struct sockaddr *)&client_addr, (socklen_t *)&clientlen);
	
      currClient = (char *)inet_ntoa(client_addr.sin_addr);
      if(i > 1 || (i < 1 && !issuedStart)){ //game in play by force or 2 players
	close(clientsock); 
        fprintf(stderr, "rejecting %s "
			"only 2 clients allowed!\n", currClient);
	continue;
      }
  
      
      //spawn the receive thread for simulator
      player_t players[2];
      players[i].socket  = clientsock;
      players[i].vsocket = viewSocket;
      players[i].addr    = client_addr;
      if(rt_task_start(&recvThread[i], recv_data, (void *)&players[i]))
	rt_err("rt_task_start()");
      i++;
      viewMode++;
      /* 1st player connected */
      if(i == 1){ 
         initSock = clientsock; 
    
      /* spawn task to countdown */                
         if(rt_task_spawn(&alarmTask, "AutoStart", 4096, 99, 0, 
			  alarm_handler, (void *)viewSocket))
            rt_err("rt_task_spawn()");
 
         /* start the ball thread and it handles further movement */
        if(rt_task_start(&ballThread, run,(void*)viewSocket))
	     rt_err("rt_task_start()");
            printf("1 Player Connected ...\n");
       	 /* now need to connect to the view */
   	
	
           memset(&view_addr[0], 0, sizeof(view_addr[0]));
            view_addr[0].sin_family = AF_INET;	
            view_addr[0].sin_addr.s_addr = inet_addr(currClient);	
            view_addr[0].sin_port = htons(view_port);

          /* Establish connection to view */
          if (connect(viewSocket,(struct sockaddr *) &view_addr[0], sizeof(view_addr[0])) < 0)
	        rt_err("connect()");  
      }


      /* the case when both players start before one-shot alarm */
      if(i == 2){


	/*Now need to connect the second view if there is one*/
           if ((viewSocket2= socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
 	       rt_err("socket()");/*2 View mode case*/


 	   memset(&view_addr[1], 0, sizeof(view_addr[1]));
 	   view_addr[1].sin_family = AF_INET;	
 	   view_addr[1].sin_addr.s_addr = inet_addr(currClient);	
 	   view_addr[1].sin_port = htons(view_port);

           if (connect(viewSocket2,(struct sockaddr *) &view_addr[1], sizeof(view_addr[1])) < 0)
	   rt_err("connect()2"); 

       

 
        rt_mutex_acquire(&autoStart, TM_INFINITE);
	issuedStart = false;
	rt_mutex_release(&autoStart);
	rt_task_resume(&recvThread[0]); 
	rt_task_resume(&recvThread[1]);
      }
    }
      //join on receive
      rt_task_join(&recvThread[0]);
      rt_task_join(&recvThread[1]);
    return 0;
}
Exemple #15
0
int main(int argc, char **argv)
{
    int serversock, clientsock;
    size_t clientlen;  
    struct sockaddr_in server_addr, client_addr, view_addr;
    unsigned int port = MODEL_DEFAULT_PORT; //models and view iff on same cpu

    char *ipAddress;
    
    mlockall(MCL_CURRENT | MCL_FUTURE);

    if(argc < 3){
	usage(*argv);
	exit(0);
    }
	
    int c;
    while((c = getopt(argc, argv, "i:b:")) != -1){
 	switch(c){
	   case 'i':
	     ipAddress = strdup(optarg);
	   break;
	  
	   /* many potential super fast speeds can be achieved */
	   case 'b':    
	   ballThreadSpeed = atoi(optarg) > 0 ?
			     atoi(optarg) : BTHREAD_RATIO;	
      	   break;
	   default:
		usage(*argv);
		exit(0);
	   break;
        }
    }

    initializePong();

    /* task creation */
    if(rt_task_create(&ballThread, "BallThread", 4096, 99, 0))
	rt_err("rt_task_create()");
    if(rt_task_create(&recvThread[0], "RecvThread", 4096, 99, T_JOINABLE))
	rt_err("rt_task_create()");    
    if(rt_task_create(&recvThread[1], "RecvThread1", 4096, 99, T_JOINABLE))
	rt_err("rt_task_create()"); 
    
    /* creation of mutexes for buffer */
    unsigned int i = 0; 
    for(; i < 3; i++){
       char buf[8];
       sprintf(buf, "Mutex %d", i);
       if(rt_mutex_create(&txMutex[i], buf))
          rt_err("rt_mutex_create()");
    }
    if(rt_mutex_create(&autoStart, "AutoStartMutex"))
       rt_err("rt_mutex_create()");

    i = 0;
     /* socket creation and structure initialization*/
    if ((serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) 
	    errx(EXIT_FAILURE, "socket()", strerror(-errno));
   
    memset(&server_addr, 0, sizeof(server_addr));	
    server_addr.sin_family = AF_INET;	/* Internet/IP */
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port = htons(port);	/* server port */
    
    if (bind(serversock, (struct sockaddr *) &server_addr, 
                sizeof(server_addr)) < 0){
	    close(serversock); 
	    errx(EXIT_FAILURE, "bind():", strerror(errno));
    }
    /* listens for Game Controller only*/
    if (listen(serversock, 1) < 0)
       errx(EXIT_FAILURE, "listen():", strerror(-errno));

    
    /* now need to connect to the view */
    int viewSocket; 
    if ((viewSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
        rt_err("socket()");

    memset(&view_addr, 0, sizeof(view_addr));
    view_addr.sin_family = AF_INET;	
    view_addr.sin_addr.s_addr = inet_addr(ipAddress);	
    view_addr.sin_port = htons(VIEW_DEFAULT_PORT);
    /* Establish connection to view */
    if (connect(viewSocket,
		(struct sockaddr *) &view_addr, sizeof(view_addr)) < 0)
	rt_err("connect()"); 

       /* connection loop */
    printf("Waiting for connections ...\n");

    while(true){ 
      char *currClient;
        
        clientlen = sizeof(struct sockaddr_in);
        clientsock = accept(serversock,
                (struct sockaddr *)&client_addr, 
                (socklen_t *)&clientlen);
	
      currClient = (char *)inet_ntoa(client_addr.sin_addr);
      if(i > 1 || (i < 1 && !issuedStart)){ //game in play by force or 2 players
	close(clientsock); 
        fprintf(stderr, "rejecting %s "
			"only 2 clients allowed!\n", currClient);
	continue;
      }
  
      
      //spawn the receive thread for simulator
      player_t players[2];
      players[i].socket  = clientsock;
      players[i].vsocket = viewSocket;
      players[i].addr    = client_addr;
      if(rt_task_start(&recvThread[i], recv_data, (void *)&players[i]))
	rt_err("rt_task_start()");
      i++;
      
      /* 1st player connected */
      if(i == 1){ 
         initSock = clientsock; 
    
      /* spawn task to countdown */                
         if(rt_task_spawn(&alarmTask, "AutoStart", 4096, 99, 0, 
			  alarm_handler, (void *)viewSocket))
            rt_err("rt_task_spawn()");
 
         /* start the ball thread and it handles further movement */
        if(rt_task_start(&ballThread, run,(void*)viewSocket))
	  rt_err("rt_task_start()");
        printf("1 Player Connected ...\n");
       	  
      }

      /* the case when both players start before one-shot alarm */
      if(i == 2){
        rt_mutex_acquire(&autoStart, TM_INFINITE);
	issuedStart = false;
	rt_mutex_release(&autoStart);
	rt_task_resume(&recvThread[0]); 
	rt_task_resume(&recvThread[1]);
      }
    }
      //join on receive
      rt_task_join(&recvThread[0]);
      rt_task_join(&recvThread[1]);
    return 0;
}
Exemple #16
0
int main(void)
{
	unsigned long long before;
	RT_ALARM nalrm;
	RT_BUFFER nbuf;
	RT_COND ncond;
	RT_EVENT nevt;
	RT_HEAP nheap;
	RT_MUTEX nmtx;
	RT_PIPE npipe;
	RT_QUEUE nq;
	RT_SEM nsem;
	RT_TASK ntsk;
	int failed = 0;

	mlockall(MCL_CURRENT|MCL_FUTURE);

	rt_print_auto_init(1);

	rt_fprintf(stderr, "Checking for leaks in native skin services\n");
	before = get_used();
	check_native(rt_alarm_create(&nalrm, NULL));
	check_native(rt_alarm_delete(&nalrm));
	check_used("alarm", before, failed);

	before = get_used();
	check_native(rt_buffer_create(&nbuf, NULL, 16384, B_PRIO));
	check_native(rt_buffer_delete(&nbuf));
	check_used("buffer", before, failed);

	before = get_used();
	check_native(rt_cond_create(&ncond, NULL));
	check_native(rt_cond_delete(&ncond));
	check_used("cond", before, failed);

	before = get_used();
	check_native(rt_event_create(&nevt, NULL, 0, EV_PRIO));
	check_native(rt_event_delete(&nevt));
	check_used("event", before, failed);

	before = get_used();
	check_native(rt_heap_create(&nheap, "heap", 16384, H_PRIO | H_SHARED));
	check_native(rt_heap_delete(&nheap));
	check_used("heap", before, failed);

	before = get_used();
	check_native(rt_mutex_create(&nmtx, NULL));
	check_native(rt_mutex_delete(&nmtx));
	check_used("mutex", before, failed);

	before = get_used();
	check_native(rt_pipe_create(&npipe, NULL, P_MINOR_AUTO, 0));
	check_native(rt_pipe_delete(&npipe));
	check_used("pipe", before, failed);

	before = get_used();
	check_native(rt_queue_create(&nq, "queue", 16384, Q_UNLIMITED, Q_PRIO));
	check_native(rt_queue_delete(&nq));
	check_used("queue", before, failed);

	before = get_used();
	check_native(rt_sem_create(&nsem, NULL, 0, S_PRIO));
	check_native(rt_sem_delete(&nsem));
	check_used("sem", before, failed);

	before = get_used();
	check_native(rt_task_spawn(&ntsk, NULL, 0, 1, T_JOINABLE, empty, NULL));
	check_native(rt_task_join(&ntsk));
	sleep(1);		/* Leave some time for xnheap
				 * deferred free */
	check_used("task", before, failed);

	return failed ? EXIT_FAILURE : EXIT_SUCCESS;
}
Exemple #17
0
int main(int argc, char **argv)
{
    char tempname[] = "/tmp/sigdebug-XXXXXX";
    char buf[BUFSIZ], dev[BUFSIZ];
    RT_TASK main_task, rt_task;
    long int start, trash, end;
    unsigned char *mayday, *p;
    struct sigaction sa;
    int old_wd_value;
    char r, w, x, s;
    int tmp_fd, d;
    FILE *maps;
    int err;

    rt_print_auto_init(1);

    if (argc < 2 || strcmp(argv[1], "--skip-watchdog") != 0) {
        wd = fopen("/sys/module/xeno_nucleus/parameters/"
                   "watchdog_timeout", "w+");
        if (!wd) {
            fprintf(stderr, "FAILURE: no watchdog available and "
                    "--skip-watchdog not specified\n");
            exit(EXIT_FAILURE);
        }
        err = fscanf(wd, "%d", &old_wd_value);
        check("get watchdog", err, 1);
        err = fprintf(wd, "2");
        check("set watchdog", err, 1);
        fflush(wd);
    }

    maps = fopen("/proc/self/maps", "r");
    if (maps == NULL) {
        perror("open /proc/self/maps");
        exit(EXIT_FAILURE);
    }

    while (fgets(buf, sizeof(buf), maps)) {
        if (sscanf(buf, "%lx-%lx %c%c%c%c %lx %x:%x %d%s\n",
                   &start, &end, &r, &w, &x, &s, &trash,
                   &d, &d, &d, dev) == 11
                && r == 'r' && x == 'x'
                && !strcmp(dev, "/dev/rtheap") && end - start == 4096) {
            printf("mayday page starting at 0x%lx [%s]\n"
                   "mayday code:", start, dev);
            mayday = (unsigned char *)start;
            for (p = mayday; p < mayday + 32; p++)
                printf(" %.2x", *p);
            printf("\n");
        }
    }
    fclose(maps);

    sigemptyset(&sa.sa_mask);
    sa.sa_sigaction = sigdebug_handler;
    sa.sa_flags = SA_SIGINFO;
    sigaction(SIGDEBUG, &sa, NULL);

    sa.sa_sigaction = dummy_handler;
    sigaction(SIGUSR1, &sa, NULL);

    printf("mlockall\n");
    munlockall();
    setup_checkdebug(SIGDEBUG_NOMLOCK);
    err = rt_task_shadow(&main_task, "main_task", 0, 0);
    check("rt_task_shadow", err, -EINTR);
    check_sigdebug_received("SIGDEBUG_NOMLOCK");

    mlockall(MCL_CURRENT | MCL_FUTURE);

    errno = 0;
    tmp_fd = mkstemp(tempname);
    check_no_error("mkstemp", -errno);
    unlink(tempname);
    check_no_error("unlink", -errno);
    mem = mmap(NULL, 1, PROT_READ | PROT_WRITE, MAP_SHARED, tmp_fd, 0);
    check_no_error("mmap", -errno);
    err = write(tmp_fd, "X", 1);
    check("write", err, 1);

    err = rt_task_shadow(&main_task, "main_task", 0, 0);
    check_no_error("rt_task_shadow", err);

    err = rt_mutex_create(&prio_invert, "prio_invert");
    check_no_error("rt_mutex_create", err);

    err = rt_mutex_acquire(&prio_invert, TM_INFINITE);
    check_no_error("rt_mutex_acquire", err);

    err = rt_sem_create(&send_signal, "send_signal", 0, S_PRIO);
    check_no_error("rt_sem_create", err);

    err = rt_task_spawn(&rt_task, "rt_task", 0, 1, T_WARNSW | T_JOINABLE,
                        rt_task_body, NULL);
    check_no_error("rt_task_spawn", err);

    err = rt_sem_p(&send_signal, TM_INFINITE);
    check_no_error("rt_sem_signal", err);
    pthread_kill(rt_task_thread, SIGUSR1);

    rt_task_sleep(rt_timer_ns2ticks(20000000LL));

    err = rt_mutex_release(&prio_invert);
    check_no_error("rt_mutex_release", err);

    err = rt_task_join(&rt_task);
    check_no_error("rt_task_join", err);

    err = rt_mutex_delete(&prio_invert);
    check_no_error("rt_mutex_delete", err);

    err = rt_sem_delete(&send_signal);
    check_no_error("rt_sem_delete", err);

    if (wd) {
        fprintf(wd, "%d", old_wd_value);
        fclose(wd);
    }

    fprintf(stderr, "Test OK\n");

    return 0;
}