File:  [ELWIX - Embedded LightWeight unIX -] / libaitsess / inc / aitsess.h
Revision 1.3.2.5: download - view: text, annotated - select for diffs - revision graph
Fri Feb 10 17:14:21 2012 UTC (12 years, 4 months ago) by misho
Branches: sess3_0
Diff to: branchpoint 1.3: preferred, unified
not completed codez !!!!

    1: /*************************************************************************
    2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: aitsess.h,v 1.3.2.5 2012/02/10 17:14:21 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #ifndef __AITSESS_H
   47: #define __AITSESS_H
   48: 
   49: 
   50: #include <assert.h>
   51: #include <semaphore.h>
   52: #include <sys/types.h>
   53: #include <aitio.h>
   54: 
   55: #define SHARED_UNKNOWN	-1
   56: #define SHARED_IPC	0
   57: #define SHARED_MAP	1
   58: 
   59: #define IS_VAL		0x0
   60: #define IS_ADD		0x40000000
   61: #define IS_DEF		0x80000000
   62: 
   63: #define ISNEW(x)	(((x) & IS_ADD) == IS_ADD)
   64: #define ISDEF(x)	(((x) & IS_DEF) == IS_DEF)
   65: 
   66: #define MAX_ATTRIBUTE	64
   67: #define MAX_SEMNAME	14
   68: 
   69: 
   70: struct tagSess;
   71: typedef struct tagSess ait_sess_t;
   72: 
   73: struct tagSess {
   74: 	key_t	key;
   75: 	char	type;
   76: 	char	zcpy;
   77: 
   78: 	char	name[BUFSIZ];
   79: 	off_t	eom;
   80: 	void	*addr;
   81: 	off_t	offset;
   82: 	union {
   83: 		int	shmid;
   84: 		int	fd;
   85: 	} mem;
   86: 	union {
   87: 		int	semid;
   88: 		sem_t	*sid;
   89: 	} id;
   90: 
   91: 	struct {
   92: 		int (*create)(int, long, ait_sess_t * __restrict, ...);
   93: 		void (*destroy)(ait_sess_t * __restrict);
   94: 		void *(*attach)(ait_sess_t * __restrict, void *);
   95: 		void (*detach)(ait_sess_t * __restrict);
   96: 		void (*notSem)(ait_sess_t * __restrict);
   97: 		int (*isSemOK)(ait_sess_t * __restrict);
   98: 		int (*incSem)(ait_sess_t * __restrict);
   99: 		int (*decSem)(ait_sess_t * __restrict);
  100: 	} sess;
  101: };
  102: 
  103: 
  104: // -------------------------------------------------------
  105: // sess_GetErrno() Get error code of last operation
  106: inline int sess_GetErrno();
  107: // sess_GetError() Get error text of last operation
  108: inline const char *sess_GetError();
  109: // sess_SetErr() Set error to variables for internal use!!!
  110: inline void sess_SetErr(int eno, char *estr, ...);
  111: // -------------------------------------------------------
  112: 
  113: 
  114: /*
  115:  * sess_initSession() Initializing session structure, if session file not exists creating with specified tech
  116:  *
  117:  * @id = Technology using in session. SHARED_IPC IPC tech orSHARED_MAP BSD MemoryMap tech
  118:  * @csFName = Session filename for build key and identified
  119:  * @Sess = Session item, if =NULL allocate memory for session after use must be free!
  120:  * return: 0 OK new key created, -1 error: no memory or file not created, 1 OK key finded
  121:  */
  122: int sess_initSession(int id, const char *csFName, ait_sess_t ** __restrict Sess);
  123: /*
  124:  * sess_freeSession() Free allocated memory for session item and delete session file if present name
  125:  *
  126:  * @csFName = Session filename for delete, if NULL nothing delete
  127:  * @Sess = Session item
  128:  * return: none
  129:  */
  130: void sess_freeSession(const char *csFName, ait_sess_t ** __restrict Sess);
  131: 
  132: 
  133: /*
  134:  * map_createSession() MMAP Created session and allocated resources
  135:  *
  136:  * @nSeed = Seed for securing key, if =-1 must add ready for use key
  137:  * @nSize = Allocated shared memory size in bytes
  138:  * @Sess = Session item
  139:  * @... = If nSeed == -1 add ready for use key value
  140:  * return: 0 Ok successful, -1 error: not allocated resources
  141:  */
  142: int map_createSession(int nSeed, long nSize, ait_sess_t * __restrict Sess, ...);
  143: /*
  144:  * map_destroySession() MMAP free shared resources
  145:  *
  146:  * @Sess = Session item
  147:  * return: none
  148:  */
  149: void map_destroySession(ait_sess_t * __restrict Sess);
  150: 
  151: /*
  152:  * ipc_createSession() IPC Created session and allocated resources
  153:  *
  154:  * @nSeed = Seed for securing key, if =-1 must add ready for use key
  155:  * @nSize = Allocated shared memory size in bytes
  156:  * @Sess = Session item
  157:  * @... = If nSeed == -1 add ready for use key value
  158:  * return: 0 Ok successful, -1 error: not allocated resources
  159:  */
  160: int ipc_createSession(int nSeed, long nSize, ait_sess_t * __restrict Sess, ...);
  161: /*
  162:  * ipc_destroySession() IPC free shared resources
  163:  *
  164:  * @Sess = Session item
  165:  * return: none
  166:  */
  167: void ipc_destroySession(ait_sess_t * __restrict Sess);
  168: 
  169: /*
  170:  * map_attachSession() MMAP Attach to shared memory & return begin address
  171:  *
  172:  * @s = Session item
  173:  * @procMem = Custom start address (optionl) *default must be 0*
  174:  * return: NULL failed attach, !=NULL begin address of memory
  175:  */
  176: void *map_attachSession(ait_sess_t * __restrict s, void *procMem);
  177: /*
  178:  * map_detachSession() MMAP Detach from shared memory
  179:  *
  180:  * @s = Session item
  181:  * return: none
  182:  */
  183: void map_detachSession(ait_sess_t * __restrict s);
  184: 
  185: /*
  186:  * ipc_attachSession() IPC Attach to shared memory & return begin address
  187:  *
  188:  * @s = Session item
  189:  * @procMem = Custom start address (optionl) *default must be 0*
  190:  * return: NULL failed attach, !=NULL begin address of memory
  191:  */
  192: void *ipc_attachSession(ait_sess_t * __restrict s, void *procMem);
  193: /*
  194:  * ipc_detachSession() IPC Detach from shared memory
  195:  *
  196:  * @s = Session item
  197:  * return: none
  198:  */
  199: void ipc_detachSession(ait_sess_t * __restrict s);
  200: 
  201: /*
  202:  * sess_isAttached() Check for attached shared memory
  203:  *
  204:  * @s = Session item
  205:  * return: -1 null session item, 0 not attached, 1 attached memory
  206:  */
  207: inline int sess_isAttached(ait_sess_t * __restrict s);
  208: 
  209: 
  210: /*
  211:  * map_notSemaphore() MMAP negative block if semaphore isn`t signaled
  212:  *
  213:  * @s = Session item
  214:  * return: none
  215:  */
  216: void map_notSemaphore(ait_sess_t * __restrict s);
  217: /*
  218:  * map_isSemaphoreOK() MMAP Check semaphore
  219:  *
  220:  * @s = Session item
  221:  * return: -1 error: can`t return semaphore, 0 = false, 1 = true
  222:  */
  223: int map_isSemaphoreOK(ait_sess_t * __restrict s);
  224: /*
  225:  * map_incSemaphore() MMAP unblock semaphore, increment semaphore
  226:  *
  227:  * @s = Session item
  228:  * return: 0 Ok, -1 error: can`t increment 
  229:  */
  230: int map_incSemaphore(ait_sess_t * __restrict s);
  231: /*
  232:  * map_decSemaphore() MMAP block semaphore, decrement semaphore
  233:  *
  234:  * @s = Session item
  235:  * return: 0 Ok, -1 error: can`t decrement 
  236:  */
  237: int map_decSemaphore(ait_sess_t * __restrict s);
  238: 
  239: /*
  240:  * ipc_notSemaphore() IPC negative block if semaphore isn`t signaled
  241:  *
  242:  * @s = Session item
  243:  * return: none
  244:  */
  245: void ipc_notSemaphore(ait_sess_t * __restrict s);
  246: /*
  247:  * ipc_isSemaphoreOK() IPC Check semaphore
  248:  *
  249:  * @s = Session item
  250:  * return: -1 error: can`t return semaphore, 0 = false, 1 = true
  251:  */
  252: int ipc_isSemaphoreOK(ait_sess_t * __restrict s);
  253: /*
  254:  * ipc_incSemaphore() IPC unblock semaphore, increment semaphore
  255:  *
  256:  * @s = Session item
  257:  * return: 0 Ok, -1 error: can`t increment 
  258:  */
  259: int ipc_incSemaphore(ait_sess_t * __restrict s);
  260: /*
  261:  * ipc_decSemaphore() IPC block semaphore, decrement semaphore
  262:  *
  263:  * @s = Session item
  264:  * return: 0 Ok, -1 error: can`t decrement 
  265:  */
  266: int ipc_decSemaphore(ait_sess_t * __restrict s);
  267: 
  268: 
  269: /* --------------------------------------------------------- */
  270: 
  271: #define ALLOC_MEMORY(sd, siz, s, ...)	(assert((s)), (s)->sess.create((sd), \
  272: 							(siz), (s), ## __VA_ARGS__))
  273: #define ATTACH_MEMORY(s)		(assert((s)), (s)->sess.attach((s), NULL))
  274: #define DETACH_MEMORY(s)		do { assert((s)); (s)->sess.detach((s)); } while(0)
  275: #define FREE_MEMORY(s)			do { assert((s)); (s)->sess.destroy((s)); } while(0)
  276: 
  277: #define IS_SEMOK(s)			(assert((s)), (s)->sess.isSemOK((s)))
  278: #define INC_SEM(s)			(assert((s)), (s)->sess.incSem((s)))
  279: #define DEC_SEM(s)			(assert((s)), (s)->sess.decSem((s)))
  280: #define NOT_SEM(s)			do { assert((s)); (s)->sess.notSem((s)); } while(0)
  281: 
  282: /* --------------------------------------------------------- */
  283: 
  284: /*
  285:  * sess_FreeValues() Free all values from value array allocated from sess_GetValues()
  286:  *
  287:  * @ppsVals = Array strings
  288:  * return: none
  289:  */
  290: inline void sess_FreeValues(char *** __restrict ppsVals);
  291: /*
  292:  * sess_GetValues() Get all values from session shared memory
  293:  *
  294:  * @s = Session item
  295:  * @ppsVals = Return array strings
  296:  * return: -1 error: in parameter, !=-1 count of returned strings in ppsVals (must be free after use!)
  297:  */
  298: int sess_GetValues(ait_sess_t * __restrict s, char ***ppsVals);
  299: /*
  300:  * sess_GetValue() Get value from session shared memory from attribute
  301:  *
  302:  * @s = Session item
  303:  * @csAttr = Attribute for search
  304:  * @psVal = Return string buffer
  305:  * @pnLen = Length of return string buffer, 
  306: 	// *{pnLen} input is max_size of buffer & output is really taken bytes
  307:  * return: 0 not found, -1 error: in parameter, >0 get position, if define item merged with IS_DEF
  308:  */
  309: int sess_GetValue(ait_sess_t * __restrict s, const char *csAttr, char *psVal, int *pnLen);
  310: /*
  311:  * sess_DelValue() Delete item from session shared memory
  312:  *
  313:  * @s = Session item
  314:  * @csAttr = Attribute for erasing
  315:  * return: 0 Ok, -1 error: in parameter
  316:  */
  317: int sess_DelValue(ait_sess_t * __restrict s, const char *csAttr);
  318: /*
  319:  * sess_SetValue() Set item into session shared memory or update if find it
  320:  *
  321:  * @s = Session item
  322:  * @csAttr = Attribute
  323:  * @psVal = Value
  324:  * return: 0 nothing, -1 error: in parameter, 
  325:  	>0 set position, if add item merged with IS_ADD and if define item merged with IS_DEF
  326:  */
  327: int sess_SetValue(ait_sess_t * __restrict s, const char *csAttr, const char *psVal);
  328: 
  329: /*
  330:  * sess_prepareSession() Attach to shared memory and de-marshaling data
  331:  *
  332:  * @s = Session
  333:  * @useDirect = Use direct shared memory if !=0 or snapshot of data to array
  334:  * return: NULL error or no data, !=NULL array with variables, 
  335:  *		after use must free resources with sess_doneSession()
  336:  */
  337: array_t *sess_prepareSession(ait_sess_t * __restrict s, char useDirect);
  338: /*
  339:  * sess_doneSession() Free resources allocated with sess_prepareSession()
  340:  *
  341:  * @s = Session
  342:  * @arr = Array with variables for free
  343:  * return: none
  344:  */
  345: void sess_doneSession(ait_sess_t * __restrict s, array_t ** __restrict arr);
  346: /*
  347:  * sess_commitSession() Commit data to shared memory
  348:  *
  349:  * @s = Session
  350:  * @arr = Array with variables for save
  351:  * return -1 error or !=-1 size of stored variables into shared memory
  352:  */
  353: int sess_commitSession(ait_sess_t * __restrict s, array_t * __restrict arr);
  354: 
  355: 
  356: #endif

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