1219820Sjeff/*
2219820Sjeff * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
3219820Sjeff * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4219820Sjeff * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
5219820Sjeff *
6219820Sjeff * This software is available to you under a choice of one of two
7219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
8219820Sjeff * General Public License (GPL) Version 2, available from the file
9219820Sjeff * COPYING in the main directory of this source tree, or the
10219820Sjeff * OpenIB.org BSD license below:
11219820Sjeff *
12219820Sjeff *     Redistribution and use in source and binary forms, with or
13219820Sjeff *     without modification, are permitted provided that the following
14219820Sjeff *     conditions are met:
15219820Sjeff *
16219820Sjeff *      - Redistributions of source code must retain the above
17219820Sjeff *        copyright notice, this list of conditions and the following
18219820Sjeff *        disclaimer.
19219820Sjeff *
20219820Sjeff *      - Redistributions in binary form must reproduce the above
21219820Sjeff *        copyright notice, this list of conditions and the following
22219820Sjeff *        disclaimer in the documentation and/or other materials
23219820Sjeff *        provided with the distribution.
24219820Sjeff *
25219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32219820Sjeff * SOFTWARE.
33219820Sjeff *
34219820Sjeff */
35219820Sjeff
36219820Sjeff#if HAVE_CONFIG_H
37219820Sjeff#  include <config.h>
38219820Sjeff#endif				/* HAVE_CONFIG_H */
39219820Sjeff
40219820Sjeff#include <stdio.h>
41219820Sjeff#include <unistd.h>
42219820Sjeff#include <complib/cl_thread.h>
43219820Sjeff
44219820Sjeff/*
45219820Sjeff * Internal function to run a new user mode thread.
46219820Sjeff * This function is always run as a result of creation a new user mode thread.
47219820Sjeff * Its main job is to synchronize the creation and running of the new thread.
48219820Sjeff */
49219820Sjeffstatic void *__cl_thread_wrapper(void *arg)
50219820Sjeff{
51219820Sjeff	cl_thread_t *p_thread = (cl_thread_t *) arg;
52219820Sjeff
53219820Sjeff	CL_ASSERT(p_thread);
54219820Sjeff	CL_ASSERT(p_thread->pfn_callback);
55219820Sjeff
56219820Sjeff	p_thread->pfn_callback((void *)p_thread->context);
57219820Sjeff
58219820Sjeff	return (NULL);
59219820Sjeff}
60219820Sjeff
61219820Sjeffvoid cl_thread_construct(IN cl_thread_t * const p_thread)
62219820Sjeff{
63219820Sjeff	CL_ASSERT(p_thread);
64219820Sjeff
65219820Sjeff	p_thread->osd.state = CL_UNINITIALIZED;
66219820Sjeff}
67219820Sjeff
68219820Sjeffcl_status_t
69219820Sjeffcl_thread_init(IN cl_thread_t * const p_thread,
70219820Sjeff	       IN cl_pfn_thread_callback_t pfn_callback,
71219820Sjeff	       IN const void *const context, IN const char *const name)
72219820Sjeff{
73219820Sjeff	int ret;
74219820Sjeff
75219820Sjeff	CL_ASSERT(p_thread);
76219820Sjeff
77219820Sjeff	cl_thread_construct(p_thread);
78219820Sjeff
79219820Sjeff	/* Initialize the thread structure */
80219820Sjeff	p_thread->pfn_callback = pfn_callback;
81219820Sjeff	p_thread->context = context;
82219820Sjeff
83219820Sjeff	ret = pthread_create(&p_thread->osd.id, NULL,
84219820Sjeff			     __cl_thread_wrapper, (void *)p_thread);
85219820Sjeff
86219820Sjeff	if (ret != 0)		/* pthread_create returns a "0" for success */
87219820Sjeff		return (CL_ERROR);
88219820Sjeff
89219820Sjeff	p_thread->osd.state = CL_INITIALIZED;
90219820Sjeff
91219820Sjeff	return (CL_SUCCESS);
92219820Sjeff}
93219820Sjeff
94219820Sjeffvoid cl_thread_destroy(IN cl_thread_t * const p_thread)
95219820Sjeff{
96219820Sjeff	CL_ASSERT(p_thread);
97219820Sjeff	CL_ASSERT(cl_is_state_valid(p_thread->osd.state));
98219820Sjeff
99219820Sjeff	if (p_thread->osd.state == CL_INITIALIZED)
100219820Sjeff		pthread_join(p_thread->osd.id, NULL);
101219820Sjeff
102219820Sjeff	p_thread->osd.state = CL_UNINITIALIZED;
103219820Sjeff}
104219820Sjeff
105219820Sjeffvoid cl_thread_suspend(IN const uint32_t pause_ms)
106219820Sjeff{
107219820Sjeff	/* Convert to micro seconds */
108219820Sjeff	usleep(pause_ms * 1000);
109219820Sjeff}
110219820Sjeff
111219820Sjeffvoid cl_thread_stall(IN const uint32_t pause_us)
112219820Sjeff{
113219820Sjeff	/*
114219820Sjeff	 * Not quite a busy wait, but Linux is lacking in terms of high
115219820Sjeff	 * resolution time stamp information in user mode.
116219820Sjeff	 */
117219820Sjeff	usleep(pause_us);
118219820Sjeff}
119219820Sjeff
120219820Sjeffint cl_proc_count(void)
121219820Sjeff{
122219820Sjeff	uint32_t ret;
123219820Sjeff
124219820Sjeff	ret = sysconf(_SC_NPROCESSORS_ONLN);
125219820Sjeff	if (!ret)
126219820Sjeff		return 1;	/* Workaround for PPC where get_nprocs() returns 0 */
127219820Sjeff
128219820Sjeff	return ret;
129219820Sjeff}
130219820Sjeff
131219820Sjeffboolean_t cl_is_current_thread(IN const cl_thread_t * const p_thread)
132219820Sjeff{
133219820Sjeff	pthread_t current;
134219820Sjeff
135219820Sjeff	CL_ASSERT(p_thread);
136219820Sjeff	CL_ASSERT(p_thread->osd.state == CL_INITIALIZED);
137219820Sjeff
138219820Sjeff	current = pthread_self();
139219820Sjeff	return (pthread_equal(current, p_thread->osd.id));
140219820Sjeff}
141