Skip to content

Commit 589eb28

Browse files
author
Oleksandr Motsak
committed
added autotools build system + fixed debugging checks (and examples)
NOTE: for the optional debug version: use libxthread_g.la only if compiling with -DDEBUG_THREADS NOTE: be careful - where to include xthread.h (better at the end) as it includes the autogenerated public config: xthread_config.h see TODO for unfinished tasks
1 parent abe0fa4 commit 589eb28

20 files changed

+1329
-24
lines changed

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
^\.scon
2+
\.[oa]$
3+
^config\.log$
4+
.*\~$
5+
.*--$
6+
autom4te.cache
7+
auxes
8+
m4/libtool\.m4
9+
m4/lt\~obsolete\.m4
10+
m4/ltoptions\.m4
11+
m4/ltsugar\.m4
12+
m4/ltversion\.m4
13+
xthread_config.h.in
14+
configure
15+
Makefile.in
16+
aclocal.m4
17+

.hgignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

Makefile.am

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
ACLOCAL_AMFLAGS = -I m4
2+
3+
AM_CPPFLAGS = -I${srcdir} -I${builddir}
4+
AM_CFLAGS = ${PIPE}
5+
AM_CXXFLAGS = -Wextra -Wall ${PIPE} $(CXXTEMPLFLAGS)
6+
7+
if WANT_DEBUG
8+
LIB_G=libxthread_g.la
9+
else
10+
LIB_G=
11+
endif
12+
13+
libxthread_LTLIBRARIES = libxthread.la ${LIB_G}
14+
libxthreaddir = $(libdir)
15+
16+
CXXTEMPLFLAGS =
17+
### --no-exceptions ## -fno-implicit-templates
18+
19+
libxthread_la_CFLAGS = -O3 -fomit-frame-pointer ${PIPE}
20+
libxthread_g_la_CFLAGS = ${PIPE}
21+
22+
libxthread_la_CXXFLAGS = -O3 -fomit-frame-pointer ${PIPE} ${CXXTEMPLFLAGS}
23+
## --no-rtti
24+
libxthread_g_la_CXXFLAGS = -g -Wextra -Wall -pedantic ${PIPE} ${CXXTEMPLFLAGS}
25+
libxthread_g_la_LDFLAGS = -g -Wextra -Wall -pedantic
26+
# -fdiagnostics-show-option
27+
28+
libxthread_g_la_CPPFLAGS = ${AM_CPPFLAGS} -DDEBUG_THREADS
29+
30+
SOURCES = xthread.cc
31+
libxthread_la_SOURCES = $(SOURCES)
32+
libxthread_g_la_SOURCES = $(SOURCES)
33+
34+
# $(RT_LIBS) # librt is for the POSIX realtime extensions. I think it's only needed on Solaris.
35+
libxthread_la_LIBADD = $(PTHREAD_LIBS)
36+
libxthread_g_la_LIBADD = $(PTHREAD_LIBS)
37+
38+
# where should the header go?
39+
libxthread_includedir =$(includedir)/xthread/
40+
41+
libxthread_include_HEADERS = xthread.h tlsize.inc
42+
nodist_libxthread_include_HEADERS = xthread_config.h
43+
44+
if WANT_DEBUG
45+
TESTS = example-s-g example-s-r example-d-g example-d-r example2-s-g example2-s-r example2-d-g example2-d-r
46+
else
47+
TESTS = example-s-r example-d-r example2-s-r example2-d-r
48+
endif
49+
check_PROGRAMS = $(TESTS)
50+
51+
52+
53+
examplesources = example.cc
54+
example_s_r_SOURCES = $(examplesources)
55+
example_s_g_SOURCES = $(examplesources)
56+
example_d_r_SOURCES = $(examplesources)
57+
example_d_g_SOURCES = $(examplesources)
58+
59+
example_s_r_CFLAGS = -O3 -fomit-frame-pointer ${AM_CFLAGS}
60+
example_d_r_CFLAGS = -O3 -fomit-frame-pointer ${AM_CFLAGS}
61+
62+
example_s_r_LDFLAGS = -static ${AM_LDFLAGS}
63+
example_s_g_LDFLAGS = -static ${AM_LDFLAGS}
64+
65+
example_s_r_CXXFLAGS = -O3 -fomit-frame-pointer ${AM_CXXFLAGS}
66+
example_s_g_CXXFLAGS = -g -pedantic ${AM_CXXFLAGS}
67+
example_d_r_CXXFLAGS = -O3 -fomit-frame-pointer ${AM_CXXFLAGS}
68+
example_d_g_CXXFLAGS = -g -pedantic ${AM_CXXFLAGS}
69+
70+
example_s_r_LDADD = libxthread.la
71+
example_s_g_LDADD = libxthread_g.la
72+
example_d_r_LDADD = libxthread.la
73+
example_d_g_LDADD = libxthread_g.la
74+
75+
example_s_g_CPPFLAGS = ${AM_CPPFLAGS} -DDEBUG_THREADS
76+
example_d_g_CPPFLAGS = ${AM_CPPFLAGS} -DDEBUG_THREADS
77+
78+
example2sources = example2.cc
79+
example2_s_r_SOURCES = $(example2sources)
80+
example2_s_g_SOURCES = $(example2sources)
81+
example2_d_r_SOURCES = $(example2sources)
82+
example2_d_g_SOURCES = $(example2sources)
83+
84+
example2_s_r_CFLAGS = -O3 -fomit-frame-pointer ${AM_CFLAGS}
85+
example2_d_r_CFLAGS = -O3 -fomit-frame-pointer ${AM_CFLAGS}
86+
87+
example2_s_r_LDFLAGS = -static ${AM_LDFLAGS}
88+
example2_s_g_LDFLAGS = -static ${AM_LDFLAGS}
89+
90+
example2_s_r_CXXFLAGS = -O3 -fomit-frame-pointer ${AM_CXXFLAGS}
91+
example2_s_g_CXXFLAGS = -g -pedantic ${AM_CXXFLAGS}
92+
example2_d_r_CXXFLAGS = -O3 -fomit-frame-pointer ${AM_CXXFLAGS}
93+
example2_d_g_CXXFLAGS = -g -pedantic ${AM_CXXFLAGS}
94+
95+
example2_s_r_LDADD = libxthread.la
96+
example2_s_g_LDADD = libxthread_g.la
97+
example2_d_r_LDADD = libxthread.la
98+
example2_d_g_LDADD = libxthread_g.la
99+
100+
example2_s_g_CPPFLAGS = ${AM_CPPFLAGS} -DDEBUG_THREADS
101+
example2_d_g_CPPFLAGS = ${AM_CPPFLAGS} -DDEBUG_THREADS
102+
103+

