1d6b92ffaSHans Petter Selasky /*
2d6b92ffaSHans Petter Selasky * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
3d6b92ffaSHans Petter Selasky * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4d6b92ffaSHans Petter Selasky * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
5d6b92ffaSHans Petter Selasky *
6d6b92ffaSHans Petter Selasky * This software is available to you under a choice of one of two
7d6b92ffaSHans Petter Selasky * licenses. You may choose to be licensed under the terms of the GNU
8d6b92ffaSHans Petter Selasky * General Public License (GPL) Version 2, available from the file
9d6b92ffaSHans Petter Selasky * COPYING in the main directory of this source tree, or the
10d6b92ffaSHans Petter Selasky * OpenIB.org BSD license below:
11d6b92ffaSHans Petter Selasky *
12d6b92ffaSHans Petter Selasky * Redistribution and use in source and binary forms, with or
13d6b92ffaSHans Petter Selasky * without modification, are permitted provided that the following
14d6b92ffaSHans Petter Selasky * conditions are met:
15d6b92ffaSHans Petter Selasky *
16d6b92ffaSHans Petter Selasky * - Redistributions of source code must retain the above
17d6b92ffaSHans Petter Selasky * copyright notice, this list of conditions and the following
18d6b92ffaSHans Petter Selasky * disclaimer.
19d6b92ffaSHans Petter Selasky *
20d6b92ffaSHans Petter Selasky * - Redistributions in binary form must reproduce the above
21d6b92ffaSHans Petter Selasky * copyright notice, this list of conditions and the following
22d6b92ffaSHans Petter Selasky * disclaimer in the documentation and/or other materials
23d6b92ffaSHans Petter Selasky * provided with the distribution.
24d6b92ffaSHans Petter Selasky *
25d6b92ffaSHans Petter Selasky * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26d6b92ffaSHans Petter Selasky * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27d6b92ffaSHans Petter Selasky * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28d6b92ffaSHans Petter Selasky * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29d6b92ffaSHans Petter Selasky * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30d6b92ffaSHans Petter Selasky * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31d6b92ffaSHans Petter Selasky * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32d6b92ffaSHans Petter Selasky * SOFTWARE.
33d6b92ffaSHans Petter Selasky *
34d6b92ffaSHans Petter Selasky */
35d6b92ffaSHans Petter Selasky
36d6b92ffaSHans Petter Selasky #if HAVE_CONFIG_H
37d6b92ffaSHans Petter Selasky # include <config.h>
38d6b92ffaSHans Petter Selasky #endif /* HAVE_CONFIG_H */
39d6b92ffaSHans Petter Selasky
40d6b92ffaSHans Petter Selasky #include <complib/cl_spinlock.h>
41d6b92ffaSHans Petter Selasky
cl_spinlock_construct(IN cl_spinlock_t * const p_spinlock)42d6b92ffaSHans Petter Selasky void cl_spinlock_construct(IN cl_spinlock_t * const p_spinlock)
43d6b92ffaSHans Petter Selasky {
44d6b92ffaSHans Petter Selasky CL_ASSERT(p_spinlock);
45d6b92ffaSHans Petter Selasky
46d6b92ffaSHans Petter Selasky p_spinlock->state = CL_UNINITIALIZED;
47d6b92ffaSHans Petter Selasky }
48d6b92ffaSHans Petter Selasky
cl_spinlock_init(IN cl_spinlock_t * const p_spinlock)49d6b92ffaSHans Petter Selasky cl_status_t cl_spinlock_init(IN cl_spinlock_t * const p_spinlock)
50d6b92ffaSHans Petter Selasky {
51d6b92ffaSHans Petter Selasky CL_ASSERT(p_spinlock);
52d6b92ffaSHans Petter Selasky
53d6b92ffaSHans Petter Selasky cl_spinlock_construct(p_spinlock);
54d6b92ffaSHans Petter Selasky
55d6b92ffaSHans Petter Selasky /* Initialize with pthread_mutexattr_t = NULL */
56d6b92ffaSHans Petter Selasky if (pthread_mutex_init(&p_spinlock->mutex, NULL))
57d6b92ffaSHans Petter Selasky return (CL_ERROR);
58d6b92ffaSHans Petter Selasky
59d6b92ffaSHans Petter Selasky p_spinlock->state = CL_INITIALIZED;
60d6b92ffaSHans Petter Selasky return (CL_SUCCESS);
61d6b92ffaSHans Petter Selasky }
62d6b92ffaSHans Petter Selasky
cl_spinlock_destroy(IN cl_spinlock_t * const p_spinlock)63d6b92ffaSHans Petter Selasky void cl_spinlock_destroy(IN cl_spinlock_t * const p_spinlock)
64d6b92ffaSHans Petter Selasky {
65d6b92ffaSHans Petter Selasky CL_ASSERT(p_spinlock);
66d6b92ffaSHans Petter Selasky CL_ASSERT(cl_is_state_valid(p_spinlock->state));
67d6b92ffaSHans Petter Selasky
68d6b92ffaSHans Petter Selasky if (p_spinlock->state == CL_INITIALIZED) {
69d6b92ffaSHans Petter Selasky p_spinlock->state = CL_UNINITIALIZED;
70d6b92ffaSHans Petter Selasky pthread_mutex_lock(&p_spinlock->mutex);
71d6b92ffaSHans Petter Selasky pthread_mutex_unlock(&p_spinlock->mutex);
72d6b92ffaSHans Petter Selasky pthread_mutex_destroy(&p_spinlock->mutex);
73d6b92ffaSHans Petter Selasky }
74d6b92ffaSHans Petter Selasky p_spinlock->state = CL_UNINITIALIZED;
75d6b92ffaSHans Petter Selasky }
76d6b92ffaSHans Petter Selasky
cl_spinlock_acquire(IN cl_spinlock_t * const p_spinlock)77d6b92ffaSHans Petter Selasky void cl_spinlock_acquire(IN cl_spinlock_t * const p_spinlock)
78d6b92ffaSHans Petter Selasky {
79d6b92ffaSHans Petter Selasky CL_ASSERT(p_spinlock);
80d6b92ffaSHans Petter Selasky CL_ASSERT(p_spinlock->state == CL_INITIALIZED);
81d6b92ffaSHans Petter Selasky
82d6b92ffaSHans Petter Selasky pthread_mutex_lock(&p_spinlock->mutex);
83d6b92ffaSHans Petter Selasky }
84d6b92ffaSHans Petter Selasky
cl_spinlock_release(IN cl_spinlock_t * const p_spinlock)85d6b92ffaSHans Petter Selasky void cl_spinlock_release(IN cl_spinlock_t * const p_spinlock)
86d6b92ffaSHans Petter Selasky {
87d6b92ffaSHans Petter Selasky CL_ASSERT(p_spinlock);
88d6b92ffaSHans Petter Selasky CL_ASSERT(p_spinlock->state == CL_INITIALIZED);
89d6b92ffaSHans Petter Selasky
90d6b92ffaSHans Petter Selasky pthread_mutex_unlock(&p_spinlock->mutex);
91d6b92ffaSHans Petter Selasky }
92