Example #1
0
File: main.c Project: eerimoq/simba
static int test_all(struct harness_t *harness_p)
{
    struct time_t timeout = {
        .seconds = 0,
        .nanoseconds = 0
    };

    sem_init(&sem, 1, 1);
    sem_init(&sem2, 2, 2);
    
    /* 1. Take all resources. */
    BTASSERT(sem_take(&sem, &timeout) == -ETIMEDOUT);

    /* Create two thrds with higher priority than this thrd. */
    thrd_spawn(sem_main,
               NULL,
               -10,
               t0_stack,
               sizeof(t0_stack));
    thrd_spawn(sem_main,
               NULL,
               -10,
               t1_stack,
               sizeof(t1_stack));

    /* 3. Wait until both threads are waiting for sem. */
    sem_take(&sem2, NULL);
    sem_take(&sem2, NULL);

    /* 4. Start both threads. */
    sem_give(&sem, 2);

    /* 6. Wait for both threads. */
    sem_take(&sem2, NULL);
    sem_take(&sem2, NULL);

    return (0);
}

int main()
{
    struct harness_t harness;
    struct harness_testcase_t harness_testcases[] = {
        { test_all, "test_all" },
        { NULL, NULL }
    };

    sys_start();

    harness_init(&harness);
    harness_run(&harness, harness_testcases);

    return (0);
}
Example #2
0
/*
    memory_tracker_lock_mutex()
    Locks the memory tracker mutex with a platform specific call
    Returns:
        0: Success
       <0: Failure, either the mutex was not initialized
           or the call to lock the mutex failed
*/
static int memory_tracker_lock_mutex()
{
    int ret = -1;

    if (g_b_mem_tracker_inited)
    {

#if defined(LINUX) || defined(__uClinux__)
        ret = pthread_mutex_lock(&memtrack.mutex);
#elif defined(WIN32) || defined(_WIN32_WCE)
        ret = WaitForSingleObject(memtrack.mutex, INFINITE);
#elif defined(VXWORKS)
        ret = sem_take(memtrack.mutex, WAIT_FOREVER);
#elif defined(NDS_NITRO)
        os_lock_mutex(&memtrack.mutex);
        ret = 0;
#endif

        if (ret)
        {
            memtrack_log("memory_tracker_lock_mutex: mutex lock failed\n");
        }
    }

    return ret;
}
Example #3
0
File: main.c Project: eerimoq/simba
/**
 * Music player callback to get the current song path.
 */
static const char *get_current_song_path(void *arg_p)
{
    struct song_t *song_p;

    sem_take(&sem, NULL);
    song_p = hash_map_get(&song_map, current_song);
    sem_give(&sem, 1);

    return (song_p->name);
}
Example #4
0
File: main.c Project: eerimoq/simba
static void *sem_main(void *arg_p)
{
    /* 2. Give the second semaphore. */
    sem_give(&sem2, 1);
    sem_take(&sem, NULL);

    /* 7. Give the second semaphore. */
    sem_give(&sem2, 1);

    return (NULL);
}
Example #5
0
File: spi.c Project: eerimoq/simba
int spi_take_bus(struct spi_driver_t *self_p)
{
    ASSERTN(self_p != NULL, EINVAL);

    sem_take(&self_p->dev_p->sem, NULL);

    /* Configure and start SPI hardware with driver configuration. */
    if (self_p->dev_p->drv_p != self_p) {
        self_p->dev_p->drv_p = self_p;
        spi_port_start(self_p);
    }

    return (0);
}
Example #6
0
File: main.c Project: eerimoq/simba
static int next()
{
    music_player_song_stop(&music_player);

    /* Increment current song. */
    sem_take(&sem, NULL);
    current_song++;

    if (hash_map_get(&song_map, current_song) == NULL) {
        current_song = FIRST_SONG_NUMBER;
    }

    sem_give(&sem, 1);

    return (music_player_song_play(&music_player));
}
void tl_daemonzie(int iSem)
{
    pid_t pid, sid;

    pid = fork();
    if(pid < 0)
        exit(1);
    if(pid > 0)
    {
        sem_take(iSem);
        sem_delete(iSem);
        exit(0);
    }
    sid = setsid();
    if(sid < 0)
        exit(1);
}
Example #8
0
File: main.c Project: eerimoq/simba
static int prev()
{
    music_player_song_stop(&music_player);

    /* Increment current song. */
    sem_take(&sem, NULL);

    if (current_song == FIRST_SONG_NUMBER) {
        current_song = last_song_number;
    } else {
        current_song--;
    }

    sem_give(&sem, 1);

    return (music_player_song_play(&music_player));
}
Example #9
0
File: main.c Project: eerimoq/simba
/**
 * Music player callback to get the next song path.
 */
