* Copyright (c) 2005-2008 VMware, Inc. All rights reserved.
* **********************************************************/
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of VMware, Inc. nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL VMWARE, INC. OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
#include "tools.h"
* For now, use 50 threads so this test will pass for thin_client. We
* should put this back to 100 once we address memory usage issues.
* xref cases 8960, 9366, 10376
*/
enum { THREADS = 50, THREAD_STACK = 8192 };
enum { LOOP_WORK = 100 };
#define UNIPROC
#ifdef UNIPROC
# define YIELD() thread_yield()
#else
# define YIELD()
#endif
ptr_int_t thread[THREADS];
long global_started = 0;
long global_finished = 0;
int
compare(const void *arg1, const void *arg2)
{
return _stricmp(*(char **)arg1, *(char **)arg2);
}
void
sort()
{
int argc = 5;
char *argv[] = { "one", "two", "three", "five", "six", "unsorted" };
qsort((void *)argv, (size_t)argc, sizeof(char *), compare);
#ifdef VERY_VERBOSE
for (i = 0; i < argc; ++i)
print(" %s", argv[i]);
print("\n");
#endif
}
DWORD WINAPI
executor(LPVOID parm)
{
int w;
sort();
InterlockedIncrement(&global_started);
for (w = 0; w < LOOP_WORK; w++) {
sort();
if (w % 10 == 0)
YIELD();
}
InterlockedIncrement(&global_finished);
return 0;
}
int
main()
{
void *p;
int reserved = 0;
int commit = 0;
int t;
INIT();
for (t = 0; t < THREADS; t++) {
thread[t] = (ptr_int_t)CreateThread(NULL, THREAD_STACK, executor, NULL,
CREATE_SUSPENDED, NULL);
if (thread[t] == 0)
print("GLE: %d\n", GetLastError());
assert(thread[t] != 0);
}
VERBOSE_PRINT("created %d\n", THREADS);
do {
int size = 8 * PAGE_SIZE;
p = reserve_memory(size);
if (p)
reserved += size;
} while (p != NULL);
VERBOSE_PRINT("reserved %d\n", reserved);
for (t = 0; t < THREADS; t++) {
resume_thread((thread_handle)thread[t]);
}
VERBOSE_PRINT("resumed %d\n", THREADS);
while (global_started < THREADS / 2)
YIELD();
VERBOSE_PRINT("started %d, finished %d\n", global_started, global_finished);
print("Successful\n");
#ifdef WAIT
getchar();
#endif
}