xref: /freebsd/contrib/ofed/opensm/complib/cl_thread.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
1 /*
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.
5  *
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:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
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.
24  *
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
32  * SOFTWARE.
33  *
34  */
35 
36 #if HAVE_CONFIG_H
37 #  include <config.h>
38 #endif				/* HAVE_CONFIG_H */
39 
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <sys/sysctl.h>
43 #include <complib/cl_thread.h>
44 
45 /*
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.
49  */
50 static void *__cl_thread_wrapper(void *arg)
51 {
52 	cl_thread_t *p_thread = (cl_thread_t *) arg;
53 
54 	CL_ASSERT(p_thread);
55 	CL_ASSERT(p_thread->pfn_callback);
56 
57 	p_thread->pfn_callback((void *)p_thread->context);
58 
59 	return (NULL);
60 }
61 
62 void cl_thread_construct(IN cl_thread_t * const p_thread)
63 {
64 	CL_ASSERT(p_thread);
65 
66 	p_thread->osd.state = CL_UNINITIALIZED;
67 }
68 
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)
73 {
74 	int ret;
75 
76 	CL_ASSERT(p_thread);
77 
78 	cl_thread_construct(p_thread);
79 
80 	/* Initialize the thread structure */
81 	p_thread->pfn_callback = pfn_callback;
82 	p_thread->context = context;
83 
84 	ret = pthread_create(&p_thread->osd.id, NULL,
85 			     __cl_thread_wrapper, (void *)p_thread);
86 
87 	if (ret != 0)		/* pthread_create returns a "0" for success */
88 		return (CL_ERROR);
89 
90 	p_thread->osd.state = CL_INITIALIZED;
91 
92 	return (CL_SUCCESS);
93 }
94 
95 void cl_thread_destroy(IN cl_thread_t * const p_thread)
96 {
97 	CL_ASSERT(p_thread);
98 	CL_ASSERT(cl_is_state_valid(p_thread->osd.state));
99 
100 	if (p_thread->osd.state == CL_INITIALIZED)
101 		pthread_join(p_thread->osd.id, NULL);
102 
103 	p_thread->osd.state = CL_UNINITIALIZED;
104 }
105 
106 void cl_thread_suspend(IN const uint32_t pause_ms)
107 {
108 	/* Convert to micro seconds */
109 	usleep(pause_ms * 1000);
110 }
111 
112 void cl_thread_stall(IN const uint32_t pause_us)
113 {
114 	/*
115 	 * Not quite a busy wait, but Linux is lacking in terms of high
116 	 * resolution time stamp information in user mode.
117 	 */
118 	usleep(pause_us);
119 }
120 
121 int cl_proc_count(void)
122 {
123 	int ret;
124 	size_t size = sizeof(ret);
125 
126 	if (sysctlbyname("hw.ncpu", &ret, &size, NULL, 0) != 0 || ret < 1)
127 		ret = 1;
128 	return ret;
129 }
130 
131 boolean_t cl_is_current_thread(IN const cl_thread_t * const p_thread)
132 {
133 	pthread_t current;
134 
135 	CL_ASSERT(p_thread);
136 	CL_ASSERT(p_thread->osd.state == CL_INITIALIZED);
137 
138 	current = pthread_self();
139 	return (pthread_equal(current, p_thread->osd.id));
140 }
141