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 */
40 #include <complib/cl_event.h>
41 #include <complib/cl_debug.h>
43 #include <sys/errno.h>
45 void cl_event_construct(IN cl_event_t * p_event)
49 p_event->state = CL_UNINITIALIZED;
53 cl_event_init(IN cl_event_t * const p_event, IN const boolean_t manual_reset)
57 cl_event_construct(p_event);
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;
68 void cl_event_destroy(IN cl_event_t * const p_event)
70 CL_ASSERT(cl_is_state_valid(p_event->state));
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);
79 p_event->state = CL_UNINITIALIZED;
82 cl_status_t cl_event_signal(IN cl_event_t * const p_event)
84 /* Make sure that the event was started */
85 CL_ASSERT(p_event->state == CL_INITIALIZED);
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);
93 pthread_cond_signal(&p_event->condvar);
95 pthread_mutex_unlock(&p_event->mutex);
100 cl_status_t cl_event_reset(IN cl_event_t * const p_event)
102 /* Make sure that the event was started */
103 CL_ASSERT(p_event->state == CL_INITIALIZED);
105 pthread_mutex_lock(&p_event->mutex);
106 p_event->signaled = FALSE;
107 pthread_mutex_unlock(&p_event->mutex);
113 cl_event_wait_on(IN cl_event_t * const p_event,
114 IN const uint32_t wait_us, IN const boolean_t interruptible)
118 struct timespec timeout;
119 struct timeval curtime;
121 /* Make sure that the event was Started */
122 CL_ASSERT(p_event->state == CL_INITIALIZED);
124 pthread_mutex_lock(&p_event->mutex);
126 /* Return immediately if the event is signalled. */
127 if (p_event->signaled) {
128 if (!p_event->manual_reset)
129 p_event->signaled = FALSE;
131 pthread_mutex_unlock(&p_event->mutex);
135 /* If just testing the state, return CL_TIMEOUT. */
137 pthread_mutex_unlock(&p_event->mutex);
141 if (wait_us == EVENT_NO_TIMEOUT) {
142 /* Wait for condition variable to be signaled or broadcast. */
143 if (pthread_cond_wait
144 (&p_event->condvar, &p_event->mutex))
145 status = CL_NOT_DONE;
149 /* Get the current time */
150 if (gettimeofday(&curtime, NULL) == 0) {
151 timeout.tv_sec = curtime.tv_sec + (wait_us / 1000000);
153 (curtime.tv_usec + (wait_us % 1000000)) * 1000;
155 wait_ret = pthread_cond_timedwait(&p_event->condvar,
161 signaled ? CL_SUCCESS : CL_NOT_DONE);
162 else if (wait_ret == ETIMEDOUT)
165 status = CL_NOT_DONE;
170 if (!p_event->manual_reset)
171 p_event->signaled = FALSE;
173 pthread_mutex_unlock(&p_event->mutex);