[fpc-devel] how to get same pthreads code to compile between Linux and FreeBSD
Graeme Geldenhuys
graeme at geldenhuys.co.uk
Sun Feb 3 11:51:02 CET 2013
Hi,
I initially posted this in the fpc-pascal list, but I think fpc-devel is
maybe more appropriate, because the pthreads include files might need
amendment.
I'm trying to get tiOPF to compile under FreeBSD. It works under Linux
and Windows without problems.
I got compiler errors using FPC 2.6.0 under FreeBSD, saying that the
compiler couldn't decide which version of the sem_* methods to use.
I had code like this in tiOPF:
{$IFDEF MSWINDOWS}
FSemaphore: THandle;
{$ENDIF MSWINDOWS}
{$IFDEF UNIX}
FSemaphore: TSemaphore;
{$ENDIF UNIX}
...
procedure TtiPool.CreatePoolSemaphore;
begin
{$IFDEF MSWINDOWS}
if FSemaphore <> 0 then
CloseHandle(FSemaphore);
FSemaphore := CreateSemaphore(nil, FMaxPoolSize, FMaxPoolSize, nil);
{$ENDIF MSWINDOWS}
{$IFDEF UNIX}
FillChar(FSemaphore, sizeof(FSemaphore), 0);
// pShared = 0 means, shared between the threads of a process
writeln('FMaxPoolSize = ', FMaxPoolSize);
if sem_init(@FSemaphore, 0, FMaxPoolSize) <> 0 then
raise EtiOPFInternalException.Create('Failed to initialize the
semaphore');
{$ENDIF UNIX}
end;
Note the sem_init() call takes a pointer.
As you can see from the quoted code below, FreeBSD has two versions of
the sem_* methods. The compiler couldn't decide which version to use:
Error: Can't determine which overloaded function to call
To get it to compiler under FreeBSD, I must change the sem_init() line
too the following...
if sem_init(FSemaphore, 0, FMaxPoolSize) <> 0 then
...but this breaks compiling under Linux again.
1) How do I get the tiOPF code to work under both Linux and FreeBSD
using a single {$IFDEF UNIX}. I don't really want to introduce {$IFDEF
LINUX} and {$IFDEF FREEBSD} in the code.
2) Why does FreeBSD have two versions of these methods and Linux only
one? I had a look at the other pthr*.inc units. Linux and SunOS has a
single version, BSD (which I believe is MacOSX too) and Haiku has two
versions.
----------------[ pthrbsd.inc freebsd ]-------------------
function sem_init(__sem:Psem_t;
__pshared:cint;__value:dword):cint;cdecl; external;
function sem_destroy(__sem:Psem_t):cint;cdecl;external ;
function sem_close(__sem:Psem_t):cint;cdecl;external ;
function sem_unlink(__name:Pchar):cint;cdecl;external ;
function sem_wait(__sem:Psem_t):cint;cdecl;external ;
function sem_trywait(__sem:Psem_t):cint;cdecl;external ;
function sem_post(__sem:Psem_t):cint;cdecl;external ;
function sem_getvalue(__sem:Psem_t; __sval:Pcint):cint;cdecl;external;
function sem_init(var __sem: sem_t; __pshared:cint;
__value:dword):cint cdecl;external;
function sem_destroy(var __sem: sem_t):cint;cdecl;external;
function sem_close(var __sem: sem_t):cint;cdecl;external;
function sem_wait(var __sem: sem_t):cint;cdecl;external;
function sem_trywait(var __sem: sem_t):cint;cdecl;external;
function sem_post(var __sem: sem_t):cint;cdecl;external;
function sem_getvalue(var __sem: sem_t; var
__sval:cint):cint;cdecl;external;
--------------------[ end ]----------------------
-------------------[ pthrlinux.inc linux ]---------------------
function sem_init(__sem:Psem_t; __pshared:cint;
__value:dword):cint;cdecl;external libthreads;
function sem_destroy(__sem:Psem_t):cint;cdecl;external libthreads;
function sem_close(__sem:Psem_t):cint;cdecl;external libthreads;
function sem_unlink(__name:Pchar):cint;cdecl;external libthreads;
function sem_wait(__sem:Psem_t):cint;cdecl;external libthreads;
function sem_trywait(__sem:Psem_t):cint;cdecl;external libthreads;
function sem_post(__sem:Psem_t):cint;cdecl;external libthreads;
function sem_getvalue(__sem:Psem_t; __sval:pcint):cint;cdecl;external
libthreads;
function sem_timedwait(__sem: Psem_t; __abstime:
Ptimespec):cint;cdecl; external libthreads;
-------------------[ end ]-----------------------
Attached is a small test project using snippets of code that tiOPF uses.
The current version of this test project will compiler under FreeBSD,
but will not compiler under Linux.
Regards,
- Graeme -
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: semp_test.tar.gz
Type: application/x-gzip
Size: 1579 bytes
Desc: not available
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20130203/f1e4bc7f/attachment.bin>
More information about the fpc-devel
mailing list