2 * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4 * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 #endif /* HAVE_CONFIG_H */
42 #include <complib/cl_thread.h>
45 * Internal function to run a new user mode thread.
46 * This function is always run as a result of creation a new user mode thread.
47 * Its main job is to synchronize the creation and running of the new thread.
49 static void *__cl_thread_wrapper(void *arg)
51 cl_thread_t *p_thread = (cl_thread_t *) arg;
54 CL_ASSERT(p_thread->pfn_callback);
56 p_thread->pfn_callback((void *)p_thread->context);
61 void cl_thread_construct(IN cl_thread_t * const p_thread)
65 p_thread->osd.state = CL_UNINITIALIZED;
69 cl_thread_init(IN cl_thread_t * const p_thread,
70 IN cl_pfn_thread_callback_t pfn_callback,
71 IN const void *const context, IN const char *const name)
77 cl_thread_construct(p_thread);
79 /* Initialize the thread structure */
80 p_thread->pfn_callback = pfn_callback;
81 p_thread->context = context;
83 ret = pthread_create(&p_thread->osd.id, NULL,
84 __cl_thread_wrapper, (void *)p_thread);
86 if (ret != 0) /* pthread_create returns a "0" for success */
89 p_thread->osd.state = CL_INITIALIZED;
94 void cl_thread_destroy(IN cl_thread_t * const p_thread)
97 CL_ASSERT(cl_is_state_valid(p_thread->osd.state));
99 if (p_thread->osd.state == CL_INITIALIZED)
100 pthread_join(p_thread->osd.id, NULL);
102 p_thread->osd.state = CL_UNINITIALIZED;
105 void cl_thread_suspend(IN const uint32_t pause_ms)
107 /* Convert to micro seconds */
108 usleep(pause_ms * 1000);
111 void cl_thread_stall(IN const uint32_t pause_us)
114 * Not quite a busy wait, but Linux is lacking in terms of high
115 * resolution time stamp information in user mode.
120 int cl_proc_count(void)
124 ret = sysconf(_SC_NPROCESSORS_ONLN);
126 return 1; /* Workaround for PPC where get_nprocs() returns 0 */
131 boolean_t cl_is_current_thread(IN const cl_thread_t * const p_thread)
136 CL_ASSERT(p_thread->osd.state == CL_INITIALIZED);
138 current = pthread_self();
139 return (pthread_equal(current, p_thread->osd.id));