Signalsafe data structures


 
Thread Tools Search this Thread
Top Forums Programming Signalsafe data structures
# 8  
Old 12-22-2011
I now realized that I need to save two values per array position instead of one. Now my question is how to do this without breaking it.

Fill the array with structs holding two elements (integers)? But then I need to return a struct with get_value. Is that OK?
# 9  
Old 12-22-2011
That's a trivial change.

Code:
#define SIGBUF_SIZE 1024

typedef struct sigdata
{
        int a;
        int b;
} sigdata;

struct
{
        unsigned int rdpos, wrpos;
        sigdata buffer[SIGBUF_SIZE];
} sigbuf={ 0, 0 };

int put_value(sigdata val)
{
        unsigned int wrpos=__sync_fetch_and_add(&sigbuf.wrpos, 1);
        unsigned int rdpos=__sync_fetch_and_add(&sigbuf.rdpos, 0);
        // Compare this way, to prevent integer wraparound problems!
        unsigned int off=(wrpos-rdpos);

        if(off >= SIGBUF_SIZE)
        {
                __sync_fetch_and_sub(&sigbuf.wrpos, 1);
                return(-1);
        }

        sigbuf.buffer[wrpos % SIGBUF_SIZE]=val;
        return(0);
}

// use like:
//         sigdata d;
//         get_value(&d);
int get_value(sigdata *val)
{
        unsigned int wrpos=__sync_fetch_and_add(&sigbuf.wrpos, 0);
        // Do NOT add here!  If a signal happens, it may see a 'free'
        // element even though there's not.  Only add once we're sure
        // there's anything to read.
        unsigned int rdpos=__sync_fetch_and_add(&sigbuf.rdpos, 0);
        unsigned int off=(wrpos-rdpos);

        if(off == 0)    // Empty buffer
                return(-1);

        (*val)=sigbuf.buffer[__sync_fetch_and_add(&sigbuf.rdpos, 1)%SIGBUF_SIZE]);
        return(0);
}

I use a pointer instead of returning a struct, because that allows you to tell when the buffer was empty -- 0 means it got a value, -1 means nothing was found.
# 10  
Old 12-23-2011
Ok, this doesn't work either. Because those functions are not supported on the target machine.
# 11  
Old 12-23-2011
That might have been good to know weeks ago.

What is the target machine?
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl Data Structures

Here is what i need to do. @data #has all column wise data so say info for col 1 location for all rows would be in this array $array = \@data But i need to create a file which should contain these information in a format for all columns even if i have got no values from some of the index... (0 Replies)
Discussion started by: dinjo_jo
0 Replies

2. Programming

shared memory - userdefined data structures

Hello, I wonder if I can write my userdefined data structures(ex: a list) to a shared memory segment? I know, the shm functions get (void*) parameter so I should be able to read and write a list into the shared memory. may someone inform and clarify me about that, please? (1 Reply)
Discussion started by: xyzt
1 Replies

3. Programming

Recommendations For Generic C Data Structures & Algorithms

Hi All, Rather than re-invent the wheel, I am trying to find a mature C library that provides generic support for lists, trees, etc. I understand C doesn't formally support "generics", but am aware of a few solutions like GLib and SGLib. Can anyone kindly recommend what they think is best?... (1 Reply)
Discussion started by: tristan12
1 Replies
Login or Register to Ask a Question