static const char *get_next_song_path(void *arg_p)
{
    struct song_t *song_p;

    sem_take(&sem, NULL);

    /* Increment current song. */
    if (repeat == 0) {
        current_song++;
    }

    song_p = hash_map_get(&song_map, current_song);
    sem_give(&sem, 1);

    if (song_p != NULL) {
        return (song_p->name);
    } else {
        current_song = FIRST_SONG_NUMBER;
        return (NULL);
    }
}
Example #10
0
File: main.c Project: eerimoq/simba
static int cmd_play_cb(int argc,
                       const char *argv[],
                       void *out_p,
                       void *in_p,
                       void *arg_p,
                       void *call_arg_p)
{
    long song_number;
    struct song_t *song_p;

    if (argc > 2) {
        std_fprintf(out_p, FSTR("Usage: %s [<song number>]\r\n"), argv[0]);

        return (-EINVAL);
    }

    if (argc == 2) {
        if (std_strtol(argv[1], &song_number) != 0) {
            return (-EINVAL);
        }

        /* Find the song in the hash map. */
        song_p = hash_map_get(&song_map, song_number);

        if (song_p == NULL) {
            return (-EINVAL);
        }

        /* Stop the current song and set the new current song. */
        music_player_song_stop(&music_player);
        sem_take(&sem, NULL);
        current_song = song_number;
        sem_give(&sem, 1);
    }

    /* Play the song or resume it if it's paused. */
    return (music_player_song_play(&music_player));
}
Example #11
0
/*
 ******************************************************************************
 * dgadmin_rest_sync_init --                                                  *//**
 *
 * \brief This routine creates sync task and resource.
 *
 * \retval 0 	Success
 * \retval >0 	Failure
 *
 *****************************************************************************/
