IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 8, 2008, 8:05:09 AM (18 years ago)
Author:
Paul Price
Message:

Cleaning up thread functions, documenting header. Added 'harvest' option to psThreadPoolWait to save user from the trouble of harvesting. Fixed memory problem with psListGetAndRemove.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/sys/psThread.h

    r18827 r18953  
    44 *
    55 *  @author EAM, IFA
    6  *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
    7  *  @date $Date: 2008-07-31 23:56:29 $
     6 *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
     7 *  @date $Date: 2008-08-08 18:05:09 $
    88 *
    99 *  Copyright 2004-2005 Insitute for Astronomy, University of Hawaii
     
    1616/// @{
    1717
     18/// Job to be executed on a thread
     19///
     20/// This job is passed to the function that executes it
    1821typedef struct {
    19     psString type;
    20     psArray *args;
     22    psString type;                      // Type of thread
     23    psArray *args;                      // Arguments to job
    2124} psThreadJob;
    2225
     26#define PS_ASSERT_THREAD_JOB_NON_NULL(JOB, RVAL) \
     27if (!(JOB) || !(JOB)->type || !(JOB)->args) { \
     28    psError(PS_ERR_UNEXPECTED_NULL, true, "Thread job %s or one of its components is NULL.", #JOB); \
     29    return RVAL; \
     30}
     31
     32/// A thread, which executes a job
     33///
     34/// Wraps pthread with a few extra conveniences
    2335typedef struct {
    24     bool busy;
    25     bool fault;
    26     pthread_t pt;
     36    bool busy;                          // Is the thread busy?
     37    bool fault;                         // Has the thread faulted?
     38    pthread_t pt;                       // The thread itself
    2739} psThread;
    2840
    29 typedef bool (*psThreadTaskFunction)(psThreadJob *job);
     41/// Function to execute a thread job
     42typedef bool (*psThreadTaskFunction)(const psThreadJob *job);
    3043
     44/// Task that is executed on a thread
    3145typedef struct {
    32     psString type;
    33     int nArgs;
    34     psThreadTaskFunction function;
     46    psString type;                      // Type of task
     47    int nArgs;                          // Number of arguments that function takes
     48    psThreadTaskFunction function;      // Function to execute
    3549} psThreadTask;
    3650
    37 // typedef void *(*psThreadLaunchJobsFunction)(void *data);
     51#define PS_ASSERT_THREAD_TASK_NON_NULL(TASK, RVAL) \
     52if (!(TASK) || !(TASK)->type || (TASK)->nArgs < 0 || !(TASK)->function) { \
     53    psError(PS_ERR_UNEXPECTED_NULL, true, "Thread task %s or one of its components is NULL.", #TASK); \
     54    return RVAL; \
     55}
    3856
    39 void psThreadLock ();
    40 void psThreadUnlock ();
    4157
    42 psThread *psThreadAlloc ();
     58/// Lock the thread mutex
     59void psThreadLock(void);
    4360
    44 psThreadJob *psThreadJobAlloc (char *type);
    45 bool psThreadJobAddPending (psThreadJob *job);
    46 psThreadJob *psThreadJobGetPending ();
    47 psThreadJob *psThreadJobGetDone ();
     61/// Unlock the thread mutex
     62void psThreadUnlock(void);
    4863
    49 psThreadTask *psThreadTaskAlloc (char *type, int nArgs);
    50 bool psThreadTaskAdd (psThreadTask *task);
    51 void *psThreadLauncher (void *data);
     64/// Allocate a thread
     65psThread *psThreadAlloc(void);
    5266
    53 bool psThreadPoolInit (int nThreads);
    54 bool psThreadPoolWait ();
    55 bool psThreadPoolFinalize ();
     67/// Allocate a thread job
     68psThreadJob *psThreadJobAlloc(const char *type);
     69
     70/// Add a pending job to the queue
     71bool psThreadJobAddPending(psThreadJob *job);
     72
     73/// Get a job off the queue of pending jobs
     74psThreadJob *psThreadJobGetPending(void);
     75
     76/// Get a job off the queue of done jobs
     77psThreadJob *psThreadJobGetDone(void);
     78
     79/// Allocate a thread task
     80psThreadTask *psThreadTaskAlloc(const char *type, // Type of task
     81                                int nArgs // Number of arguments
     82    );
     83
     84/// Add a task to the list
     85bool psThreadTaskAdd(psThreadTask *task // Task to add
     86    );
     87
     88/// Launch jobs on a thread
     89void *psThreadLauncher(void *thread     // Thread (of type psThread)
     90    );
     91
     92/// Initialise a pool of threads
     93bool psThreadPoolInit(int nThreads      // Number of threads
     94    );
     95
     96/// Return size of thread pool
     97int psThreadPoolSize(void);
     98
     99/// Wait for the thread pool to finish
     100///
     101/// This function blocks (waits in usleep) until either an error is detected on one of the threads or until ll
     102/// threads are idle and no jobs are left on the queue
     103bool psThreadPoolWait(bool harvest      // Harvest the jobs from the queue?
     104    );
     105
     106/// Clean up the thread pool
     107bool psThreadPoolFinalize(void);
    56108
    57109/// @}
Note: See TracChangeset for help on using the changeset viewer.