Blame complib/cl_thread.c

Packit 13e616
/*
Packit 13e616
 * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
Packit 13e616
 * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
Packit 13e616
 * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
Packit 13e616
 *
Packit 13e616
 * This software is available to you under a choice of one of two
Packit 13e616
 * licenses.  You may choose to be licensed under the terms of the GNU
Packit 13e616
 * General Public License (GPL) Version 2, available from the file
Packit 13e616
 * COPYING in the main directory of this source tree, or the
Packit 13e616
 * OpenIB.org BSD license below:
Packit 13e616
 *
Packit 13e616
 *     Redistribution and use in source and binary forms, with or
Packit 13e616
 *     without modification, are permitted provided that the following
Packit 13e616
 *     conditions are met:
Packit 13e616
 *
Packit 13e616
 *      - Redistributions of source code must retain the above
Packit 13e616
 *        copyright notice, this list of conditions and the following
Packit 13e616
 *        disclaimer.
Packit 13e616
 *
Packit 13e616
 *      - Redistributions in binary form must reproduce the above
Packit 13e616
 *        copyright notice, this list of conditions and the following
Packit 13e616
 *        disclaimer in the documentation and/or other materials
Packit 13e616
 *        provided with the distribution.
Packit 13e616
 *
Packit 13e616
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit 13e616
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit 13e616
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Packit 13e616
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
Packit 13e616
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
Packit 13e616
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit 13e616
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Packit 13e616
 * SOFTWARE.
Packit 13e616
 *
Packit 13e616
 */
Packit 13e616
Packit 13e616
#if HAVE_CONFIG_H
Packit 13e616
#  include <config.h>
Packit 13e616
#endif				/* HAVE_CONFIG_H */
Packit 13e616
Packit 13e616
#include <stdio.h>
Packit 13e616
#include <unistd.h>
Packit 13e616
#include <sys/sysinfo.h>
Packit 13e616
#include <complib/cl_thread.h>
Packit 13e616
Packit 13e616
/*
Packit 13e616
 * Internal function to run a new user mode thread.
Packit 13e616
 * This function is always run as a result of creation a new user mode thread.
Packit 13e616
 * Its main job is to synchronize the creation and running of the new thread.
Packit 13e616
 */
Packit 13e616
static void *__cl_thread_wrapper(void *arg)
Packit 13e616
{
Packit 13e616
	cl_thread_t *p_thread = (cl_thread_t *) arg;
Packit 13e616
Packit 13e616
	CL_ASSERT(p_thread);
Packit 13e616
	CL_ASSERT(p_thread->pfn_callback);
Packit 13e616
Packit 13e616
	p_thread->pfn_callback((void *)p_thread->context);
Packit 13e616
Packit 13e616
	return (NULL);
Packit 13e616
}
Packit 13e616
Packit 13e616
void cl_thread_construct(IN cl_thread_t * const p_thread)
Packit 13e616
{
Packit 13e616
	CL_ASSERT(p_thread);
Packit 13e616
Packit 13e616
	p_thread->osd.state = CL_UNINITIALIZED;
Packit 13e616
}
Packit 13e616
Packit 13e616
cl_status_t cl_thread_init(IN cl_thread_t * const p_thread,
Packit 13e616
			   IN cl_pfn_thread_callback_t pfn_callback,
Packit 13e616
			   IN const void *const context,
Packit 13e616
			   IN const char *const name)
Packit 13e616
{
Packit 13e616
	int ret;
Packit 13e616
Packit 13e616
	CL_ASSERT(p_thread);
Packit 13e616
Packit 13e616
	cl_thread_construct(p_thread);
Packit 13e616
Packit 13e616
	/* Initialize the thread structure */
Packit 13e616
	p_thread->pfn_callback = pfn_callback;
Packit 13e616
	p_thread->context = context;
Packit 13e616
Packit 13e616
	ret = pthread_create(&p_thread->osd.id, NULL,
Packit 13e616
			     __cl_thread_wrapper, (void *)p_thread);
Packit 13e616
Packit 13e616
	if (ret != 0)		/* pthread_create returns a "0" for success */
Packit 13e616
		return (CL_ERROR);
Packit 13e616
Packit 13e616
	p_thread->osd.state = CL_INITIALIZED;
Packit 13e616
Packit 13e616
	return (CL_SUCCESS);
Packit 13e616
}
Packit 13e616
Packit 13e616
void cl_thread_destroy(IN cl_thread_t * const p_thread)
Packit 13e616
{
Packit 13e616
	CL_ASSERT(p_thread);
Packit 13e616
	CL_ASSERT(cl_is_state_valid(p_thread->osd.state));
Packit 13e616
Packit 13e616
	if (p_thread->osd.state == CL_INITIALIZED)
Packit 13e616
		pthread_join(p_thread->osd.id, NULL);
Packit 13e616
Packit 13e616
	p_thread->osd.state = CL_UNINITIALIZED;
Packit 13e616
}
Packit 13e616
Packit 13e616
void cl_thread_suspend(IN const uint32_t pause_ms)
Packit 13e616
{
Packit 13e616
	/* Convert to micro seconds */
Packit 13e616
	usleep(pause_ms * 1000);
Packit 13e616
}
Packit 13e616
Packit 13e616
void cl_thread_stall(IN const uint32_t pause_us)
Packit 13e616
{
Packit 13e616
	/*
Packit 13e616
	 * Not quite a busy wait, but Linux is lacking in terms of high
Packit 13e616
	 * resolution time stamp information in user mode.
Packit 13e616
	 */
Packit 13e616
	usleep(pause_us);
Packit 13e616
}
Packit 13e616
Packit 13e616
int cl_proc_count(void)
Packit 13e616
{
Packit 13e616
	uint32_t ret;
Packit 13e616
Packit 13e616
	ret = get_nprocs();
Packit 13e616
	if (!ret)
Packit 13e616
		return 1;	/* Workaround for PPC where get_nprocs() returns 0 */
Packit 13e616
Packit 13e616
	return ret;
Packit 13e616
}
Packit 13e616
Packit 13e616
boolean_t cl_is_current_thread(IN const cl_thread_t * const p_thread)
Packit 13e616
{
Packit 13e616
	pthread_t current;
Packit 13e616
Packit 13e616
	CL_ASSERT(p_thread);
Packit 13e616
	CL_ASSERT(p_thread->osd.state == CL_INITIALIZED);
Packit 13e616
Packit 13e616
	current = pthread_self();
Packit 13e616
	return (pthread_equal(current, p_thread->osd.id));
Packit 13e616
}