]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/ofed/management/opensm/complib/cl_thread.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / ofed / management / opensm / complib / cl_thread.c
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 <complib/cl_thread.h>
43
44 /*
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.
48  */
49 static void *__cl_thread_wrapper(void *arg)
50 {
51         cl_thread_t *p_thread = (cl_thread_t *) arg;
52
53         CL_ASSERT(p_thread);
54         CL_ASSERT(p_thread->pfn_callback);
55
56         p_thread->pfn_callback((void *)p_thread->context);
57
58         return (NULL);
59 }
60
61 void cl_thread_construct(IN cl_thread_t * const p_thread)
62 {
63         CL_ASSERT(p_thread);
64
65         p_thread->osd.state = CL_UNINITIALIZED;
66 }
67
68 cl_status_t
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)
72 {
73         int ret;
74
75         CL_ASSERT(p_thread);
76
77         cl_thread_construct(p_thread);
78
79         /* Initialize the thread structure */
80         p_thread->pfn_callback = pfn_callback;
81         p_thread->context = context;
82
83         ret = pthread_create(&p_thread->osd.id, NULL,
84                              __cl_thread_wrapper, (void *)p_thread);
85
86         if (ret != 0)           /* pthread_create returns a "0" for success */
87                 return (CL_ERROR);
88
89         p_thread->osd.state = CL_INITIALIZED;
90
91         return (CL_SUCCESS);
92 }
93
94 void cl_thread_destroy(IN cl_thread_t * const p_thread)
95 {
96         CL_ASSERT(p_thread);
97         CL_ASSERT(cl_is_state_valid(p_thread->osd.state));
98
99         if (p_thread->osd.state == CL_INITIALIZED)
100                 pthread_join(p_thread->osd.id, NULL);
101
102         p_thread->osd.state = CL_UNINITIALIZED;
103 }
104
105 void cl_thread_suspend(IN const uint32_t pause_ms)
106 {
107         /* Convert to micro seconds */
108         usleep(pause_ms * 1000);
109 }
110
111 void cl_thread_stall(IN const uint32_t pause_us)
112 {
113         /*
114          * Not quite a busy wait, but Linux is lacking in terms of high
115          * resolution time stamp information in user mode.
116          */
117         usleep(pause_us);
118 }
119
120 int cl_proc_count(void)
121 {
122         uint32_t ret;
123
124         ret = sysconf(_SC_NPROCESSORS_ONLN);
125         if (!ret)
126                 return 1;       /* Workaround for PPC where get_nprocs() returns 0 */
127
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 }