#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/autoconf/pthread                     */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Wed Aug  9 13:27:23 1995                          */
#*    Last change :  Wed Mar 24 13:54:13 2004 (serrano)                */
#*    -------------------------------------------------------------    */
#*    Check for the host pthread library.                              */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    flags                                                            */
#*---------------------------------------------------------------------*/
cc=gcc
cflags=
pthreadlibs="-lpthread"
tmp=/tmp
user=bigloo
posixos="unix"

#*---------------------------------------------------------------------*/
#*    We parse the arguments                                           */
#*---------------------------------------------------------------------*/
while : ; do
  case $1 in
    "")
      break;;

    --user=*)
      user="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --cc=*|-cc=*)
      cc="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --cflags=*|-cflags=*)
      cflags="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --lib=*|-lib=*)
      pthreadlibs="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --tmp=*|-tmp=*)
      tmp="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --posixos=*|-posixos=*)
      posixos="`echo $1 | sed 's/^[-a-z]*=//'`";;

    -*)
      echo "Unknown option \"$1\", ignored" >&2;;
  esac
  shift
done


if [ "$posixos" = "unix" ]; then
  case `uname -s` in
    FreeBSD*)
      pthreadlibs="-pthread";;

    solaris*|SunOS*)
      pthreadlibs="-lpthread -lposix4";;
  esac
elif [ "$posixos" = "mingw" ]; then
# THIS IS the RedHat pthread implementation
  pthreadlibs="-lpthreadGC"
fi
 
file=$tmp/actest$user
aout=$tmp/Xactest$user

#*---------------------------------------------------------------------*/
#*    compile                                                          */
#*---------------------------------------------------------------------*/
compile="$cc $cflags $file.c -o $aout $pthreadlibs >/dev/null 2> /dev/null"

#*---------------------------------------------------------------------*/
#*    The test C file                                                  */
#*---------------------------------------------------------------------*/
if( test -f $file.c ); then
   rm -f $file.c || exit $?
fi

#*---------------------------------------------------------------------*/
#*    Test                                                             */
#*---------------------------------------------------------------------*/
cat > $file.c <<EOF
#include <pthread.h>
#include <sched.h>

main( argc, argv )
int   argc;
char *argv[];
{
   pthread_mutex_t mutex;
   pthread_cond_t cv;
   pthread_t thread;
   pthread_key_t key;

   pthread_key_create( &key, 0L );
   pthread_mutex_init( &mutex, 0L );
   pthread_cond_init( &cv, 0L );
   pthread_create( &thread, 0L, 0L, 0L );
   pthread_setspecific( key, (void *)1 );
   printf( "%d\n", pthread_getspecific( key ) );

   pthread_mutex_lock( &mutex );
   pthread_cond_wait( &cv, &mutex );
   pthread_mutex_unlock( &mutex );

   pthread_mutex_lock( &mutex );
   pthread_cond_signal( &cv );
   pthread_mutex_unlock( &mutex );

   sched_yield();
   pthread_join( thread, 0L );
}
EOF

#*---------------------------------------------------------------------*/
#*    Compilation test                                                 */
#*---------------------------------------------------------------------*/
if eval $compile; then
   echo "$pthreadlibs"
else
   echo "no"
fi

\rm -f $file.*
\rm -f $aout
\rm -f $aout*

exit 0
