Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

PL_malloc_mutex in XS

by finisterra (Initiate)
on Jul 15, 2014 at 09:49 UTC ( [id://1093665]=perlquestion: print w/replies, xml ) Need Help??

finisterra has asked for the wisdom of the Perl Monks concerning the following question:

Hi all!
I work with perl version 5.14.2-6ubuntu2.4.
Recently I had a problem writing a new XS module.

In my unsafe sighandler I want check, if PL_malloc_mutex is free, so I can work in my perl part of sighandler code. So, I try to compile this code:

1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #include "ppport.h"
6
7 MODULE = Timeout PACKAGE = Timeout
8
9 void
10 try_get_mutex()
11 CODE:
12 if (pthread_mutex_trylock(PL_malloc_mutex) == EBUSY) {
13 printf("Busy!\n");
14 } else {
15 printf("Free!\n");
16 }

But I have an error:
cc -c -I. -D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -O2 -g -DVERSION=\"0.01\" -DXS_VERSION=\"0.01\" -fPIC "-I/usr/lib/perl/5.14/CORE" Timeout.c
Timeout.xs: In function ‘XS_Timeout_try_get_mutex’:
Timeout.xs:12:29: error: ‘PL_malloc_mutex’ undeclared (first use in this function)
Timeout.xs:12:29: note: each undeclared identifier is reported only once for each function it appears in
Makefile:334: recipe for target 'Timeout.o' failed
make: *** Timeout.o Error 1

shell returned 2

Including perlapi.h didn't help me.
Plase, help me, how can I get this var in my XS code?

Replies are listed 'Best First'.
Re: PL_malloc_mutex in XS
by syphilis (Archbishop) on Jul 15, 2014 at 10:20 UTC
    Timeout.xs:12:29: error: ‘PL_malloc_mutex’ undeclared (first use in this function)

    Is ‘PL_malloc_mutex’ one of those things that you're supposed to access as ‘&PL_malloc_mutex’ ?
    At line 12 try:
    if (pthread_mutex_trylock(&PL_malloc_mutex) == EBUSY) {
    Cheers,
    Rob
      As far as I know, it is mutex, that is used in perl memory allocator.
      Of course, I want give a pointer on it to function pthread_mutex_trylock(), but this code doesn't work too:

      13 if (pthread_mutex_trylock(&PL_malloc_mutex) == EBUSY) {

      Timeout.xs: In function ‘XS_Timeout_try_get_mutex’:
      Timeout.xs:13:30: error: ‘PL_malloc_mutex’ undeclared (first use in this function)
      Timeout.xs:13:30: note: each undeclared identifier is reported only once for each function it appears in
        Hmmm ... yes, not a very intelligent suggestion on my part.

        Cheers,
        Rob
Re: PL_malloc_mutex in XS
by dave_the_m (Monsignor) on Jul 15, 2014 at 11:49 UTC
    PL_malloc_mutex will only be defined in a perl configured with both -Duseithreads and -Dusemymalloc.

    Dave.

      Are these flags set in standard perl build for ubuntu?

      And, generally, how can I check, if perl caught a signal on memory allocating?
        And, generally, how can I check, if perl caught a signal on memory allocating?
        Generally, I think you're onto a loser here. Trying to make unsafe signals work is not easy, which is is why perl itself abandoned them about 10 years ago.

        Normal perl builds use the OS's malloc() library, since its is likely to be superior to perl's own implementaion. You could of course build a perl with perl's malloc by using the -Dusemymalloc on the Configure commnand line, then you'd have a perl with a lockable malloc and rubbish performance.

        Even then, you would still be able to do almost nothing in your signal handler, because just about anything you tried to do (apart now from malloc) would be unsafe.

        Dave.

        Are these flags set in standard perl build for ubuntu?

        On the system perl on my Ubuntu (12.04LTS), useithreads is defined, but usemymalloc is not:
        sisyphus@sisyphus5-desktop:~$ perl -V:usemymalloc usemymalloc='n'; sisyphus@sisyphus5-desktop:~$ perl -V:useithreads useithreads='define'; sisyphus@sisyphus5-desktop:~$
        (You'll want "usemymalloc='y'".)
        I have built some other perls, but none with -Dusemymalloc.
        Maybe you should build one ?

        Cheers,
        Rob

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1093665]
Approved by AppleFritter
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-23 07:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found