Annotation of embedaddon/libpdel/util/tinfo.h, revision 1.1.1.1

1.1       misho       1: 
                      2: /*
                      3:  * Copyright (c) 2001-2002 Packet Design, LLC.
                      4:  * All rights reserved.
                      5:  * 
                      6:  * Subject to the following obligations and disclaimer of warranty,
                      7:  * use and redistribution of this software, in source or object code
                      8:  * forms, with or without modifications are expressly permitted by
                      9:  * Packet Design; provided, however, that:
                     10:  * 
                     11:  *    (i)  Any and all reproductions of the source or object code
                     12:  *         must include the copyright notice above and the following
                     13:  *         disclaimer of warranties; and
                     14:  *    (ii) No rights are granted, in any manner or form, to use
                     15:  *         Packet Design trademarks, including the mark "PACKET DESIGN"
                     16:  *         on advertising, endorsements, or otherwise except as such
                     17:  *         appears in the above copyright notice or in the software.
                     18:  * 
                     19:  * THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND
                     20:  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO
                     21:  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING
                     22:  * THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED
                     23:  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
                     24:  * OR NON-INFRINGEMENT.  PACKET DESIGN DOES NOT WARRANT, GUARANTEE,
                     25:  * OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS
                     26:  * OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY,
                     27:  * RELIABILITY OR OTHERWISE.  IN NO EVENT SHALL PACKET DESIGN BE
                     28:  * LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE
                     29:  * OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT,
                     30:  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL
                     31:  * DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF
                     32:  * USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF
                     33:  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     34:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
                     35:  * THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF
                     36:  * THE POSSIBILITY OF SUCH DAMAGE.
                     37:  *
                     38:  * Author: Archie Cobbs <archie@freebsd.org>
                     39:  */
                     40: 
                     41: #ifndef _PDEL_UTIL_TINFO_H_
                     42: #define _PDEL_UTIL_TINFO_H_
                     43: 
                     44: /*
                     45:  * Support for generic per-thread data described by a structs type.
                     46:  */
                     47: 
                     48: struct tinfo;
                     49: 
                     50: /*
                     51:  * Optional user-supplied initialization routine. The routine should
                     52:  * initialize the memory pointed to by "data" as an instance of the
                     53:  * structs type for this tinfo data (i.e., t->type). If this method
                     54:  * is not supplied (i.e., NULL), then structs_init() is invoked instead.
                     55:  */
                     56: typedef int    tinfo_init_t(struct tinfo *t, void *data);
                     57: 
                     58: /* User-supplied data structure for a tinfo variable */
                     59: struct tinfo {
                     60:        const struct structs_type       *const type;    /* type for data */
                     61:        const char                      *const mtype;   /* typed mem string */
                     62:        pthread_key_t                   pkey;           /* per-thread var */
                     63:        tinfo_init_t                    *init;          /* initialize data */
                     64: };
                     65: 
                     66: /* Invalid key value */
                     67: #define TINFO_KEY_INIT         (-1)                    /* XXX not portable */
                     68: 
                     69: /* Always use this macro to initialize a struct tinfo */
                     70: #define TINFO_INIT(type, mtype, init)  { type, mtype, TINFO_KEY_INIT, init }
                     71: 
                     72: __BEGIN_DECLS
                     73: 
                     74: /*
                     75:  * Get thread-local variable.
                     76:  *
                     77:  * If the variable has not been initialized, a new one is instantiated
                     78:  * using the t->type's init method, and t->init is called (if not NULL).
                     79:  *
                     80:  * The caller should NOT free the returned value, but may modify it
                     81:  * in a way consistent with its structs type (i.e., t->type).
                     82:  */
                     83: extern void    *tinfo_get(struct tinfo *t);
                     84: 
                     85: /*
                     86:  * Set thread-local variable by copying "data".
                     87:  *
                     88:  * The "data" pointer must point to an object having structs type t->type.
                     89:  * The data is copied using structs_get(). Any previous thread-local
                     90:  * value is free'd and replaced.
                     91:  *
                     92:  * If "data" is NULL, then any existing information is free'd, so that
                     93:  * the next call to tinfo_get() will cause the init routine to be called
                     94:  * again.
                     95:  */
                     96: extern int     tinfo_set(struct tinfo *t, const void *data);
                     97: 
                     98: /*
                     99:  * Set thread-local variable to "data".
                    100:  *
                    101:  * This sets the thread-local variable to "data" without copying it.
                    102:  * "data" must point to memory allocated with type t->mtype.
                    103:  * Any previous thread-local value is free'd and replaced.
                    104:  *
                    105:  * The caller should not reference "data" if this function returns
                    106:  * successfully.
                    107:  */
                    108: extern int     tinfo_set_nocopy(struct tinfo *t, void *data);
                    109: 
                    110: __END_DECLS
                    111: 
                    112: #endif /* _PDEL_UTIL_TINFO_H_ */
                    113: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>