SConstruct

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
libs = [ "xthread" ]
2-
cflags = "-g -O2"
2+
cflags = "-g -O0"
33
linkflags = "-g"
44

55
Threads=DefaultEnvironment()
@@ -11,11 +11,11 @@ Threads.Append(LINKFLAGS=linkflags)
1111
conf = Configure(Threads)
1212
if conf.CheckLib("pthread"):
1313
libs.append("pthread")
14-
if conf.CheckLib("rt"):
15-
libs.append("rt")
14+
# if conf.CheckLib("rt"):
15+
# libs.append("rt")
1616

1717
Threads.Append(LIBS=libs)
1818

19-
Threads.Library(["xthread"], ["thread.cc"])
19+
Threads.Library(["xthread"], ["xthread.cc"])
2020
Program("example.cc")
2121
Program("example2.cc")

TODO

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
TODOS:
2+
3+
. fail if necessary stuff was not found
4+
5+
. check for __builtin_frame_address, alloca
6+
7+
8+
FIX:
9+
10+
AX_CHECK_FUNC_IN produces wrong configure output
11+
12+
. check for __builtin_frame_address, alloca
13+
14+
15+
OPTIMIZATIONS:
16+
17+
make use of posix_memalign if available

autogen.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/sh
2+
3+
autoreconf -vif `dirname "$0"`
4+
5+
# TOP_DIR=$PWD
6+
#
7+
# autotools ()
8+
# {
9+
# aclocal -I $TOP_DIR/m4
10+
# autoheader
11+
# autoconf
12+
# automake -a
13+
# }
14+
#
15+
#
16+
# for d in . factory libpolys; do
17+
# echo "==============="
18+
# echo "run libtoolize --force, aclocal -I $TOP_DIR/m4, autoconf, automake -a in $d"
19+
# echo "---------------"
20+
# cd $d
21+
# libtoolize --force
22+
# autotools
23+
# cd $TOP_DIR
24+
# echo
25+
# done
26+
#
27+
# for d in omalloc; do
28+
# echo "==============="
29+
# echo "run aclocal -I $TOP_DIR/m4, autotools in $d"
30+
# echo "---------------"
31+
# cd $d
32+
# autotools
33+
# cd $TOP_DIR
34+
# echo
35+
# done

