Ejemplo n.º 1
0
void thr_join(){
  Pthread_mutex_lock(&m);
  while (done==0)
    Pthread_cond_wait(&c,&m);
  Pthread_mutex_unlock(&m);

}
Ejemplo n.º 2
0
int threadpool_task_over(threadpool_t *pool, int block, int timeout) {
    int ret;

    Pthread_mutex_lock(&pool->mutex);
    if (pool->task_queue.len != 0) {
        if (!block) {
            Pthread_mutex_unlock(&pool->mutex);
            return -1;
        } else {
            struct timespec ts;
            struct timeval  tv;
            gettimeofday(&tv, NULL);
            ts.tv_sec = tv.tv_sec + timeout;
            ts.tv_nsec = tv.tv_usec * 1000;

            while (pool->task_queue.len != 0) {
                if (timeout == 0) {
                    Pthread_cond_wait(&pool->task_over_cond, &pool->mutex);
                } else {
                    ret = Pthread_cond_timedwait(&pool->task_over_cond, 
                        &pool->mutex, &ts);
                    if (ret == 0) {
                        Pthread_mutex_unlock(&pool->mutex);
                        return 0;
                    } else if (ret == ETIMEDOUT) {
                        Pthread_mutex_unlock(&pool->mutex);
                        return -1;
                    }
                }
            }
        }
    }
    Pthread_mutex_unlock(&pool->mutex);
    return 0;
}
Ejemplo n.º 3
0
void *
consume(void *arg)
{
	for ( ; ; ) {
		Pthread_mutex_lock(&nready.mutex);
		if (nready.nconsumer > 0) {
			Pthread_mutex_unlock(&nready.mutex);
			continue;
		} else if (nready.nget >= nitems) {
			Pthread_mutex_unlock(&nready.mutex);
			return(NULL);	
		} else {
			nready.nconsumer++;
			while (nready.nready == 0)
				Pthread_cond_wait(&nready.cond, &nready.mutex);
				nready.nready--;
				nready.nget++;
				nready.nconsumer--;
				Pthread_mutex_unlock(&nready.mutex);

				// if (buff[i] != i)
				// 	printf("buff[%d] = %d\n", i, buff[i]);
			
				*((int *) arg) += 1;
			}
	}
	
	return(NULL);
}
Ejemplo n.º 4
0
void *
Consumer(void *v)
{
	struct thread_data *data = (struct thread_data *)v;
	unsigned sum = 0;
	int n, item;
	int waits = 0;
	int l = maxbuflen - 1;
	
	for (n = 0; ; ++n) {
		Pthread_mutex_lock(&buf_mutex);
		while (buflen == 0) {
			if (eof) {
				Pthread_mutex_unlock(&buf_mutex);
				goto done;
			}
			++waits;
			Pthread_cond_wait(&empty_cond, &buf_mutex);
		}
		item = buf[--buflen];
		if (buflen == l)
			Pthread_cond_broadcast(&full_cond);
		Pthread_mutex_unlock(&buf_mutex);

		ConsumeItem(item, &sum);
	}

done:
	data->sum = sum;
	printf("Consumer %d exit, %d items received, %d waits\n", 
		data->no, n, waits);
	return data;
}
Ejemplo n.º 5
0
void *
Producer(void *v)
{
	struct thread_data *data = (struct thread_data *)v;
	unsigned seed = data->seed;
	int n;
	unsigned item, sum;
	int waits = 0;

	data->sum = 0;
	for (n = 0; n < num_items; ++n) {
		pthread_testcancel();

		sum = 0;
		item = ProduceItem(&seed, &sum);
	
		pthread_cleanup_push(pthread_mutex_unlock, &buf_mutex);

		Pthread_mutex_lock(&buf_mutex);
		while (buflen == maxbuflen) {
			++waits;
			Pthread_cond_wait(&full_cond, &buf_mutex);
		}
		buf[buflen++] = item;
		data->sum += sum;
		if (buflen == 1)
			Pthread_cond_broadcast(&empty_cond);

		pthread_cleanup_pop(1);
	}

	printf("Producer %d exit, %d waits\n", data->no, waits);
	return data;
}
Ejemplo n.º 6
0
int crew_start(crew_p crew,char* path,char* string){
    Pthread_mutex_lock(&crew->mutex);
    while(crew->work_cnt>0){
        Pthread_cond_wait(&crew->done,&crew->mutex);
    }
    work_p work = (work_p)malloc(sizeof(work));
    errno =0;
    name_max = pathconf(path,_PC_NAME_MAX);
    if((int)name_max==-1){
        if(errno==0)
            name_max = 2560;
        else{
            printf("%s\n",strerror(errno));
            exit(-1);
        }
    }
    path_max = pathconf(path,_PC_PATH_MAX);
    if((int)path_max==-1){
        if(errno==0)
            path_max=102400;
        else{
            printf("%s\n",strerror(errno));
            exit(-1);
        }
    }
    path_max++;
    name_max++;

    work->string =string;
    work->path = (char*)malloc(path_max);
    strcpy(work->path,path);
    work->next = NULL;
    if(crew->first_work==NULL){
        crew->first_work = work;
        crew->last_work = work;
    }else{
        crew->last_work->next = work;
        crew->last_work = work;
    }
    crew->work_cnt ++;
    Pthread_cond_signal(&crew->go);
    while(crew->work_cnt>0){
        Pthread_cond_wait(&crew->done,&crew->mutex);
    }
    return 0;
}
Ejemplo n.º 7
0
void *
Producer(void *v)
{
	struct producer *p = (struct producer *)v;
	struct consumer *c;
	struct packet *pa, *old;
	unsigned seed = p->seed;
	int n, k, cons;
	unsigned sum = 0;
	int waits = 0;
	int more = 1;
	int item_cnt = 0;

	cons = seed % num_consumers;
	for (n = 0; more; ++n) {

		/* Hole Packet aus lokaler emptyPacketQueue */
		Pthread_mutex_lock(&p->queue_mutex);
		while (!p->emptyPacketQueue) {
			++waits;
			Pthread_cond_wait(&p->empty_cond, &p->queue_mutex);
		}
		pa = p->emptyPacketQueue;
		p->emptyPacketQueue = pa->next;
		Pthread_mutex_unlock(&p->queue_mutex);

		/* Fuelle Packet */
		for (k = 0; k < buflen; ++k) {
			pa->buf[k] = ProduceItem(&seed, &sum);
			if (++item_cnt == num_items) {
				more = 0;
				++k;
				break;
			}
		}
		pa->len = k;

		/* Versende Packet an Consumer cons */
		c = consumers + cons;
		Pthread_mutex_lock(&c->queue_mutex);
		old = pa->next = c->fullPacketQueue;
		c->fullPacketQueue = pa;
		Pthread_mutex_unlock(&c->queue_mutex);
		if (!old)
			Pthread_cond_broadcast(&c->empty_cond);

		if (++cons == num_consumers)
			cons = 0;
	}

	p->sum = sum;
	printf("Producer %d exit, %d waits, %d packets\n", p->no, waits, n);
	return NULL;
}
Ejemplo n.º 8
0
void *
thread_main_dns(void *arg)
{
	int dnsrequest(const char *name, uint32 *ipaddr);
	int connfd, *number, count, total;
	char  *domain ;
	uint32 *ipptr;
	pthread_mutex_t *mutex;
	printf("dns thread %d starting \n", (int)arg );

	for(;;){

		Pthread_mutex_lock(&dns_array_mutex);

		while(iget == iput)
			Pthread_cond_wait(&dns_array_cond,
					&dns_array_mutex);
		
		printf("thread %d  working \n", pthread_self());

		connfd	= dns_array[iget].sockfd;
		domain  = dns_array[iget].domain;
		number	= dns_array[iget].number;
 		count	= dns_array[iget].count;
		total	= dns_array[iget].total;
		ipptr	= dns_array[iget].ipptr;
		mutex	= dns_array[iget].mutex;
		
		if(++iget == ARRAYSIZE)
			iget = 0;		

		Pthread_mutex_unlock(&dns_array_mutex);

		//do our job 
		if(dnsrequest(domain, ipptr + count) != 0) 
			inet_pton(AF_INET, "127.0.0.1", ipptr+count);
		dc_free(domain);	//dc_free the memory of domain
		Pthread_mutex_lock(mutex);
		(*number)--;
		Pthread_mutex_unlock(mutex);
		if((*number) == 0){
			write(connfd, ipptr, total*sizeof(uint32));
			printf("sended\n");
	//		close(connfd);	//todo
			dc_free(ipptr);
			dc_free(number);
			dc_free(mutex);
		} 


	}

}
Ejemplo n.º 9
0
int threadpool_destroy(threadpool_t *pool, int block, int timeout) {
    int ret;
    assert(pool);
    Pthread_mutex_lock(&pool->mutex);
    if (!pool->exit) {
        /* you should call `threadpool_exit' first */
        Pthread_mutex_unlock(&pool->mutex);
        return -1;
    }

    if (pool->threads_num != 0) {
        if (!block) {
            Pthread_mutex_unlock(&pool->mutex);
            return -1;
        } else {
            struct timespec ts;
            struct timeval  tv;
            gettimeofday(&tv, NULL);
            ts.tv_sec = tv.tv_sec + timeout;
            ts.tv_nsec = tv.tv_usec * 1000;

            while (pool->threads_num != 0) {
                if (timeout == 0) {
                    Pthread_cond_wait(&pool->exit_cond, &pool->mutex);
                    goto CONT;
                } else {
                    ret = Pthread_cond_timedwait(&pool->exit_cond, 
                        &pool->mutex, &ts);
                    if (ret == 0) {
                        goto CONT;
                    } else if (ret == ETIMEDOUT) {
                        Pthread_mutex_unlock(&pool->mutex);
                        return -1;
                    }
                }
            }
        }
    }
 
CONT:
    Pthread_mutex_unlock(&pool->mutex);
    heap_destroy(&pool->task_queue);
    Pthread_mutex_destroy(&pool->mutex);
    Pthread_cond_destroy(&pool->cond);
    Pthread_cond_destroy(&pool->exit_cond);
    Pthread_cond_destroy(&pool->task_over_cond);
    free(pool);
    return 0;
}
Ejemplo n.º 10
0
void *consume(void *arg)
{
	int i;
	for(i = 0; i < nitems; i++){
		Pthread_mutex_lock(&nready.mutex);
		while(nready.nready == 0)
			Pthread_cond_wait(&nready.cond, &nready.mutex);/* wait for producer */
		nready.nready--;
		Pthread_mutex_unlock(&nready.mutex);

		if(buff[i] != i)
			printf("buff[%d] = %d\n", i, buff[i]);
	}
	return (NULL);
}
Ejemplo n.º 11
0
void *
Consumer(void *v)
{
	struct consumer *c = (struct consumer *)v;
	struct producer *p;
	struct packet *pa, *old;
	unsigned sum = 0;
	int n, k;
	int waits = 0;
	int item_cnt = 0;
	
	for (n = 0; ; ++n) {
		/* Hole Packet aus lokaler fullPacketQueue */
		Pthread_mutex_lock(&c->queue_mutex);
		while (!c->fullPacketQueue) {
			if (c->eof) {
				Pthread_mutex_unlock(&c->queue_mutex);
				goto done;
			}
			++waits;
			Pthread_cond_wait(&c->empty_cond, &c->queue_mutex);
		}
		pa = c->fullPacketQueue;
		c->fullPacketQueue = pa->next;
		Pthread_mutex_unlock(&c->queue_mutex);

		/* Verarbeite Packet */
		for (k = 0; k < pa->len; ++k)
			ConsumeItem(pa->buf[k], &sum);
		item_cnt += pa->len;
		
		/* Stelle Packet zurueck in emptyPacketQueue des Owners */
		p = producers + pa->owner;
		Pthread_mutex_lock(&p->queue_mutex);
		old = pa->next = p->emptyPacketQueue;
		p->emptyPacketQueue = pa;
		Pthread_mutex_unlock(&p->queue_mutex);
		if (!old)
			Pthread_cond_broadcast(&p->empty_cond);
	}

done:
	c->sum = sum;
	printf("Consumer %d exit, %d items in %d packets received, %d waits\n", 
		c->no, item_cnt, n, waits);
	return NULL;
}
Ejemplo n.º 12
0
void produce(struct buf_t* bptr, int val)
{
    Pthread_mutex_lock(&bptr->b_mutex);
    /* Wait if buffer is full */
    while (bptr->b_nitems >= BUFFSIZE)
        Pthread_cond_wait(&bptr->b_cond_producer, &bptr->b_mutex);

    /* There is room, store the new value */
    printf("produce %d\n", val);
    bptr->b_buf[bptr->b_nextput] = val;
    if (++bptr->b_nextput >= BUFFSIZE) bptr->b_nextput = 0;
    bptr->b_nitems++;

    /* Signal consumer */
    Pthread_cond_signal(&bptr->b_cond_consumer);
    Pthread_mutex_unlock(&bptr->b_mutex);
}
Ejemplo n.º 13
0
void *comsume(void *arg)
{
  int i;
  
  for(i = 0; i < nitems; ++i){
    /*消费者等待变为非0*/
    Pthread_mutex_lock(&nready.mutex);
    while(nready.nready == 0)/*如果计数器=0*/
      Pthread_cond_wait(&nready.cond, &nready.mutex);/*消费者线程等待*/
    nready.nready--;
    Pthread_mutex_unlock(&nready.mutex);

    if(buff[i] != i)
      printf("buff[%d] = %d\n", i, buff[i]);
  }
  return(NULL);
}
Ejemplo n.º 14
0
void* thread_main(void* arg)
{
    int connfd;
    void web_child(int);

    printf("thread %d starting\n", (int)arg);
    for (;;) {
        Pthread_mutex_lock(&clifd_mutex);
        while (iget == iput) Pthread_cond_wait(&clifd_cond, &clifd_mutex);
        connfd = clifd[iget]; /* connected socket to service */
        if (++iget == MAXNCLI) iget = 0;
        Pthread_mutex_unlock(&clifd_mutex);
        tptr[(int)arg].thread_count++;

        web_child(connfd); /* process request */
        Close(connfd);
    }
}
Ejemplo n.º 15
0
int consume(struct buf_t* bptr)
{
    int val;

    Pthread_mutex_lock(&bptr->b_mutex);
    /* Wait if buffer is empty */
    while (bptr->b_nitems <= 0)
        Pthread_cond_wait(&bptr->b_cond_consumer, &bptr->b_mutex);

    /* There is data, fetch the value */
    val = bptr->b_buf[bptr->b_nextget];
    printf("consume %d\n", val);
    if (++bptr->b_nextget >= BUFFSIZE) bptr->b_nextget = 0;
    bptr->b_nitems--;

    /* Signal producer; it might be waiting for space to store */
    Pthread_cond_signal(&bptr->b_cond_producer);
    Pthread_mutex_unlock(&bptr->b_mutex);

    return (val);
}
Ejemplo n.º 16
0
int
main(int argc, char **argv)
{
  int     i, maxnconn;
  pthread_t tid;
  struct file *fptr;

  if (argc < 5)
    err_quit("usage: web <#conns> <IPaddr> <homepage> file1 ...");
  maxnconn = atoi(argv[1]);

  nfiles = min(argc - 4, MAXFILES);
  for (i = 0; i < nfiles; i++) {
    file[i].f_name = argv[i + 4];
    file[i].f_host = argv[2];
    file[i].f_flags = 0;
  }
  printf("nfiles = %d\n", nfiles);

  home_page(argv[2], argv[3]);

  nlefttoread = nlefttoconn = nfiles;
  nconn = 0;
/* include web2 */
  while (nlefttoread > 0) {
    while (nconn < maxnconn && nlefttoconn > 0) {
        /* 4find a file to read */
      for (i = 0 ; i < nfiles; i++)
        if (file[i].f_flags == 0)
          break;
      if (i == nfiles)
        err_quit("nlefttoconn = %d but nothing found", nlefttoconn);

      file[i].f_flags = F_CONNECTING;
      Pthread_create(&tid, NULL, &do_get_read, &file[i]);
      file[i].f_tid = tid;
      nconn++;
      nlefttoconn--;
    }

      /* 4Wait for thread to terminate */
    Pthread_mutex_lock(&ndone_mutex);
    while (ndone == 0)
      Pthread_cond_wait(&ndone_cond, &ndone_mutex);

    for (i = 0; i < nfiles; i++) {
      if (file[i].f_flags & F_DONE) {
        Pthread_join(file[i].f_tid, (void **) &fptr);

        if (&file[i] != fptr)
          err_quit("file[i] != fptr");
        fptr->f_flags = F_JOINED; /* clears F_DONE */
        ndone--;
        nconn--;
        nlefttoread--;
        printf("thread %d for %s done\n", fptr->f_tid, fptr->f_name);
      }
    }
    Pthread_mutex_unlock(&ndone_mutex);
  }

  exit(0);
}
int main(int argc, char *argv[])
{
    int i, maxconn, nfiles, nlefttoread, nlefttoconn, nconn;
    char *pchServaddr, *pchWeb, *pchHomepage;
    struct file *pf;
    pthread_t tid;

    if (argc < 5)
        err_quit("Usage: %s <#conns> <webName> <homepage> file1 ...", basename(argv[0]));
    maxconn = atoi(argv[1]);
    pchWeb = argv[2];
    pchHomepage = argv[3];
    nfiles = nlefttoread = nlefttoconn = argc-4;
    nconn = 0;

    thread_home_page(pchWeb, pchHomepage, &pchServaddr);
    /* don't forget free(pchServaddr); */

#ifdef _TEST
    char buff[MAXLINE];
    if (inet_ntop(AF_INET, pchServaddr, buff, sizeof(buff)) == NULL)
        err_sys("inet_ntop error");
    printf("web address is %s\n", buff);

    printf("\nnfiles = %d\n", nfiles);

    for (i = 0; i < nfiles; i++) {
        file[i].f_pchName = argv[i+4];
        file[i].f_pchHost = pchServaddr;
        file[i].f_iFlags = 0;
        Pthread_create(&tid, NULL, thread_do_get_read, &file[i]);
    }

    /* wait for the thread exit */
    printf("sleeping for wait thread\n");
    sleep(10);

#else
    printf("\nnfiles = %d\n", nfiles);
    for (i = 0; i < nfiles; i++) {
        file[i].f_pchName = argv[i+4];
        file[i].f_pchHost = pchServaddr;
        file[i].f_iFlags = 0;
    }

    while (nlefttoread > 0) {
        while (nconn < maxconn && nlefttoconn > 0) {
            /* find a file to read */
            for (i = 0; i < nfiles; i++)
                if (file[i].f_iFlags == 0)
                    break;
            assert(i < nfiles);
            file[i].f_iFlags = F_CONNECTING;
            Pthread_create(&tid, NULL, thread_do_get_read, &file[i]);
            nconn++;
            nlefttoconn--;
        }
        /* wait for any thread exit */
        Pthread_mutex_lock(&ndone_mutex);
        while (ndone == 0)
            Pthread_cond_wait(&ndone_cond, &ndone_mutex);
        for (i = 0; i < nfiles; i++) {
            if (file[i].f_iFlags == F_DONE) {
                Pthread_join(file[i].f_thrId, (void **)&pf);
                assert(&file[i] == pf);
                pf->f_iFlags = F_JOINED;
                ndone--;
                nconn--;
                nlefttoread--;
                printf("thread %u for %s done\n", pf->f_thrId, pf->f_pchName);
            }
        }
        Pthread_mutex_unlock(&ndone_mutex);
    }
#endif

    free(pchServaddr);

    return 0;
}
Ejemplo n.º 18
0
void* work_loop(void* args){

    worker_p mine = (worker_p)args;
    crew_p crew = mine->crew;
    struct stat file_stat;
    struct dirent *entry;
    work_p  work;
    entry = (struct dirent*)malloc(sizeof(struct dirent)+name_max);
    Pthread_mutex_lock(&crew->mutex);
    while(crew->work_cnt==0){
        Pthread_cond_wait(&crew->go,&crew->mutex);
    }
    Pthread_mutex_unlock(&crew->mutex);
    while(1){
        Pthread_mutex_lock(&crew->mutex);
        while(crew->first_work==NULL)
            Pthread_cond_wait(&crew->go,&crew->mutex);
        printf("woker %d works on :#%lx ,conunt is %d\n", mine->index,crew->first_work,crew->work_cnt);
        work = crew->first_work;
        crew->first_work = work->next;
        if(crew->first_work==NULL)
            crew->last_work = NULL;
        printf("woker %d took #%lx ,leaave first is #%lx last is #%lx\n", mine->index,work,crew->first_work,crew->last_work);
        Pthread_mutex_unlock(&crew->mutex);

        if(lstat(work->path,&file_stat)<0){
            printf("%s error: %s\n",work->path,strerror(errno));
        }
        if(S_ISLNK(file_stat.st_mode)){
            printf("%s is a link\n", work->path);
        }
        else if (S_ISDIR(file_stat.st_mode)){
            DIR* directory = opendir(work->path);
            struct dirent* result;
            if(directory==NULL){
                fprintf(stderr, "open %s errro:%s\n", work->path,strerror(errno));
                continue;
            }
            while(1){
                int status = readdir_r(directory,entry,&result);
                if(status!=0){
                    fprintf(stderr, "unable to open %s,error:%s\n", work->path,strerror(status));
                    break;
                }
                if(result==NULL)
                    break;
                if(strcmp(entry->d_name,".")==0||strcmp(entry->d_name,"..")==0)
                    continue;
                work_p new_work= (work_p)malloc(sizeof(work_t));
                new_work->string = work->string;
                new_work->path = (char*) malloc(path_max);
                strcpy(new_work->path,work->path);
                strcat(new_work->path,"/");
                strcat(new_work->path,entry->d_name);
                new_work->next= NULL;
                Pthread_mutex_lock(&crew->mutex);
                if(crew->first_work==NULL){
                    crew->first_work=new_work;
                    crew->last_work = new_work;
                }else{
                    crew->last_work->next = new_work;
                    crew->last_work = new_work;
                }
                crew->work_cnt++;
                Pthread_cond_signal(&crew->go);
                Pthread_mutex_unlock(&crew->mutex);
            }
            closedir(directory);
        }
        else if (S_ISREG(file_stat.st_mode)){
            FILE* file;
            char buf[MAXLINE],*fbuf;
            if((file = fopen(work->path,"r"))==NULL){
                fprintf(stderr, "can not open %s:%s\n", work->path,strerror(errno));
                continue;
            }
            else{
                while(1){
                   if((fbuf= fgets(buf,MAXLINE,file))==NULL){
                        if(feof(file))
                            break;
                        else if(ferror(file)){
                            fprintf(stderr, "read %s error:%s\n", work->path,strerror(errno));
                            break;
                        }
                   }
                   if(strstr(buf,work->string)!=NULL){
                        printf("found %s in %s\n",work->string,work->path);
                        break;
                   }
                }
                fclose(file);

            }
        }
        else{
            fprintf(stderr, "%s's type is %s\n", work->path,S_ISFIFO(file_stat.st_mode)?"FIFO":
                                                            S_ISCHR(file_stat.st_mode)?"CHAR":
                                                            S_ISBLK(file_stat.st_mode)?"BLK":
                                                            S_ISSOCK(file_stat.st_mode)?"SOCK":"unknown");

        }
        free(work->path);
        free(work);
        Pthread_mutex_lock(&crew->mutex);
        crew->work_cnt--;
        if(crew->work_cnt<=0){
            Pthread_cond_broadcast(&crew->done);
            Pthread_mutex_unlock(&crew->mutex);
            break;
        }
        Pthread_mutex_unlock(&crew->mutex);

    }
    free(entry);
    return NULL;
}
Ejemplo n.º 19
0
void *
cerebrod_event_queue_monitor(void *arg)
{
  List temp_event_queue;

  _event_queue_monitor_initialize();

  /* Don't bother if there isn't an event queue (i.e. no event modules) */
  if (!event_queue)
    return NULL;

  temp_event_queue = List_create((ListDelF)cerebrod_event_to_send_destroy);

  /* 
   * achu: The listener and thus event update initialization is
   * started after this thread is started.  So the and event_index may
   * not be set up the first time this loop is reached.  
   *
   * However, it must be set after the condition is signaled, b/c the
   * listener (and thus event update code) and event node timeout
   * thread begin after the listener is setup.
   *
   * Thus, we put the event_queue assert inside the loop.
   */
  for (;;)
    {
      struct cerebrod_event_to_send *ets;
      ListIterator eitr;
      ListIterator titr;

      Pthread_mutex_lock(&event_queue_lock);
      assert(event_queue);
      while (list_count(event_queue) == 0)
        Pthread_cond_wait(&event_queue_cond, &event_queue_lock);

      /* Debug dumping in the below loop can race with the debug
       * dumping from the listener, b/c of racing on the
       * event_queue_lock.  To avoid this race, we copy the data off
       * the event_queue, so the event_queue_lock can be freed up.
       */

      eitr = List_iterator_create(event_queue);
      while ((ets = list_next(eitr)))
        {
          List_append(temp_event_queue, ets);
          List_remove(eitr);
        }
      List_iterator_destroy(eitr);
      Pthread_mutex_unlock(&event_queue_lock);

      titr = List_iterator_create(temp_event_queue);
      while ((ets = list_next(titr)))
        {
          List connections;

          _event_dump(ets->event);
          
          Pthread_mutex_lock(&event_connections_lock);
          if ((connections = Hash_find(event_connections_index, ets->event_name)))
            {
              char buf[CEREBRO_MAX_PACKET_LEN];
              int elen;

              if ((elen = _event_marshall(ets->event, 
                                          buf, 
                                          CEREBRO_MAX_PACKET_LEN)) > 0)
                {
                  ListIterator citr;
                  int *fd;

                  citr = List_iterator_create(connections);
                  while ((fd = list_next(citr)))
                    {
                      if (fd_write_n(*fd, buf, elen) < 0)
                        {
                          CEREBROD_DBG(("fd_write_n: %s", strerror(errno)));
                          if (errno == EPIPE
                              || errno == EINVAL
                              || errno == EBADF
                              || errno == ENODEV
                              || errno == ENETDOWN
                              || errno == ENETUNREACH)
                            {
                              if (conf.event_server_debug)
                                {
                                  Pthread_mutex_lock(&debug_output_mutex);
                                  fprintf(stderr, "**************************************\n");
                                  fprintf(stderr, "* Event Connection Died: errno = %d\n", errno);
                                  fprintf(stderr, "**************************************\n");
                                  Pthread_mutex_unlock(&debug_output_mutex);
                                }
                              List_delete(citr);
                            }
                          continue;
                        }
                    }
                  List_iterator_destroy(citr);
                }
            }
          Pthread_mutex_unlock(&event_connections_lock);

          List_delete(titr);
        }
      
      List_iterator_destroy(titr);
    }

  List_destroy(temp_event_queue);

  return NULL;			/* NOT REACHED */
}
Ejemplo n.º 20
0
int 
main(int argc, char **argv)
{
  cerebro_err_init(argv[0]);
  cerebro_err_set_flags(CEREBRO_ERROR_STDERR | CEREBRO_ERROR_SYSLOG);

  cerebrod_config_setup(argc, argv);

#if CEREBRO_DEBUG
  if (!conf.debug)
    {
      cerebrod_daemon_init();
      cerebro_err_set_flags(CEREBRO_ERROR_SYSLOG);
    }
  else
    cerebro_err_set_flags(CEREBRO_ERROR_STDERR);
#else  /* !CEREBRO_DEBUG */
  cerebrod_daemon_init();
  cerebro_err_set_flags(CEREBRO_ERROR_SYSLOG);
#endif /* !CEREBRO_DEBUG */

  /* Call after daemonization, since daemonization closes currently
   * open fds 
   */
  openlog(argv[0], LOG_ODELAY | LOG_PID, LOG_DAEMON);

#if !WITH_CEREBROD_SPEAKER_ONLY

  /* Start metric server before the listener begins receiving data. */
  if (conf.metric_server)
    {
      pthread_t thread;
      pthread_attr_t attr;

      Pthread_attr_init(&attr);
      Pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
      Pthread_attr_setstacksize(&attr, CEREBROD_THREAD_STACKSIZE);
      Pthread_create(&thread, &attr, cerebrod_metric_server, NULL);
      Pthread_attr_destroy(&attr);

      /* Wait for initialization to complete */
      Pthread_mutex_lock(&metric_server_init_lock);
      while (!metric_server_init)
        Pthread_cond_wait(&metric_server_init_cond, &metric_server_init_lock);
      Pthread_mutex_unlock(&metric_server_init_lock);
    }

  /* Start listening server before speaker so that listener
   * can receive packets from a later created speaker
   */
  if (conf.listen)
    {
      int i;

      for (i = 0; i < conf.listen_threads; i++)
        {
          pthread_t thread;
          pthread_attr_t attr;

          Pthread_attr_init(&attr);
          Pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
          Pthread_attr_setstacksize(&attr, CEREBROD_THREAD_STACKSIZE);
          Pthread_create(&thread, &attr, cerebrod_listener, NULL);
          Pthread_attr_destroy(&attr);
        }

      /* Wait for initialization to complete */
      Pthread_mutex_lock(&listener_init_lock);
      while (!listener_init)
        Pthread_cond_wait(&listener_init_cond, &listener_init_lock);
      Pthread_mutex_unlock(&listener_init_lock);
    }

  /* Start all the event server, queue monitor, and node timeout
   * threads after the listener thread, since they use data created by
   * the listener thread.
   */
  if (conf.event_server)
    {
      pthread_t thread;
      pthread_attr_t attr;

      Pthread_attr_init(&attr);
      Pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
      Pthread_attr_setstacksize(&attr, CEREBROD_THREAD_STACKSIZE);
      Pthread_create(&thread, &attr, cerebrod_event_queue_monitor, NULL);
      Pthread_attr_destroy(&attr);

      /* Wait for initialization to complete */
      Pthread_mutex_lock(&event_queue_monitor_init_lock);
      while (!event_queue_monitor_init)
        Pthread_cond_wait(&event_queue_monitor_init_cond, &event_queue_monitor_init_lock);
      Pthread_mutex_unlock(&event_queue_monitor_init_lock);

      Pthread_attr_init(&attr);
      Pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
      Pthread_attr_setstacksize(&attr, CEREBROD_THREAD_STACKSIZE);
      Pthread_create(&thread, &attr, cerebrod_event_server, NULL);
      Pthread_attr_destroy(&attr);

      /* Wait for initialization to complete */
      Pthread_mutex_lock(&event_server_init_lock);
      while (!event_server_init)
        Pthread_cond_wait(&event_server_init_cond, &event_server_init_lock);
      Pthread_mutex_unlock(&event_server_init_lock);

      Pthread_attr_init(&attr);
      Pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
      Pthread_attr_setstacksize(&attr, CEREBROD_THREAD_STACKSIZE);
      Pthread_create(&thread, &attr, cerebrod_event_node_timeout_monitor, NULL);
      Pthread_attr_destroy(&attr);

      /* Wait for initialization to complete */
      Pthread_mutex_lock(&event_node_timeout_monitor_init_lock);
      while (!event_node_timeout_monitor_init)
        Pthread_cond_wait(&event_node_timeout_monitor_init_cond, 
                          &event_node_timeout_monitor_init_lock);
      Pthread_mutex_unlock(&event_node_timeout_monitor_init_lock);
    }

  /* Start metric controller - see comments at speaker below */ 
  if (conf.metric_controller)
    {
      pthread_t thread;
      pthread_attr_t attr;

      Pthread_attr_init(&attr);
      Pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
      Pthread_attr_setstacksize(&attr, CEREBROD_THREAD_STACKSIZE);
      Pthread_create(&thread, &attr, cerebrod_metric_controller, NULL);
      Pthread_attr_destroy(&attr);

      /* Wait for initialization to complete */
      Pthread_mutex_lock(&metric_controller_init_lock);
      while (!metric_controller_init)
        Pthread_cond_wait(&metric_controller_init_cond, 
                          &metric_controller_init_lock);
      Pthread_mutex_unlock(&metric_controller_init_lock);
    }

#endif /* !WITH_CEREBROD_SPEAKER_ONLY */

  /* Start speaker  
   *
   * It may make more logical sense to start the metric controller
   * after the speaker since metric data cannot be propogated until
   * after the speaker has finished being setup.  We run the speaker
   * last b/c it is the common case.  Most machines (particularly
   * compute nodes in a cluster) will only speak, and do nothing else.
   * By having the speaker last, it does not need to run in a thread.
   * We run it out of "main" instead to minimize memory usage by
   * not needing to start the speaker in a thread.
   */
  if (conf.speak)
    cerebrod_speaker(NULL);

  /* If speak is set, we do not reach this point */

  for (;;) 
    sleep(INT_MAX);

  return 0;			/* NOT REACHED */
}
Ejemplo n.º 21
0
	void wait(pthread_mutex_t *mutex) {
		Pthread_cond_wait(&c, mutex);
	}