Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[6model/c] test availability of posix or win32 threads
  • Loading branch information
mberends committed Aug 2, 2011
1 parent 74a549e commit 2795df3
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 11 deletions.
100 changes: 100 additions & 0 deletions c/t/01-toolchain/01b-threads.c
@@ -0,0 +1,100 @@
/* 01b-threads.c */
/* Check that threading in the operating system and libraries are ok */


#include <stdio.h> /* perror printf */
#include <stdlib.h> /* system */
#include "../Test.h" /* is_ii plan */

#ifdef _WIN32
#include <windows.h>
#define sleep(seconds) Sleep(seconds*1000)
#else
#include <pthread.h> /* pthread_create */
#endif


struct threadargs {
int testnumber;
int seconds;
char * description;
};


/* test1_thread */
void *
test1_thread(void * args)
{
struct threadargs * targs = (struct threadargs *) args;
int testnumber = targs->testnumber;
char * description = targs->description;
sleep(targs->seconds);
printf("ok %d - from %s thread\n", testnumber, description);
return NULL;
}

/* tests_1_4 */
void
tests1_4()
{
int status;
#ifdef _MSC_VER
HANDLE threadhandle1, threadhandle2;
DWORD threadId1, threadId2;
#else
pthread_t thread_id;
pthread_attr_t thread_attr;
#endif
int threadnumber, threadstacksize;
struct threadargs thread1arguments, thread2arguments;

/* Create the first thread */
thread1arguments.testnumber = 3;
thread1arguments.seconds = 2;
thread1arguments.description = "first";
threadstacksize = 16384; /* Minimum allowed by Posix threads */
#ifdef _MSC_VER
threadhandle1 = CreateThread(NULL, threadstacksize,
(LPTHREAD_START_ROUTINE) test1_thread, &thread1arguments, 0,
&threadId1);
status = (threadhandle1 == NULL);
#else
status = pthread_attr_init(&thread_attr);
pthread_attr_setstacksize(&thread_attr, threadstacksize);
status = pthread_create(&thread_id, &thread_attr, test1_thread,
&thread1arguments);
#endif
is_ii(status, 0, "created first thread");
sleep(1); /* Let the first thread run immediately */

/* Create the second thread */
thread2arguments.testnumber = 4;
thread2arguments.seconds = 2;
thread2arguments.description = "second";
#ifdef _MSC_VER
threadhandle2 = CreateThread(NULL, threadstacksize,
(LPTHREAD_START_ROUTINE) test1_thread, &thread2arguments, 0,
&threadId1);
status = (threadhandle2 == NULL);
#else
status = pthread_create(&thread_id, &thread_attr, test1_thread,
&thread2arguments);
#endif
is_ii(status, 0, "created second thread");
#ifndef _MSC_VER
pthread_attr_destroy(&thread_attr);
#endif

/* Give both threads enough time to complete */
sleep(3);
}


/* main */
int
main(int arg, char * argv[])
{
plan(4);
tests1_4(); /* two simple child threads */
return 0;
}
8 changes: 7 additions & 1 deletion c/t/Test.h
Expand Up @@ -23,7 +23,9 @@ main(int argc, char * argv[])

#include <stdio.h>

#define plan(count) int testnumber=0; printf("1..%d\n", count)
int testnumber=0; /* yes, namespace pollution. patches welcome ;-) */

#define plan(count) printf("1..%d\n", count)

#define \
ok(flag,desc) \
Expand All @@ -42,4 +44,8 @@ is_ss(got,expected,desc) \
printf("%sok %d - %s\n", \
strcmp(got,expected)?"not ":"",++testnumber,desc)

#define \
diag(message) \
printf("# %s\n", message)

/* end of Test.h */
38 changes: 29 additions & 9 deletions c/tools/build/Configure.c
@@ -1,10 +1,23 @@
/* Configure.c */
/* Compiled and run by 6model/c/Configure.(sh|bat) */

