home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1998 May
/
Pcwk5b98.iso
/
WEBSERVE
/
PI3
/
PI3WEB.EXE
/
DEVEL
/
INCLUDE
/
PIThread.h
< prev
next >
Wrap
C/C++ Source or Header
|
1997-10-19
|
21KB
|
701 lines
/*____________________________________________________________________________*\
*
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: PIThread.h$
* $Date: Sun Aug 10 06:29:38 1997$
*
Description:
\*____________________________________________________________________________*/
/* $HeaderTop:$ */
#ifndef PITHREAD_H_
#define PITHREAD_H_
#include "PiAPI.h"
/*____________________________________________________________________________*\
*
Typedefs:
\*____________________________________________________________________________*/
typedef struct Thread PIThread;
typedef struct Allocator PIMemoryPool;
typedef unsigned long (* PIThreadFn)( unsigned long);
/*____________________________________________________________________________*\
*
Name:
PIThread_new
Synopsis:
PIThread *PIThread_new( PIMemoryPool *pPool, int iStackSize )
Description:
Creates and initializes a new thread object. The function PIThread_begin()
can be used to run the thread.
The parameters pPool and iStackSize are for future use but
the values NULL and 0 respectively should be used to ensure
future compatibility.
Notes:
If successful a new thread object will be created but will not
be executed.
The pointer to the thread object remains valid until PIThread_delete() is
called.
Return Values:
On success PIThread_new() returns a pointer to a new
thread object.
Errors:
On error PIThread_new() returns NULL.
PIPlatform_getLastError() can be used to obtain more detailed
error information.
See Also:
PIThread_delete().
\*____________________________________________________________________________*/
PUBLIC_PIAPI PIThread *PIThread_new( PIMemoryPool *pPool, int iStackSize );
/*____________________________________________________________________________*\
*
Name:
PIThread_begin
Synopsis:
int PIThread_begin( PIThread *pThread, PIThreadFn fnEntry,
unsigned long ulData, int iPriority, int iFlags )
Description:
Schedules the thread object pThread to be executed. The entry point
function fnEntry will invoked with the argument ulData.
The value iPriority can be one of the thread priority values
described in PIThread_setPriority() and indicates the initial
priority at which the thread runs.
The value of iFlags is formed by OR'ing together zero or more of
the following values:-
PITHREAD_FLAGS_SUSPENDED The thread not run until PIThread_resume()
Notes:
The thread object pThread can be a newly created object from
PIThread_new() or a previously run thread which has terminated.
Return Values:
On success PIThread_begin() returns zero (PIAPI_COMPLETED).
Errors:
PIAPI_ERROR if an error occurred.
PIPlatform_getLastError() can be used to obtain more detailed error
information.
See Also:
PIThread_terminate(), PIThread_waitForThread().
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIThread_begin( PIThread *pThread, PIThreadFn fnEntry,
unsigned long ulData, int iPriority, int iFlags );
/*____________________________________________________________________________*\
*
Name:
PIThread_terminate
Synopsis:
int PIThread_terminate( PIThread *pThread, unsigned long ulExitCode )
Description:
Schedules the specified thread for termination. The thread will exit
with the status code ulExitCode.
Notes:
** IMPORTANT **
Use of this function is not recommended as the target thread will
usually have no opportunity to cleanup critical activity or
synchronized actions it may be performing. This can cause the state
of global objects to become unstable. This is true for user context
threads, and particularly true for some operating system threads
implementations.
PIThread_terminate is not guaranteed to wait for the thread to
terminate. Use the function PIThread_waitForThread() after
PIThread_terminate() to poll for the termination of
another thread.
If pThread is the main thread then the following actions will also
occur in order:-
<UL>
<ITEM>PIProgram_enter() will return PIAPI_COMPLETED, if used.
<ITEM>All other threads will be terminated, and thread objects
destroyed with PIThread_delete().
<ITEM>PIPlatform_enter() will return PIAPI_COMPLETED.
</UL>
Return Values:
On success PIThread_terminate returns zero (PIAPI_COMPLETED).
Errors:
PIAPI_ERROR an error occurred.
PIPlatform_getLastError() can be used to get more specific
error information.
See Also:
PIThread_begin(), PIThread_waitForThread().
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIThread_terminate( PIThread *pThread,
unsigned long ulExitCode );
/*____________________________________________________________________________*\
*
Name:
PIThread_delete
Synopsis:
int PIThread_delete( PIThread *pThread )
Description:
Destroys a thread object, and invalidates the pointer pThread.
Notes:
If pThread has not terminated, PIThread_delete() will kill the thread
and poll for its termination using PIThread_terminate() and
PIThread_waitForThread().
PIThread_delete completely invalidates the pointer pThread, the
results are undefined if the pointer pThread is used in a thread function
again. A defensive use of this function would set pThread to NULL after
PIThread_delete() has been successfully completed.
Return Values:
On success PIThread_delete() returns zero (PIAPI_COMPLETED).
Errors:
PIAPI_ERROR if an error occurred.
PIPlatform_getLastError() can be used to get more specific error
information.
See Also:
PIThread_new().
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIThread_delete( PIThread *pThread );
/*____________________________________________________________________________*\
*
Name:
PIThread_suspend
Synopsis:
int PIThread_suspend( PIThread *pThread )
Description:
Suspends execution of the thread pThread.
Notes:
Return Values:
PIThread_suspend() return zero (PIAPI_COMPLETED) if the thread was
succesfully suspended or if the thread had previously been suspended.
Errors:
PIAPI_ERROR if an error occurred.
PIPlatform_getLastError() can be used to get more specific error
information.
See Also:
PIThread_resume().
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIThread_suspend( PIThread *pThread );
/*____________________________________________________________________________*\
*
Name:
PIThread_resume
Synopsis:
int PIThread_resume( pThread )
Description:
Resumes execution of a thread.
Notes:
Return Values:
PIThread_resume() returns zero (PIAPI_COMPLETED) if the thread was
succesfully resumed or if the thread was not suspended.
Errors:
PIAPI_ERROR if an error occurred.
PIPlatform_getLastError() can be used to get more specific error
information.
See Also:
PIThread_suspend().
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIThread_resume( PIThread *pThread );
/*____________________________________________________________________________*\
*
Name:
PIThread_getSystemHandle
Synopsis:
void *PIThread_getSystemHandle( PIThread *pThread )
Description:
Returns the implementation specific handle or pointer of the the
thread.
This value can be used to reference the thread in additional thread
library functions supplied by the operating system.
Notes:
The value returned by PIThread_getSystemHandle() must be cast to the
appriopriate type e.g. 'thread_t', 'HANDLE'.
Return Values:
On success PIThread_getSystemHandle() returns the specific
operating system handle of the thread.
Errors:
The function PIPlatform_getLastError() must be used to determine
if a handle value of NULL is an error or the valid system
dependant handle of the current thread.
PIPlatform_getLastError() can be used to get more specific error
information.
See Also:
\*____________________________________________________________________________*/
PUBLIC_PIAPI void *PIThread_getSystemHandle( PIThread *pThread );
/*____________________________________________________________________________*\
*
Name:
PIThread_yield
Synopsis:
int PIThread_yield()
Description:
Yield processing to another thread.
Notes:
PIThread_waitForThread() does not attempt to terminate the thread
referenced by pThread, the function PIThread_terminate() can be used
for that.
When multithreading is implemented using the kernel threads
implementation of the current operating system with function will
invoke the appropriate theads system call to cause the current
thread to yield.
PIThread_yield() does not guarantee that the current thread will
yield. This is especially true when no other threads of equal or
greater priority are runnable.
Return Values:
On success PIThread_yield() returns zero (PIAPI_COMPLETED).
Errors:
PIAPI_ERROR if an error occurred.
PIPlatform_getLastError() can be used to get more specific error
information.
See Also:
PIThread_userYield().
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIThread_yield();
/*____________________________________________________________________________*\
*
Name:
PIThread_userYield
Synopsis:
int PIThread_userYield()
Description:
Yield processing to another user thread. This function has no
effect when multi-threading is implemented with operating system
specific kernel threads.
When multithreading is implemented with user context threads this
function will behave like PIThread_Yield().
Notes:
Unlike kernel supplied threads (operating system specific)
user context threads will not context switch until a synchronization
function, such as PISync_lock(), PIPlatform_pollNetFD() is invoked. In
code which does not use these synchronization functions (such as
complex mathematical algorithms) one thread would monopolize all the
processing in the application. PIThread_userYield() can be inserted into
such code as a portable way to force preemptive thread behaviour
in a non-preemptive threads environment without added unnecessary
PIThread_Yield() calls to kernel multithreading environments.
Return Values:
On success PIThread_userYield() returns zero (PIAPI_COMPLETED).
Errors:
PIAPI_ERROR if an error occurred.
PIPlatform_getLastError() can be used to get more specific error
information.
See Also:
PIThread_yield().
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIThread_userYield();
/*____________________________________________________________________________*\
*
Name:
PIThread_waitForThread
Synopsis:
int PIThread_waitForThread( PIThread *pThread );
Description:
Suspends execution of the current thread until the thread referenced
by pThread has terminated.
Notes:
PIThread_waitForThread() does not attempt to terminate the thread
referenced by pThread, the function PIThread_terminate() can be used
for that.
Return Values:
PIThread_waitForThread() returns zero (PIAPI_COMPLETED) if the
thread referenced by pThread has terminated.
Errors:
PIAPI_ERROR if an error occurred.
PIPlatform_getLastError() can be used to get more specific error
information.
See Also:
PIThread_terminate().
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIThread_waitForThread( PIThread *pThread );
/*____________________________________________________________________________*\
*
Name:
PIThread_initThreadKey
Synopsis:
int PIThread_initThreadKey()
Description:
Returns a new key (index) that can be used in PIThread_setData() and
PIThread_getData() to save and retrieve thread local data.
Notes:
Return Values:
On success PIThread_initThreadKey() returns a non-negative thread
key.
Errors:
On failure PIThread_initThreadKey() returns a negative number.
PIPlatform_getLastError() can be used to get more specific error
information.
See Also:
PIThread_destroyThreadKey().
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIThread_initThreadKey();
/*____________________________________________________________________________*\
*
Name:
PIThread_destroyThreadKey
Synopsis:
int PIThread_destroyThreadKey( int iKey )
Description:
Destroy the thread key (index) iKey. This key can subsequently be
reassigned by PIThread_initThreadKey().
Notes:
Return Values:
On success PIThread_destroyThreadKey() returns zero (PIAPI_COMPLETED).
Errors:
On failure PIThread_destroyThreadKey() returns PIAPI_ERROR.
PIPlatform_getLastError() can be used to get more specific error
information.
See Also:
PIThread_initThreadKey().
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIThread_destroyThreadKey( int iKey );
/*____________________________________________________________________________*\
*
Name:
PIThread_setData
Synopsis:
int PIThread_setData( PIThread *pThread, int iKey, void *pData )
Description:
Sets the local data to pData for the thread referenced by pThread
at the key iKey.
Notes:
Return Values:
On success PIThread_setData() returns zero (PIAPI_COMPLETED).
Errors:
PIAPI_ERROR if an error occurred.
PIPlatform_getLastError() can be used to get more specific error
information.
See Also:
PIThread_getData().
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIThread_setData( PIThread *pThread, int iKey, void *pData );
/*____________________________________________________________________________*\
*
Name:
PIThread_getData
Synopsis:
void *PIThread_getData( PIThread *pThread, int iKey )
Description:
Returns the thread local data for the thread referenced by pThread
at key iKey. This is the value previously set by PIThread_setData() for
this thread and key.
Notes:
All thread local data is zero initialized by PIThread_new().
It is undefined whether using PIThread_terminate() followed by
PIThread_begin() will modify thread local data when used to reuse a
thread.
Return Values:
On success PIThread_getData() returns the thread local data for
this thread object at the specified key and PIPlatform_getLastError()
will return PIAPI_COMPLETED.
Errors:
On error PIThread_getData() will return NULL and PIPlatform_getLastError()
will return a specific error code.
See Also:
PIThread_setData().
\*____________________________________________________________________________*/
PUBLIC_PIAPI void *PIThread_getData( PIThread *pThread, int iKey );
/*____________________________________________________________________________*\
*
Name:
PIThread_getCurrent
Synopsis:
PIThread *PIThread_getCurrent()
Description:
Returns a pointer to the currently executing thread object.
Notes:
Return Values:
On success PIThread_getCurrent() returns the non-NULL pointer
to the currently executing thread object.
Errors:
NULL if an error occurs.
PIPlatform_getLastError() can be used to get more specific error
information.
See Also:
PIThread_getMain().
\*____________________________________________________________________________*/
PUBLIC_PIAPI PIThread *PIThread_getCurrent();
/*____________________________________________________________________________*\
*
Name:
PIThread_getMain
Synopsis:
PIThread *PIThread_getMain()
Description:
Returns a pointer to the main thread for the process.
Notes:
Return Values:
On success PIThread_getCurrent() returns the non-NULL pointer
to the main thread object.
Errors:
NULL if an error occurs.
PIPlatform_getLastError() can be used to get more specific error
information.
See Also:
PIThread_getCurrent().
\*____________________________________________________________________________*/
PUBLIC_PIAPI PIThread *PIThread_getMain();
/*____________________________________________________________________________*\
*
Name:
PIThread_getPriority
Synopsis:
int PIThread_getPriority( PIThread *pThread, int *piPriority )
Description:
Returns the priority of the thread pThread. piPriority points
to the location where the priority will be written.
Notes:
See PIThread_setPriority() for a description of thread priorities.
Return Values:
On success PIThread_getPriority() write the thread priority into
piPriority and returns zero (PIAPI_COMPLETED).
Errors:
PIAPI_ERROR if an error occurs.
PIPlatform_getLastError() can be used to get more specific error
information.
See Also:
PIThread_setPriority().
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIThread_getPriority( PIThread *pThread, int *piPriority );
/*____________________________________________________________________________*\
*
Name:
PIThread_setPriority
Synopsis:
int PIThread_setPriority( PIThread *pThread, int iNewPriority )
Description:
Set the priority of the thread pThread. The following are valid
priority values.
<TABLE>
<TR> PITHREAD_PRIORITY_LOWEST
<TR> PITHREAD_PRIORITY_LOW
<TR> PITHREAD_PRIORITY_MED
<TR> PITHREAD_PRIORITY_HIGH
<TR> PITHREAD_PRIORITY_HIGHEST
</TABLE>
Notes:
The precise behaviour of threads and thread priorities varies
greatly across implementations. In general a higher priority
thread will be scheduled before and run for longer than a
lower priority thread. However whether or not a lower priority
thread with be scheduled AT ALL when higher priority threads
are runnable is implementation defined.
The main thread initially runs at priority PITHREAD_PRIORITY_MED.
Return Values:
On success PIThread_setPriority() returns zero (PIAPI_COMPLETED).
Errors:
PIAPI_ERROR if an error occurs.
PIPlatform_getLastError() can be used to get more specific error
information.
See Also:
PIThread_getPriority().
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIThread_setPriority( PIThread *pThread, int iNewPriority );
/*____________________________________________________________________________*\
*
Name:
PIThread_dbgDump
Synopsis:
void PIThread_dbgDump( const char *pDumpFile )
Description:
Opens the specified file for write append and writes internal
information on thread status within the program.
If pDumpFile is NULL, information will be written to the stdout stream.
Notes:
This function is mainly for getting verbose information on
thread scheduling when the internal user thread implementation is
being used.
If operating system kernel threads are used this debugging
information will be terse.
Return Values:
PIThread_dbgDump does not return a value.
Errors:
PIThread_dbgDump does not return errors.
PIPlatform_getLastError() can be used to get more specific error
information.
See Also:
\*____________________________________________________________________________*/
PUBLIC_PIAPI void PIThread_dbgDump( const char *pDbgDump );
#endif /* PITHREAD_H_ */