Example #1
0
int main(int argc,char **argv)
{
	if(argc < 4)
	{
		fprintf(stderr,"No enough parameter.\n");
		exit(EXIT_FAILURE);
	}
	thread_pool_t *pool = thread_pool_create(atoi(argv[1]));
	thread_pool_start(pool);
	int i;
	int end = atoi(argv[2]);
	for(i = 0; i < end; ++i)
	{
		thread_pool_execute(pool,callback,&value);
	}
	printf("\n");
	sleep(5);
	for(i = 0; i < end; ++i)
	{
		thread_pool_execute(pool,callback_function,(void*)i);
	}
	sleep(atoi(argv[3]));
	thread_pool_destroy(pool);
	printf("\n");
	return 0;
}
Example #2
0
int main(int argc, char **argv) {

    int s;
    int nthread = -1;
    int ssize   = -1;
    char *ip, *port;
    char c;

    ip = port = NULL;

    while ((c = getopt(argc, argv, "p:i:n:s:h")) != -1) {
        switch(c) {
            case 'p':
                port = optarg;
                break;
            case 'i':
                ip   = optarg;
                break;
            case 'n':
                nthread = atoi(optarg);
                break;
            case 's':
                ssize   = atoi(optarg);
                if (!STACKSIZE(ssize))
                    ssize = 10;
                break;
            case 'h':
                usage();
                exit(0);
            default:
                usage();
                exit(1);
        }
    }
    if (port == NULL) {
        usage();
        return 1;
    }

    s = tcp_listen(ip, port);
    if (s == -1)
        return 1;

    struct thread_pool *pool;

    int r = thread_pool_create(&pool, nthread, process_msg, s, ssize);
    printf("thread pool create = %d\n", r);
    thread_pool_destroy(pool);
    close(s);

    return 0;
}
Example #3
0
File: init.cpp Project: azalpy/sdk
static thread_pool_t sys_init()
{
	thread_pool_t pool;
	size_t num = system_getcpucount();
	pool = thread_pool_create(num*2, num, num*128);
	
	// init timer
	systimer_init(pool);

	// init socket
	socket_init();
	return pool;
}
Example #4
0
void thread_pool_test(void)
{
	int i, r;
	thread_pool_t pool;
	pool = thread_pool_create(4, 2, 8);

	for(i=0; i<20; i++)
	{
		r = thread_pool_push(pool, worker, &i);
		assert(0 == r);
	}
	
	thread_pool_destroy(pool);
}
Example #5
0
void test_empty_thread_pool() {
  dna_log(INFO,  "<-------------------- test_empty_thread_pool ---------------------");
  int i = 0;
  for (i = 0; i < 10 ; i++ ) {
    dna_log(INFO, ">> --- cycle %i --- <<", i+1);
    // global here, for all threads to share
    fifo = fifo_create("values", 0);
    dna_log(DEBUG, "starting thread pool");
    thread_pool_t *pool = thread_pool_create("main pool", 1);
    dna_log(DEBUG, "destroying thread pool");
    thread_pool_exit_all(pool);
    thread_pool_destroy(pool);
    fifo_check();
    fifo_destroy(fifo);
  }
}
Example #6
0
int scanner_init(char *addr, int _scan_method, char *_nic)
{
	unsigned long inaddr;
	struct hostent *host;

	/*设置默认的扫函数*/
	default_scanner_info->scan_func = half_scanner;
	default_scanner_info->type = SCAN_HALF;
	/*初始化扫描结果*/
	scan_reslut = NULL;

	/*初始化扫描函数表*/
	scanner_table = hash_new(10,  key_equal, copy_value, free_value);
	if(!scanner_table)
		return -1;
	/*解析地址*/
	memset((void*)&global_sockaddr, 0, sizeof(global_sockaddr));
	inaddr = inet_addr(addr);
	if(inaddr == INADDR_NONE)
	{
		host = gethostbyname(addr);
		if(!host)
		{
			printf("gethostbyname");
			return -1;
		}
		memcpy((char*)&global_sockaddr.sin_addr, host->h_addr, host->h_length);
	}
	else
	{
		memcpy((char*)&global_sockaddr.sin_addr, (char*)&inaddr, sizeof(inaddr));
	}
	global_sockaddr.sin_family = AF_INET;
	/*初始化线程池*/
	scan_thread_pool = thread_pool_create(200);
	if(!scan_thread_pool)
	{
		perror("thread_pool_create");
		return -1;
	}
	/*扫描方式*/
	scan_method = _scan_method;
	/*设置网卡*/
	strcpy(nic, _nic);
	/*将半开扫描设为全局扫描器*/
	return add_global_scanner(half_scanner, SCAN_HALF);
}
int main(void)
{
    int i;
    thread_pool_t *p = NULL;

    p = thread_pool_create(2);
    printf("%p\n", p);
    thread_pool_init(p, 0, 0);
    for(i = 0; i < 20; i++)
    {
        task_t *t = task_create();
        task_init(t, task_callback, NULL);
        task_add(p, t);
    }
    sleep(60);
    return 0;
}
Example #8
0
dnscache_t *
dnscache_create(char *server_addr,
                uint16_t port,
                uint32_t thread_num)
{
    dnscache_t *dnscache = malloc(sizeof(dnscache_t));
    if (!dnscache)
        return NULL;

    dnscache->thread_count_ = thread_num;
    dnscache->base_event_ = event_base_new();
    dnscache_init_sig(dnscache);

    dnscache->tp_ = thread_pool_create(thread_num);
    ASSERT(dnscache->tp_, "thread pool create failed");

    addr_t addr;
    addr_init(&addr, server_addr, port);
    dnscache->listen_socket = (socket_t *)malloc(sizeof(socket_t));
    int socket_type = addr_get_type(&addr) == ADDR_IPV4 ? AF_INET : AF_INET6;
    socket_open(dnscache->listen_socket, socket_type, SOCK_DGRAM, 0);
    socket_set_unblock(dnscache->listen_socket, true);
    socket_set_addr_reusable(dnscache->listen_socket);
    int ret = socket_bind(dnscache->listen_socket, &addr);
    ASSERT(ret == 0, "udp server bind failed\n");

    int threads_len = thread_num * sizeof(thread_local_param_t *);
    dnscache->thread_params_ = (thread_local_param_t **)malloc(threads_len);
    ASSERT(dnscache->thread_params_  != NULL, "udp server bind failed\n");
    int i = 0;

    for (; i < thread_num; ++i)
    {
        dnscache->thread_params_[i] =
            (thread_local_param_t *)malloc(sizeof(thread_local_param_t));
        thread_local_param_t *tlp = dnscache->thread_params_[i];
        tlp->thread_id_ = i;
        tlp->base_ = event_base_new();
        tlp->dns_server_ =
            dns_server_create(tlp->base_, UDP_SERVER, dnscache->listen_socket, queue_size);
        thread_pool_set_thread_data(dnscache->tp_, i, tlp);
    }

    return dnscache;
}
Example #9
0
void test_busy_thread_pool() {
  dna_log(INFO,  "<-------------------- test_busy_thread_pool  ---------------------");
  fifo = fifo_create("<(busy_thread_pool) value fifo>", 0);
  thread_pool_t *pool = thread_pool_create("<busy thread pool>", 8); // should auto-determine thread count maybe?
  dna_log(DEBUG, "adding %i tasks to the queue...", ELEMS);
  int i = 0;
  for( i = 0; i < ELEMS; i++ ) {
    thread_pool_enqueue(pool, &fifo_fill, NULL);
  }
  dna_log(DEBUG, "waiting for threads to complete on their own...");
  thread_pool_exit_all(pool);
  thread_pool_join_all( pool );
  dna_log(DEBUG, "destroying thread pool...");
  thread_pool_destroy( pool );
  fifo_check();
  dna_log(DEBUG, "destroying global value fifo.");
  fifo_destroy( fifo );
}
Example #10
0
File: main.c Project: ireader/sdk
int main(int argc, char* argv[])
{
	char c;
	int cpu = system_getcpucount();
	socket_t server;
	aio_socket_t aioserver;

	memset(s_buffer, 'A', sizeof(s_buffer)-1);

	aio_socket_init(cpu);
	s_thpool = thread_pool_create(cpu, cpu, cpu*2);

	while(cpu > 0)
	{
		thread_pool_push(s_thpool, AIOWorker, NULL);
		--cpu;
	}

	server = Listen(50000);
	aioserver = aio_socket_create(server, 1);
	aio_socket_accept(aioserver, OnAccept, aioserver);

	printf("server listen at: %d\n", 50000);
	for(c = getchar(); 'q' != c; c = getchar())
	{
		switch(c)
		{
		case 'c': // close socket
			aio_socket_destroy(aioserver);
			break;

		default:
			printf("unknown command.\nc : close socket\n");
		}
	}

	aio_socket_destroy(aioserver);
	thread_pool_destroy(s_thpool);
	aio_socket_clean();
	return 0;
}
Example #11
0
int main(int argc, char* argv[])
{
    tcpserver_t tcpserver;
    tcpserver_handler_t tcphandler;
    tcphandler.onerror = OnTcpError;
    tcphandler.onconnected = OnTcpConnected;

    socket_init();
    g_thdpool = thread_pool_create(2, 1, 64);
    tcpserver = tcpserver_start(NULL, 10000, &tcphandler, NULL);

    http_proxy_find(OnFindProxy, NULL);
    while('q' != getchar())
    {
    }

    tcpserver_stop(tcpserver);
    thread_pool_destroy(g_thdpool);
    socket_cleanup();
    return 0;
}
Example #12
0
int main(int argc, char* argv[])
{
#if defined(OS_LINUX)
    /* ignore pipe signal */
    struct sigaction sa;
    sa.sa_handler = SIG_IGN;
    sigaction(SIGCHLD, &sa, 0);
    sigaction(SIGPIPE, &sa, 0);
#endif

    int port = 554;
    for(int i=1; i<argc; i++)
    {
        if(streq(argv[i], "--port") && i+1<argc)
        {
            port = atoi(argv[++i]);
        }
    }

    size_t cpu = system_getcpucount();
    g_thpool = thread_pool_create(cpu, cpu, cpu*4);

    aio_socket_init(cpu * 2);
    for(size_t i=0; i<cpu * 2; i++)
        thread_pool_push(g_thpool, AioWorker, NULL); // start worker

    // start server
    StartTcpServer(NULL, port);
    StartUdpServer(NULL, port);

    for(int c = getchar(); 'q' != c ; c = getchar())
    {
    }

    aio_socket_destroy(s_tcp);
    aio_socket_clean();
    thread_pool_destroy(g_thpool);
    return 0;
}
int main(int argc, char **argv)
{
    /* declare variables we'll be using */
    thread_pool_attr_t   pool_attr;
    resmgr_attr_t        resmgr_attr;
    dispatch_t           *dpp;
    thread_pool_t        *tpp;
    dispatch_context_t   *ctp;
    int                  id;

    /* initialize dispatch interface */
    if((dpp = dispatch_create()) == NULL) {
        fprintf(stderr,
                "%s: Unable to allocate dispatch handle.\n",
                argv[0]);
        return EXIT_FAILURE;
    }

    /* initialize resource manager attributes */
    memset(&resmgr_attr, 0, sizeof resmgr_attr);
    resmgr_attr.nparts_max = 1;
    resmgr_attr.msg_max_size = 2048;

    /* initialize functions for handling messages */
    iofunc_func_init(_RESMGR_CONNECT_NFUNCS, &connect_funcs, 
                     _RESMGR_IO_NFUNCS, &io_funcs);
    io_funcs.read = io_read;
    io_funcs.write = io_write;
    io_funcs.devctl = io_devctl;

    /* initialize attribute structure used by the device */
    iofunc_attr_init(&attr, S_IFNAM | 0666, 0, 0);
    attr.nbytes = strlen (buffer)+1;

    /* attach our device name */
    id = resmgr_attach(
            dpp,            /* dispatch handle        */
            &resmgr_attr,   /* resource manager attrs */
            "/dev/sample",  /* device name            */
            _FTYPE_ANY,     /* open type              */
            0,              /* flags                  */
            &connect_funcs, /* connect routines       */
            &io_funcs,      /* I/O routines           */
            &attr);         /* handle                 */
    if(id == -1) {
        fprintf(stderr, "%s: Unable to attach name.\n", argv[0]);
        return EXIT_FAILURE;
    }

    /* initialize thread pool attributes */
    memset(&pool_attr, 0, sizeof pool_attr);
    pool_attr.handle = dpp;
    pool_attr.context_alloc = dispatch_context_alloc;
    pool_attr.block_func = dispatch_block;
    pool_attr.unblock_func = dispatch_unblock;
    pool_attr.handler_func = dispatch_handler;
    pool_attr.context_free = dispatch_context_free;
    pool_attr.lo_water = 4;
    pool_attr.hi_water = 8;
    pool_attr.increment = 2;
    pool_attr.maximum = 50;

    /* allocate a thread pool handle */
    if((tpp = thread_pool_create(&pool_attr, 
                                 POOL_FLAG_EXIT_SELF)) == NULL) {
        fprintf(stderr, "%s: Unable to initialize thread pool.\n",
                argv[0]);
        return EXIT_FAILURE;
    }

    /* start the threads, will not return */
    thread_pool_start(tpp);
    
    return EXIT_SUCCESS;
}
Example #14
0
int _aio_init(thread_pool_attr_t *pool_attr)
{
	static thread_pool_attr_t default_pool_attr = {
		  NULL,
		  _aio_block,
		  _aio_unblock,
		  _aio_handler,
		  _aio_context_alloc,
		  _aio_context_free,
		  NULL,
		  3,
		  1,
		  8,
		  10
	};
	struct _aio_control_block *cb;
	struct _aio_context *ctp;
	struct _aio_prio_list *plist;
	sigset_t set, oset;
	int i;
	  
	_mutex_lock(&_aio_init_mutex);
	if (_aio_cb) {
		_mutex_unlock(&_aio_init_mutex);
		return 0;
	}
	
	if ((cb = malloc(sizeof(*_aio_cb))) == NULL) {
		_mutex_unlock(&_aio_init_mutex);
		return -1;
	}
	memset(cb, 0, sizeof(*cb));
	pthread_mutex_init(&cb->cb_mutex, 0);
	(void)pthread_cond_init(&cb->cb_cond, 0);
	
	if (pool_attr == NULL) {
		pool_attr = &default_pool_attr;
	} else {
		pool_attr->block_func = _aio_block;
		pool_attr->context_alloc = _aio_context_alloc;
		pool_attr->unblock_func = _aio_unblock;
		pool_attr->handler_func = _aio_handler;
		pool_attr->context_free = _aio_context_free;
	}
	pool_attr->handle = (void *)cb;

	/* prepare some priority list entries */
	for (i = 0; i < _AIO_PRIO_LIST_LOW; i++) {
		plist = (struct _aio_prio_list *)malloc(sizeof(*plist));
		if (!plist) {
			goto err_ret;
		}
		plist->next = cb->cb_plist_free;
		cb->cb_plist_free = plist;
	}
	cb->cb_nfree = _AIO_PRIO_LIST_LOW;
	
	/* prepare the context */
	cb->ct_free = NULL;
	for (i = 0; i < pool_attr->maximum; i++) {
		if ((ctp = malloc(sizeof(*ctp))) == NULL) {
			goto err_ret;
		}
		ctp->next = cb->ct_free;
		cb->ct_free = ctp;
	}
	
	cb->tp = thread_pool_create(pool_attr, 0);
	if (cb->tp == NULL) {
		goto err_ret;
	}

	/* we can hook _aio_cb now */
	_aio_cb = cb;

	/* start the pool with all sigal blocked */
	if (sigfillset(&set) != 0 || (errno = pthread_sigmask(SIG_BLOCK, &set, &oset)) != EOK) {
		goto err_ret;
	}
	if (thread_pool_start(cb->tp) != EOK) {
		pthread_sigmask(SIG_SETMASK, &oset, NULL);
		goto err_ret;
	}
	pthread_sigmask(SIG_SETMASK, &oset, NULL);
	_mutex_unlock(&_aio_init_mutex);
	return 0;
		
err_ret:
	_mutex_lock(&cb->cb_mutex);
	
	if (cb->tp) {
		(void)thread_pool_destroy(cb->tp);
	}
	
	while ((plist = cb->cb_plist_free)) {
		cb->cb_plist_free = plist->next;
		free(plist);
	}
	
	while ((ctp = cb->ct_free)) {
		cb->ct_free = ctp->next;
		free(ctp);
	}

	pthread_cond_destroy(&cb->cb_cond);
	pthread_mutex_destroy(&cb->cb_mutex);
	
	free(cb);
	_mutex_unlock(&_aio_init_mutex);
	return -1;
}
Example #15
0
int 
begin(int argc, char *argv[])
{
        int                   	i, j;
	uint32_t		ph_flags = 0;
        thread_pool_attr_t	tp_attr;
	static void 		*tpp;
        dispatch_t              *dpp;

        dpp = dispatch_create();
        if (dpp == NULL) {
               char * pMsgTxt = "Error: cannot create dispatch interface\n";
               fprintf(stderr, pMsgTxt);
               slogf(_SLOG_SETCODE(_SLOGC_INPUT, 0), _SLOG_ERROR, pMsgTxt);
                return (EXIT_FAILURE);
        }
        devi_set_dispatch_handle(dpp);
        /* set up the module table */
        if (modules != NULL) {

                for (i = 0; i < MODULE_TABLE_SIZE && ModuleTable[i] != NULL; 
		     i++)
                        ;
                /* add extra modules to end of ModuleTable */
                
                for (j = 0; j < (MODULE_TABLE_SIZE - i) && modules[j] != NULL; 
		     j++, i++) 
                        ModuleTable[i] = modules[j];
        }
        
        if(ThreadCtl(_NTO_TCTL_IO, 0) == -1) {
                errno_print("ThreadCtl");
                return (EXIT_FAILURE);
        }
        // Call global callback if it was specified        
        if(commonCallbacks.pre_init && commonCallbacks.pre_init())
		return (EXIT_FAILURE);
        if (options(argc, argv, &ph_flags, &OptFlags) < 0)
		return (EXIT_FAILURE);
        // Call global callback if it was specified        
        if(commonCallbacks.post_init && commonCallbacks.post_init())
		return (EXIT_FAILURE);
        sig_install(signal_table, termination_hndlr);

	if (procmgr_daemon(0, PROCMGR_DAEMON_NODEVNULL | 
			   PROCMGR_DAEMON_NOCLOSE) < 0) {
		errno_print("procmgr_daemon");
		return (EXIT_FAILURE);
	}


        if (!(OptFlags & OPT_NO_PHOTON))  {
                if (start_photon_interface(ph_flags) < 0) {
                        errno_print("unable to start photon interface");
                        return (EXIT_FAILURE);
                }
        }

	memset(&tp_attr, 0, sizeof(tp_attr));
        tp_attr.handle = devi_get_dispatch_handle();
        tp_attr.context_alloc = dispatch_context_alloc;
        tp_attr.block_func = dispatch_block;
        tp_attr.handler_func = dispatch_handler;
        tp_attr.context_free = dispatch_context_free;
    /* We'd rather not to use thread pool for device driver - it could 
       cause desynchronizing and data losing                       */
	tp_attr.lo_water  = 1;   /* min amount threads waiting blocked */
	tp_attr.hi_water  = 1;   /* max amount threads waiting blocked */
	tp_attr.increment = 1;  
	tp_attr.maximum   = 1;    

	tpp = thread_pool_create(&tp_attr, POOL_FLAG_USE_SELF);
	if (tpp == NULL) {
                errno_print("thread_pool_create");
                return (EXIT_FAILURE);
        }

	thread_pool_start(tpp);

        return (EOK);
}