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 <sys/sysctl.h>
43 #include <complib/cl_thread.h>
46 * Internal function to run a new user mode thread.
47 * This function is always run as a result of creation a new user mode thread.
48 * Its main job is to synchronize the creation and running of the new thread.
50 static void *__cl_thread_wrapper(void *arg)
52 cl_thread_t *p_thread = (cl_thread_t *) arg;
55 CL_ASSERT(p_thread->pfn_callback);
57 p_thread->pfn_callback((void *)p_thread->context);
62 void cl_thread_construct(IN cl_thread_t * const p_thread)
66 p_thread->osd.state = CL_UNINITIALIZED;
69 cl_status_t 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,
72 IN const char *const name)
78 cl_thread_construct(p_thread);
80 /* Initialize the thread structure */
81 p_thread->pfn_callback = pfn_callback;
82 p_thread->context = context;
84 ret = pthread_create(&p_thread->osd.id, NULL,
85 __cl_thread_wrapper, (void *)p_thread);
87 if (ret != 0) /* pthread_create returns a "0" for success */
90 p_thread->osd.state = CL_INITIALIZED;
95 void cl_thread_destroy(IN cl_thread_t * const p_thread)
98 CL_ASSERT(cl_is_state_valid(p_thread->osd.state));
100 if (p_thread->osd.state == CL_INITIALIZED)
101 pthread_join(p_thread->osd.id, NULL);
103 p_thread->osd.state = CL_UNINITIALIZED;
106 void cl_thread_suspend(IN const uint32_t pause_ms)
108 /* Convert to micro seconds */
109 usleep(pause_ms * 1000);
112 void cl_thread_stall(IN const uint32_t pause_us)
115 * Not quite a busy wait, but Linux is lacking in terms of high
116 * resolution time stamp information in user mode.
121 int cl_proc_count(void)
124 size_t size = sizeof(ret);
126 if (sysctlbyname("hw.ncpu", &ret, &size, NULL, 0) != 0 || ret < 1)
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));