void *wait_for_work(void *tp_v)
{
	tpool_t *tp = tp_v;

	for(;;) {
		
		/* aquire M  & K */
		//pthread_mutex_lock(&tp->work_lock);
		//INFO("aquired work_lock");
		pthread_mutex_lock(&tp->addr_valid_lock);
		INFO("aquired addr_valid_lock");
		

		while( tp->new_fn == NULL ) {
			pthread_cond_wait(&tp->addr_valid_signal,
				          &tp->addr_valid_lock);
		}

		dispatch_fn work_func = tp->new_fn;
		void *      work_args = tp->new_fn_arg;

		pthread_mutex_lock(&tp->addr_null_lock);
		tp->new_fn = NULL;
		pthread_cond_signal(&tp->addr_null_signal);
		pthread_mutex_unlock(&tp->addr_null_lock);

		pthread_mutex_unlock(&tp->addr_valid_lock);
		//pthread_mutex_unlock(&tp->work_lock);

		work_func(work_args);
	}
}
Exemple #2
0
Server::Server(void (*a_work_func)(char *, char *, int), int my_port) {
  work_func = a_work_func;
  port = my_port;
  int address_size;
  int i, len;
  char buf[4000]; // for receiving data from clients

  temp_buf = new char[16384]; // space for return data

  sock_descriptor = socket(AF_INET, SOCK_STREAM, 0);
  if (sock_descriptor == -1) {
    perror("call to socket");
    exit(1);
  }

  bzero(&sin, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = INADDR_ANY;
  sin.sin_port = htons(port);

  if (bind(sock_descriptor, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
    perror("call to bind");
    exit(1);
  }

  if (listen(sock_descriptor, 20) == -1) {
    perror("call to listen");
    exit(1);
  }

  while(1) {
    temp_sock_descriptor =
      accept(sock_descriptor, (struct sockaddr *)&pin,
	     &address_size);
    if (temp_sock_descriptor == -1) {
      perror("call to accept");
      exit(1);
    }

    if (recv(temp_sock_descriptor, buf, 4000, 0) == -1) {
      perror("call to recv");
      exit(1);
    }

    // this calls the work function passed to the class constructor:
    work_func(buf, temp_buf, 16384);

    // the virtual function doWork has filled in the
    // data to be returned to the client in 'temp_buf':

    len = strlen(temp_buf);
    
    if (send(temp_sock_descriptor, temp_buf, len, 0) == -1) {
      perror("call to send");
      exit(1);
    }

    close(temp_sock_descriptor);

  }
}
Exemple #3
0
void thread_func(detail::barrier& work_barrier, const random_generator_ptr& rng)
{
  work_barrier.count_down_and_wait();
  work_func(rng);
}