Made qsort() thread-safe.

svn path=/trunk/; revision=2294
This commit is contained in:
Eric Kohl 2001-10-11 23:37:47 +00:00
parent 061fadc77a
commit 0a88910738
2 changed files with 53 additions and 48 deletions

View file

@ -7,16 +7,21 @@
typedef struct _ThreadData typedef struct _ThreadData
{ {
int terrno; /* *nix error code */ int terrno; /* *nix error code */
unsigned long tdoserrno; /* Win32 error code (for I/O only) */ unsigned long tdoserrno; /* Win32 error code (for I/O only) */
unsigned long long tnext; /* used by rand/srand */ unsigned long long tnext; /* used by rand/srand */
char *lasttoken; /* used by strtok */ char *lasttoken; /* used by strtok */
wchar_t *wlasttoken; /* used by wcstok */ wchar_t *wlasttoken; /* used by wcstok */
int fpecode; /* fp exception code */ int fpecode; /* fp exception code */
/* qsort variables */
int (*qcmp)(const void *, const void *); /* the comparison routine */
int qsz; /* size of each record */
int thresh; /* THRESHold in chars */
int mthresh; /* MTHRESHold in chars */
} THREADDATA, *PTHREADDATA; } THREADDATA, *PTHREADDATA;

View file

@ -1,5 +1,6 @@
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
#include <msvcrt/stdlib.h> #include <msvcrt/stdlib.h>
#include <msvcrt/internal/tls.h>
/*- /*-
* Copyright (c) 1980, 1983 The Regents of the University of California. * Copyright (c) 1980, 1983 The Regents of the University of California.
@ -32,11 +33,6 @@
#define THRESH 4 /* threshold for insertion */ #define THRESH 4 /* threshold for insertion */
#define MTHRESH 6 /* threshold for median */ #define MTHRESH 6 /* threshold for median */
static int (*qcmp)(const void *, const void *); /* the comparison routine */
static int qsz; /* size of each record */
static int thresh; /* THRESHold in chars */
static int mthresh; /* MTHRESHold in chars */
/* /*
* qst: * qst:
* Do a quicksort * Do a quicksort
@ -53,7 +49,7 @@ static int mthresh; /* MTHRESHold in chars */
*/ */
static void static void
qst(char *base, char *max) qst(PTHREADDATA pThreadData, char *base, char *max)
{ {
char c, *i, *j, *jj; char c, *i, *j, *jj;
int ii; int ii;
@ -71,20 +67,20 @@ qst(char *base, char *max)
*/ */
lo = max - base; /* number of elements as chars */ lo = max - base; /* number of elements as chars */
do { do {
mid = i = base + qsz * ((lo / qsz) >> 1); mid = i = base + pThreadData->qsz * ((lo / pThreadData->qsz) >> 1);
if (lo >= mthresh) if (lo >= pThreadData->mthresh)
{ {
j = (qcmp((jj = base), i) > 0 ? jj : i); j = (pThreadData->qcmp((jj = base), i) > 0 ? jj : i);
if (qcmp(j, (tmp = max - qsz)) > 0) if (pThreadData->qcmp(j, (tmp = max - pThreadData->qsz)) > 0)
{ {
/* switch to first loser */ /* switch to first loser */
j = (j == jj ? i : jj); j = (j == jj ? i : jj);
if (qcmp(j, tmp) < 0) if (pThreadData->qcmp(j, tmp) < 0)
j = tmp; j = tmp;
} }
if (j != i) if (j != i)
{ {
ii = qsz; ii = pThreadData->qsz;
do { do {
c = *i; c = *i;
*i++ = *j; *i++ = *j;
@ -95,18 +91,18 @@ qst(char *base, char *max)
/* /*
* Semi-standard quicksort partitioning/swapping * Semi-standard quicksort partitioning/swapping
*/ */
for (i = base, j = max - qsz; ; ) for (i = base, j = max - pThreadData->qsz; ; )
{ {
while (i < mid && qcmp(i, mid) <= 0) while (i < mid && pThreadData->qcmp(i, mid) <= 0)
i += qsz; i += pThreadData->qsz;
while (j > mid) while (j > mid)
{ {
if (qcmp(mid, j) <= 0) if (pThreadData->qcmp(mid, j) <= 0)
{ {
j -= qsz; j -= pThreadData->qsz;
continue; continue;
} }
tmp = i + qsz; /* value of i after swap */ tmp = i + pThreadData->qsz; /* value of i after swap */
if (i == mid) if (i == mid)
{ {
/* j <-> mid, new mid is j */ /* j <-> mid, new mid is j */
@ -116,7 +112,7 @@ qst(char *base, char *max)
{ {
/* i <-> j */ /* i <-> j */
jj = j; jj = j;
j -= qsz; j -= pThreadData->qsz;
} }
goto swap; goto swap;
} }
@ -129,10 +125,10 @@ qst(char *base, char *max)
/* i <-> mid, new mid is i */ /* i <-> mid, new mid is i */
jj = mid; jj = mid;
tmp = mid = i; /* value of i after swap */ tmp = mid = i; /* value of i after swap */
j -= qsz; j -= pThreadData->qsz;
} }
swap: swap:
ii = qsz; ii = pThreadData->qsz;
do { do {
c = *i; c = *i;
*i++ = *jj; *i++ = *jj;
@ -148,21 +144,21 @@ qst(char *base, char *max)
* (recursively or by branching) if the partition is * (recursively or by branching) if the partition is
* of at least size THRESH. * of at least size THRESH.
*/ */
i = (j = mid) + qsz; i = (j = mid) + pThreadData->qsz;
if ((lo = j - base) <= (hi = max - i)) if ((lo = j - base) <= (hi = max - i))
{ {
if (lo >= thresh) if (lo >= pThreadData->thresh)
qst(base, j); qst(pThreadData, base, j);
base = i; base = i;
lo = hi; lo = hi;
} }
else else
{ {
if (hi >= thresh) if (hi >= pThreadData->thresh)
qst(i, max); qst(pThreadData, i, max);
max = j; max = j;
} }
} while (lo >= thresh); } while (lo >= pThreadData->thresh);
} }
/* /*
@ -174,21 +170,25 @@ qst(char *base, char *max)
void void
qsort(const void *base0, size_t n, size_t size, _pfunccmp_t compar) qsort(const void *base0, size_t n, size_t size, _pfunccmp_t compar)
{ {
PTHREADDATA pThreadData;
char *base = (char *)base0; char *base = (char *)base0;
char c, *i, *j, *lo, *hi; char c, *i, *j, *lo, *hi;
char *min, *max; char *min, *max;
if (n <= 1) if (n <= 1)
return; return;
qsz = size;
qcmp = compar; pThreadData = GetThreadData();
thresh = qsz * THRESH;
mthresh = qsz * MTHRESH; pThreadData->qsz = size;
max = base + n * qsz; pThreadData->qcmp = compar;
pThreadData->thresh = pThreadData->qsz * THRESH;
pThreadData->mthresh = pThreadData->qsz * MTHRESH;
max = base + n * pThreadData->qsz;
if (n >= THRESH) if (n >= THRESH)
{ {
qst(base, max); qst(pThreadData, base, max);
hi = base + thresh; hi = base + pThreadData->thresh;
} }
else else
{ {
@ -200,13 +200,13 @@ qsort(const void *base0, size_t n, size_t size, _pfunccmp_t compar)
* the first THRESH elements (or the first n if n < THRESH), finding * the first THRESH elements (or the first n if n < THRESH), finding
* the min, and swapping it into the first position. * the min, and swapping it into the first position.
*/ */
for (j = lo = base; (lo += qsz) < hi; ) for (j = lo = base; (lo += pThreadData->qsz) < hi; )
if (qcmp(j, lo) > 0) if (pThreadData->qcmp(j, lo) > 0)
j = lo; j = lo;
if (j != base) if (j != base)
{ {
/* swap j into place */ /* swap j into place */
for (i = base, hi = base + qsz; i < hi; ) for (i = base, hi = base + pThreadData->qsz; i < hi; )
{ {
c = *j; c = *j;
*j++ = *i; *j++ = *i;
@ -220,15 +220,15 @@ qsort(const void *base0, size_t n, size_t size, _pfunccmp_t compar)
* Then, do the standard insertion sort shift on a character at a time * Then, do the standard insertion sort shift on a character at a time
* basis for each element in the frob. * basis for each element in the frob.
*/ */
for (min = base; (hi = min += qsz) < max; ) for (min = base; (hi = min += pThreadData->qsz) < max; )
{ {
while (qcmp(hi -= qsz, min) > 0) while (pThreadData->qcmp(hi -= pThreadData->qsz, min) > 0)
/* void */; /* void */;
if ((hi += qsz) != min) { if ((hi += pThreadData->qsz) != min) {
for (lo = min + qsz; --lo >= min; ) for (lo = min + pThreadData->qsz; --lo >= min; )
{ {
c = *lo; c = *lo;
for (i = j = lo; (j -= qsz) >= hi; i = j) for (i = j = lo; (j -= pThreadData->qsz) >= hi; i = j)
*i = *j; *i = *j;
*i = c; *i = c;
} }