public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
* Borland C++Builder support
@ 2004-06-14  4:30 Will Bryant
  0 siblings, 0 replies; 6+ messages in thread
From: Will Bryant @ 2004-06-14  4:30 UTC (permalink / raw)
  To: Ross Johnson; +Cc: pthreads-win32

[-- Attachment #1: Type: text/plain, Size: 2425 bytes --]

Hi Ross/list,

Please consider the attached files, which add Borland C++Builder 
support, for inclusion into the next release of pthreads-win32.  My 
understanding is that they will also work with the free Borland C++ 
compiler, but I haven't tried it.

In ptw32_InterlockedCompareExchange.c, I've added a section for 
Borland's compiler; it's identical to that for the MS compiler except 
that it uses /* ... */ comments instead of ; comments.

The other file is a makefile suitable for use with Borland's compiler 
(run "make -fBmakefile" in the directory).  It builds a single version 
of the library, pthreadBC.dll and the corresponding pthreadBC.lib import 
library, which is comparable to the pthreadVC version; I can't 
personally see any demand for the versions that include structured or 
C++ exception cancellation handling so I haven't attempted to build 
those versions of the library.  (I imagine a static version might be of 
use to some, but we can't legally use that on my commercial projects so 
I can't try that out, unfortunately.)

Borland C++ doesn't define the ENOSYS constant used by pthreads-win32; 
rather than make more extensive patches to the pthreads-win32 source I 
have a mostly-arbitrary constant for it in the makefile.  However this 
doesn't make it visible to the application using the library, so if 
anyone actually wants to use this constant in their apps (why?) someone 
might like to make a seperate NEED_BCC_something define to add this stuff.

The makefile also #defines EDEADLK as EDEADLOCK, _timeb as timeb, and 
_ftime as ftime, to deal with the minor differences between the two 
RTLs' naming conventions, and sets the compiler flags as required to get 
a normal compile of the library.


(While I'm on the subject, the reason Borland users should recompile the 
library, rather than using the impdef/implib technique suggested 
previously on the mailing list, is that a) the errno constants are 
different, so the results returned by the pthread_* functions can be 
meaningless, and b) the errno variable/pseudo-variable itself is 
different in the MS & BCC runtimes, so you can't access the pthreadVC's 
errno from a Borland C++-compiled host application correctly - I imagine 
there are other potential problems from the RTL mismatch too.)

Best regards,
Will

-- 
Will Bryant
Systems Architect, eCOSM Limited
Cell +64 21 655 443, office +64 3 365 4176
http://www.ecosm.com/


