Exemplo n.º 1
0
Arquivo: test.c Projeto: zgbkny/ctoolx
static void start_threads(void)
{
  long i, n;

  /* Create access log flushing thread */
  if (log_access && st_thread_create(flush_acclog_buffer, NULL, 0, 0) == NULL)
    err_sys_quit(errfd, "ERROR: process %d (pid %d): can't create"
		 " log flushing thread", my_index, my_pid);

  /* Create connections handling threads */
  for (i = 0; i < sk_count; i++) {
    err_report(errfd, "INFO: process %d (pid %d): starting %d threads"
	       " on %s:%u", my_index, my_pid, max_wait_threads,
	       srv_socket[i].addr, srv_socket[i].port);
    WAIT_THREADS(i) = 0;
    BUSY_THREADS(i) = 0;
    RQST_COUNT(i) = 0;
    for (n = 0; n < max_wait_threads; n++) {
      if (st_thread_create(handle_connections, (void *)i, 0, 0) != NULL)
	WAIT_THREADS(i)++;
      else
	err_sys_report(errfd, "ERROR: process %d (pid %d): can't create"
		       " thread", my_index, my_pid);
    }
    if (WAIT_THREADS(i) == 0)
      exit(1);
  }
}
Exemplo n.º 2
0
CAMLprim value caml_thread_new(value clos)          /* ML */
{
  caml_thread_t th;
  st_retcode err;

  /* Create a thread info block */
  th = caml_thread_new_info();
  if (th == NULL) caml_raise_out_of_memory();
  /* Equip it with a thread descriptor */
  th->descr = caml_thread_new_descriptor(clos);
  /* Add thread info block to the list of threads */
  th->next = curr_thread->next;
  th->prev = curr_thread;
  curr_thread->next->prev = th;
  curr_thread->next = th;
  /* Create the new thread */
  err = st_thread_create(NULL, caml_thread_start, (void *) th);
  if (err != 0) {
    /* Creation failed, remove thread info block from list of threads */
    caml_thread_remove_info(th);
    st_check_error(err, "Thread.create");
  }
  /* Create the tick thread if not already done.  
     Because of PR#4666, we start the tick thread late, only when we create
     the first additional thread in the current process*/
  if (! caml_tick_thread_running) {
    err = st_thread_create(&caml_tick_thread_id, caml_thread_tick, NULL);
    st_check_error(err, "Thread.create");
    caml_tick_thread_running = 1;
  }
  return th->descr;
}
Exemplo n.º 3
0
int main (int argc, const char * argv[]) {
	thread_Producer_Consumer_init inputProcessing_init;
	thread_Producer_Consumer_init carriageToSpace_init;
	thread_Producer_Consumer_init starsToCaret_init;
	thread_Producer_Consumer_init outputProcessing_init;
	if (st_init()<0) {
		perror("st_init");
		exit(1);
	}
	
	circularBuffer buf1, buf2, buf3;
	newBuffer(&buf1, BUFFER_SIZE); //for buffer inputProcessing & carriageToSpace
	newBuffer(&buf2, BUFFER_SIZE); //for buffer carriageToSpace & starsToCaret
	newBuffer(&buf3, BUFFER_SIZE); //for buffer starsToCaret & outputProcessing
		
	inputProcessing_init.buffer_C = NULL;
	inputProcessing_init.buffer_P = &buf1;
	
	carriageToSpace_init.buffer_C = &buf1;
	carriageToSpace_init.buffer_P = &buf2;	
	
	starsToCaret_init.buffer_C = &buf2;
	starsToCaret_init.buffer_P = &buf3;
	
	outputProcessing_init.buffer_C = &buf3;
	outputProcessing_init.buffer_P = NULL;
	
	if (st_thread_create(inputProcessing, &inputProcessing_init, 0, 0)==NULL) {
		perror("st_thread_create \n");
		exit(1);
	}
	
	if (st_thread_create(carriageToSpace, &carriageToSpace_init, 0, 0)==NULL) {
		perror("st_thread_create \n");
		exit(1);
	}
	
	if (st_thread_create(starsToCaret, &starsToCaret_init, 0, 0)==NULL) {
		perror("st_thread_create \n");
		exit(1);
	}
	
	if (st_thread_create(outputProcessing, &outputProcessing_init, 0, 0)==NULL) {
		perror("st_thread_create \n");
		exit(1);
	}
	printf("Hello World~~~~~~\n");
	
	fflush(stdout);
	st_thread_exit(NULL);
	
    return 0;	
}
Exemplo n.º 4
0
/*
 * Asynchronous DNS host name resolution. This program creates one
 * ST thread for each host name (specified as command line arguments).
 * All threads do host name resolution concurrently.
 */
int main(int argc, char *argv[])
{
  if (st_init() < 0) {
    perror("st_init");
    exit(1);
  }
  condition = st_cond_new();
  mutex = st_mutex_new();

  int maxQueueP = 15;
  int maxQueueC = 10;
  st_thread_t p[6],c[6];

  int i = 0;
  for (; i < maxQueueP; ++i)
  {
    if (  (p[i] = st_thread_create(producer,i,1,0) ) == NULL) {
      perror("st_thread_create producer failed");
      exit(1);
    }
  }

  i = 0;
  for (; i < maxQueueC; ++i)
  {
    if ( (c[i] = st_thread_create(consumer,i,1,0) ) == NULL) {
      perror("st_thread_create consumer failed");
      exit(1);
    }
  }
  

  i = 0;
  for (; i < maxQueueP; ++i)
  {
    st_thread_join(p[i],0);
  }
  i = 0;
  for (; i < maxQueueC; ++i)
  {
    st_thread_join(c[i],0);
  }

  

  /* NOTREACHED */
  return 1;
}
Exemplo n.º 5
0
void
accept_connection(int fd)
{
    struct sockaddr_in from;
    st_netfd_t st_fd,fd2;
    int fromlen = sizeof(from);
    
    if((st_fd = st_netfd_open_socket(fd)) == NULL)
      {perror("st_netfd_open_socket") ; exit(1);}
    
    while(1) {
        fd2 = st_accept(st_fd,(struct sockaddr *)&from, &fromlen, -1);
        if(fd2 == NULL) {
            if(errno == EINTR) {
              continue;
            } else {
                perror("accept");
                st_sleep(0);
                continue;
            }
        }

        if(st_thread_create(handle_connection, (void *)fd2,0,0) == NULL){
            perror("st_thread_create");
            exit(1);
        }
    }
}
int SrsThread::start()
{
    int ret = ERROR_SUCCESS;
    
    if(tid) {
        srs_info("thread already running.");
        return ret;
    }
    
    if((tid = st_thread_create(thread_fun, this, (_joinable? 1:0), 0)) == NULL){
        ret = ERROR_ST_CREATE_CYCLE_THREAD;
        srs_error("st_thread_create failed. ret=%d", ret);
        return ret;
    }
    
    // we set to loop to true for thread to run.
    loop = true;
    
    // wait for cid to ready, for parent thread to get the cid.
    while (_cid < 0 && loop) {
        st_usleep(10 * 1000);
    }
    
    // now, cycle thread can run.
    can_run = true;
    
    return ret;
}
Exemplo n.º 7
0
/*
 * Asynchronous DNS host name resolution. This program creates one
 * ST thread for each host name (specified as command line arguments).
 * All threads do host name resolution concurrently.
 */
int main(int argc, char *argv[])
{
  int i;

  if (argc < 2) {
    fprintf(stderr, "Usage: %s <hostname1> [<hostname2>] ...\n", argv[0]);
    exit(1);
  }

  if (st_init() < 0) {
    perror("st_init");
    exit(1);
  }

  for (i = 1; i < argc; i++) {
    /* Create a separate thread for each host name */
    if (st_thread_create(do_resolve, argv[i], 0, 0) == NULL) {
      perror("st_thread_create");
      exit(1);
    }
  }

  st_thread_exit(NULL);

  /* NOTREACHED */
  return 1;
}
int SrsEncoder::on_publish(std::string _vhost, std::string _port, std::string _app, std::string _stream)
{
	int ret = ERROR_SUCCESS;

	vhost = _vhost;
	port = _port;
	app = _app;
	stream = _stream;

	ret = parse_scope_engines();
	
	// ignore the loop encoder
	if (ret == ERROR_ENCODER_LOOP) {
		ret = ERROR_SUCCESS;
	}
	
	// return for error or no engine.
	if (ret != ERROR_SUCCESS || ffmpegs.empty()) {
		return ret;
	}
    
    // start thread to run all encoding engines.
    srs_assert(!tid);
    if((tid = st_thread_create(encoder_thread, this, 1, 0)) == NULL) {
		ret = ERROR_ST_CREATE_FORWARD_THREAD;
        srs_error("st_thread_create failed. ret=%d", ret);
        return ret;
    }
    
	return ret;
}
Exemplo n.º 9
0
/*
 * Initialize this Virtual Processor
 */
int st_init(void)
{
  _st_thread_t *thread;

  if (_st_active_count) {
    /* Already initialized */
    return 0;
  }

  /* We can ignore return value here */
  //st_set_eventsys(ST_EVENTSYS_DEFAULT);
  st_set_eventsys(ST_EVENTSYS_ALT);

  if (_st_io_init() < 0)
    return -1;

  memset(&_st_this_vp, 0, sizeof(_st_vp_t));

  ST_INIT_CLIST(&_ST_RUNQ);
  ST_INIT_CLIST(&_ST_IOQ);
  ST_INIT_CLIST(&_ST_ZOMBIEQ);
#ifdef DEBUG
  ST_INIT_CLIST(&_ST_THREADQ);
#endif

  if ((*_st_eventsys->init)() < 0)
    return -1;

  _st_this_vp.pagesize = getpagesize();
  _st_this_vp.last_clock = st_utime();

  /*
   * Create idle thread
   */
  _st_this_vp.idle_thread = st_thread_create(_st_idle_thread_start,
					     NULL, 0, 0);
  if (!_st_this_vp.idle_thread)
    return -1;
  _st_this_vp.idle_thread->flags = _ST_FL_IDLE_THREAD;
  _st_active_count--;
  _ST_DEL_RUNQ(_st_this_vp.idle_thread);

  /*
   * Initialize primordial thread
   */
  thread = (_st_thread_t *) calloc(1, sizeof(_st_thread_t) +
				   (ST_KEYS_MAX * sizeof(void *)));
  if (!thread)
    return -1;
  thread->private_data = (void **) (thread + 1);
  thread->state = _ST_ST_RUNNING;
  thread->flags = _ST_FL_PRIMORDIAL;
  _ST_SET_CURRENT_THREAD(thread);
  _st_active_count++;
#ifdef DEBUG
  _ST_ADD_THREADQ(thread);
#endif

  return 0;
}
Exemplo n.º 10
0
int
main()
{
    st_thread_t thread;
    int rc;
    int i, j, s;

    arg = -1;
    res = -1;
    st_init();
    for(i = 0; i < 10; i++) {
        s = 0;
        for(j = 0; j < 10000; j++) {
            res = -1;
            arg = j;
            thread = st_thread_create(thread_routine, NULL, 1, 0);
            st_sleep(0);
            while(res < 0)
                st_sleep(0);
            s += res;
            st_thread_join(thread, NULL);
            arg = -1;
        }
    }
    printf("%d\n", s);
    return 0;
}
Exemplo n.º 11
0
void add_probe(gpointer key, gpointer value, gpointer user_data)
{
  if (st_thread_create(probe, value, 0, 0) == NULL) {
    LOG(LOG_WARNING, "couldn't create thread");
  } else {
    thread_count++;
  }
}
Exemplo n.º 12
0
int main(int argc, char *argv[]) {
  int status;
  st_init();
  status = ares_library_init(ARES_LIB_INIT_ALL);
  if (status != ARES_SUCCESS)
  {
    fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status));
    return 1;
  }

  st_thread_t t = st_thread_create(do_lookup, (void *)"A", 1, 1024 * 128);
  st_thread_t t2 = st_thread_create(do_lookup, (void *)"B", 1, 1024 * 128);
  st_thread_join(t, NULL);
  st_thread_join(t2, NULL);

  ares_library_cleanup();
  return 0;
}
Exemplo n.º 13
0
int main(int argc, char *argv[]) {

  g_assert(st_set_eventsys(ST_EVENTSYS_ALT) == 0);
  st_init();
  int status = ares_library_init(ARES_LIB_INIT_ALL);
  if (status != ARES_SUCCESS)
  {
    fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status));
    return 1;
  }

  int sock;
  int n;
  struct sockaddr_in serv_addr;

  if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket");
  }

  n = 1;
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof(n)) < 0) {
    perror("setsockopt SO_REUSEADDR");
  }

  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(8080);
  serv_addr.sin_addr.s_addr = inet_addr("0.0.0.0");

  if (bind(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    perror("bind");
  }

  if (listen(sock, 10) < 0) {
    perror("listen");
  }

  st_netfd_t server_nfd = st_netfd_open_socket(sock);
  st_netfd_t client_nfd;
  struct sockaddr_in from;
  int fromlen = sizeof(from);

  for (;;) {
    client_nfd = st_accept(server_nfd,
      (struct sockaddr *)&from, &fromlen, ST_UTIME_NO_TIMEOUT);
    printf("accepted\n");
    if (st_thread_create(handle_connection,
      (void *)client_nfd, 0, 1024 * 1024) == NULL)
    {
      fprintf(stderr, "st_thread_create error\n");
    }
  }
  ares_library_cleanup();
  return EXIT_SUCCESS;
}
Exemplo n.º 14
0
int peer_listen_and_interconnect(void) {
  // 先创建自己的侦听
  if (_peer_listen(self_index) != 0)
    return -1;

  // 创建自己的任务队列
  queue = queue_create();
  st_thread_create(_queue_worker, queue, 0, 0);

  // 用线程进行到其它结点的互联
  if (st_thread_create(_interconnect_to_peers, NULL, 0, 0) == NULL) {
    ERR("[%d] failed to create the peer link thread: %s\n", self_index, strerror(errno));
    return -1;
  }

  // 主线程回环,用于接收远程 rpc 结点发来的数据事件
  _peer_accept();

  return 0;
}
Exemplo n.º 15
0
hoxResult
hoxDbClient::initialize( const char* szHost,
                         int         nPort )
{
    const char* FNAME = "hoxDbClient::initialize";
    hoxResult result = hoxRC_UNKNOWN;

    hoxLog(LOG_INFO, "%s: ENTER. [%s:%d]", FNAME, szHost, nPort);

    if ( s_bInitialized )
    {
        hoxLog(LOG_DEBUG, "%s: The module has already initialized.", FNAME);
        return hoxRC_OK;
    }

    s_mutex = st_mutex_new();  // ... to provide exclusive assess

    /* Open a "shared" client socket. */
    s_nfd = _open_client_socket( szHost, nPort );
    if ( s_nfd == NULL )
    {
        hoxLog(LOG_ERROR, "%s: Failed to open a client socket.", FNAME);
        return hoxRC_OK;
    }

    s_bInitialized = true;

    /* Send an "HELLO" request to make sure that the DB Agent is OK. */
    result = send_HELLO();
    if ( result != hoxRC_OK )
    {
        hoxLog(LOG_ERROR, "%s: Failed to send HELLO request.", FNAME);
        return hoxRC_ERR;
    }

    /* Start a "write" thread to help avoiding blocking
     * handlers of client requests.
     */

    s_writeCond = st_cond_new();

    s_writeThread = st_thread_create( _handle_db_write,
                                      ( void * ) NULL,
                                      1 /* joinable */,
                                      0 /* stack-size */ );
    if ( s_writeThread == NULL )
    {
        hoxLog(LOG_ERROR, "%s: Failed to create write DB thread.", FNAME);
        return hoxRC_ERR;
    }

    hoxLog(LOG_DEBUG, "%s: END. (OK)", FNAME);
    return hoxRC_OK;
}
int SrsConnection::start()
{
	int ret = ERROR_SUCCESS;
    
    if (st_thread_create(cycle_thread, this, 0, 0) == NULL) {
        ret = ERROR_ST_CREATE_CYCLE_THREAD;
        srs_error("st_thread_create conn cycle thread error. ret=%d", ret);
        return ret;
    }
    srs_verbose("create st conn cycle thread success.");
	
	return ret;
}
Exemplo n.º 17
0
static void create_q_threads(gpointer key, gpointer value, gpointer user_data)
{
#ifdef USE_ST
  if (st_thread_create(read_queue, value, 0, 0) == NULL) { 
    LOG(LOG_WARNING, "couldn't create queue thread for %s", (char *)key);
  } else {
    if (debug > 3) { LOG(LOG_DEBUG, "created new thread"); }
    thread_count++;
  }
#else
  read_queue(value);
#endif
}
Exemplo n.º 18
0
/*
 * Initialize this Virtual Processor
 */
