mutex.h revision 26d20cd51c968e111b4122536825368a17b5ca82
1633838b8255282d10af15c5c84cee5a51466712Bob Halley/*
49dbdb0186eb23d87d685b96eaefa9ec3c71d9b8David Lawrence * Copyright (C) 1998-2000 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * Permission to use, copy, modify, and distribute this software for any
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * purpose with or without fee is hereby granted, provided that the above
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * copyright notice and this permission notice appear in all copies.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
15a44745412679c30a6d022733925af70a38b715David Lawrence * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
15a44745412679c30a6d022733925af70a38b715David Lawrence * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
15a44745412679c30a6d022733925af70a38b715David Lawrence * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
15a44745412679c30a6d022733925af70a38b715David Lawrence * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
15a44745412679c30a6d022733925af70a38b715David Lawrence * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley */
9a4ce0c25809073f31226faa6ed94c70474cf363Bob Halley
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington/* $Id: mutex.h,v 1.17 2000/12/29 18:19:52 bwelling Exp $ */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley#ifndef ISC_MUTEX_H
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley#define ISC_MUTEX_H 1
9a4ce0c25809073f31226faa6ed94c70474cf363Bob Halley
9a4ce0c25809073f31226faa6ed94c70474cf363Bob Halley#include <pthread.h>
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#include <stdio.h>
9a4ce0c25809073f31226faa6ed94c70474cf363Bob Halley
9c4f33b6718407e94d50dbfb4977e16d3f83de9dDavid Lawrence#include <isc/result.h> /* for ISC_R_ codes */
9c4f33b6718407e94d50dbfb4977e16d3f83de9dDavid Lawrence
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halleytypedef pthread_mutex_t isc_mutex_t;
9a4ce0c25809073f31226faa6ed94c70474cf363Bob Halley
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley/* XXX We could do fancier error handling... */
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington/*
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington * Define ISC_MUTEX_PROFILE to turn on profiling of mutexes by line. When
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington * enabled, isc_mutex_stats() can be used to print a table showing the
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington * number of times each type of mutex was locked and the amount of time
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington * waiting to obtain the lock.
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington */
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#ifndef ISC_MUTEX_PROFILE
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#define ISC_MUTEX_PROFILE 0
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#endif
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley#define isc_mutex_init(mp) \
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley ((pthread_mutex_init((mp), NULL) == 0) ? \
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley ISC_R_SUCCESS : ISC_R_UNEXPECTED)
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#if ISC_MUTEX_PROFILE
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#define isc_mutex_lock(mp) \
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington isc_mutex_lockprofile((mp), __FILE__, __LINE__)
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#else
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley#define isc_mutex_lock(mp) \
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley ((pthread_mutex_lock((mp)) == 0) ? \
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley ISC_R_SUCCESS : ISC_R_UNEXPECTED)
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#endif
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley#define isc_mutex_unlock(mp) \
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley ((pthread_mutex_unlock((mp)) == 0) ? \
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley ISC_R_SUCCESS : ISC_R_UNEXPECTED)
f671a5c51cc59e266620c0c4026b054908fdd80cBob Halley#define isc_mutex_trylock(mp) \
e4b9761b0ef03597c35d1ef1d86e12514c621f90Michael Graff ((pthread_mutex_trylock((mp)) == 0) ? \
f671a5c51cc59e266620c0c4026b054908fdd80cBob Halley ISC_R_SUCCESS : ISC_R_LOCKBUSY)
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley#define isc_mutex_destroy(mp) \
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley ((pthread_mutex_destroy((mp)) == 0) ? \
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley ISC_R_SUCCESS : ISC_R_UNEXPECTED)
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#if ISC_MUTEX_PROFILE
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#define isc_mutex_stats(fp) isc_mutex_statsprofile(fp);
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#else
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#define isc_mutex_stats(fp)
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#endif
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#if ISC_MUTEX_PROFILE
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellingtonisc_result_t
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellingtonisc_mutex_lockprofile(isc_mutex_t *mp, const char * _file, int _line);
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellingtonvoid
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellingtonisc_mutex_statsprofile(FILE *fp);
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#endif /* ISC_MUTEX_PROFILE */
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley#endif /* ISC_MUTEX_H */