1 /*
2 * Copyright (c) 2004-2009 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 <complib/cl_event.h>
41 #include <complib/cl_debug.h>
42 #include <sys/time.h>
43 #include <sys/errno.h>
44
cl_event_construct(IN cl_event_t * p_event)45 void cl_event_construct(IN cl_event_t * p_event)
46 {
47 CL_ASSERT(p_event);
48
49 p_event->state = CL_UNINITIALIZED;
50 }
51
cl_event_init(IN cl_event_t * const p_event,IN const boolean_t manual_reset)52 cl_status_t cl_event_init(IN cl_event_t * const p_event,
53 IN const boolean_t manual_reset)
54 {
55 CL_ASSERT(p_event);
56
57 cl_event_construct(p_event);
58
59 pthread_cond_init(&p_event->condvar, NULL);
60 pthread_mutex_init(&p_event->mutex, NULL);
61 p_event->signaled = FALSE;
62 p_event->manual_reset = manual_reset;
63 p_event->state = CL_INITIALIZED;
64
65 return CL_SUCCESS;
66 }
67
cl_event_destroy(IN cl_event_t * const p_event)68 void cl_event_destroy(IN cl_event_t * const p_event)
69 {
70 CL_ASSERT(cl_is_state_valid(p_event->state));
71
72 /* Destroy only if the event was constructed */
73 if (p_event->state == CL_INITIALIZED) {
74 pthread_cond_broadcast(&p_event->condvar);
75 pthread_cond_destroy(&p_event->condvar);
76 pthread_mutex_destroy(&p_event->mutex);
77 }
78
79 p_event->state = CL_UNINITIALIZED;
80 }
81
cl_event_signal(IN cl_event_t * const p_event)82 cl_status_t cl_event_signal(IN cl_event_t * const p_event)
83 {
84 /* Make sure that the event was started */
85 CL_ASSERT(p_event->state == CL_INITIALIZED);
86
87 pthread_mutex_lock(&p_event->mutex);
88 p_event->signaled = TRUE;
89 /* Wake up one or all depending on whether the event is auto-resetting. */
90 if (p_event->manual_reset)
91 pthread_cond_broadcast(&p_event->condvar);
92 else
93 pthread_cond_signal(&p_event->condvar);
94
95 pthread_mutex_unlock(&p_event->mutex);
96
97 return CL_SUCCESS;
98 }
99
cl_event_reset(IN cl_event_t * const p_event)100 cl_status_t cl_event_reset(IN cl_event_t * const p_event)
101 {
102 /* Make sure that the event was started */
103 CL_ASSERT(p_event->state == CL_INITIALIZED);
104
105 pthread_mutex_lock(&p_event->mutex);
106 p_event->signaled = FALSE;
107 pthread_mutex_unlock(&p_event->mutex);
108
109 return CL_SUCCESS;
110 }
111
cl_event_wait_on(IN cl_event_t * const p_event,IN const uint32_t wait_us,IN const boolean_t interruptible)112 cl_status_t cl_event_wait_on(IN cl_event_t * const p_event,
113 IN const uint32_t wait_us,
114 IN const boolean_t interruptible)
115 {
116 cl_status_t status;
117 int wait_ret;
118 struct timespec timeout;
119 struct timeval curtime;
120
121 /* Make sure that the event was Started */
122 CL_ASSERT(p_event->state == CL_INITIALIZED);
123
124 pthread_mutex_lock(&p_event->mutex);
125
126 /* Return immediately if the event is signalled. */
127 if (p_event->signaled) {
128 if (!p_event->manual_reset)
129 p_event->signaled = FALSE;
130
131 pthread_mutex_unlock(&p_event->mutex);
132 return CL_SUCCESS;
133 }
134
135 /* If just testing the state, return CL_TIMEOUT. */
136 if (wait_us == 0) {
137 pthread_mutex_unlock(&p_event->mutex);
138 return CL_TIMEOUT;
139 }
140
141 if (wait_us == EVENT_NO_TIMEOUT) {
142 /* Wait for condition variable to be signaled or broadcast. */
143 if (pthread_cond_wait(&p_event->condvar, &p_event->mutex))
144 status = CL_NOT_DONE;
145 else
146 status = CL_SUCCESS;
147 } else {
148 /* Get the current time */
149 if (gettimeofday(&curtime, NULL) == 0) {
150 unsigned long n_sec =
151 (curtime.tv_usec + (wait_us % 1000000)) * 1000;
152 timeout.tv_sec = curtime.tv_sec + (wait_us / 1000000)
153 + (n_sec / 1000000000);
154 timeout.tv_nsec = n_sec % 1000000000;
155
156 wait_ret = pthread_cond_timedwait(&p_event->condvar,
157 &p_event->mutex,
158 &timeout);
159 if (wait_ret == 0)
160 status =
161 (p_event->
162 signaled ? CL_SUCCESS : CL_NOT_DONE);
163 else if (wait_ret == ETIMEDOUT)
164 status = CL_TIMEOUT;
165 else
166 status = CL_NOT_DONE;
167 } else
168 status = CL_ERROR;
169 }
170 if (!p_event->manual_reset)
171 p_event->signaled = FALSE;
172
173 pthread_mutex_unlock(&p_event->mutex);
174 return status;
175 }
176