extern "C" int st_init(void)
{
	st_thread_t *thread;

	if (_st_active_count) {
		/* Already initialized */
		return 0;
	}

	if (_st_io_init() < 0)
		return -1;

	memset(&_st_this_vp, 0, sizeof(st_vp_t));

	ST_INIT_CLIST(&_ST_RUNQ);
	ST_INIT_CLIST(&_ST_IOQ);
	ST_INIT_CLIST(&_ST_SLEEPQ);
	ST_INIT_CLIST(&_ST_ZOMBIEQ);

	_st_this_vp.maxfd = -1;
	_st_this_vp.pagesize = getpagesize();
	_st_this_vp.last_clock = st_utime();

	/*
	 * Create idle thread
	 */
	_st_this_vp.idle_thread = st_thread_create(_st_idle_thread_start,
		NULL, 0, 0);
	if (!_st_this_vp.idle_thread)
		return -1;
	_st_this_vp.idle_thread->flags = _ST_FL_IDLE_THREAD;
	_st_active_count--;
	_ST_DEL_RUNQ(_st_this_vp.idle_thread);

	/*
	 * Initialize primordial thread
	 */
	thread = (st_thread_t *)calloc(1, sizeof(st_thread_t)+
		(ST_KEYS_MAX * sizeof(void *)));
	if (!thread)
		return -1;
	thread->private_data = (void **)(thread + 1);
	thread->state = _ST_ST_RUNNING;
	thread->flags = _ST_FL_PRIMORDIAL;
	_ST_SET_CURRENT_THREAD(thread);
	_st_active_count++;

	_st_notify_event = CreateEvent(NULL, TRUE, FALSE, NULL);
	_st_semaphore		 = CreateSemaphore(NULL, 0, 10,NULL);
	return 0;
}
Exemplo n.º 19
0
/**
 * 接收 rpc 连接
 * 进程的主线程在这里回环
 */
