Sponsored Content
Full Discussion: Signalsafe data structures
Top Forums Programming Signalsafe data structures Post 302580087 by Corona688 on Wednesday 7th of December 2011 12:18:56 PM
Old 12-07-2011
Working on it. It's harder than it sounds to guarantee order AND guarantee safety.

---------- Post updated at 11:18 AM ---------- Previous update was at 11:09 AM ----------

How about this:

Code:
#define SIGBUF_SIZE 1024

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

int put_value(int 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);
}

int get_value(void)
{
        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);

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

basically, rdpos and wrpos get incremented without limit, and % SIGBUF_SIZE is used to wrap their values to inside the array. We do 'unsigned int off=(wrpos-rdpos)' instead of just comparing rdpos and wrpos because, if it hits integer wraparound, rdpos might be near INT_MAX while rdpos is practically zero. Subtracting gets rid of the wraparound.


You shouldn't call get_value in a signal handler, but put_value ought to be safe.
 

3 More Discussions You Might Find Interesting

1. 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

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. 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
RSA_sign_ASN1_OCTET_STRING(3SSL)				      OpenSSL					  RSA_sign_ASN1_OCTET_STRING(3SSL)

NAME
RSA_sign_ASN1_OCTET_STRING, RSA_verify_ASN1_OCTET_STRING - RSA signatures SYNOPSIS
#include <openssl/rsa.h> int RSA_sign_ASN1_OCTET_STRING(int dummy, unsigned char *m, unsigned int m_len, unsigned char *sigret, unsigned int *siglen, RSA *rsa); int RSA_verify_ASN1_OCTET_STRING(int dummy, unsigned char *m, unsigned int m_len, unsigned char *sigbuf, unsigned int siglen, RSA *rsa); DESCRIPTION
RSA_sign_ASN1_OCTET_STRING() signs the octet string m of size m_len using the private key rsa represented in DER using PKCS #1 padding. It stores the signature in sigret and the signature size in siglen. sigret must point to RSA_size(rsa) bytes of memory. dummy is ignored. The random number generator must be seeded prior to calling RSA_sign_ASN1_OCTET_STRING(). RSA_verify_ASN1_OCTET_STRING() verifies that the signature sigbuf of size siglen is the DER representation of a given octet string m of size m_len. dummy is ignored. rsa is the signer's public key. RETURN VALUES
RSA_sign_ASN1_OCTET_STRING() returns 1 on success, 0 otherwise. RSA_verify_ASN1_OCTET_STRING() returns 1 on successful verification, 0 otherwise. The error codes can be obtained by ERR_get_error(3). BUGS
These functions serve no recognizable purpose. SEE ALSO
ERR_get_error(3), objects(3), rand(3), rsa(3), RSA_sign(3), RSA_verify(3) HISTORY
RSA_sign_ASN1_OCTET_STRING() and RSA_verify_ASN1_OCTET_STRING() were added in SSLeay 0.8. 1.0.0e 2002-09-25 RSA_sign_ASN1_OCTET_STRING(3SSL)
All times are GMT -4. The time now is 08:34 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy