Diff for /libaitio/src/exec.c between versions 1.1.2.7 and 1.1.2.12

version 1.1.2.7, 2013/12/05 15:56:10 version 1.1.2.12, 2013/12/06 01:30:22
Line 28  io_progInit(const char *progName, u_int initNum, u_int Line 28  io_progInit(const char *progName, u_int initNum, u_int
         prg->prog_maxn = maxNum;          prg->prog_maxn = maxNum;
         strlcpy(prg->prog_name, progName, sizeof prg->prog_name);          strlcpy(prg->prog_name, progName, sizeof prg->prog_name);
   
           prg->prog_used = e_malloc(E_ALIGN(prg->prog_maxn, sizeof *prg->prog_used) / 
                           sizeof *prg->prog_used);
           if (!prg->prog_used) {
                   io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
                   e_free(prg);
                   return NULL;
           }
   
         prg->prog_fds = array_Init(prg->prog_maxn);          prg->prog_fds = array_Init(prg->prog_maxn);
         if (!prg->prog_fds) {          if (!prg->prog_fds) {
                 io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());                  io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
                   e_free(prg->prog_used);
                 e_free(prg);                  e_free(prg);
                 return NULL;                  return NULL;
         }          }
   
         pthread_mutex_init(&prg->prog_mtx, NULL);          pthread_mutex_init(&prg->prog_mtx, NULL);
           signal(SIGPIPE, SIG_IGN);
   
         if (io_progOpen(prg, prg->prog_inin) < 0) {          if (io_progOpen(prg, prg->prog_inin) < 0) {
                 io_progDestroy(&prg);                  io_progDestroy(&prg);
Line 58  io_progDestroy(prog_t ** __restrict pprg) Line 68  io_progDestroy(prog_t ** __restrict pprg)
   
         io_progClose(*pprg, 0);          io_progClose(*pprg, 0);
   
           e_free((*pprg)->prog_used);
         array_Destroy(&(*pprg)->prog_fds);          array_Destroy(&(*pprg)->prog_fds);
         pthread_mutex_destroy(&(*pprg)->prog_mtx);          pthread_mutex_destroy(&(*pprg)->prog_mtx);
   
Line 89  io_progClose(prog_t * __restrict prg, u_int closeNum) Line 100  io_progClose(prog_t * __restrict prg, u_int closeNum)
         for (i = array_Size(prg->prog_fds) - 1;           for (i = array_Size(prg->prog_fds) - 1; 
                         (closeNum ? ret < closeNum : 42) && i > -1; i--)                          (closeNum ? ret < closeNum : 42) && i > -1; i--)
                 if (array_Get(prg->prog_fds, i)) {                  if (array_Get(prg->prog_fds, i)) {
   #ifdef POPEN_STREAM
                         e_pclose(array(prg->prog_fds, i, FILE*));                          e_pclose(array(prg->prog_fds, i, FILE*));
   #else
                           e_pclose((int) array(prg->prog_fds, i, intptr_t));
   #endif
                         array_Del(prg->prog_fds, i, 0);                          array_Del(prg->prog_fds, i, 0);
                           clrbit(prg->prog_used, i);
                         prg->prog_cnum--;                          prg->prog_cnum--;
                         ret++;                          ret++;
                 }                  }
Line 109  io_progClose(prog_t * __restrict prg, u_int closeNum) Line 125  io_progClose(prog_t * __restrict prg, u_int closeNum)
 int  int
 io_progOpen(prog_t * __restrict prg, u_int execNum)  io_progOpen(prog_t * __restrict prg, u_int execNum)
 {  {
   #ifdef POPEN_STREAM
         FILE *f;          FILE *f;
   #else
           int f;
   #endif
         int stat, ret = 0;          int stat, ret = 0;
         register int i;          register int i;
         pid_t pid;          pid_t pid;
   
         if (!prg)          if (!prg)
                 return 0;                  return 0;
        if (execNum > prg->prog_maxn) {        if (prg->prog_cnum + execNum > prg->prog_maxn) {
                 io_SetErr(EINVAL, "Requested number for program execution is over pool's limit");                  io_SetErr(EINVAL, "Requested number for program execution is over pool's limit");
                 return 0;                  return 0;
         }          }
Line 125  io_progOpen(prog_t * __restrict prg, u_int execNum) Line 145  io_progOpen(prog_t * __restrict prg, u_int execNum)
         for (i = 0; (execNum ? ret < execNum : 42) && i < array_Size(prg->prog_fds); i++)          for (i = 0; (execNum ? ret < execNum : 42) && i < array_Size(prg->prog_fds); i++)
                 if (!array_Get(prg->prog_fds, i)) {                  if (!array_Get(prg->prog_fds, i)) {
                         f = e_popen(prg->prog_name, "r+", &pid);                          f = e_popen(prg->prog_name, "r+", &pid);
   #ifdef POPEN_STREAM
                         if (!f) {                          if (!f) {
   #else
                           if (f == -1) {
   #endif
                                 LOGERR;                                  LOGERR;
                                 ret = -1;                                  ret = -1;
                                 break;                                  break;
Line 145  io_progOpen(prog_t * __restrict prg, u_int execNum) Line 169  io_progOpen(prog_t * __restrict prg, u_int execNum)
 }  }
   
 /*  /*
    * io_progGrow() - Execute to number of programs in pool
    *
    * @prg = program pool
    * @toNum = execute to number of programs (0 max)
    * return: 0 error, >0 executed programs and abs(<0) executed programs with logged error
    */
   int
   io_progGrow(prog_t * __restrict prg, u_int toNum)
   {
           if (!prg)
                   return 0;
           if (toNum > prg->prog_maxn) {
                   io_SetErr(EINVAL, "Requested number for program execution is over pool's limit");
                   return 0;
           }
           if (!toNum)
                   toNum = prg->prog_maxn;
   
           return io_progOpen(prg, toNum - prg->prog_cnum);
   }
   
   /*
  * io_progVacuum() - Vacuum pool to running number of programs   * io_progVacuum() - Vacuum pool to running number of programs
  *   *
  * @prg = program pool   * @prg = program pool
Line 168  io_progVacuum(prog_t * __restrict prg, u_int toNum) Line 214  io_progVacuum(prog_t * __restrict prg, u_int toNum)
   
         pthread_mutex_lock(&prg->prog_mtx);          pthread_mutex_lock(&prg->prog_mtx);
         for (i = array_Size(prg->prog_fds) - 1; prg->prog_cnum > toNum && i > -1; i--)          for (i = array_Size(prg->prog_fds) - 1; prg->prog_cnum > toNum && i > -1; i--)
                if (array_Get(prg->prog_fds, i)) {                if (array_Get(prg->prog_fds, i) && isclr(prg->prog_used, i)) {
 #ifdef POPEN_STREAM
                         e_pclose(array(prg->prog_fds, i, FILE*));                          e_pclose(array(prg->prog_fds, i, FILE*));
   #else
                           e_pclose((int) array(prg->prog_fds, i, intptr_t));
   #endif
                         array_Del(prg->prog_fds, i, 0);                          array_Del(prg->prog_fds, i, 0);
                         prg->prog_cnum--;                          prg->prog_cnum--;
                         ret++;                          ret++;
Line 195  io_progCheck(prog_t * __restrict prg) Line 245  io_progCheck(prog_t * __restrict prg)
         if (!prg)          if (!prg)
                 return -1;                  return -1;
   
           pthread_mutex_lock(&prg->prog_mtx);
         for (i = 0; i < array_Size(prg->prog_fds); i++)          for (i = 0; i < array_Size(prg->prog_fds); i++)
                 if (array_Get(prg->prog_fds, i) &&                   if (array_Get(prg->prog_fds, i) && 
   #ifdef POPEN_STREAM
                                 (p = pio_pgetpid(array(prg->prog_fds, i, FILE*))))                                  (p = pio_pgetpid(array(prg->prog_fds, i, FILE*))))
                        if (waitpid(p->pid, &p->stat, WNOHANG) > 0)#else
                                 (p = pio_pgetpid((int) array(prg->prog_fds, i, intptr_t))))
 #endif
                         if (waitpid(p->pid, &p->stat, WNOHANG) > 0) {
                                 clrbit(prg->prog_used, i);
                                 ret++;                                  ret++;
                           }
           pthread_mutex_unlock(&prg->prog_mtx);
   
         return ret;          return ret;
   }
   
   /*
    * io_progAttach() - Attach to open program
    *
    * @prg = program pool
    * return: NULL error or !=NULL attached program handle
    */
   #ifdef POPEN_STREAM
   FILE *
   #else
   int
   #endif
   io_progAttach(prog_t * __restrict prg)
   {
   #ifdef POPEN_STREAM
           FILE *f = NULL;
   #else
           int f = -1;
   #endif
           register int i;
   
           if (!prg)
   #ifdef POPEN_STREAM
                   return NULL;
   #else
                   return -1;
   #endif
   
           pthread_mutex_lock(&prg->prog_mtx);
           for (i = 0; i < array_Size(prg->prog_fds); i++)
                   if (array_Get(prg->prog_fds, i) && isclr(prg->prog_used, i)) {
                           setbit(prg->prog_used, i);
   #ifdef POPEN_STREAM
                           f = array(prg->prog_fds, i, FILE*);
   #else
                           f = array(prg->prog_fds, i, intptr_t);
   #endif
                           break;
                   }
           pthread_mutex_unlock(&prg->prog_mtx);
   
           return f;
   }
   
   /*
    * io_progDetach() - Detch from open program
    *
    * @prg= program pool
    * @pfd = attached program handle
    * return: none
    */
   void
   #ifdef POPEN_STREAM
   io_progDetach(prog_t * __restrict prg, FILE *pfd)
   #else
   io_progDetach(prog_t * __restrict prg, int pfd)
   #endif
   {
           register int i;
   
           if (!prg || !pfd)
                   return;
   
           pthread_mutex_lock(&prg->prog_mtx);
           for (i = 0; i < array_Size(prg->prog_fds); i++)
   #ifdef POPEN_STREAM
                   if (array(prg->prog_fds, i, FILE*) == pfd) {
   #else
                   if (array(prg->prog_fds, i, intptr_t) == pfd) {
   #endif
                           clrbit(prg->prog_used, i);
                           break;
                   }
           pthread_mutex_unlock(&prg->prog_mtx);
 }  }

Removed from v.1.1.2.7  
changed lines
  Added in v.1.1.2.12


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>