void accept_client(void) {
  st_netfd_t client;
  struct sockaddr from;
  int fromlen = sizeof(from);

  while ((client = st_accept(rpc_fd, &from, &fromlen, ST_UTIME_NO_TIMEOUT)) != NULL) {
    st_netfd_setspecific(client, get_in_addr(&from), NULL);

    if (st_thread_create(handle_clientconn, &client, 0, 0) == NULL)
      fprintf(stderr, "[%d] failed to create the client thread: %s\n", my_index, strerror(errno));
  }

  st_netfd_close(rpc_fd);
}
Exemplo n.º 20
0
int
main()
{
    int n;
    st_thread_t thread;

    st_init();
    n = 0;
    thread = st_thread_create(thread_routine, NULL, 1, 4096);
    inc_n();

    while(1)
        st_sleep(1);
    return 0;
}
Exemplo n.º 21
0
void *_queue_worker(void *arg) {
  struct rpc_queue *queue;
  queue = (struct rpc_queue*)arg;
  arg = NULL;
  struct rpc_package_head *head;

  for (; ; st_usleep(10)) {
    if (worker_thread_count < 1000 && queue->count > 0) {
      head = queue_get(queue);
      assert(head != NULL);

      st_thread_create(_peer_task_worker, head, 0, 0);

      worker_thread_count ++;
    }
  }
}
Exemplo n.º 22
0
int SrsThread::start()
{
	int ret = ERROR_SUCCESS;
	
    if(tid) {
        srs_info("thread already running.");
        return ret;
    }
    
    if((tid = st_thread_create(thread_fun, this, 1, 0)) == NULL){
		ret = ERROR_ST_CREATE_CYCLE_THREAD;
        srs_error("st_thread_create failed. ret=%d", ret);
        return ret;
    }
    
    return ret;
}
Exemplo n.º 23
0
static void *read_queue(void *data)
{
  struct q_info *q = (struct q_info *)data;
extern int forever;
static char path[PATH_MAX];
  G_CONST_RETURN gchar *filename;
  GDir *dir;
 
  q->fatal = 0;
  //sprintf(path, "%s/%s/new", OPT_ARG(SPOOLDIR), q->name);
  strcpy(path, OPT_ARG(SPOOLDIR));
  strcat(path, "/");
  strcat(path, q->name);
  strcat(path, "/new");
  if (debug > 3) { LOG(LOG_DEBUG, "reading queue %s", path); }
  dir = g_dir_open (path, 0, NULL);
  while ((filename = g_dir_read_name(dir)) != NULL && !q->fatal && forever) {
    char buffer[PATH_MAX];
    struct thr_data *td;
 
    sprintf(buffer, "%s/%s", path, filename);
    td = g_malloc0(sizeof(struct thr_data));
    td->q = q;
    td->filename = strdup(buffer);
#ifdef USE_ST
    if (st_thread_create(push, td, 0, 0) == NULL) { 
      LOG(LOG_WARNING, "couldn't create thread");
      st_sleep(1);
    } else {
      q->thread_count++;
      thread_count++;
    }
    while (q->thread_count >= q->maxthreads) {
      st_usleep(10000); /* 10 ms */
    }
#else
    push(td);
#endif
  }
  g_dir_close(dir);
#ifdef USE_ST
  thread_count--;
#endif
  return NULL;
}
Exemplo n.º 24
0
void *
thread_routine(void *dummy)
{
    st_thread_t thread;
    int k;
    while(1) {
        st_sleep(0);
        thread = st_thread_create(thread_routine, NULL, 1, 4096);
        if(thread == NULL) {
            printf("%d\n", n);
            abort();
        }
        k = inc_n();
        if(k % 100 == 0) {
            printf("%d\n", k);
        }
    }
}