Exemplo n.º 1
0
int main(int argc, const char *argv[])
{
    pool_t pool;
    thread_pool_init(&pool, 4);
    thread_pool_start(&pool);

    srand(100);
    while(1)
    {
        sleep(1);
        task_t tsk;
        tsk.thread_callback = task_func;
        tsk.arg =  (void *)(rand() % 100);
        thread_pool_add_task_to_queue(&pool, tsk);
    }


    thread_pool_stop(&pool);
    thread_pool_destroy(&pool);




    return 0;
}
Exemplo n.º 2
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;
}
Exemplo n.º 3
0
static void
dnscache_run(dnscache_t *dnscache)
{
    thread_pool_start(dnscache->tp_);
    thread_start(dnscache);
    struct timeval tv;
    struct event timeout;
    event_assign(&timeout, dnscache->base_event_, -1, EV_PERSIST, timeout_cb, NULL);
    evutil_timerclear(&tv);
    tv.tv_sec = TIMEOUT;
    event_add(&timeout, &tv);
    event_base_dispatch(dnscache->base_event_);
}
int thread_pool_init(struct thread_pool * pool, unsigned int thread_num){
	pool->thread_num = thread_num < THREAD_POOL_MAX_THREADNUM ? thread_num : THREAD_POOL_MAX_THREADNUM;
	pool->task_list = (struct task*) malloc(sizeof(struct task));
	if(!pool->task_list){
		fprintf(stderr, "Creating thread pool error.\n");
		return -1;
	} 
	pool->task_list_tail = pool->task_list;
	memset(pool->task_list, 0, sizeof(struct task));
	pool->running = 1;
	pool->task_num = 0;
	thread_pool_start(pool);
	return 0;
}
Exemplo n.º 5
0
/**
 * \brief Entry point of the program.
 * \param argc number of arguments.
 * \param argv array of arguments.
 * \return EXIT_SUCCESS or EXIT_FAILURE.
 */
int main(int argc, char** argv)
{
  const size_t tasks_size = 20;
  thread_pool th = NULL;
  struct thread_pool_task tasks[tasks_size];

  (void)argc;
  (void)argv;

  fprintf(stdout, "Begin\n");
  th = thread_pool_new(10);
  fprintf(stdout, "Thread pool: %p\n", (void*)th);

  if(!th)
  {
    fprintf(stderr, "Failed to create pool errno=%d\n",
        errno);
    exit(EXIT_FAILURE);
  }

  for(unsigned int i = 0 ; i < tasks_size ; i++)
  {
    tasks[i].data = (void*)(uintptr_t)i;
    tasks[i].run = fcn_run;
    tasks[i].cleanup = fcn_cleanup;

    if(thread_pool_push(th, &tasks[i]) != 0)
    {
      fprintf(stderr, "Failed to add task %u\n", i);
    }
  }

  thread_pool_start(th);
  sleep(1);

  fprintf(stdout, "Stop stuff\n");
  thread_pool_stop(th);
  fprintf(stdout, "Free stuff\n");
  thread_pool_free(&th);
  fprintf(stdout, "OK\n");

  fprintf(stdout, "End\n");

  return EXIT_SUCCESS;
}
Exemplo n.º 6
0
int main()
{
    pool_t pool;
    thread_pool_init(&pool, 5);
    thread_pool_start(&pool);

    srand(10086);
    while(1) {
        task_t task;
        task.thread_callback = task_func;
        task.arg = (void *)(rand() % 100);
        thread_pool_add_task(&pool, task);
        printf("ThreadPool size: %d, TaskQueue size: %d\n", (int)pool.size_, (int)pool.queue_.size_);
    }
    thread_pool_stop(&pool);
    thread_pool_destroy(&pool);
    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;
}
Exemplo n.º 8
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;
}
Exemplo n.º 9
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);
}