Exemplo n.º 1
0
void base_server_init(const Setup *setup) {
  base_conf_init(setup);
  /*cout << "event method: " << event_base_get_method(main_base) << endl;*/
  evthread_use_pthreads();
  thread_init();
  main_base = get_main_thread()->get_event_base();
  conn_init();
  clock_handler(0, 0, 0);
}
Exemplo n.º 2
0
int
thread_join(int thread_id, void** ret_val)
{
    struct proc* maint=get_main_thread(proc);

    if(maint->last_tid >= thread_id && proc->tid != thread_id) {
        struct proc *p;
        int havekids;
        void* retval;

        acquire(&ptable.lock);
        for(;;) {
            // Scan through table looking for zombie children.
            havekids = 0;
            for(p = ptable.proc; p < &ptable.proc[NPROC]; p++) {
                if(p->parent != maint || p->tid!=thread_id)
                    continue;

                havekids = 1;
                // check if another thread is waiting for this thread
                if(p->other_thread_waiting >= 0
                        && p->other_thread_waiting != proc->tid) {
                    release(&ptable.lock);
                    return -2;
                }

                // flag that p is waited for
                p->other_thread_waiting=proc->tid;

                if(p->state == ZOMBIE) {
                    // Found one.
                    retval=p->ret_val;
                    clear_thread(p);

                    *ret_val=retval;

                    release(&ptable.lock);
                    return 0;
                }
            }

            // No point waiting if we don't have any children.
            if(!havekids || proc->killed) {
                release(&ptable.lock);
                return -1;
            }

            // Wait for children to exit.  (See wakeup1 call in proc_exit.)
            sleep(proc, &ptable.lock);  //DOC: wait-sleep
        }
    }

    return -1;
}
Exemplo n.º 3
0
void inject_backref(lua_State* L, T* p, wrap_base*)
{
    weak_ref(get_main_thread(L), 1).swap(wrap_access::ref(*p));
}
Exemplo n.º 4
0
int server_socket(const char *interface, int port, int backlog) {
  int sfd;
  struct linger ling;
  struct addrinfo *ai;
  struct addrinfo *next;
  struct addrinfo hints;
  char port_buf[NI_MAXSERV];
  int error;
  int success = 0;
  int flags = 1;

  ling.l_onoff = 0;
  ling.l_linger = 0;
  memset(&hints, 0, sizeof(hints));
  hints.ai_flags = AI_PASSIVE;
  hints.ai_family = base_conf.support_ipv6 ? AF_UNSPEC : AF_INET;
  hints.ai_socktype = SOCK_STREAM;

  if (port == -1) {
    port = 0;
  }
  
  snprintf(port_buf, sizeof(port_buf), "%d", port);
  error= getaddrinfo(interface, port_buf, &hints, &ai);
  if (error != 0) {
    if (error != EAI_SYSTEM)
      fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(error));
    else
      perror("getaddrinfo()");
    return 1;
  }
  
  for (next= ai; next; next= next->ai_next) {
    conn *listen_conn_add;
    if ((sfd = new_socket(next)) == -1) {
      close(sfd);
      freeaddrinfo(ai);
      return 1; 
    }

#ifdef IPV6_V6ONLY
    if (next->ai_family == AF_INET6) {
      error = setsockopt(sfd, IPPROTO_IPV6, IPV6_V6ONLY,
                         (char *) &flags, sizeof(flags));
      if (error != 0) {
        perror("setsockopt");
        close(sfd);
        continue;
      }
    }
#endif

    setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (void *)&flags, sizeof(flags));

    error = setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, (void *)&flags,
                       sizeof(flags));
    if (error != 0)
      perror("setsockopt");

    error = setsockopt(sfd, SOL_SOCKET, SO_LINGER, (void *)&ling, sizeof(ling));
    if (error != 0)
      perror("setsockopt");

    error = setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (void *)&flags,
                       sizeof(flags));
    if (error != 0)
      perror("setsockopt");

    if (bind(sfd, next->ai_addr, next->ai_addrlen) == -1) {
      perror("bind()");
      close(sfd);
      freeaddrinfo(ai);
      return 1;
    } else {
      success++;
      if (listen(sfd, backlog) == -1) {
        perror("listen()");
        close(sfd);
        freeaddrinfo(ai);
        return 1;
      }
    }

    if (!(listen_conn_add = conn_new(sfd, conn_listening,
                                     EV_READ | EV_PERSIST, get_main_thread()))) {
      fprintf(stderr, "failed to create listening connection\n");
      exit(EXIT_FAILURE);
    }
    
    listen_conn_add->host->assign(interface?interface:"0.0.0.0");
    listen_conn_add->port = port;
    listen_conn_add->next = listen_conn;
    listen_conn = listen_conn_add;
  }

  freeaddrinfo(ai);
  return success == 0;
}
Exemplo n.º 5
0
int
thread_getProcId(void)
{
    return get_main_thread(proc)->pid;
}