/* This program (Configure) uses environment variables and C macros */
/* to autodetect the operating system, compiler and other utilities */
/* that can be used to build your software. It then creates your */
/* Makefile based on a template (Makefile.in). To work also with */
/* non-GNU systems such as Microsoft Visual C++, it follows the */
/* style of Automake and Autoconf, but is written in only C and does */
/* not rely on other tools such as M4. */

/* This work will never be finished. Reliable autodetection is hard. */
/* There are always newer environments and tools to try out. */
/* Systems usually predefine some variables and macros. Users might */
/* make the same definitions. */

/*
* TODO
* Evaluate potential Win32 C compilers and development environments
* Explore more Win32 C compilers and toolchains
* http://www.thefreecountry.com/compilers/cpp.shtml
* Most of them alias cc to their own filenames.
Expand Down Expand Up @@ -60,35 +73,38 @@ void trans(char ** text, char * search, char * replace);
/* config_set */
/* Use environment variables and other clues to assign values to */
/* members of the config[] array */

void
config_set(void)
{
char * s;
/* Operating system */
if ((s=getenv("OS")) && strcmp(s,"Windows_NT")==0) { /* any Windows system */
/* Operating system */ s=getenv("OS");
if (s && strcmp(s,"Windows_NT")==0) { /* any Windows system */
config[OS_TYPE] = "Windows";
config[EXE] = ".exe";
}
else { /* non Windows operating systems default to Unix settings */
config[OS_TYPE] = "Unix";
config[EXE] = "";
}
/* C compiler */
if ((s=getenv("COMPILER")) && strcmp(s,"MSVC")==0) {
/* C compiler */ s=getenv("COMPILER");
if (s && strcmp(s,"MSVC")==0) {
/* TODO: use _MSC_VER */
/* See http://msdn.microsoft.com/en-US/library/b0084kay%28v=VS.100%29.aspx */
config[CC] = "cl -DMSVC ";
config[OUT] = "-Fe";
config[RM_RF] = "del /F /Q /S";
}
if ((s=getenv("COMPILER")) && strcmp(s,"GCC")==0) {
if ((s=getenv("OS")) && strcmp(s,"Windows_NT")==0)
if (s && strcmp(s,"GCC")==0) { s=getenv("OS");
if (s && strcmp(s,"Windows_NT")==0)
config[CC] = "cc -DGCC ";
else
config[CC] = "cc -DGCC -ldl ";
config[OUT] = "-o";
config[RM_RF] = "rm -rf";
}
/* Make utility */
if ((s=getenv("COMPILER")) && strcmp(s,"GCC")==0) {
/* Make utility */ s=getenv("COMPILER");
if (s && strcmp(s,"GCC")==0) {
config[MAKE_COMMAND] = "make";
} else {
config[MAKE_COMMAND] = "nmake";
Expand Down Expand Up @@ -229,4 +245,8 @@ main(int argc, char * argv[])
return 0;
}

/* See also: */
/* Autoconf http://www.gnu.org/software/autoconf */
/* Automake http://www.gnu.org/software/automake */

/* end of Configure.c */
3 changes: 2 additions & 1 deletion c/tools/build/Makefile.in
Expand Up @@ -13,6 +13,7 @@ CC = @cc@
EXE = @exe@
O = @out@
RM_RF = @rm_rf@
THREADS = @threads@

# The first target is default, will be used by a plain 'make' command.
all: test
Expand All @@ -23,7 +24,7 @@ t/01-toolchain/01a-cc.exe: t/01-toolchain/01a-cc.c t/Test.h
-$(RM_RF) 01a-cc.obj

t/01-toolchain/01b-threads.exe: t/01-toolchain/01b-threads.c t/Test.h
$(CC) $(O)t/01-toolchain/01b-threads.exe t/01-toolchain/01b-threads.c
$(CC) -pthread $(O)t/01-toolchain/01b-threads.exe t/01-toolchain/01b-threads.c
-$(RM_RF) 01b-threads.obj

t/02-components/02a-hashtable.exe: t/02-components/02a-hashtable.c \
Expand Down

0 comments on commit 2795df3

Please sign in to comment.