'
' POSIX threads - their license below.
'
' Ported to BaCon INCLUDE file by Peter van Eerten - GPL.
'
' Version 1.0: Initial release
' Version 1.1: Adapted for BaCon 3.0 and higher
'
' pthread.h
'
' Note that these are POSIX threads where the types are defined
'    in <sys/types.h>.
'
' Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
' Free Software Foundation, Inc.
' This file is part of the GNU C Library.
'
' The GNU C Library is free software; you can redistribute it and/or
' modify it under the terms of the GNU Lesser General Public
' License as published by the Free Software Foundation; either
' version 2.1 of the License, or (at your option) any later version.
'
' The GNU C Library is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
' Lesser General Public License for more details.
'
' You should have received a copy of the GNU Lesser General Public
' License along with the GNU C Library; if not, write to the Free
' Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
' 02111-1307 USA.
'
CATCH GOTO pthread_handle_error

pthread_LIB$ = "libpthread.so"
pthread_seq = -1

LABEL pthread_import_retry
    INCR pthread_seq
    pthread_retry = FALSE
    IMPORT "pthread_create(long,long,void*,long)" FROM pthread_LIB$ TYPE int
    IF pthread_retry THEN GOTO pthread_import_retry

GOTO pthread_continue

LABEL pthread_handle_error
    IF pthread_seq IS 0 THEN pthread_LIB$ = "libpthread.so.0"
    ELSE pthread_LIB$ = CONCAT$(LEFT$(pthread_LIB$, INSTRREV(pthread_LIB$, ".")), STR$(pthread_seq))
    IF pthread_seq < 30 THEN
        pthread_retry = TRUE
    ELSE
        PRINT "No pthread library found!"
        END
    END IF
    RESUME

LABEL pthread_continue

' Some constants
CONST PTHREAD_CANCEL_ASYNCHRONOUS = 1
CONST PTHREAD_CANCEL_DEFERRED = 0
CONST PTHREAD_CANCEL_DISABLE = 1
CONST PTHREAD_CANCEL_ENABLE = 0
CONST PTHREAD_CREATE_DETACHED = 1
CONST PTHREAD_CREATE_JOINABLE = 0
CONST PTHREAD_EXPLICIT_SCHED = 1
CONST PTHREAD_INHERIT_SCHED = 0
CONST PTHREAD_MUTEX_ADAPTIVE_NP = 3
CONST PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
CONST PTHREAD_MUTEX_ERRORCHECK_NP = 2
CONST PTHREAD_MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK_NP
CONST PTHREAD_MUTEX_FAST_NP = PTHREAD_MUTEX_TIMED_NP
CONST PTHREAD_MUTEX_NORMAL = PTHREAD_MUTEX_TIMED_NP
CONST PTHREAD_MUTEX_RECURSIVE_NP = 1
CONST PTHREAD_MUTEX_RECURSIVE = PTHREAD_MUTEX_RECURSIVE_NP
CONST PTHREAD_MUTEX_ROBUST = 1
CONST PTHREAD_MUTEX_ROBUST_NP = PTHREAD_MUTEX_ROBUST
CONST PTHREAD_MUTEX_STALLED = 0
CONST PTHREAD_MUTEX_STALLED_NP = PTHREAD_MUTEX_STALLED
CONST PTHREAD_MUTEX_TIMED_NP = 0
CONST PTHREAD_PRIO_INHERIT = 1
CONST PTHREAD_PRIO_NONE = 0
CONST PTHREAD_PRIO_PROTECT = 2
CONST PTHREAD_PROCESS_PRIVATE = 0
CONST PTHREAD_PROCESS_SHARED = 1
CONST PTHREAD_RWLOCK_DEFAULT_NP = PTHREAD_RWLOCK_PREFER_READER_NP
CONST PTHREAD_RWLOCK_PREFER_READER_NP = 0
CONST PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP = 2
CONST PTHREAD_RWLOCK_PREFER_WRITER_NP = 1
CONST PTHREAD_SCOPE_PROCESS = 1
CONST PTHREAD_SCOPE_SYSTEM = 0

' Most used PTHREAD functions
IMPORT "pthread_attr_destroy(long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_attr_getdetachstate(long,long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_attr_getstackaddr(long,long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_attr_getstacksize(long,long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_attr_init(long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_attr_setdetachstate(long,int)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_attr_setstackaddr(long,long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_attr_setstacksize(long,long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_cancel(long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_cond_broadcast(long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_cond_destroy(long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_cond_init(long,long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_cond_signal(long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_cond_timedwait(long,long,long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_cond_wait(long,long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_condattr_destroy(long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_condattr_init(long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_detach(long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_equal(long,long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_exit(long)" FROM pthread_LIB$ TYPE void
IMPORT "pthread_getconcurrency" FROM pthread_LIB$ TYPE int
IMPORT "pthread_getschedparam(long,long,long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_join(long,long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_mutex_destroy(long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_mutex_init(long,long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_mutex_lock(long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_mutex_trylock(long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_mutex_unlock(long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_mutexattr_destroy(long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_mutexattr_init(long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_once(long,void*)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_self" FROM pthread_LIB$ TYPE long
IMPORT "pthread_setconcurrency(int)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_setschedparam(long,int,long)" FROM pthread_LIB$ TYPE int
IMPORT "pthread_setschedprio(long,int)" FROM pthread_LIB$ TYPE int

CATCH RESET