home *** CD-ROM | disk | FTP | other *** search
- /*____________________________________________________________________________*\
- *
-
- Copyright (c) 1997 John Roy. 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. Redistributions of any form whatsoever and all advertising materials
- mentioning features must contain the following
- acknowledgment:
- "This product includes software developed by John Roy
- (http://www.johnroy.com/pi3/)."
-
- 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: PIPlat.h$
- * $Date: Sun Aug 10 06:29:37 1997$
- *
- Description:
- Definition of platform specific functions.
- \*____________________________________________________________________________*/
- /* $HeaderTop:$ */
-
- #ifndef PIPLAT_H_
- #define PIPLAT_H_
-
- #include "PiAPI.h"
- #include "PISync.h"
- #include "PIThread.h"
-
- /*____________________________________________________________________________*\
- *
- Typedefs:
- \*____________________________________________________________________________*/
- typedef void (* PIPlatformFn)(void);
- #define PIPLATFORM_VERSION_1_0 100
-
- #define PIPLATFORM_POLL_READ 0x1
- #define PIPLATFORM_POLL_WRITE 0x2
- #define PIPLATFORM_POLL_NOTIMEOUT (-1)
- #define PIPLATFORM_POLL_NOWAIT (0)
-
- #define PIPLATFORM_FD int /* a regular file descriptor */
- #define PIPLATFORM_NETFD int /* a network file descriptor */
- #define PIPLATFORM_FD_INVALID ((PIPLATFORM_FD)-1)
-
- #define PIPLATFORM_TIME int /* should be time_t */
-
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIPlatform_enter
-
- Synopsis:
- int PIPlatform_enter( const char *pArgv0, int iVersion, PIPlatformFn fn )
-
- Description:
- Initializes the platform facilities including user context threads
- and custom memory management, then calls the user supplied function
- fn. PIPlatform_enter() cleans platform facilities before returning.
-
- Notes:
- The parameter pArgv0 should be passed argv[0] from the main() entry
- point to the program, as certain implementations of dynamic loading
- require this. However this is not essential to the functioning of
- most platform functionality and NULL may be passed instead.
- The constant PLATFORM_VERSION_1_0 should be used as parameter iVersion
- to this function.
- A typical use of this function is to invoke it immediately on entering
- main() or equivalent function.
- The function fn is run in the main thread with initial priority
- PITHREAD_PRIORIY_MED.
-
- <!--
- */
- #if 0
- --><PRE>
- /* --- globals --- */
- int iArgc;
- char **ppArgv;
- int iRet;
-
- /* --- Main program function --- */
- void foo()
- {
-
- /* ... */
-
- iRet = 0;
- }
-
- /* --- initialize the platform and invoke foo() --- */
- int main( int iArgc, char *ppArgv[] )
- {
- iArgc = iTheArgc;
- ppArgv = ppTheArgv;
- if ( PIPlatform_enter( ppArgv[0], PLATFORM_VERSION_1_0, foo ) )
- {
- printf( "Error initializing platform\n" );
- return 0;
- };
- return iRet;
- }
- </PRE>
- <!--
- #endif
- /*
- -->
-
- Return Values:
- PIPlatform_enter() returns zero (PIAPI_COMPLETED) on success.
-
- Errors:
- On error PIPlatform_enter() returns a negative value.
-
- More error information can be retrieved using PIPlatform_getLastError().
-
- See Also:
- PIPlatform_getDescription().
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIPlatform_enter( const char *pArgv0, int iVersion,
- PIPlatformFn fn );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIPlatform_getDescription
-
- Synopsis:
- const char *PIPlatform_getDescription()
-
- Description:
- Returns a string which describes the range of operating systems
- and machine/processor sets for which this build is compatible.
-
- Notes:
- The description is of the form
-
- Operating system ( machine processor )
-
- Return Values:
- Returns an internally allocated pointer to a string. The caller should
- not modify this string.
-
- Errors:
- PIPlatform_getDescription returns NULL if PIPlatform_Init() has not
- been called.
-
- More error information can be retrieved using PIPlatform_getLastError().
-
- See Also:
- PIPlatform_enter().
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI const char *PIPlatform_getDescription();
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIPlatform_allocLocalMutex
-
- Synopsis:
- PISync *PIPlatform_allocLocalMutex()
-
- Description:
- Returns a new local mutex which can be used to synchronize threads of
- control within a process.
-
- Notes:
- The synchronization object should be destroyed using PISync_delete() when
- it is no longer needed.
-
- Return Values:
- Returns a pointer to a new synchronization object or NULL on error.
-
- Errors:
- NULL may be returned if a new syncronization object could not be
- created due to exhausted memory, kernel mutexes or user mutexes.
-
- More information can be retrieved using PIPlatform_getLastError().
-
- See Also:
- PISync_lock(), PISync_unlock(), PISync_tryLock(), PISync_delete().
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI PISync *PIPlatform_allocLocalMutex();
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIPlatform_allocLocalSemaphore
-
- Synopsis:
- PISync *PIPlatform_allocLocalSemaphore( int iInitalCount, int iMaxCount )
-
- Description:
- Returns a new local semaphore which can be used to synchronize
- threads of control within a process.
-
- Notes:
- The synchronization object should be destroyed using PISync_delete() when
- it is no longer needed.
-
- Return Values:
- Returns a pointer to a new synchronization object or NULL on error.
-
- Errors:
- NULL may be returned if a new syncronization object could not be
- created due to exhausted memory, kernel semaphores or user semaphores.
-
- More specific error information can be retrieved using
- PIPlatform_getLastError().
-
- See Also:
- PISync_lock(), PISync_unlock(), PISync_tryLock(), PISync_delete().
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI PISync *PIPlatform_allocLocalSemaphore( int iInitialCount,
- int iMaxCount );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIPlatform_pollNetFD
-
- Synopsis:
- int PIPlatform_pollNetFD( PIPLATFORM_NETFD fdSocket, int iFlags,
- int iTimeout )
-
- Description:
- Waits for the activity on the specified network file descriptor
- (socket). The value iFlags is formed by OR'ing together any
- combination of the values PIPLATFORM_POLL_READ and PIPLATFORM_POLL_WRITE.
- The value piTimeout is a pointer to an integer containing a time to
- wait in seconds.
-
- Notes:
- A value of PIPLATFORM_NOTIMEOUT for iTimeout specifies no timeout.
- A value of PIPLATFORM_NOWAIT for iTimeout specifies that
- PIPlatform_pollNetFD() should test for activity on fdSocket but return
- immediately even if there is none.
-
- Return Values:
- On success PIPlatform_pollNetFD() returns a value formed by OR'ing
- some of the values PIPLATFORM_POLL_READ and PIPLATFORM_POLL_WRITE together
- indicating which operations may be performed on socket fdSocket
- without blocking.
- If the specified timeout period expires, PIPlatform_pollNetFD() returns
- a value of zero.
-
- Errors:
- On failure PIPlatform_pollNetFD() returns a negative value.
-
- More specific error information can be retreived using
- PIPlatform_getLastError().
-
- See Also:
- PIPlatform_pollFD(), PIPlatform_pollPipeFD(), PIPlatform_sleep().
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIPlatform_pollNetFD( PIPLATFORM_NETFD fdSocket, int iFlags,
- int iTimeout );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIPlatform_pollPipeFD
-
- Synopsis:
- int PIPlatform_pollPipeFD( PIPLATFORM_FD fd, int iFlags,
- int iTimeout )
-
- Description:
- Waits for activity on the specified pipe.
- The value iFlags is formed by OR'ing together any
- combination of the values PIPLATFORM_POLL_READ and PIPLATFORM_POLL_WRITE.
- The value piTimeout is a pointer to an integer containing a time to
- wait in seconds.
-
- Notes:
- A value of PIPLATFORM_NOTIMEOUT for iTimeout specifies no timeout.
- A value of PIPLATFORM_NOWAIT for iTimeout specifies that
- PIPlatform_pollNetFD() should test for activity on fd but return
- immediately even if there is none.
-
- On systems where differences between pipes and file descriptors
- are transparent, PIPlatform_pollPipeFD() is implemented using
- PIPlatform_pollFD(). On other systems PIPlatform_pollPipeFD() may not
- be implemented, see below under 'Errors'.
-
- Return Values:
- On success PIPlatform_pollPipeFD() returns a value formed by OR'ing
- some of the values PIPLATFORM_POLL_READ and PIPLATFORM_POLL_WRITE
- together indicating which operations may be performed on
- pipe fd without blocking.
- If the specified timeout period expires, PIPlatform_pollPipeFD()
- returns a value of zero.
-
- Errors:
- On failure PIPlatform_pollNetFD() returns a negative value.
-
- More specific error information can be retreived using
- PIPlatform_getLastError().
-
- On some systems PIPlatform_pollPipeFD() is not yet implemented, in this
- case a value of PIAPI_ERROR will be returned and
- PIPlatform_getLastError() will return a value of PIAPI_NOTSUPPORTED.
-
- See Also:
- PIPlatform_pollPipeFD(), IPlatform_pollNetFD(), PIPlatform_sleep().
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIPlatform_pollPipeFD( PIPLATFORM_FD fd, int iFlags,
- int iTimeout );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIPlatform_pollFD
-
- Synopsis:
- int PIPlatform_pollFD( PIPLATFORM_FD fd, int iFlags,
- int iTimeout )
-
- Description:
- Waits for the activity on the specified file descriptor.
- The value iFlags is formed by OR'ing together any
- combination of the values PIPLATFORM_POLL_READ and PIPLATFORM_POLL_WRITE.
- The value piTimeout is a pointer to an integer containing a time to
- wait in seconds.
-
- Notes:
- A value of PIPLATFORM_NOTIMEOUT for iTimeout specifies no timeout.
- A value of PIPLATFORM_NOWAIT for iTimeout specifies that
- PIPlatform_pollNetFD() should test for activity on fd but return
- immediately even if there is none.
-
- On systems where differences between sockets and file descriptors
- are transparent, PIPlatform_pollFD() is implemented using
- PIPlatform_pollNetFD(). On other systems PIPlatform_pollFD() may not
- be implemented, see below under 'Errors'.
-
- Return Values:
- On success PIPlatform_pollFD() returns a value formed by OR'ing
- some of the values PIPLATFORM_POLL_READ and PIPLATFORM_POLL_WRITE together
- indicating which operations may be performed on file description fd
- without blocking.
- If the specified timeout period expires, PIPlatform_pollFD() returns
- a value of zero.
-
- Errors:
- On failure PIPlatform_pollNetFD() returns a negative value.
-
- More specific error information can be retreived using
- PIPlatform_getLastError().
-
- On some systems PIPlatform_pollFD() is not yet implemented, in this
- case a value of PIAPI_ERROR will be returned and PIPlatform_getLastError()
- will return a value of PIAPI_NOTSUPPORTED.
-
- See Also:
- PIPlatform_pollNetFD(), PIPlatform_pollPipeFD(), PIPlatform_sleep().
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIPlatform_pollFD( PIPLATFORM_FD fd, int iFlags,
- int iTimeout );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIPlatform_sleep
-
- Synopsis:
- int PIPlatform_sleep( int iMilliseconds )
-
- Description:
- Causes the current thread to sleep for at least iMilliseconds
- thousandths of a second. The current thread may sleep for longer
- than the specified time period. This is particularly true where
- user context threads are in use.
-
- Notes:
- If user context threads are being used and no other threads are
- runnable PIPlatform_sleep() will cause the process to enter a
- 'tight loop'. This may be expensive on CPU utilization with some
- programs.
-
- Return Values:
- On success PIPlatform_sleep() returns a value of 0.
-
- Errors:
- On failure PIPlatform_sleep() returns a negative value.
-
- PIPlatform_getLastError() returns more specific error information.
-
- See Also:
- PIPlatform_pollNetFD(), PIPlatform_pollFD().
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIPlatform_sleep( int iMilliseconds );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIPlatform_getLastError
-
- Synopsis:
- int PIPlatform_getLastError()
-
- Description:
- Returns the last error code for the current thread.
-
- Notes:
- Error codes are only set on error. Except where noted they are
- not cleared on success.
-
- Return Values:
- PIPlatform_getLastError() always returns the last error code.
-
- Errors:
- This function does not generate an error.
-
- See Also:
- PIPlatform_setLastError(), Errors.
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIPlatform_getLastError();
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIPlatform_setLastError
-
- Synopsis:
- void PIPlatform_setLastError( int iErrorCode )
-
- Description:
- Set the error code for this thread. Subsequent calls to
- PIPlatform_getLastError() will return this error code.
-
- Notes:
-
- Return Values:
- PIPlatform_setLastError() does not return a value.
-
- Errors:
- This function does not generate an error.
-
- See Also:
- PIPlatform_getLastError(), Errors
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI void PIPlatform_setLastError( int iErrorCode );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIPlatform_catchExceptions
-
- Synopsis:
- void PIPlatform_catchExceptions( void (*)(void *), void * )
-
- Description:
- Not yet documented.
-
- Notes:
- Return Values:
- Errors:
- See Also:
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI void PIPlatform_catchExceptions(
- void (*)(void *), void * );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIPlatform_beforeUnsafeBlock
-
- Synopsis:
- int PIPlatform_beforeUnsafeBlock()
-
- Description:
- Protects reentrance into non-thread safe blocks by blocking on a
- semaphore.
-
- Notes:
- Calls to PIPlatform_beforeUnsafeBlock() must be matched by calls to
- PIPlatform_afterUnsafeBlock().
-
- Return Values:
- Returns 0 on success.
-
- Errors:
- On error a non-zero value is returned, the function
- PIPlatform_getLastError() can be used to get more specific error
- information.
-
- See Also:
- PIPlatform_afterUnsafeBlock
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIPlatform_beforeUnsafeBlock();
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIPlatform_afterUnsafeBlock
-
- Synopsis:
- int PIPlatform_afterUnsafeBlock()
-
- Description:
- Ends a block protected by reentrance with PIPlatform_beforeUnsafeBlock().
-
- Notes:
- Return Values:
- Returns 0 on success.
-
- Errors:
- On error a non-zero value is returned, the function
- PIPlatform_getLastError() can be used to get more specific error
- information.
-
- See Also:
- PIPlatform_beforeUnsafeBlock
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIPlatform_afterUnsafeBlock();
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIPlatform_getCurrentDirectory
-
- Synopsis:
- int PIPlatform_getCurrentDirectory( char *pBuffer, int iMaxLen )
-
- Description:
- Retrieve the current directory for the process and write it
- into the buffer pBuffer up to iMaxLen characters.
-
- The buffer pBuffer may not be null terminated if the length
- of the current directory is greater than or equal to iMaxLen.
-
- Notes:
- Return Values:
- Returns the length of the current directory. Although no more
- than iMaxLen characters will be written into pBuffer,
- PIPlatform_getCurrentDirectory() may return a length greater
- than iMaxLen.
-
-
- Errors:
- On error PIPlatform_getCurrentDirectory() returns 0.
- PIPlatform_getLastError() can be used to get more specific error
- information.
-
- See Also:
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIPlatform_getCurrentDirectory( char *pBuffer, int iMaxLen );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIPlatform_getProcess
-
- Synopsis:
- int PIPlatform_getProcess()
-
- Description:
- Retrieve the platform specific identifier or handle for the current
- process.
-
- Notes:
- Return Values:
- Idenifier or handle of Current process.
-
- Errors:
- PIPlatform_getProcess() does not generate errors.
-
- See Also:
- PIPlatform_getProcessId().
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIPlatform_getProcess();
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIPlatform_getProcessId
-
- Synopsis:
- int PIPlatform_getProcessId()
-
- Description:
- Retrieve the platform specific identifier for the current
- process.
-
- Notes:
- PIPlatform_getProcessId() differs from PIPlatform_getProcess() in that
- PIPlatform_getProcessId() always returns an integer process index that
- can be used to reference a specific process across all processes.
- PIPlatform_getProcess() returns the native reference to a process and
- may only be valid within the process space of the current process.
-
- Return Values:
- Identifier for the Current process.
-
- Errors:
- PIPlatform_getProcessId() does not generate errors.
-
- See Also:
- PIPlatform_getProcess().
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIPlatform_getProcessId();
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIPlatform_waitForProcess
-
- Synopsis:
- int PIPlatform_waitForProcess( int tProcess, int iFlags, int
- iTimeout )
-
- Description:
- Cause the current thread to sleep until the process specified by the
- operating system specific identifier or handle (tProcess) has
- terminated. iFlags is reserved for future use and should always be 0.
- iTimeout specifies the maximum time to wait, measured in seconds.
-
- Notes:
- The parameter iFlags must be passed the value 0.
-
- Return Values:
- Returns PIAPI_COMPLETED on success, PIAPI_TIMEOUT on timeout,
- otherwise PIAPI_ERROR.
-
- Errors:
- On error PIAPI_ERROR is returned. The function PIPlatform_getLastError()
- can be used to retreive more specific error information.
-
- See Also:
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIPlatform_waitForProcess( int tProcess, int iFlags,
- int iTimeout );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIPlatform_advisaryLockFd
-
- Synopsis:
- PIPlatform_advisaryLockFd( PIPLATFORM_FD tFD )
-
- Description:
- Place an advisary lock on a file descriptor, blocking until the
- the lock can be obtained. The blocking is performed in an
- efficient manner for the process and thread model beng used.
-
- Notes:
- Return Values:
- On success PIPlatform_advisaryLockFd() returns zero (PIAPI_COMPLETED).
-
- Errors:
- On error, the function PIPlatform_getLastError() can be used to get
- more specific error information.
-
- See Also:
- PIPlatform_advisaryUnlockFd()
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIPlatform_advisaryLockFd( PIPLATFORM_FD tFD );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIPlatform_advisaryUnlockFd
-
- Synopsis:
- PIPlatform_advisaryUnlockFd( PIPLATFORM_FD tFD )
-
- Description:
- Release an advisary lock from a file descriptor, previously placed
- with PIPlatform_advisaryLockFd().
-
- Notes:
- Return Values:
- On success PIPlatform_advisaryUnlockFd() returns zero (PIAPI_COMPLETED).
-
- Errors:
- On error, the function PIPlatform_getLastError() can be used to get
- more specific error information.
-
- See Also:
- PIPlatform_advisaryLockFd()
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIPlatform_advisaryUnlockFd( PIPLATFORM_FD tFD );
-
- #endif /* PIPLAT_H_ */
-
-