[-- Attachment #2: ptw32_InterlockedCompareExchange.c --]
[-- Type: text/plain, Size: 4132 bytes --]

/*
 * ptw32_InterlockedCompareExchange.c
 *
 * Description:
 * This translation unit implements routines which are private to
 * the implementation and may be used throughout it.
 *
 * --------------------------------------------------------------------------
 *
 *      Pthreads-win32 - POSIX Threads Library for Win32
 *      Copyright(C) 1998 John E. Bossom
 *      Copyright(C) 1999,2003 Pthreads-win32 contributors
 * 
 *      Contact Email: rpj@callisto.canberra.edu.au
 * 
 *      The current list of contributors is contained
 *      in the file CONTRIBUTORS included with the source
 *      code distribution. The list can also be seen at the
 *      following World Wide Web location:
 *      http://sources.redhat.com/pthreads-win32/contributors.html
 * 
 *      This 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 of the License, or (at your option) any later version.
 * 
 *      This 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 this library in the file COPYING.LIB;
 *      if not, write to the Free Software Foundation, Inc.,
 *      59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include "pthread.h"
#include "implement.h"


/*
 * ptw32_InterlockedCompareExchange --
 *
 * Needed because W95 doesn't support InterlockedCompareExchange.
 * It is only used when running the dll on W95. Other versions of
 * Windows use the Win32 supported version, which may be running on
 * different processor types.
 *
 * This can't be inlined because we need to know it's address so that
 * we can call it through a pointer.
 */

PTW32_INTERLOCKED_LONG WINAPI
ptw32_InterlockedCompareExchange (PTW32_INTERLOCKED_LPLONG location,
				  PTW32_INTERLOCKED_LONG value,
				  PTW32_INTERLOCKED_LONG comparand)
{

#if defined(__WATCOMC__)
/* Don't report that result is not assigned a value before being referenced */
#pragma disable_message (200)
#endif

  PTW32_INTERLOCKED_LONG result;

/* *INDENT-OFF* */

#if defined(_M_IX86) || defined(_X86_)

#if defined(_MSC_VER) || defined(__WATCOMC__)

  _asm {
    PUSH         ecx
    PUSH         edx
    MOV          ecx,dword ptr [location]
    MOV          edx,dword ptr [value]
    MOV          eax,dword ptr [comparand]
    LOCK CMPXCHG dword ptr [ecx],edx        ; if (EAX == [ECX]), 
                                            ;   [ECX] = EDX
                                            ; else
                                            ;   EAX = [ECX]
    MOV          dword ptr [result], eax
    POP          edx
    POP          ecx
  }


#elif defined(__BORLANDC__)

  _asm {
	PUSH	 ecx
	PUSH	 edx
	MOV 	 ecx,dword ptr [location]
	MOV 	 edx,dword ptr [value]
	MOV 	 eax,dword ptr [comparand]
	LOCK CMPXCHG dword ptr [ecx],edx	    /* if (EAX == [ECX]),
							[ECX] = EDX
						 else
							EAX = [ECX] */
	MOV 	 dword ptr [result], eax
	POP 	 edx
	POP 	 ecx
  }

#elif defined(__GNUC__)

  __asm__
    (
     "lock\n\t"
     "cmpxchgl       %3,(%0)"    /* if (EAX == [location]), */
                                 /*   [location] = value    */
                                 /* else                    */
                                 /*   EAX = [location]           */
     :"=r" (location), "=a" (result)
     :"0"  (location), "q" (value), "a" (comparand)
     : "memory" );

#endif

#else

  /*
   * If execution gets to here then we should be running on a Win95 system
   * but either running on something other than an X86 processor, or a
   * compiler other than MSVC or GCC. Pthreads-win32 doesn't support that
   * platform (yet).
   */

  result = 0;

#endif

/* *INDENT-ON* */

  return result;

#if defined(__WATCOMC__)
#pragma enable_message (200)
#endif

}

[-- Attachment #3: Bmakefile --]
[-- Type: text/plain, Size: 6208 bytes --]

# This makefile is compatible with BCB make.  Use "make -fBMakefile" to compile.
# 
# The variables $DLLDEST and $LIBDEST hold the destination directories for the
# dll and the lib, respectively. Probably all that needs to change is $DEVROOT.

DEVROOT=H:\libraries\pthreads-win32\pthreads

DLLDEST=$(DEVROOT)\DLL
LIBDEST=$(DEVROOT)\DLL

DLLS	= pthreadBC.dll

OPTIM	= /O2

# most of the below flags are to provide compatibility with the MSC-orientated source:
# borland uses EDEADLOCK, timeb, and ftime instead of EDEADLK, _timeb, and _ftime.
# borland doesn't define ENOSYS in errno.h, so we define an arbitrary value for it here (we don't use ENOSYS in our client apps; if someone wants this, we could add a seperate case to the config & need_errno header files for it).

CFLAGS	= /I. /D_WIN32_WINNT=0x400 /DHAVE_CONFIG_H=1 /4 /tWM \
	/DEDEADLK=EDEADLOCK /D_timeb=timeb /D_ftime=ftime \
	/DENOSYS=140
	
#C cleanup code
BCFLAGS	= /D__CLEANUP_C $(CFLAGS)

# Agregate modules for inlinability
DLL_OBJS	= \
		attr.obj \
		barrier.obj \
		cancel.obj \
		cleanup.obj \
		condvar.obj \
		create.obj \
		dll.obj \
		errno.obj \
		exit.obj \
		fork.obj \
		global.obj \
		misc.obj \
		mutex.obj \
		nonportable.obj \
		private.obj \
		rwlock.obj \
		sched.obj \
		semaphore.obj \
		signal.obj \
		spin.obj \
		sync.obj \
		tsd.obj

INCL	= config.h implement.h semaphore.h pthread.h need_errno.h

ATTR_SRCS	= \
		pthread_attr_init.c \
		pthread_attr_destroy.c \
		pthread_attr_getdetachstate.c \
		pthread_attr_setdetachstate.c \
		pthread_attr_getstackaddr.c \
		pthread_attr_setstackaddr.c \
		pthread_attr_getstacksize.c \
		pthread_attr_setstacksize.c \
		pthread_attr_getscope.c \
		pthread_attr_setscope.c

BARRIER_SRCS = \
		pthread_barrier_init.c \
		pthread_barrier_destroy.c \
		pthread_barrier_wait.c \
		pthread_barrierattr_init.c \
		pthread_barrierattr_destroy.c \
		pthread_barrierattr_setpshared.c \
		pthread_barrierattr_getpshared.c

CANCEL_SRCS	= \
		pthread_setcancelstate.c \
		pthread_setcanceltype.c \
		pthread_testcancel.c \
		pthread_cancel.c 

CONDVAR_SRCS	= \
		ptw32_cond_check_need_init.c \
		pthread_condattr_destroy.c \
		pthread_condattr_getpshared.c \
		pthread_condattr_init.c \
		pthread_condattr_setpshared.c \
		pthread_cond_destroy.c \
		pthread_cond_init.c \
		pthread_cond_signal.c \
		pthread_cond_wait.c

EXIT_SRCS	= \
		pthread_exit.c

MISC_SRCS	= \
		pthread_equal.c \
		pthread_getconcurrency.c \
		pthread_once.c \
		pthread_self.c \
		pthread_setconcurrency.c \
		ptw32_calloc.c \
		ptw32_new.c \
		w32_CancelableWait.c

MUTEX_SRCS	= \
		ptw32_mutex_check_need_init.c \
		pthread_mutex_init.c \
		pthread_mutex_destroy.c \
		pthread_mutexattr_init.c \
		pthread_mutexattr_destroy.c \
		pthread_mutexattr_getpshared.c \
		pthread_mutexattr_setpshared.c \
		pthread_mutexattr_settype.c \
		pthread_mutexattr_gettype.c \
		pthread_mutex_lock.c \
		pthread_mutex_timedlock.c \
		pthread_mutex_unlock.c \
		pthread_mutex_trylock.c

NONPORTABLE_SRCS = \
		pthread_mutexattr_setkind_np.c \
		pthread_mutexattr_getkind_np.c \
		pthread_getw32threadhandle_np.c \
		pthread_delay_np.c \
		pthread_num_processors_np.c \
		pthread_win32_attach_detach_np.c \
		pthread_timechange_handler_np.c 

PRIVATE_SRCS	= \
		ptw32_is_attr.c \
		ptw32_processInitialize.c \
		ptw32_processTerminate.c \
		ptw32_threadStart.c \
		ptw32_threadDestroy.c \
		ptw32_tkAssocCreate.c \
		ptw32_tkAssocDestroy.c \
		ptw32_callUserDestroyRoutines.c \
		ptw32_timespec.c \
		ptw32_throw.c \
		ptw32_InterlockedCompareExchange.c \
		ptw32_getprocessors.c

RWLOCK_SRCS	= \
		ptw32_rwlock_check_need_init.c \
		ptw32_rwlock_cancelwrwait.c \
		pthread_rwlock_init.c \
		pthread_rwlock_destroy.c \
		pthread_rwlockattr_init.c \
		pthread_rwlockattr_destroy.c \
		pthread_rwlockattr_getpshared.c \
		pthread_rwlockattr_setpshared.c \
		pthread_rwlock_rdlock.c \
		pthread_rwlock_timedrdlock.c \
		pthread_rwlock_wrlock.c \
		pthread_rwlock_timedwrlock.c \
		pthread_rwlock_unlock.c \
		pthread_rwlock_tryrdlock.c \
		pthread_rwlock_trywrlock.c

SCHED_SRCS	= \
		pthread_attr_setschedpolicy.c \
		pthread_attr_getschedpolicy.c \
		pthread_attr_setschedparam.c \
		pthread_attr_getschedparam.c \
		pthread_attr_setinheritsched.c \
		pthread_attr_getinheritsched.c \
		pthread_setschedparam.c \
		pthread_getschedparam.c \
		sched_get_priority_max.c \
		sched_get_priority_min.c \
		sched_setscheduler.c \
		sched_getscheduler.c \
		sched_yield.c

SEMAPHORE_SRCS = \
		sem_init.c \
		sem_destroy.c \
		sem_trywait.c \
		sem_timedwait.c \
		sem_wait.c \
		sem_post.c \
		sem_post_multiple.c \
		sem_getvalue.c \
		sem_open.c \
		sem_close.c \
		sem_unlink.c \
		ptw32_increase_semaphore.c \
		ptw32_decrease_semaphore.c

SPIN_SRCS	= \
		ptw32_spinlock_check_need_init.c \
		pthread_spin_init.c \
		pthread_spin_destroy.c \
		pthread_spin_lock.c \
		pthread_spin_unlock.c \
		pthread_spin_trylock.c

SYNC_SRCS	= \
		pthread_detach.c \
		pthread_join.c

TSD_SRCS	= \
		pthread_key_create.c \
		pthread_key_delete.c \
		pthread_setspecific.c \
		pthread_getspecific.c


all: clean $(DLLS)
	
realclean: clean
	if exist *.dll del *.dll
	if exist *.lib del *.lib
	if exist *.stamp del *.stamp

clean:
	if exist *.obj del *.obj
	if exist *.ilk del *.ilk
	if exist *.pdb del *.pdb
	if exist *.exp del *.exp
	if exist *.o del *.o
	if exist *.i del *.i


install: $(DLLS)
	copy pthread*.dll $(DLLDEST)
	copy pthread*.lib $(LIBDEST)

$(DLLS): $(DLL_OBJS)
	ilink32 /Tpd /Gi $(DLL_OBJS), \
		$@, ,\
		import32.lib cw32mt.lib c0d32x.obj

.c.obj:
	bcc32 $(OPTIM) $(BCFLAGS) -c $<

attr.obj:	attr.c $(ATTR_SRCS) $(INCL)
barrier.obj:	barrier.c $(BARRIER_SRCS) $(INCL)
cancel.obj:	cancel.c $(CANCEL_SRCS) $(INCL)
condvar.obj:	condvar.c $(CONDVAR_SRCS) $(INCL)
exit.obj:	exit.c $(EXIT_SRCS) $(INCL)
misc.obj:	misc.c $(MISC_SRCS) $(INCL)
mutex.obj:	mutex.c $(MUTEX_SRCS) $(INCL)
nonportable.obj:	nonportable.c $(NONPORTABLE_SRCS) $(INCL)
private.obj:	private.c $(PRIVATE_SRCS) $(INCL)
rwlock.obj:	rwlock.c $(RWLOCK_SRCS) $(INCL)
sched.obj:	sched.c $(SCHED_SRCS) $(INCL)
semaphore.obj:	semaphore.c $(SEMAPHORE_SRCS) $(INCL)
spin.obj:	spin.c $(SPIN_SRCS) $(INCL)
sync.obj:	sync.c $(SYNC_SRCS) $(INCL)
tsd.obj:	tsd.c $(TSD_SRCS) $(INCL)

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Borland C++Builder support
  2004-08-06 16:55   ` Ross Johnson
@ 2004-08-07  2:52     ` Will Bryant
  0 siblings, 0 replies; 6+ messages in thread
From: Will Bryant @ 2004-08-07  2:52 UTC (permalink / raw)
  To: pthreads-win32

Ross Johnson wrote:

> There are pthreads implementations that allow a NULL thread parameter 
> (Solaris - see below) and the question of a NULL value has been asked 
> before on this list. My copy of the SUSV3 standard doesn't say that 
> NULL can't be passed and doesn't require an error be returned. It 
> appears to be left to the implementation.

My Linux man page says "On success, the identifier of the newly created 
thread is stored in the location pointed by the _thread_ argument, and a 
0 is returned.", and my Solaris man page says "Upon successful  
completion, pthread_create() stores the ID of the created thread in the 
location referenced by _thread_.", so strictly speaking since neither 
says "if _thread_ is non-NULL" one should _expect_ that it would 
segfault, though only a true pedant would claim that it's a bug if it 
doesn't.

> In pthreads-win32, a memory protection fault is raised if NULL is 
> passed, but it also starts the thread before the fault is raised, 
> which is probably a bug.

I agree, it should do one or the other.

> Pthreads-win32 could probably emulate Solaris with a one line change. 
> The question is:- which behaviour is preferrable?

Good question.  Presumably anyone who's using pthreads-win32 is using it 
to acheive cross-platform portability.  I was initially going to put in 
my vote for cleanly handling this as an error condition, on the basis 
that that way people can expect their pthreads-win32 programs to run on 
Linux.  But, having thought about it a bit more, I think it's more 
likely that people using pthreads-win32 will be porting solaris 
applications to win32 than writing win32 apps with pthreads and then 
porting them to Linux.

So my vote is to go with the Solaris behaviour and accept NULL arguments.

But I would like it to be documented somewhere that this behaviour is 
not required.  Either behaviour seems acceptable to me, just not the 
current half-works, half-doesn't behaviour.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Borland C++Builder support
  2004-08-06 13:31 ` Will Bryant
  2004-08-06 16:55   ` Ross Johnson
@ 2004-08-07  2:50   ` Will Bryant
  1 sibling, 0 replies; 6+ messages in thread
From: Will Bryant @ 2004-08-07  2:50 UTC (permalink / raw)
  To: Gianluca; +Cc: pthreads-win32

Will Bryant wrote:

> In general (ie. not for pthreads-win32 specifically), you need to 
> declare a pthread_id_t and pass it's address as the first argument to 
> pthread_create.  Try making that change first.

This was a typo: the typedef is pthread_t, not pthread_id_t.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Borland C++Builder support
  2004-08-06 13:31 ` Will Bryant
@ 2004-08-06 16:55   ` Ross Johnson
  2004-08-07  2:52     ` Will Bryant
  2004-08-07  2:50   ` Will Bryant
  1 sibling, 1 reply; 6+ messages in thread
From: Ross Johnson @ 2004-08-06 16:55 UTC (permalink / raw)
  To: pthreads-win32

Will Bryant wrote:

> Gianluca wrote:
>
>> I have built the library with the Will Bryant's bmakefile.
>> I've received a bunch of warnings but it was OK.
>> I put the PthreadBC.dll on Windows directory, I included
>> PthreadBC.lib in my .bpr project, I compiled and linked the program 
>> below.
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>> #include <pthread.h>
>>
>> void*  function( void*  arg )
>> {
>>   printf( "This is thread %d\n", pthread_self() );
>>   return( 0 );
>> }
>>
>> int main( void )
>> {
>>   pthread_attr_t attr;
>>
>>   pthread_attr_init( &attr );
>>   pthread_attr_setdetachstate(
>>      &attr, PTHREAD_CREATE_DETACHED );
>>   pthread_create( NULL, &attr, &function, NULL );
>>  
>>
> In general (ie. not for pthreads-win32 specifically), you need to 
> declare a pthread_id_t and pass it's address as the first argument to 
> pthread_create.  Try making that change first.
>
There are pthreads implementations that allow a NULL thread 
parameter (Solaris - see below) and the question of a NULL 
value has been asked before on this list. My copy of the 
SUSV3 standard doesn't say that NULL can't be passed and 
doesn't require an error be returned. It appears to be left 
to the implementation.

In pthreads-win32, a memory protection fault is raised if 
NULL is passed, but it also starts the thread before the 
fault is raised, which is probably a bug.

I've run the following test on machines that I have access to:

#include <stdio.h>
#include <pthread.h>
 


void *
thr(void * arg)
{
  printf("Inside thread\n");

  /* Mandatory if we're going to be well behaved. */
  pthread_detach(pthread_self());
}
 


int
main()
{
  pthread_t t;
  int result = 0;

  result == pthread_create(NULL, NULL, thr, NULL);
  sleep(2);

  return result;
}


Linux (Redhat 9) segfaults without running the thread.
Solaris 7 runs the thread and exits with no error or fault.

Pthreads-win32 could probably emulate Solaris with a one 
line change. The question is:- which behaviour is preferrable?

Ross


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Borland C++Builder support
  2004-08-03 10:04 Gianluca
@ 2004-08-06 13:31 ` Will Bryant
  2004-08-06 16:55   ` Ross Johnson
  2004-08-07  2:50   ` Will Bryant
  0 siblings, 2 replies; 6+ messages in thread
From: Will Bryant @ 2004-08-06 13:31 UTC (permalink / raw)
  To: pthreads-win32

Gianluca wrote:

>I have built the library with the Will Bryant's bmakefile.
>I've received a bunch of warnings but it was OK.
>I put the PthreadBC.dll on Windows directory, I included
>PthreadBC.lib in my .bpr project, I compiled and linked the 
>program below.
>
>#include <stdio.h>
>#include <stdlib.h>
>#include <pthread.h>
>
>void*  function( void*  arg )
>{
>   printf( "This is thread %d\n", pthread_self() );
>   return( 0 );
>}
>
>int main( void )
>{
>   pthread_attr_t attr;
>
>   pthread_attr_init( &attr );
>   pthread_attr_setdetachstate(
>      &attr, PTHREAD_CREATE_DETACHED );
>   pthread_create( NULL, &attr, &function, NULL );
>  
>
In general (ie. not for pthreads-win32 specifically), you need to 
declare a pthread_id_t and pass it's address as the first argument to 
pthread_create.  Try making that change first.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Borland C++Builder support
@ 2004-08-03 10:04 Gianluca
  2004-08-06 13:31 ` Will Bryant
  0 siblings, 1 reply; 6+ messages in thread
From: Gianluca @ 2004-08-03 10:04 UTC (permalink / raw)
  To: pthreads-win32

I have built the library with the Will Bryant's bmakefile.
I've received a bunch of warnings but it was OK.
I put the PthreadBC.dll on Windows directory, I included
PthreadBC.lib in my .bpr project, I compiled and linked the 
program below.

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void*  function( void*  arg )
{
   printf( "This is thread %d\n", pthread_self() );
   return( 0 );
}

int main( void )
{
   pthread_attr_t attr;

   pthread_attr_init( &attr );
   pthread_attr_setdetachstate(
      &attr, PTHREAD_CREATE_DETACHED );
   pthread_create( NULL, &attr, &function, NULL );

   /* Allow threads to run for 10 seconds. */
   sleep(10);
   return EXIT_SUCCESS;
}

On debugging the thread writes out "This is thread 19922" and does 
not return.
If I run the executable just created Win XP gives me error.

Could someone help me?

Thanks in advance,
Gianluca

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2004-08-07  2:52 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-06-14  4:30 Borland C++Builder support Will Bryant
2004-08-03 10:04 Gianluca
2004-08-06 13:31 ` Will Bryant
2004-08-06 16:55   ` Ross Johnson
2004-08-07  2:52     ` Will Bryant
2004-08-07  2:50   ` Will Bryant

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).