configure.ac

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
AC_INIT([xthread], [0.1])
2+
3+
AC_CONFIG_MACRO_DIR([m4])
4+
AC_CONFIG_AUX_DIR([auxes])
5+
AC_CONFIG_SRCDIR([xthread.h])
6+
AC_CONFIG_HEADER([xthread_config.h])
7+
8+
AM_MAINTAINER_MODE
9+
AM_INIT_AUTOMAKE([-Wall foreign subdir-objects]) # -Wno-extra-portability -Werror silent-rules
10+
11+
dnl Check if build env is sane
12+
AM_SANITY_CHECK
13+
14+
#
15+
# - Check for CC and CXX but be careful about CFLAGS.
16+
#
17+
test "${CFLAGS+set}" = set || cflags_expl_set=no
18+
AC_PROG_CC
19+
test "${CXXFLAGS+set}" = set || cxxflags_expl_set=no
20+
AC_PROG_CXX
21+
if test "x$GCC" != xyes && test "x$GXX" != xyes; then
22+
AC_MSG_WARN(you better use gcc to compile Factory)
23+
else
24+
if test "x$GXX" = xyes && test "x$cxxflags_expl_set" = xno; then
25+
AC_MSG_WARN([we use \`' as default for \`CXXFLAGS'
26+
(instead of the configure default \`-g -O')])
27+
fi
28+
if test "x$GCC" = xyes && test "x$cflags_expl_set" = xno; then
29+
AC_MSG_WARN([we use \`' as default for \`CFLAGS'
30+
(instead of the configure default \`-g -O')])
31+
fi
32+
fi
33+
34+
AM_PROG_CC_C_O
35+
# AM_PROG_AR
36+
37+
AC_PROG_LN_S
38+
AC_PROG_INSTALL
39+
40+
AC_FUNC_MALLOC
41+
AC_HEADER_STDC
42+
AC_CHECK_HEADERS([stdlib.h string.h unistd.h sys/mman.h])
43+
44+
AC_CHECK_FUNCS(posix_memalign mmap mprotect alloca)
45+
# __builtin_frame_address
46+
47+
#AX_CHECK_FUNC_IN(alloca.h, alloca, [], [AC_MSG_ERROR([Sorry but alloca is necessary!])])
48+
#AX_CHECK_FUNC_IN(sys/mman.h, mmap, [], [AC_MSG_ERROR([Sorry but mmap is necessary!])])
49+
#AX_CHECK_FUNC_IN(sys/mman.h, mprotect, [], [AC_MSG_ERROR([Sorry but mprotect is necessary!])])
50+
#AX_CHECK_FUNC_IN(stdlib.h, posix_memalign, [], [])
51+
52+
# Checks for typedefs, structures, and compiler characteristics.
53+
AC_C_CONST
54+
AC_C_INLINE
55+
#AC_HEADER_STDBOOL
56+
#AC_TYPE_SIZE_T
57+
58+
# Turn off shared libraries during beta-testing, since they
59+
# make the build process take too long.
60+
LT_INIT
61+
# ([shared])
62+
# LT_INIT(dlopen disable-static) # doesn't work on PowerPC!
63+
64+
65+
SING_CHECK_PIPE
66+
67+
AX_CHECK_ATOMIC_OPS
68+
69+
AX_GCC_ALWAYS_INLINE
70+
71+
# ?????
72+
# AC_CHECK_LIB(rt, aio_error, RT_LIBS=-lrt, RT_LIBS=)
73+
# AC_SUBST(RT_LIBS)
74+
75+
# In fact, a lot of the code in this directory depends on pthreads
76+
AX_PTHREAD([], [AC_MSG_ERROR([Sorry but pthread is necessary!])])
77+
78+
LIBS="$PTHREAD_LIBS $LIBS"
79+
CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
80+
CC="$PTHREAD_CC"
81+
82+
AC_CHECK_LIB(pthread, pthread_attr_setstack,[], [AC_MSG_ERROR([Sorry but pthread_attr_setstack from pthread is necessary!])])
83+
84+
AX__THREAD
85+
86+
87+
AC_ARG_ENABLE(debug, AS_HELP_STRING([--enable-debug], [build the debugging version of the libraries]),
88+
[if test "x$enableval" = "xyes"; then
89+
ENABLE_DEBUG=yes
90+
fi], ENABLE_DEBUG=no)
91+
92+
AM_CONDITIONAL(WANT_DEBUG, test x"${ENABLE_DEBUG}" == xyes)
93+
94+
#if test x"${ENABLE_DEBUG}" == xyes; then
95+
# AC_DEFINE([DEBUG_THREADS],[1],[Enable Thread Debugging checks])
96+
#fi
97+
98+
# CFLAGS
99+
if test "x$GCC" = xyes && test "x$cflags_expl_set" = xno; then
100+
CFLAGS=""
101+
fi
102+
103+
# CXXFLAGS
104+
if test "x$GXX" = xyes && test "x$cxxflags_expl_set" = xno; then
105+
CXXFLAGS=""
106+
fi
107+
108+
# Add pre'prefixed config
109+
AX_PREFIX_CONFIG_H([xthread_config.h],[],[xthread_config.h])
110+
111+
AH_VERBATIM([PTHREADS_CRASHES_IF_RUN_TOO_EARLY],
112+
[/* Mark the systems where we know it's bad if pthreads runs too
113+
early before main (before threads are initialized, presumably). */
114+
#ifdef __FreeBSD__
115+
#define PTHREADS_CRASHES_IF_RUN_TOO_EARLY 1
116+
#endif])
117+
118+
119+
AH_BOTTOM([/* This can be used instead of 'inline' key-word wherever it is time-critical */
120+
#ifdef GCC_ALWAYS_INLINE
121+
#define FORCE_INLINE GCC_ALWAYS_INLINE
122+
#else
123+
#define FORCE_INLINE inline
124+
#endif
125+
])
126+
127+
AC_CONFIG_FILES([Makefile])
128+
AC_OUTPUT

example.cc

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "cstddef"
22
#include "iostream"
3-
#include "thread.h"
3+
#include "xthread.h"
44

55
struct ThreadLocalData {
66
volatile unsigned long counter;
@@ -11,7 +11,7 @@ struct ThreadInfo {
1111
unsigned long result;
1212
};
1313

14-
#include "tlsize.cc"
14+
#include "tlsize.inc"
1515

1616
#define TL (Thread::currentMemory())
1717
#define TI (Thread::current()->info())
@@ -27,8 +27,12 @@ class ThreadBody : public ThreadAction {
2727
}
2828
};
2929

30+
#include <iostream>
31+
3032
int main() {
3133
Thread *t1, *t2;
34+
try
35+
{
3236
ThreadBody body;
3337
ThreadInitMainStack(); // Initialize main thread's thread-local memory.
3438
// This has to be done in the main function;
@@ -38,10 +42,15 @@ int main() {
3842
// calls it.
3943
t1 = new Thread(); // Create two new threads
4044
t2 = new Thread(); // Creating threads does not start them
41-
t1->info().arg =
42-
t2->info().arg = 100000000; // Pass arguments to both
45+
t1->info().arg = t2->info().arg = 100000000; // Pass arguments to both
4346
t1->run(body); t2->run(body); // Actually run them
4447
t1->wait(); t2->wait(); // Wait for both to complete
4548
std::cout << "Result: " << t1->info().result + t2->info().result << "\n";
4649
delete t1; delete t2; // reclaim thread memory
50+
}
51+
catch( const std::exception & ex )
52+
{
53+
std::cerr << "There was an exception: " << ex.what() << std::endl;
54+
throw;
55+
}
4756
}

0 commit comments

Comments
 (0)