da7408caa7131f1732dde89ee45a29822ee10c85
[alexxy/gromacs.git] / include / gmx_sort.h
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  *
3  * 
4  * This file is part of Gromacs        Copyright (c) 1991-2010
5  * David van der Spoel, Erik Lindahl, Berk Hess, University of Groningen.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * To help us fund GROMACS development, we humbly ask that you cite
13  * the research papers on the package. Check out http://www.gromacs.org
14  * 
15  * And Hey:
16  * Gnomes, ROck Monsters And Chili Sauce
17  */
18 #ifndef _GMX_SORT_H_
19 #define _GMX_SORT_H_
20
21 #include "visibility.h"
22
23 /** @file gmx_sort.h
24  *
25  *  @brief Portable implementation of thread-safe sort routines.
26  *
27  *
28  *  This module provides a Gromacs version of the qsort() routine defined.
29  *  It is not highly optimized, but it is thread safe, i.e. multiple threads
30  *  can simultaneously call gmx_qsort with different data.
31  */
32
33 #include <stdlib.h>
34
35 #ifdef __cplusplus
36 extern "C"
37 {
38 #endif
39 #if 0
40 } /* fixes auto-indentation problems */
41 #endif
42
43
44 /*
45  *  @param base    Pointer to first element in list to sort
46  *  @param nmemb   Number of elements in list
47  *  @param size    Size in bytes of each element
48  *  @param compar  Comparison function that takes two pointers to elements
49  *                 being compared as arguments. The function should return an
50  *                 integer less than, equal to, or greater than zero if the 
51  *                 first argument is considered to be respectively less than,
52  *                 equal to, or greater than the second.
53  */
54 GMX_LIBGMX_EXPORT
55 void
56 gmx_qsort(void *           base, 
57           size_t           nmemb, 
58           size_t           size, 
59           int            (*compar)(const void *, const void *));
60
61
62 #ifdef GMX_THREAD_MPI
63 /* Some implementations of qsort are not threadsafe.
64  * For instance qsort in glibc contains a bug which makes it non-threadsafe:
65  * http://sources.redhat.com/bugzilla/show_bug.cgi?id=11655
66  */
67 #define qsort_threadsafe gmx_qsort
68 #else
69 /* System qsort might be faster than our own */
70 #define qsort_threadsafe qsort
71 #endif
72
73
74 #ifdef __cplusplus
75 }
76 #endif
77
78
79 #endif /* _GMX_SORT_H_ */