File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / inc / aitsess.h
Revision 1.6: download - view: text, annotated - select for diffs - revision graph
Sat Feb 8 22:06:16 2014 UTC (10 years, 4 months ago) by misho
Branches: MAIN
CVS tags: io7_4, io7_3, io7_2, io7_1, io7_0, IO7_3, IO7_2, IO7_1, IO7_0, IO6_9, HEAD
version 6.9

    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.6 2014/02/08 22:06:16 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 - 2014
   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 <pthread.h>
   51: #include <semaphore.h>
   52: #include <elwix.h>
   53: #include <aitio.h>
   54: 
   55: #define SHARED_UNKNOWN	-1
   56: #define SHARED_IPC	0
   57: #define SHARED_MAP	1
   58: 
   59: #define SESS_OPT_SEED	-1
   60: 
   61: #define SESS_F_ADD	0x10000000
   62: #define SESS_F_DEF	0x20000000
   63: 
   64: #define SESS_RET_NEW(x)	(((x) & SESS_F_ADD) == SESS_F_ADD)
   65: #define SESS_RET_DEF(x)	(((x) & SESS_F_DEF) == SESS_F_DEF)
   66: #define SESS_RET_VAL(x)	((x) & 0x0fffffff)
   67: 
   68: /* Shared memory session */
   69: 
   70: typedef struct tagSess {
   71: 	key_t	sess_key;
   72: 	char	sess_type;
   73: 	char	sess_zcpy;
   74: 
   75: 	char	sess_name[STRSIZ];
   76: 	off_t	sess_size;
   77: 	void	*sess_addr;
   78: 	off_t	sess_offset;
   79: 
   80: 	union {
   81: 		int	shmid;
   82: 		int	fd;
   83: 	} sess_mem;
   84: 	union {
   85: 		int	semid;
   86: 		sem_t	*sid;
   87: 	} sess_id;
   88: 
   89: 	/* Session callbacks */
   90: 	struct {
   91: 		int (*create)(int, long, void *, ...);
   92: 		void (*destroy)(void *);
   93: 		void *(*attach)(void *, void *);
   94: 		void (*detach)(void *);
   95: 		void (*notSem)(void *);
   96: 		int (*isSemOK)(void *);
   97: 		int (*incSem)(void *);
   98: 		int (*decSem)(void *);
   99: 	} sess;
  100: } sess_t;
  101: 
  102: /* --------------------------------------------------------- */
  103: 
  104: #define ALLOC_MEMORY(sd, siz, s, ...)	(assert((s) && (s)->sess.create), \
  105: 						(s)->sess.create((sd), (siz), (s), ## __VA_ARGS__))
  106: #define ATTACH_MEMORY(s)		(assert((s) && (s)->sess.attach), (s)->sess.attach((s), NULL))
  107: #define DETACH_MEMORY(s)		(assert((s) && (s)->sess.detach), (s)->sess.detach((s)))
  108: #define FREE_MEMORY(s)			(assert((s) && (s)->sess.destroy), (s)->sess.destroy((s)))
  109: 
  110: #define IS_SEMOK(s)			(assert((s) && (s)->sess.isSemOK), (s)->sess.isSemOK((s)))
  111: #define INC_SEM(s)			(assert((s) && (s)->sess.incSem), (s)->sess.incSem((s)))
  112: #define DEC_SEM(s)			(assert((s) && (s)->sess.decSem), (s)->sess.decSem((s)))
  113: #define NOT_SEM(s)			(assert((s) && (s)->sess.notSem), (s)->sess.notSem((s)))
  114: 
  115: /* --------------------------------------------------------- */
  116: 
  117: 
  118: /*
  119:  * sess_initSession() - Initializing session structure, 
  120:  * 			if session file not exists creating with specified tech
  121:  *
  122:  * @id = Technology using in session. SHARED_IPC IPC tech or SHARED_MAP BSD MemoryMap tech
  123:  * @csFName = Session filename for build key and identified
  124:  * @Sess = Session item, if =NULL allocate memory for session after use must be free!
  125:  * return: 0 OK new key created, -1 error: no memory or file not created, 1 OK key finded
  126:  */
  127: int sess_initSession(int id, const char *csFName, sess_t ** __restrict Sess);
  128: /*
  129:  * sess_freeSession() - Free allocated memory for session item and delete session file if present name
  130:  *
  131:  * @Sess = Session item
  132:  * return: none
  133:  */
  134: void sess_freeSession(sess_t ** __restrict Sess);
  135: 
  136: /*
  137:  * map_createSession() - MMAP Created session and allocated resources
  138:  *
  139:  * @nSeed = Seed for securing key, if =SESS_OPT_SEED must add ready for use key
  140:  * @nSize = Allocated shared memory size in bytes
  141:  * @Sess = Session item
  142:  * @... = If nSeed == -1 add ready for use key value
  143:  * return: 0 Ok successful, -1 error: not allocated resources
  144:  */
  145: int map_createSession(int nSeed, long nSize, sess_t * __restrict Sess, ...);
  146: /*
  147:  * map_destroySession() - MMAP free shared resources
  148:  *
  149:  * @Sess = Session item
  150:  * return: none
  151:  */
  152: void map_destroySession(sess_t * __restrict Sess);
  153: 
  154: /*
  155:  * ipc_createSession() - IPC Created session and allocated resources
  156:  *
  157:  * @nSeed = Seed for securing key, if =SESS_OPT_SEED must add ready for use key
  158:  * @nSize = Allocated shared memory size in bytes
  159:  * @Sess = Session item
  160:  * @... = If nSeed == -1 add ready for use key value
  161:  * return: 0 Ok successful, -1 error: not allocated resources
  162:  */
  163: int ipc_createSession(int nSeed, long nSize, sess_t * __restrict Sess, ...);
  164: /*
  165:  * ipc_destroySession() - IPC free shared resources
  166:  *
  167:  * @Sess = Session item
  168:  * return: none
  169:  */
  170: void ipc_destroySession(sess_t * __restrict Sess);
  171: 
  172: /*
  173:  * map_attachSession() - MMAP Attach to shared memory & return begin address
  174:  *
  175:  * @s = Session item
  176:  * @procMem = Custom start address (optionl) *default must be 0*
  177:  * return: NULL failed attach, !=NULL begin address of memory
  178:  */
  179: void *map_attachSession(sess_t * __restrict s, void *procMem);
  180: /*
  181:  * map_detachSession() - MMAP Detach from shared memory
  182:  *
  183:  * @s = Session item
  184:  * return: none
  185:  */
  186: void map_detachSession(sess_t * __restrict s);
  187: 
  188: /*
  189:  * ipc_attachSession() - IPC Attach to shared memory & return begin address
  190:  *
  191:  * @s = Session item
  192:  * @procMem = Custom start address (optionl) *default must be 0*
  193:  * return: NULL failed attach, !=NULL begin address of memory
  194:  */
  195: void *ipc_attachSession(sess_t * __restrict s, void *procMem);
  196: /*
  197:  * ipc_detachSession() - IPC Detach from shared memory
  198:  *
  199:  * @s = Session item
  200:  * return: none
  201:  */
  202: void ipc_detachSession(sess_t * __restrict s);
  203: 
  204: /*
  205:  * sess_isAttached() - Check for attached shared memory
  206:  *
  207:  * @s = Session item
  208:  * return: -1 null session item, 0 not attached, 1 attached memory
  209:  */
  210: int sess_isAttached(sess_t * __restrict s);
  211: 
  212: 
  213: /*
  214:  * map_notSemaphore() - MMAP negative block if semaphore isn`t signaled
  215:  *
  216:  * @s = Session item
  217:  * return: none
  218:  */
  219: void map_notSemaphore(sess_t * __restrict s);
  220: /*
  221:  * map_isSemaphoreOK() - MMAP Check semaphore
  222:  *
  223:  * @s = Session item
  224:  * return: -1 error: can`t return semaphore, 0 = false, 1 = true
  225:  */
  226: int map_isSemaphoreOK(sess_t * __restrict s);
  227: /*
  228:  * map_incSemaphore() - MMAP unblock semaphore, increment semaphore
  229:  *
  230:  * @s = Session item
  231:  * return: 0 Ok, -1 error: can`t increment 
  232:  */
  233: int map_incSemaphore(sess_t * __restrict s);
  234: /*
  235:  * map_decSemaphore() - MMAP block semaphore, decrement semaphore
  236:  *
  237:  * @s = Session item
  238:  * return: 0 Ok, -1 error: can`t decrement 
  239:  */
  240: int map_decSemaphore(sess_t * __restrict s);
  241: 
  242: /*
  243:  * ipc_notSemaphore() - IPC negative block if semaphore isn`t signaled
  244:  *
  245:  * @s = Session item
  246:  * return: none
  247:  */
  248: void ipc_notSemaphore(sess_t * __restrict s);
  249: /*
  250:  * ipc_isSemaphoreOK() - IPC Check semaphore
  251:  *
  252:  * @s = Session item
  253:  * return: -1 error: can`t return semaphore, 0 = false, 1 = true
  254:  */
  255: int ipc_isSemaphoreOK(sess_t * __restrict s);
  256: /*
  257:  * ipc_incSemaphore() - IPC unblock semaphore, increment semaphore
  258:  *
  259:  * @s = Session item
  260:  * return: 0 Ok, -1 error: can`t increment 
  261:  */
  262: int ipc_incSemaphore(sess_t * __restrict s);
  263: /*
  264:  * ipc_decSemaphore() - IPC block semaphore, decrement semaphore
  265:  *
  266:  * @s = Session item
  267:  * return: 0 Ok, -1 error: can`t decrement 
  268:  */
  269: int ipc_decSemaphore(sess_t * __restrict s);
  270: 
  271: 
  272: /*
  273:  * sess_GetValues() - Get all values from session shared memory
  274:  *
  275:  * @s = Session item
  276:  * @Vals = Return array strings
  277:  * return: -1 error: in parameter, !=-1 count of returned strings in Vals 
  278:  * 		(must call ait_freeVars() after use!)
  279:  */
  280: int sess_GetValues(sess_t * __restrict s, array_t ** __restrict Vals);
  281: /*
  282:  * sess_GetValue() - Get value from session shared memory from attribute
  283:  *
  284:  * @s = Session item
  285:  * @csAttr = Attribute for search
  286:  * @v = Return string value and after use must call ait_freeVar(), also may be =NULL
  287:  * return: 0 not found, -1 error: in parameter, >0 get position, 
  288:  * 	if define item merged with IS_DEF
  289:  */
  290: int sess_GetValue(sess_t * __restrict s, const char *csAttr, ait_val_t ** __restrict v);
  291: /*
  292:  * sess_DelValue() - Delete item from session shared memory
  293:  *
  294:  * @s = Session item
  295:  * @csAttr = Attribute for erasing
  296:  * return: -1 error: in parameter or !=-1 deleted items
  297:  */
  298: int sess_DelValue(sess_t * __restrict s, const char *csAttr);
  299: /*
  300:  * sess_SetValue() - Set item into session shared memory or update if exists
  301:  *
  302:  * @s = Session item
  303:  * @csAttr = Attribute
  304:  * @psVal = Value
  305:  * return: 0 nothing, -1 error: in parameter, 
  306:  	>0 set position, if added new item merged with SESS_F_ADD and 
  307: 	if just define item merged with SESS_F_DEF
  308:  */
  309: int sess_SetValue(sess_t * __restrict s, const char *csAttr, const char *psVal);
  310: 
  311: /*
  312:  * sess_prepareSession() - Attach to shared memory and de-marshaling data
  313:  *
  314:  * @s = Session
  315:  * @useDirect = Use direct shared memory if !=0 or snapshot of data to array
  316:  * return: NULL error or no data, !=NULL array with variables, 
  317:  *		after use must free resources with sess_doneSession()
  318:  */
  319: array_t *sess_prepareSession(sess_t * __restrict s, char useDirect);
  320: /*
  321:  * sess_doneSession() - Free resources allocated with sess_prepareSession()
  322:  *
  323:  * @s = Session
  324:  * @arr = Array with variables for free
  325:  * return: none
  326:  */
  327: void sess_doneSession(sess_t * __restrict s, array_t ** __restrict arr);
  328: /*
  329:  * sess_commitSession() - Commit data to shared memory
  330:  *
  331:  * @s = Session
  332:  * @arr = Array with variables for save
  333:  * return -1 error or !=-1 size of stored variables into shared memory
  334:  */
  335: int sess_commitSession(sess_t * __restrict s, array_t * __restrict arr);
  336: 
  337: 
  338: #endif

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