Beispiel #1
0
thread_mutex::thread_mutex(bool recursive /* = true */)
{
	mutex_ = (acl_pthread_mutex_t*)
		acl_mycalloc(1, sizeof(acl_pthread_mutex_t));

#ifdef	ACL_UNIX
	int ret = pthread_mutexattr_init(&mutex_attr_);
	if (ret)
	{
		SET_ERRNO(ret);
		logger_fatal("pthread_mutexattr_init error=%s", last_serror());
	}
	if (recursive && (ret = pthread_mutexattr_settype(&mutex_attr_,
					PTHREAD_MUTEX_RECURSIVE)))
	{
		SET_ERRNO(ret);
		logger_fatal("pthread_mutexattr_settype error=%s", last_serror());
	}
	ret = acl_pthread_mutex_init(mutex_, &mutex_attr_);
	if (ret)
	{
		SET_ERRNO(ret);
		logger_fatal("pthread_mutex_init error=%s", last_serror());
	}
#else
	(void) recursive;
	int ret = acl_pthread_mutex_init(mutex_, NULL);
	if (ret)
	{
		SET_ERRNO(ret);
		logger_fatal("pthread_mutex_init error=%s", last_serror());
	}
#endif
}
Beispiel #2
0
void locker::init_mutex(bool use_spinlock acl_unused)
{

#ifdef	ACL_HAS_SPINLOCK
	if (use_spinlock)
	{
		spinlock_ = (pthread_spinlock_t*)
			acl_mycalloc(1, sizeof(pthread_spinlock_t));
		pthread_spin_init(spinlock_, PTHREAD_PROCESS_PRIVATE);
		mutex_= NULL;
		return;
	}
	else
		spinlock_ = NULL;
#endif

	mutex_ = (acl_pthread_mutex_t*)
		acl_mycalloc(1, sizeof(acl_pthread_mutex_t));
#ifdef ACL_WINDOWS
	acl_assert(acl_pthread_mutex_init(mutex_, NULL) == 0);
#else
	acl_assert(pthread_mutexattr_init(&mutex_attr_) == 0);
	acl_assert(pthread_mutexattr_settype(&mutex_attr_,
				PTHREAD_MUTEX_RECURSIVE) == 0);
	acl_assert(acl_pthread_mutex_init(mutex_, &mutex_attr_) == 0);
#endif
}
Beispiel #3
0
static void acl_pthread_init_once(void)
{
	const char *myname = "acl_pthread_init_once";
	int   i;

	acl_pthread_mutex_init(&__thread_lock, NULL);
	__thread_inited = 1;

	for (i = 0; i < ACL_PTHREAD_KEYS_MAX; i++) {
		__tls_key_list[i].destructor = NULL;
		__tls_key_list[i].key = ACL_TLS_OUT_OF_INDEXES;
	}

	__tls_value_list_key = TlsAlloc();
	if (__tls_value_list_key == ACL_TLS_OUT_OF_INDEXES)
		acl_msg_fatal("%s(%d): TlsAlloc error(%s)",
			myname, __LINE__, acl_last_serror());
	if (__tls_value_list_key < 0 || __tls_value_list_key
		>= ACL_PTHREAD_KEYS_MAX)
	{
		acl_msg_fatal("%s(%d): TlsAlloc error(%s), not in(%d, %d)",
			myname, __LINE__, acl_last_serror(),
			0, ACL_PTHREAD_KEYS_MAX);
	}

	__tls_key_list[__tls_value_list_key].destructor = NULL;
	__tls_key_list[__tls_value_list_key].key = __tls_value_list_key;
}
Beispiel #4
0
ACL_MBOX *acl_mbox_create(void)
{
	ACL_MBOX *mbox;
	ACL_SOCKET fds[2];

	if (acl_sane_socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
		acl_msg_error("%s(%d), %s: acl_duplex_pipe error %s",
			__FILE__, __LINE__, __FUNCTION__, acl_last_serror());
		return NULL;
	}

	mbox        = (ACL_MBOX *) acl_mymalloc(sizeof(ACL_MBOX));
	mbox->in    = acl_vstream_fdopen(fds[0], O_RDONLY, sizeof(__key),
			0, ACL_VSTREAM_TYPE_SOCK);
	mbox->out   = acl_vstream_fdopen(fds[1], O_WRONLY, sizeof(__key),
			0, ACL_VSTREAM_TYPE_SOCK);
	mbox->nsend = 0;
	mbox->nread = 0;
	mbox->ypipe = acl_ypipe_new();
	mbox->lock  = (acl_pthread_mutex_t *)
		acl_mycalloc(1, sizeof(acl_pthread_mutex_t));
	if (acl_pthread_mutex_init(mbox->lock, NULL) != 0)
		acl_msg_fatal("%s(%d), %s: acl_pthread_mutex_init error",
			__FILE__, __LINE__, __FUNCTION__);

	return mbox;
}
Beispiel #5
0
/*----------------------------------------------------------------------------*/
ACL_DB_POOL *acl_dbpool_mysql_create(const ACL_DB_INFO *db_info)
{
	char  myname[] = "acl_dbpool_mysql_create";
	ACL_DB_POOL_MYSQL *mysql_pool;

	mysql_pool = (ACL_DB_POOL_MYSQL *) acl_mycalloc(1, sizeof(ACL_DB_POOL_MYSQL));
	if (mysql_pool == NULL) {
		char  tbuf[256];
		acl_msg_fatal("%s, %s(%d): calloc error=%s",
			__FILE__, myname, __LINE__, acl_last_strerror(tbuf, sizeof(tbuf)));
	}

	mysql_pool->db_pool.dbh_peek    = &__dbpool_mysql_peek;
	mysql_pool->db_pool.dbh_check   = &__dbpool_mysql_check;
	mysql_pool->db_pool.dbh_release = &__dbpool_mysql_release;
	mysql_pool->db_pool.dbh_export  = &__dbpool_mysql_export;
	mysql_pool->db_pool.dbh_close   = &__dbpool_mysql_close;

	mysql_pool->db_pool.destroy     = &__dbpool_mysql_destroy;

	mysql_pool->handles  = acl_array_create(db_info->db_max);
	acl_pthread_mutex_init(&mysql_pool->mutex, NULL);

	return ((ACL_DB_POOL *) mysql_pool);
}
Beispiel #6
0
ACL_EVENT *event_new_select_thr(void)
{
	const char *myname = "event_new_select_thr";
	EVENT_SELECT_THR *event_thr;
	int   status;
	char  ebuf[256];

	event_thr = (EVENT_SELECT_THR*) event_alloc(sizeof(EVENT_SELECT_THR));

	snprintf(event_thr->event.event.name, sizeof(event_thr->event.event.name),
		 "thread events - select");
	event_thr->event.event.event_mode           = ACL_EVENT_SELECT;
	event_thr->event.event.use_thread           = 1;
	event_thr->event.event.loop_fn              = event_loop;
	event_thr->event.event.free_fn              = event_free;
	event_thr->event.event.add_dog_fn           = event_add_dog;
	event_thr->event.event.enable_read_fn       = event_enable_read;
	event_thr->event.event.enable_write_fn      = event_enable_write;
	event_thr->event.event.enable_listen_fn     = event_enable_listen;
	event_thr->event.event.disable_readwrite_fn = event_disable_readwrite;
	event_thr->event.event.isrset_fn            = event_isrset;
	event_thr->event.event.iswset_fn            = event_iswset;
	event_thr->event.event.isxset_fn            = event_isxset;
	event_thr->event.event.timer_request        = event_timer_request_thr;
	event_thr->event.event.timer_cancel         = event_timer_cancel_thr;
	event_thr->event.event.timer_keep           = event_timer_keep_thr;
	event_thr->event.event.timer_ifkeep         = event_timer_ifkeep_thr;

        FD_ZERO(&event_thr->rmask);
        FD_ZERO(&event_thr->wmask);
        FD_ZERO(&event_thr->xmask);

	status = acl_pthread_mutex_init(&event_thr->event.tm_mutex, NULL);
	if (status != 0) {
		acl_msg_fatal("%s(%d)->%s: pthread_mutex_init error=%s",
			__FILE__, __LINE__, myname, acl_last_strerror(ebuf, sizeof(ebuf)));
	}

	status = acl_pthread_mutex_init(&event_thr->event.tb_mutex, NULL);
	if (status != 0) {
		acl_msg_fatal("%s(%d)->%s: pthread_mutex_init error=%s",
			__FILE__, __LINE__, myname, acl_last_strerror(ebuf, sizeof(ebuf)));
	}

	return ((ACL_EVENT *) event_thr);
}
Beispiel #7
0
void proctl_service_init()
{
	const char *myname = "proctl_service_init";
	char  ebuf[256];

	__services = acl_array_create(10);
	__services_wait = acl_fifo_new();
	acl_pthread_mutex_init(&__mutex_running_service, NULL);
	acl_pthread_mutex_init(&__mutex_waiting_service, NULL);
	handles_init();

	__sem_handle = CreateSemaphore(NULL, 0, 1024, NULL);
	if (__sem_handle == NULL)
		acl_msg_fatal("%s(%d): CreateSemaphore error(%s)",
			myname, __LINE__, acl_last_strerror(ebuf, sizeof(ebuf)));
	handles_add(__sem_handle);
}
Beispiel #8
0
ACL_ATOMIC *acl_atomic_new(void)
{
	ACL_ATOMIC *self = (ACL_ATOMIC*) acl_mymalloc(sizeof(ACL_ATOMIC));

#ifndef HAS_ATOMIC
	acl_pthread_mutex_init(&self->lock, NULL);
#endif
	self->value = NULL;
	return self;
}
Beispiel #9
0
static void mempool_bench_test2(const char *label, int use_pool, int mutex, int loop, int size)
{
	char *buf;
	time_t begin, end;
	int   i;
	static int __pool_inited = 0;

	if (use_pool) {
		if (__pool_inited == 0) {
			acl_mempool_open(__max_size, mutex);
			__pool_inited = 1;
		} else {
			acl_mempool_ctl(ACL_MEMPOOL_CTL_MUTEX, mutex,
					ACL_MEMPOOL_CTL_END);
		}

		i = 0;
		time(&begin);
		while (i++ < loop) {
			buf = MALLOC(size);
			FREE(buf);
		}
		time(&end);
	} else {
		acl_pthread_mutex_t lock;

		if (mutex)
			acl_pthread_mutex_init(&lock, NULL);

		if (__pool_inited) {
			acl_mempool_ctl(ACL_MEMPOOL_CTL_DISABLE, 1,
					ACL_MEMPOOL_CTL_END);
		}

		i = 0;
		time(&begin);
		while (i++ < loop) {
			buf = MALLOC(size);
			FREE(buf);
		}
		time(&end);

		if (mutex)
			acl_pthread_mutex_destroy(&lock);
	}

	if (use_pool)
		printf("%s: time cost is %ld seconds, count is %d, total pool alloc %d\r\n",
			label, (long int) end - begin, loop, acl_mempool_total_allocated());
	else
		printf("%s: time cost is %ld seconds, count is %d\r\n",
			label, (long int) end - begin, loop);
}
Beispiel #10
0
static void test_nested_mutex(void)
{
	acl_pthread_t tid;
	acl_pthread_attr_t attr;
	static acl_pthread_mutex_t mutex;

#ifdef ACL_UNIX
	pthread_spin_init(&__spin_lock, 0);
#endif

	acl_pthread_mutex_init(&mutex, NULL);
	acl_pthread_attr_init(&attr);
	acl_pthread_attr_setdetachstate(&attr, ACL_PTHREAD_CREATE_DETACHED);
	acl_pthread_create(&tid, &attr, thread_nested_mutex, &mutex);
	acl_pthread_create(&tid, &attr, thread_nested_mutex, &mutex);
}
Beispiel #11
0
rpc_request::rpc_request()
: ipc_(NULL)
, wait_timedout_(false)
{
	dat_.req = this;
	dat_.ctx = NULL;

	cond_count_ = 0;
	cond_ = (acl_pthread_cond_t*) acl_mycalloc(
		1, sizeof(acl_pthread_cond_t));
	acl_pthread_cond_init(cond_, NULL);

	lock_ = (acl_pthread_mutex_t*) acl_mycalloc(
		1, sizeof(acl_pthread_mutex_t));
	acl_pthread_mutex_init(lock_, NULL);
}
Beispiel #12
0
static void test_thread_pool(void)
{
	acl_pthread_pool_t *thr_pool;
	ACL_VSTREAM *fp = acl_vstream_fopen("test.log", O_WRONLY | O_CREAT, 0600, 4096);
	int   i;

	acl_pthread_mutex_init(&__mutex, NULL);
	thr_pool = acl_thread_pool_create(10, 10);

	for (i = 0; i < 1000000; i++) {
		RUN_CTX *ctx = (RUN_CTX*) acl_mymalloc(sizeof(RUN_CTX));
		ctx->fp = fp;
		ctx->i = i;
		acl_pthread_pool_add(thr_pool, run_thread, ctx);
	}

	acl_pthread_pool_destroy(thr_pool);
	acl_pthread_mutex_destroy(&__mutex);
	acl_vstream_close(fp);
}
Beispiel #13
0
acl_pthread_mutex_t *acl_pthread_mutex_create(void)
{
	const char *myname = "acl_pthread_mutex_create";
	acl_pthread_mutex_t *mutex;
	int   status;

	mutex = (acl_pthread_mutex_t *)
		acl_mymalloc(sizeof(acl_pthread_mutex_t));
	if (mutex == NULL)
		return NULL;

	if ((status = acl_pthread_mutex_init(mutex, NULL)) < 0) {
		acl_msg_error("%s: init mutex error(%s)",
			myname, acl_last_serror());

		acl_myfree(mutex);
		return NULL;
	}

	return mutex;
}
Beispiel #14
0
acl_pthread_mutex_t *acl_pthread_mutex_create(void)
{
	const char *myname = "acl_pthread_mutex_create";
	acl_pthread_mutex_t *mutex;
	int   status;
	char  buf[256];

	mutex = (acl_pthread_mutex_t *)
		acl_mymalloc(sizeof(acl_pthread_mutex_t));
	if (mutex == NULL)
		return (NULL);

	if ((status = acl_pthread_mutex_init(mutex, NULL)) < 0) {
		acl_msg_error("%s: init mutex error(%s)(%s)",
				myname, acl_last_strerror(buf, sizeof(buf)),
				strerror(status));

		acl_myfree(mutex);
		return (NULL);
	}

	return (mutex);
}
Beispiel #15
0
static void mempool_bench_test(const char *label, int mutex, int loop, acl_mem_type type, int size)
{
	int   i = 0;
	time_t begin = 0, end = 0;
	void *buf;
#ifdef	MUTEX_INIT
	acl_pthread_mutex_t lock;
#elif defined(WIN32)
	acl_pthread_mutex_t lock;
#define MUTEX_INIT
#else
	acl_pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
#endif

	if (mutex) {
#ifdef	MUTEX_INIT
		acl_pthread_mutex_init(&lock, NULL);
#endif
		time(&begin);
		while (i++ < loop) {
			acl_pthread_mutex_lock(&lock);
			acl_pthread_mutex_unlock(&lock);
		}
		time(&end);
		printf("lock and unkock, loop %d, time cost is %ld\r\n", loop, (long int) end - begin);

		i = 0;
		if (type > ACL_MEM_TYPE_NONE && type < ACL_MEM_TYPE_MAX) {
			time(&begin);
			while (i++ < loop) {
				acl_pthread_mutex_lock(&lock);
				buf = acl_allocator_mem_alloc(__var_allocator, type);
				acl_pthread_mutex_unlock(&lock);

				acl_pthread_mutex_lock(&lock);
				acl_allocator_mem_free(__var_allocator, type, buf);
				acl_pthread_mutex_unlock(&lock);
			}
			time(&end);
		} else if (type == MEM_TYPE_GROSS) {
			acl_pthread_mutex_lock(&lock);
			buf = acl_allocator_membuf_alloc(__FILE__, __LINE__,
				__var_allocator, size);
			acl_pthread_mutex_unlock(&lock);

			acl_pthread_mutex_lock(&lock);
			acl_allocator_membuf_free(__FILE__, __LINE__,
				__var_allocator, buf);
			acl_pthread_mutex_unlock(&lock);
		} else {
			time(&begin);
			while (i++ < loop) {
				acl_pthread_mutex_lock(&lock);
				buf = MALLOC(size);
				acl_pthread_mutex_unlock(&lock);

				acl_pthread_mutex_lock(&lock);
				FREE(buf);
				acl_pthread_mutex_unlock(&lock);
			}
			time(&end);
		}
#ifdef	MUTEX_INIT
		acl_pthread_mutex_destroy(&lock);
#endif
	} else {
		if (type > ACL_MEM_TYPE_NONE && type < ACL_MEM_TYPE_MAX) {
			time(&begin);
			while (i++ < loop) {
				buf = acl_allocator_mem_alloc(__var_allocator, type);
				acl_allocator_mem_free(__var_allocator, type, buf);
			}
			time(&end);
		} else if (type == MEM_TYPE_GROSS) {
			buf = acl_allocator_membuf_alloc(__FILE__, __LINE__,
				__var_allocator, size);
			acl_allocator_membuf_free(__FILE__, __LINE__,
				__var_allocator, buf);
		} else {
			time(&begin);
			while (i++ < loop) {
				buf = MALLOC(size);
				FREE(buf);
			}
			time(&end);
		}
	}

	printf("%s: time cost is %ld seconds, count is %d\r\n",
		label, (long int) end - begin, loop);
}