dove_status dgadmin_rest_sync_init(void)
{
	dove_status retval = DOVE_STATUS_OK;
    
    if (create_sem("RSSEM", 0, 0, &gDgwySyncSemId) != OSW_OK)
    {
        printf("Failed to create semaphore \n");
        retval = DOVE_STATUS_ERROR;
        goto init_exit;
    }
            
    /* Create the system task */
    if (create_task("RSTASK", 10,
                    OSW_DEFAULT_STACK_SIZE,
                    (task_entry) dgadmin_rest_sync_main,
                    0, &gDgwySyncTaskId) != OSW_OK) 
    {
        printf("Failed to create task \n");
        goto init_failed;
    }

    if (sem_take(gDgwySyncSemId) != OSW_OK) 
    {
        retval = DOVE_STATUS_ERROR;
        goto init_failed;
    }
    else
    {
        goto init_exit;
    }

init_failed:
    del_sem(gDgwySyncSemId);

init_exit:
    return retval;
}
Example #12
0
/* Returns 1 on success and 0 on failure */
int perl_init(void)
{
   char path[MAX_FDP_LEN+1];
   char *script_list[256];
   char *myargv[] = {"", NULL};
   int i, k, len;
   int sock;
   struct sockaddr_un remote_addr;
   char temp_nick[MAX_NICK_LEN+1];
   char temp_host[MAX_HOST_LEN+1];
   char *buf, *bufp;
   int spaces=0, entries=0;
   int l;
   int erret;
   int flags;
   
   memset(&remote_addr, 0, sizeof(struct sockaddr_un));

   /* First kill off scripts that is already running.  */
   remove_all(SCRIPT, 1, 1);
   
   /* Reads the script names in the script directory */
   snprintf(path, MAX_FDP_LEN, "%s/%s", config_dir, SCRIPT_DIR);
   i = my_scandir(path, script_list);
   
   if(i == 0)
     return 1;
   
   k = i-1;
   
   for(i = 0; i <= k; i++)
     {	
	myargv[1] = script_list[i];
	if((pid = fork()) == -1)
	  {	
	     logprintf(1, "Fork failed, exiting process\n");
	     logerror(1, errno);
	     quit = 1;
	     return 0;;
	  }
	
	/* If we are the parent */   
	if(pid > 0)
	  {
	     logprintf(3, "Forked new script parsing process for script %s, childs pid is %d and parents pid is %d\n", script_list[i], pid, getpid());
	     pid = getpid();
	  }
	
	/* And if we are the child */
	else
	  {
	     pid = -1;
	     
	     /* Close the listening sockets */
	     while(((erret =  close(listening_unx_socket)) != 0) && (errno == EINTR))
	       logprintf(1, "Error - In perl_init()/close(): Interrupted system call. Trying again.\n");
	     
	     if(erret != 0)
	       {		  
		  logprintf(1, "Error - In perl_init()/close(): ");
		  logerror(1, errno);
	       }
	     
	     while(((erret =  close(listening_udp_socket)) != 0) && (errno == EINTR))
	       logprintf(1, "Error - In perl_init()/close(): Interrupted system call. Trying again.\n");
	     
	     if(erret != 0)
	       {		  
		  logprintf(1, "Error - In perl_init()/close(): ");
		  logerror(1, errno);
	       }
	     
	     /* Set the alarm */
	     alarm(ALARM_TIME);
	     
	     /* And connect to parent process */
	     if((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
	       {		  
		  logprintf(1, "Error - In perl_init()/socket(): ");
		  logerror(1, errno);
		  free(script_list[i]);
		  exit(EXIT_FAILURE);
	       }
	     
	     remote_addr.sun_family = AF_UNIX;
	     strcpy(remote_addr.sun_path, un_sock_path);
	     len = strlen(remote_addr.sun_path) + sizeof(remote_addr.sun_family) + 1;
	     if(connect(sock, (struct sockaddr *)&remote_addr, len) == -1)
	       {	     
		  logprintf(1, "Error - In perl_init()/connect(): ");
		  logerror(1, errno);
		  free(script_list[i]);
		  exit(EXIT_FAILURE);
	       }
	     
	     if((flags = fcntl(sock, F_GETFL, 0)) < 0)
	       {
		  logprintf(1, "Error - In new_human_user()/in fcntl(): ");
		  logerror(1, errno);
		  close(sock);
		  return -1;
	       }
	     
	     /* Non blocking mode */
	     if(fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
	       {
		  logprintf(1, "Error - In new_human_user()/in fcntl(): ");
		  logerror(1, errno);
		  close(sock);
		  return -1;
	       }	     
	     
	     /* The parent process will be a special kind of user */
	     /* Allocate space for the new user. Since the process
	      * should be empty on users and no one is to be added, 
	      * we use non_human_user_list.  */

	     /* Allocate space for the new user */
	     if((non_human_user_list = malloc(sizeof(struct user_t))) == NULL)
	       {		  
		  logprintf(1, "Error - In parl_init()/malloc(): ");
		  logerror(1, errno);
		  quit = 1;
		  free(script_list[i]);
		  exit(EXIT_FAILURE);
	       }	     	     
	          	
	     non_human_user_list->sock = sock;	
	     non_human_user_list->rem = 0;	
	     non_human_user_list->type = SCRIPT;
	     non_human_user_list->buf = NULL;
	     non_human_user_list->outbuf = NULL;
	     non_human_user_list->next = NULL;
	     non_human_user_list->email = NULL;
	     non_human_user_list->desc = NULL;
	     memset(non_human_user_list->nick, 0, MAX_NICK_LEN+1);
	     sprintf(non_human_user_list->nick, "parent process");
	     sprintf(non_human_user_list->hostname, "parent_process");
	     send_to_user("$NewScript|", non_human_user_list);
	     
	     /* Remove all users.  */	    
	     remove_all(~SCRIPT, 0, 0);
	     
	     /* Initialize the perl interpreter for this process */
	     if((my_perl = perl_alloc()) == NULL) 
	       {	
		  logprintf(1, "perl_alloc() failed\n");
		  free(script_list[i]);
		  exit(EXIT_FAILURE);
	       }
	     
	     perl_construct(my_perl);
	     if(perl_parse(my_perl, xs_init, 2, myargv, NULL))
	       {
		  logprintf(1, "Parse of %s failed.\n", script_list[i]);
		  free(script_list[i]);
		  exit(EXIT_FAILURE);
	       }
	     
	     if(perl_run(my_perl))
	       {
		  logprintf(1, "Couldn't run perl script %s.\n", script_list[i]);
		  free(script_list[i]);
		  exit(EXIT_FAILURE);
	       }
	     
	     /* Run the scripts main sub if it exists.  */
	       {		  
		  dSP;
		  ENTER;
		  SAVETMPS;
		  PUSHMARK(SP);
		  PUTBACK;
		  call_pv("main", G_DISCARD|G_EVAL);
		  SPAGAIN;
		  PUTBACK;
		  FREETMPS;
		  LEAVE;   
	       }

	     free(script_list[i]);
	     
	     /* Get info of all users.  */
	     if(i == 0)
	       {				  		
		  sem_take(user_list_sem);
		  
		  /* Attach to the shared segment */
		  if((buf = (char *)shmat(get_user_list_shm_id(), NULL, 0))
		     == (char *)-1)
		    {		       
		       logprintf(1, "Error - In perl_init()/shmat(): ");
		       logerror(1, errno);
		       sem_give(user_list_sem);
		       quit = 1;
		       return -1;
		    }
		  
		  if(sscanf(buf, "%d %d", &spaces, &entries) != 2)
		    {		       
		       logprintf(1, "Error - In perl_init(): Couldn't get number of entries\n");
		       shmdt(buf);
		       sem_give(user_list_sem);
		       quit = 1;
		       return -1;
		    }		  		  
		  
		  bufp = buf + 30;
		  
		  for(l = 1; l <= spaces; l++)
		    {		       
		       if(*bufp != '\0')
			 {			    
			    sscanf(bufp, "%50s %120s", temp_nick, temp_host);
			    uprintf(non_human_user_list, 
				    "$GetINFO %s $Script|", temp_nick);
			 }		       
		       
		       bufp += USER_LIST_ENT_SIZE;
		    }
		  shmdt(buf);
		  sem_give(user_list_sem);		 
	       }	     
	     return 1;
	  }
	free(script_list[i]);
     }
   return 1;
}