|
Packit |
13e616 |
/*
|
|
Packit |
13e616 |
* Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
|
|
Packit |
13e616 |
* Copyright (c) 2002-2010 Mellanox Technologies LTD. All rights reserved.
|
|
Packit |
13e616 |
* Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* This software is available to you under a choice of one of two
|
|
Packit |
13e616 |
* licenses. You may choose to be licensed under the terms of the GNU
|
|
Packit |
13e616 |
* General Public License (GPL) Version 2, available from the file
|
|
Packit |
13e616 |
* COPYING in the main directory of this source tree, or the
|
|
Packit |
13e616 |
* OpenIB.org BSD license below:
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* Redistribution and use in source and binary forms, with or
|
|
Packit |
13e616 |
* without modification, are permitted provided that the following
|
|
Packit |
13e616 |
* conditions are met:
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* - Redistributions of source code must retain the above
|
|
Packit |
13e616 |
* copyright notice, this list of conditions and the following
|
|
Packit |
13e616 |
* disclaimer.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* - Redistributions in binary form must reproduce the above
|
|
Packit |
13e616 |
* copyright notice, this list of conditions and the following
|
|
Packit |
13e616 |
* disclaimer in the documentation and/or other materials
|
|
Packit |
13e616 |
* provided with the distribution.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
Packit |
13e616 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
Packit |
13e616 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
Packit |
13e616 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
Packit |
13e616 |
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
Packit |
13e616 |
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
Packit |
13e616 |
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
Packit |
13e616 |
* SOFTWARE.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
*/
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
/*
|
|
Packit |
13e616 |
* Abstract:
|
|
Packit |
13e616 |
* This file contains the passive lock, which synchronizes passive threads.
|
|
Packit |
13e616 |
* The passive lock allows multiple readers to access a resource
|
|
Packit |
13e616 |
* simultaneously, exclusive from a single thread allowed writing.
|
|
Packit |
13e616 |
* Several writer threads are allowed - but only one can write at a given time
|
|
Packit |
13e616 |
*/
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
#ifndef _CL_PASSIVE_LOCK_H_
|
|
Packit |
13e616 |
#define _CL_PASSIVE_LOCK_H_
|
|
Packit |
13e616 |
#include <complib/cl_types.h>
|
|
Packit |
13e616 |
#include <pthread.h>
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
#ifdef __cplusplus
|
|
Packit |
13e616 |
# define BEGIN_C_DECLS extern "C" {
|
|
Packit |
13e616 |
# define END_C_DECLS }
|
|
Packit |
13e616 |
#else /* !__cplusplus */
|
|
Packit |
13e616 |
# define BEGIN_C_DECLS
|
|
Packit |
13e616 |
# define END_C_DECLS
|
|
Packit |
13e616 |
#endif /* __cplusplus */
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
BEGIN_C_DECLS
|
|
Packit |
13e616 |
/****h* Component Library/Passive Lock
|
|
Packit |
13e616 |
* NAME
|
|
Packit |
13e616 |
* Passive Lock
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* DESCRIPTION
|
|
Packit |
13e616 |
* The Passive Lock provides synchronization between multiple threads that
|
|
Packit |
13e616 |
* are sharing the lock with a single thread holding the lock exclusively.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* Passive lock works exclusively between threads and cannot be used in
|
|
Packit |
13e616 |
* situations where the caller cannot be put into a waiting state.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* The passive lock functions operate a cl_plock_t structure which should
|
|
Packit |
13e616 |
* be treated as opaque and should be manipulated only through the provided
|
|
Packit |
13e616 |
* functions.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* SEE ALSO
|
|
Packit |
13e616 |
* Structures:
|
|
Packit |
13e616 |
* cl_plock_t
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* Initialization:
|
|
Packit |
13e616 |
* cl_plock_construct, cl_plock_init, cl_plock_destroy
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* Manipulation
|
|
Packit |
13e616 |
* cl_plock_acquire, cl_plock_excl_acquire, cl_plock_release
|
|
Packit |
13e616 |
*********/
|
|
Packit |
13e616 |
/****s* Component Library: Passive Lock/cl_plock_t
|
|
Packit |
13e616 |
* NAME
|
|
Packit |
13e616 |
* cl_plock_t
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* DESCRIPTION
|
|
Packit |
13e616 |
* Passive Lock structure.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* The cl_plock_t structure should be treated as opaque and should
|
|
Packit |
13e616 |
* be manipulated only through the provided functions.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* SYNOPSIS
|
|
Packit |
13e616 |
*/
|
|
Packit |
13e616 |
typedef struct _cl_plock {
|
|
Packit |
13e616 |
pthread_rwlock_t lock;
|
|
Packit |
13e616 |
cl_state_t state;
|
|
Packit |
13e616 |
} cl_plock_t;
|
|
Packit |
13e616 |
/*
|
|
Packit |
13e616 |
* FIELDS
|
|
Packit |
13e616 |
* lock
|
|
Packit |
13e616 |
* Pthread RWLOCK object
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* state
|
|
Packit |
13e616 |
* Records the current state of the lock, such as initialized,
|
|
Packit |
13e616 |
* destroying, etc.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* SEE ALSO
|
|
Packit |
13e616 |
* Passive Lock
|
|
Packit |
13e616 |
*********/
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
/****f* Component Library: Passive Lock/cl_plock_construct
|
|
Packit |
13e616 |
* NAME
|
|
Packit |
13e616 |
* cl_plock_construct
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* DESCRIPTION
|
|
Packit |
13e616 |
* The cl_plock_construct function initializes the state of a
|
|
Packit |
13e616 |
* passive lock.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* SYNOPSIS
|
|
Packit |
13e616 |
*/
|
|
Packit |
13e616 |
static inline void cl_plock_construct(IN cl_plock_t * const p_lock)
|
|
Packit |
13e616 |
{
|
|
Packit |
13e616 |
CL_ASSERT(p_lock);
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
p_lock->state = CL_UNINITIALIZED;
|
|
Packit |
13e616 |
}
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
/*
|
|
Packit |
13e616 |
* PARAMETERS
|
|
Packit |
13e616 |
* p_lock
|
|
Packit |
13e616 |
* [in] Pointer to a cl_plock_t structure whose state to initialize.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* RETURN VALUE
|
|
Packit |
13e616 |
* This function does not return a value.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* NOTES
|
|
Packit |
13e616 |
* Allows calling cl_plock_destroy without first calling cl_plock_init.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* Calling cl_plock_construct is a prerequisite to calling any other
|
|
Packit |
13e616 |
* passive lock function except cl_plock_init.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* SEE ALSO
|
|
Packit |
13e616 |
* Passive Lock, cl_plock_init, cl_plock_destroy
|
|
Packit |
13e616 |
*********/
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
/****f* Component Library: Passive Lock/cl_plock_destroy
|
|
Packit |
13e616 |
* NAME
|
|
Packit |
13e616 |
* cl_plock_destroy
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* DESCRIPTION
|
|
Packit |
13e616 |
* The cl_plock_destroy function performs any necessary cleanup
|
|
Packit |
13e616 |
* of a passive lock.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* SYNOPSIS
|
|
Packit |
13e616 |
*/
|
|
Packit |
13e616 |
static inline void cl_plock_destroy(IN cl_plock_t * const p_lock)
|
|
Packit |
13e616 |
{
|
|
Packit |
13e616 |
CL_ASSERT(p_lock);
|
|
Packit |
13e616 |
p_lock->state = CL_DESTROYING;
|
|
Packit |
13e616 |
pthread_rwlock_destroy(&p_lock->lock);
|
|
Packit |
13e616 |
p_lock->state = CL_DESTROYED;
|
|
Packit |
13e616 |
}
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
/*
|
|
Packit |
13e616 |
* PARAMETERS
|
|
Packit |
13e616 |
* p_lock
|
|
Packit |
13e616 |
* [in] Pointer to a cl_plock_t structure whose state to initialize.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* RETURN VALUE
|
|
Packit |
13e616 |
* This function does not return a value.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* NOTES
|
|
Packit |
13e616 |
* cl_plock_destroy performs any necessary cleanup of the specified
|
|
Packit |
13e616 |
* passive lock.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* This function must only be called if cl_plock_construct or
|
|
Packit |
13e616 |
* cl_plock_init has been called. The passive lock must not be held
|
|
Packit |
13e616 |
* when calling this function.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* SEE ALSO
|
|
Packit |
13e616 |
* Passive Lock, cl_plock_construct, cl_plock_init
|
|
Packit |
13e616 |
*********/
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
/****f* Component Library: Passive Lock/cl_plock_init
|
|
Packit |
13e616 |
* NAME
|
|
Packit |
13e616 |
* cl_plock_init
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* DESCRIPTION
|
|
Packit |
13e616 |
* The cl_plock_init function initializes a passive lock.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* SYNOPSIS
|
|
Packit |
13e616 |
*/
|
|
Packit |
13e616 |
static inline cl_status_t cl_plock_init(IN cl_plock_t * const p_lock)
|
|
Packit |
13e616 |
{
|
|
Packit |
13e616 |
cl_status_t status;
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
CL_ASSERT(p_lock);
|
|
Packit |
13e616 |
status = pthread_rwlock_init(&p_lock->lock, NULL);
|
|
Packit |
13e616 |
if (status)
|
|
Packit |
13e616 |
return CL_ERROR;
|
|
Packit |
13e616 |
p_lock->state = CL_INITIALIZED;
|
|
Packit |
13e616 |
return (CL_SUCCESS);
|
|
Packit |
13e616 |
}
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
/*
|
|
Packit |
13e616 |
* PARAMETERS
|
|
Packit |
13e616 |
* p_lock
|
|
Packit |
13e616 |
* [in] Pointer to a cl_plock_t structure to initialize.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* RETURN VALUES
|
|
Packit |
13e616 |
* CL_SUCCESS if the passive lock was initialized successfully.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* CL_ERROR otherwise.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* NOTES
|
|
Packit |
13e616 |
* Allows calling cl_plock_acquire, cl_plock_release,
|
|
Packit |
13e616 |
* cl_plock_excl_acquire
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* SEE ALSO
|
|
Packit |
13e616 |
* Passive Lock, cl_plock_construct, cl_plock_destroy,
|
|
Packit |
13e616 |
* cl_plock_excl_acquire, cl_plock_acquire, cl_plock_release
|
|
Packit |
13e616 |
*********/
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
/****f* Component Library: Passive Lock/cl_plock_acquire
|
|
Packit |
13e616 |
* NAME
|
|
Packit |
13e616 |
* cl_plock_acquire
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* DESCRIPTION
|
|
Packit |
13e616 |
* The cl_plock_acquire function acquires a passive lock for
|
|
Packit |
13e616 |
* shared access.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* SYNOPSIS
|
|
Packit |
13e616 |
*/
|
|
Packit |
13e616 |
static inline void cl_plock_acquire(IN cl_plock_t * const p_lock)
|
|
Packit |
13e616 |
{
|
|
Packit |
13e616 |
cl_status_t __attribute__((unused)) status;
|
|
Packit |
13e616 |
CL_ASSERT(p_lock);
|
|
Packit |
13e616 |
CL_ASSERT(p_lock->state == CL_INITIALIZED);
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
status = pthread_rwlock_rdlock(&p_lock->lock);
|
|
Packit |
13e616 |
CL_ASSERT(status == 0);
|
|
Packit |
13e616 |
}
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
/*
|
|
Packit |
13e616 |
* PARAMETERS
|
|
Packit |
13e616 |
* p_lock
|
|
Packit |
13e616 |
* [in] Pointer to a cl_plock_t structure to acquire.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* RETURN VALUE
|
|
Packit |
13e616 |
* This function does not return a value.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* SEE ALSO
|
|
Packit |
13e616 |
* Passive Lock, cl_plock_release, cl_plock_excl_acquire
|
|
Packit |
13e616 |
*********/
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
/****f* Component Library: Passive Lock/cl_plock_excl_acquire
|
|
Packit |
13e616 |
* NAME
|
|
Packit |
13e616 |
* cl_plock_excl_acquire
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* DESCRIPTION
|
|
Packit |
13e616 |
* The cl_plock_excl_acquire function acquires exclusive access
|
|
Packit |
13e616 |
* to a passive lock.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* SYNOPSIS
|
|
Packit |
13e616 |
*/
|
|
Packit |
13e616 |
static inline void cl_plock_excl_acquire(IN cl_plock_t * const p_lock)
|
|
Packit |
13e616 |
{
|
|
Packit |
13e616 |
cl_status_t __attribute__((unused)) status;
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
CL_ASSERT(p_lock);
|
|
Packit |
13e616 |
CL_ASSERT(p_lock->state == CL_INITIALIZED);
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
status = pthread_rwlock_wrlock(&p_lock->lock);
|
|
Packit |
13e616 |
CL_ASSERT(status == 0);
|
|
Packit |
13e616 |
}
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
/*
|
|
Packit |
13e616 |
* PARAMETERS
|
|
Packit |
13e616 |
* p_lock
|
|
Packit |
13e616 |
* [in] Pointer to a cl_plock_t structure to acquire exclusively.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* RETURN VALUE
|
|
Packit |
13e616 |
* This function does not return a value.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* SEE ALSO
|
|
Packit |
13e616 |
* Passive Lock, cl_plock_release, cl_plock_acquire
|
|
Packit |
13e616 |
*********/
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
/****f* Component Library: Passive Lock/cl_plock_release
|
|
Packit |
13e616 |
* NAME
|
|
Packit |
13e616 |
* cl_plock_release
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* DESCRIPTION
|
|
Packit |
13e616 |
* The cl_plock_release function releases a passive lock from
|
|
Packit |
13e616 |
* shared or exclusive access.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* SYNOPSIS
|
|
Packit |
13e616 |
*/
|
|
Packit |
13e616 |
static inline void cl_plock_release(IN cl_plock_t * const p_lock)
|
|
Packit |
13e616 |
{
|
|
Packit |
13e616 |
cl_status_t __attribute__((unused)) status;
|
|
Packit |
13e616 |
CL_ASSERT(p_lock);
|
|
Packit |
13e616 |
CL_ASSERT(p_lock->state == CL_INITIALIZED);
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
status = pthread_rwlock_unlock(&p_lock->lock);
|
|
Packit |
13e616 |
CL_ASSERT(status == 0);
|
|
Packit |
13e616 |
}
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
/*
|
|
Packit |
13e616 |
* PARAMETERS
|
|
Packit |
13e616 |
* p_lock
|
|
Packit |
13e616 |
* [in] Pointer to a cl_plock_t structure to release.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* RETURN VALUE
|
|
Packit |
13e616 |
* This function does not return a value.
|
|
Packit |
13e616 |
*
|
|
Packit |
13e616 |
* SEE ALSO
|
|
Packit |
13e616 |
* Passive Lock, cl_plock_acquire, cl_plock_excl_acquire
|
|
Packit |
13e616 |
*********/
|
|
Packit |
13e616 |
|
|
Packit |
13e616 |
END_C_DECLS
|
|
Packit |
13e616 |
#endif /* _CL_PASSIVE_LOCK_H_ */
|