📄 syncent.cpp
字号:
/*____________________________________________________________________________*\
*
Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.
These sources, libraries and applications are
FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
as long as the following conditions are adhered to.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*____________________________________________________________________________*|
*
* $Source: /cvsroot/pi3web/Pi3Web_200/Source/Platform/SyncEnt.cpp,v $
* $Date: 2003/05/13 18:42:14 $
*
Description:
Implementation of system semaphores and mutexes.
\*____________________________________________________________________________*/
//$SourceTop:$
#include "OSConfig.h" /* --- get build options --- */
/* ---
Include this file only for builds which implement system semaphores
--- */
#if defined(CONFIG_SEMAPHORES)
#include <errno.h>
#if defined(CONFIG_SEMA_SEMIPC)
# include <sys/stat.h>
# include <sys/ipc.h>
# include <sys/sem.h>
#elif defined(CONFIG_SEMA_SYNC)
# include <thread.h>
# include <synch.h>
#else
# error Cannot support System semaphores
#endif
#include "Platform.h"
/*____________________________________________________________________________*\
*
Class:
Description:
\*____________________________________________________________________________*/
class UnixGlobalSemaphore : public Semaphore
{
private:
#if defined(CONFIG_SEMA_SYNCH)
sema_t tSemaphore;
#elif defined(CONFIG_SEMA_SEMIPC)
int iSemId;
unsigned long tAllocatingPID;
#else
# error Cannot support system semaphores
#endif
bool bError;
public:
UnixGlobalSemaphore( int iInitialCount, int iMaxCount );
virtual ~UnixGlobalSemaphore();
virtual int Lock();
virtual int TryLock();
virtual int UnLock();
virtual bool IsOK() const;
};
#if defined(CONFIG_NO_SEMUN)
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
This is defined here because some unix operating systems, notably
solaris, don't define it.
\*____________________________________________________________________________*/
union semun
{
int val;
struct semid_ds *buf;
ushort *array;
struct seminfo *__buf;
};
#endif /* CONFIG_NO_SEMUN */
/*____________________________________________________________________________*\
*
Function:
Synopsis: public constructor
Description:
\*____________________________________________________________________________*/
UnixGlobalSemaphore::UnixGlobalSemaphore( int iInitialCount, int iMaxCount )
: bError( true )
{
#if defined(CONFIG_SEMA_SYNCH)
bError=(::sema_init(
&tSemaphore,
iMaxCount,
USYNC_PROCESS,
0 )!=0);
PIMUSTBETRUE( !bError );
int iSemaphoresToLock=iMaxCount-iInitialCount;
assert( iSemaphoresToLock >= 0 );
while( iSemaphoresToLock-- )
{ PIMUSTBETRUE(!sema_trywait( &tSemaphore )); };
#elif defined(CONFIG_SEMA_SEMIPC)
for(;;)
{
iSemId = semget( IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR | S_IWUSR );
if ( iSemId==-1 && errno==EINTR )
{ continue; };
break;
};
bError = (iSemId == -1);
if ( bError ) return;
union semun tArg;
tArg.val = iInitialCount;
/* The only limit on the value of a semaphore
is the systems types limits so iMaxCount is
not used */
(void)iMaxCount; /* stop the compiler from whinning anyway */
int iRet;
for(;;)
{
iRet = semctl( iSemId, 0, SETVAL, tArg );
if ( iRet== -1 && errno==EINTR )
{ continue; };
break;
};
bError = (iRet == -1);
tAllocatingPID = Platform::GetPID();
#else
# error Cannot support semaphores
#endif
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
UnixGlobalSemaphore::~UnixGlobalSemaphore()
{
#if defined(CONFIG_SEMA_SYNCH)
PIMUSTBETRUE( !sema_destroy( &tSemaphore ) );
#elif defined(CONFIG_SEMA_SEMIPC)
if ( iSemId!=-1 && tAllocatingPID==Platform::GetPID() )
{
for(;;)
{
union semun tArg;
tArg.val = 1; /* this isn't really necessary */
int iRet = semctl( iSemId, 0, IPC_RMID, tArg );
if ( iRet==-1 && errno==EINTR ) continue;
break;
};
};
#else
# error Cannot support semaphores
#endif
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
int UnixGlobalSemaphore::Lock()
{
assert( !bError );
if ( bError )
{ return -1; };
int iRet;
#if defined(CONFIG_SEMA_SYNCH)
iRet = sema_wait( &tSemaphore );
#elif defined(CONFIG_SEMA_SEMIPC)
struct sembuf tSemBuf;
tSemBuf.sem_num = 0;
tSemBuf.sem_op = -1;
tSemBuf.sem_flg = SEM_UNDO;
for(;;)
{
iRet = semop( iSemId, &tSemBuf, 1 );
if ( iRet==-1 && errno==EINTR ) { continue; };
break;
};
#else
# error Cannot support semaphores
#endif
if ( iRet!=0 )
{ bError=true; };
return iRet;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Be careful in this function that the value PISYNC_EBUSY is not
in adverently returned.
\*____________________________________________________________________________*/
int UnixGlobalSemaphore::TryLock()
{
assert( !bError );
if ( bError )
{ return -1; };
int iRet;
#if defined(CONFIG_SEMA_SYNCH)
iRet = sema_trywait( &tSemaphore );
if ( iRet==EBUSY )
{
/* --- could not get semaphore, no error in this case --- */
return PISYNC_EBUSY;
};
#elif defined(CONFIG_SEMA_SEMIPC)
struct sembuf tSemBuf;
tSemBuf.sem_num = 0;
tSemBuf.sem_op = -1;
tSemBuf.sem_flg = SEM_UNDO | IPC_NOWAIT;
for(;;)
{
iRet = semop( iSemId, &tSemBuf, 1 );
if ( iRet==-1 && errno==EINTR )
{ continue; };
if ( iRet==-1 && errno == EAGAIN )
{ return PISYNC_EBUSY; };
break;
};
#else
# error Cannot support semaphores
#endif
if ( iRet!=0 )
{ bError = true; iRet = -1; };
return iRet;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
int UnixGlobalSemaphore::UnLock()
{
assert( !bError );
if ( bError )
{ return -1; };
int iRet;
#if defined(CONFIG_SEMA_SYNCH)
iRet = sema_post( &tSemaphore );
#elif defined(CONFIG_SEMA_SEMIPC)
assert( !bError );
struct sembuf tSemBuf;
tSemBuf.sem_num = 0;
tSemBuf.sem_op = 1;
tSemBuf.sem_flg = SEM_UNDO;
for(;;)
{
iRet = semop( iSemId, &tSemBuf, 1 );
if ( iRet==-1 && errno==EINTR ) { continue; };
break;
};
#else
# error Cannot support semaphores
#endif
if ( iRet!=0 )
{ bError=true; };
return iRet;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
bool UnixGlobalSemaphore::IsOK() const
{
return !bError;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis: static, public:
Description:
\*____________________________________________________________________________*/
Semaphore *Platform::AllocGlobalMutex()
{
return PI_NEW( UnixGlobalSemaphore( 1, 1 ) );
}
/*____________________________________________________________________________*\
*
Function:
Synopsis: static, public:
Description:
\*____________________________________________________________________________*/
Semaphore *Platform::AllocGlobalSemaphore( int iInitialCount, int iMaxCount )
{
return PI_NEW( UnixGlobalSemaphore( iInitialCount, iMaxCount ) );
}
#endif /* defined(CONFIG_SEMAPHORES) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -