# vim:ts=4
#
# Copyright (c) 2005-2010 Hypertriton, 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:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. 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.
#
# 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 THE AUTHOR 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.
my @autoIncludeDirs = (
'/usr/include/pthreads',
'/usr/local/include',
'/usr/local/include/pthreads',
);
my @autoLibDirs = (
'/usr/local/lib',
);
my @autoLibFiles = (
'pthread',
'pthreadGC1',
'pthreadGC1d',
'pthreadGCE1',
'pthreadGCE1d',
'pthreadGC2',
'pthreadGC2d',
'pthreadGCE2',
'pthreadGCE2d',
);
my $testCodeStd = << 'EOF';
#include
#include
static void *start_routine(void *arg)
{
return (NULL);
}
int main(int argc, char *argv[])
{
pthread_mutex_t mutex;
pthread_t thread;
pthread_mutex_init(&mutex, NULL);
pthread_mutex_lock(&mutex);
pthread_mutex_unlock(&mutex);
pthread_mutex_destroy(&mutex);
pthread_create(&thread, NULL, start_routine, NULL);
return (0);
}
EOF
my $testCodeXopen = << 'EOF';
#include
#include
int main(int argc, char *argv[])
{
pthread_mutex_t mutex;
pthread_mutexattr_t mutexattr;
pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&mutex, &mutexattr);
return (0);
}
EOF
sub SearchIncludes ($$)
{
my ($pfx, $def) = @_;
foreach my $dir ("$pfx/lib", @autoIncludeDirs) {
MkIfExists("$dir/pthread.h");
MkDefine('PTHREADS_CFLAGS', "-I$dir");
MkEndif;
}
}
sub SearchLibs ($$)
{
my ($pfx, $def) = @_;
foreach my $dir ("$pfx/lib", @autoLibDirs) {
foreach my $file (@autoLibFiles) {
MkIfExists("$dir/lib$file.a");
MkDefine($def, "-L$dir -l$file");
MkEndif;
}
}
}
sub TestPthreadsStd
{
my ($ver, $pfx) = @_;
MkIfNE($pfx, '');
MkDefine('PTHREADS_CFLAGS', "-I$pfx/include");
MkDefine('PTHREADS_LIBS', "-L$pfx/lib -lpthread");
MkElse;
MkDefine('PTHREADS_CFLAGS', '');
MkDefine('PTHREADS_LIBS', "-lpthread");
MkEndif;
# Try the standard -lpthread.
MkCompileC('HAVE_PTHREADS', '${PTHREADS_CFLAGS}', '${PTHREADS_LIBS}', $testCodeStd);
MkIfTrue('${HAVE_PTHREADS}');
MkDefine('CFLAGS', '${CFLAGS} ${PTHREADS_CFLAGS}');
MkSaveMK('CFLAGS', 'PTHREADS_CFLAGS', 'PTHREADS_LIBS');
MkSaveDefine('PTHREADS_CFLAGS', 'PTHREADS_LIBS');
MkElse();
# Fallback to -pthread.
MkPrintN('checking for -pthread...');
MkDefine('PTHREADS_LIBS', '-pthread');
MkCompileC('HAVE_PTHREADS', '${PTHREADS_CFLAGS}', '${PTHREADS_LIBS}', $testCodeStd);
MkIf('"${HAVE_PTHREADS}" = "yes"');
MkDefine('CFLAGS', '${CFLAGS} ${PTHREADS_CFLAGS}');
MkSaveMK('CFLAGS', 'PTHREADS_CFLAGS','PTHREADS_LIBS');
MkSaveDefine('PTHREADS_CFLAGS','PTHREADS_LIBS');
MkElse();
# Fallback to scanning libs and include files.
MkDefine('PTHREADS_CFLAGS', '');
MkDefine('PTHREADS_LIBS', '');
MkPrintN('checking for -pthread (common paths)...');
SearchIncludes($pfx, 'PTHREADS_CFLAGS');
SearchLibs($pfx, 'PTHREADS_LIBS');
MkCompileC('HAVE_PTHREADS',
'${PTHREADS_CFLAGS}', '${PTHREADS_LIBS}',
$testCodeStd);
MkIf('"${HAVE_PTHREADS}" = "yes"');
MkDefine('CFLAGS', '${CFLAGS} ${PTHREADS_CFLAGS}');
MkSaveMK('CFLAGS', 'PTHREADS_CFLAGS', 'PTHREADS_LIBS');
MkSaveDefine('PTHREADS_CFLAGS', 'PTHREADS_LIBS');
MkEndif();
MkEndif();
MkEndif();
return (0);
}
sub TestPthreadMutexRecursive
{
#
# Look for the PTHREAD_MUTEX_RECURSIVE flag of the function
# pthread_mutexattr_settype().
#
MkPrintN('checking for PTHREAD_MUTEX_RECURSIVE...');
MkCompileC('HAVE_PTHREAD_MUTEX_RECURSIVE',
'${PTHREADS_CFLAGS}', '${PTHREADS_LIBS}', << 'EOF');
#include
#include
int main(int argc, char *argv[])
{
pthread_mutex_t mutex;
pthread_mutexattr_t mutexattr;
pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&mutex, &mutexattr);
return (0);
}
EOF
MkIfTrue('${HAVE_PTHREAD_MUTEX_RECURSIVE}');
MkSaveDefine('HAVE_PTHREAD_MUTEX_RECURSIVE');
MkElse;
MkSaveUndef('HAVE_PTHREAD_MUTEX_RECURSIVE');
MkEndif;
#
# Look for the PTHREAD_MUTEX_RECURSIVE_NP flag of the function
# pthread_mutexattr_settype().
#
MkPrintN('checking for PTHREAD_MUTEX_RECURSIVE_NP...');
MkCompileC('HAVE_PTHREAD_MUTEX_RECURSIVE_NP',
'${PTHREADS_CFLAGS}', '${PTHREADS_LIBS}', << 'EOF');
#include
#include
int main(int argc, char *argv[])
{
pthread_mutex_t mutex;
pthread_mutexattr_t mutexattr;
pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE_NP);
pthread_mutex_init(&mutex, &mutexattr);
return (0);
}
EOF
MkIfTrue('${HAVE_PTHREAD_MUTEX_RECURSIVE_NP}');
MkSaveDefine('HAVE_PTHREAD_MUTEX_RECURSIVE_NP');
MkElse;
MkSaveUndef('HAVE_PTHREAD_MUTEX_RECURSIVE_NP');
MkEndif;
return (0);
}
sub TestPthreadsXOpenExt
{
my ($ver, $pfx) = @_;
MkPrintN('checking for the X/Open Threads Extension...');
MkCaseIn('${host}');
MkCaseBegin('*-*-freebsd*');
MkDefine('PTHREADS_XOPEN_CFLAGS', '');
MkCaseEnd;
MkCaseBegin('*');
MkDefine('PTHREADS_XOPEN_CFLAGS', '-U_XOPEN_SOURCE -D_XOPEN_SOURCE=600');
MkCaseEnd;
MkEsac;
# Try the standard -lpthread
MkIfNE($pfx, '');
MkDefine('PTHREADS_XOPEN_LIBS', "-L$pfx/lib -lpthread");
MkElse;
MkDefine('PTHREADS_XOPEN_LIBS', "-lpthread");
MkEndif;
MkCompileC('HAVE_PTHREADS_XOPEN', '${PTHREADS_XOPEN_CFLAGS}', '${PTHREADS_XOPEN_LIBS}', $testCodeXopen);
MkIfTrue('${HAVE_PTHREADS_XOPEN}');
MkSaveMK('PTHREADS_XOPEN_CFLAGS', 'PTHREADS_XOPEN_LIBS');
MkSaveDefine('HAVE_PTHREADS_XOPEN');
MkSaveDefine('PTHREADS_XOPEN_CFLAGS', 'PTHREADS_XOPEN_LIBS');
MkElse;
# Fallback to scanning libraries and includes.
MkDefine('PTHREADS_XOPEN_LIBS', '');
MkPrintN('checking for the X/Open Threads Extension (common paths)...');
SearchLibs($pfx, 'PTHREADS_XOPEN_LIBS');
SearchIncludes($pfx, 'PTHREADS_XOPEN_CFLAGS');
MkCompileC('HAVE_PTHREADS_XOPEN',
'${PTHREADS_XOPEN_CFLAGS}', '${PTHREADS_XOPEN_LIBS}',
$testCodeXopen);
MkIfTrue('${HAVE_PTHREADS_XOPEN}');
MkSaveMK('PTHREADS_XOPEN_CFLAGS', 'PTHREADS_XOPEN_LIBS');
MkSaveDefine('HAVE_PTHREADS_XOPEN');
MkSaveDefine('PTHREADS_XOPEN_CFLAGS', 'PTHREADS_XOPEN_LIBS');
MkElse;
MkSaveUndef('HAVE_PTHREADS_XOPEN');
MkEndif;
MkEndif;
return (0);
}
sub TestPthreads
{
TestPthreadsStd(@_);
TestPthreadsXOpenExt(@_);
TestPthreadMutexRecursive();
return (0);
}
sub Emul
{
my ($os, $osrel, $machine) = @_;
if ($os eq 'linux') {
MkDefine('HAVE_PTHREADS', 'yes');
MkDefine('HAVE_PTHREADS_XOPEN', 'yes');
MkDefine('PTHREADS_CFLAGS', '');
MkDefine('PTHREADS_LIBS', '-lpthread');
MkDefine('PTHREADS_XOPEN_CFLAGS', '-U_XOPEN_SOURCE '.
'-D_XOPEN_SOURCE=600');
MkDefine('PTHREADS_XOPEN_LIBS', '-lpthread');
MkSaveDefine('HAVE_PTHREADS', 'HAVE_PTHREADS_XOPEN', 'PTHREADS_CFLAGS',
'PTHREADS_LIBS', 'PTHREADS_XOPEN_CFLAGS',
'PTHREADS_XOPEN_LIBS');
MkSaveUndef('HAVE_PTHREAD_MUTEX_RECURSIVE');
MkSaveUndef('HAVE_PTHREAD_MUTEX_RECURSIVE_NP');
} elsif ($os eq 'darwin') {
MkDefine('HAVE_PTHREADS', 'yes');
MkDefine('HAVE_PTHREADS_XOPEN', 'yes');
MkDefine('HAVE_PTHREAD_MUTEX_RECURSIVE', 'yes');
MkDefine('PTHREADS_CFLAGS', '');
MkDefine('PTHREADS_LIBS', '-lpthread');
MkDefine('PTHREADS_XOPEN_CFLAGS', '-U_XOPEN_SOURCE '.
'-D_XOPEN_SOURCE=600');
MkDefine('PTHREADS_XOPEN_LIBS', '-lpthread');
MkSaveDefine('HAVE_PTHREADS', 'HAVE_PTHREADS_XOPEN', 'PTHREADS_CFLAGS',
'PTHREADS_LIBS', 'HAVE_PTHREAD_MUTEX_RECURSIVE');
MkSaveUndef('HAVE_PTHREAD_MUTEX_RECURSIVE_NP');
} elsif ($os eq 'windows') {
MkDefine('HAVE_PTHREADS', 'yes');
MkDefine('HAVE_PTHREADS_XOPEN', 'yes');
MkDefine('HAVE_PTHREAD_MUTEX_RECURSIVE', 'yes');
MkDefine('PTHREADS_CFLAGS', '');
MkDefine('PTHREADS_LIBS', 'pthreadVC2');
MkDefine('PTHREADS_XOPEN_CFLAGS', '-U_XOPEN_SOURCE '.
'-D_XOPEN_SOURCE=600');
MkDefine('PTHREADS_XOPEN_LIBS', 'pthreadVC2');
MkSaveDefine('HAVE_PTHREADS', 'HAVE_PTHREADS_XOPEN', 'PTHREADS_CFLAGS',
'PTHREADS_LIBS', 'HAVE_PTHREAD_MUTEX_RECURSIVE');
MkSaveUndef('HAVE_PTHREAD_MUTEX_RECURSIVE_NP');
} elsif ($os eq 'freebsd') {
MkDefine('HAVE_PTHREADS', 'yes');
MkDefine('HAVE_PTHREADS_XOPEN', 'yes');
MkDefine('HAVE_PTHREAD_MUTEX_RECURSIVE', 'yes');
MkDefine('PTHREADS_CFLAGS', '');
MkDefine('PTHREADS_LIBS', '-lpthread');
MkDefine('PTHREADS_XOPEN_CFLAGS', '');
MkDefine('PTHREADS_XOPEN_LIBS', '-lpthread');
MkSaveDefine('HAVE_PTHREADS', 'HAVE_PTHREADS_XOPEN', 'PTHREADS_CFLAGS',
'PTHREADS_LIBS', 'HAVE_PTHREAD_MUTEX_RECURSIVE');
MkSaveUndef('HAVE_PTHREAD_MUTEX_RECURSIVE_NP');
} else {
MkDefine('HAVE_PTHREADS', 'yes');
MkDefine('HAVE_PTHREADS_XOPEN', 'yes');
MkDefine('HAVE_PTHREAD_MUTEX_RECURSIVE', 'yes');
MkDefine('PTHREADS_CFLAGS', '');
MkDefine('PTHREADS_LIBS', '-lpthread');
MkDefine('PTHREADS_XOPEN_CFLAGS', '-U_XOPEN_SOURCE '.
'-D_XOPEN_SOURCE=600');
MkDefine('PTHREADS_XOPEN_LIBS', '-lpthread');
MkSaveDefine('HAVE_PTHREADS', 'HAVE_PTHREADS_XOPEN', 'PTHREADS_CFLAGS',
'PTHREADS_LIBS', 'HAVE_PTHREAD_MUTEX_RECURSIVE');
MkSaveUndef('HAVE_PTHREAD_MUTEX_RECURSIVE_NP');
}
return (1);
}
sub Link
{
my $lib = shift;
if ($lib ne 'pthreads') {
return (0);
}
PmIfHDefined('HAVE_PTHREADS');
if ($EmulOS eq 'windows') {
PmLink('pthreadVC2');
} else {
PmLink('pthread');
}
if ($EmulEnv =~ /^cb-/) {
PmIncludePath('$(#pthreads.include)');
PmLibPath('$(#pthreads.lib)');
}
PmEndif;
return (1);
}
BEGIN
{
$DESCR{'pthreads'} = 'POSIX threads';
$TESTS{'pthreads'} = \&TestPthreads;
$EMUL{'pthreads'} = \&Emul;
$LINK{'pthreads'} = \&Link;
$DEPS{'pthreads'} = 'cc';
}
;1