thread.h 4.38 KB
/*
 * used for threadsafety within exception
 * actually only pthread is supported
 */
#ifndef  THREAD_H
#define  THREAD_H

#include <stdio.h>
#include <pthread.h>

#include <scot/scot_int.h>

#ifndef INFINITE
#  define   INFINITE                   0
#endif /* INFINITE */


#define  JOIN_OK             0x00
#define  JOIN_TIMEOUT        0x01
#define  JOIN_ERROR          0x02

/*
 * i guess it is possible to write mutex code that uses cond
 * elements to timeout on lock, but right now i have no clear
 * idea of how to do this. The following thoughts i had:
 * - create a struct that holds a pthread_mutex_t and a pthread_cond_t
 *   for every mutex. 
 * - lock mutexes only by calling pthread_cond_timedwait
 * And here starts the problem...to do a cond_timedwait i need to ensure
 * that the mutex is already locked...well and then a question i have not
 * clearyfied, if a thread ends, will all locks on mutexes be removed?
 * I guess so, but i did not test it. And an even more important question,
 * is it possible to write cleanup-code that guarantees that a
 * pthread_cond_signal is called for every mutex the thread holds...
 * well, we will end up with the requirement that the programmer has to
 * call a release mutex function before ending a thread and if the programmer
 * forgets that the behaviour of out code is unspecified
 * Right now i will not support timeouts on mutex-locks and to be consistent
 * it is not supported on win32 either, also it would be much easier to
 * implement there.
 */
#define  MUTEX_LOCK_OK       0x00
#define  MUTEX_LOCK_ERROR    0x02

#define  THREAD_T                   pthread_t
#define  THREAD_ID_T                pthread_t
#define  THREAD_ID                  pthread_self    ()
#define  SELF_THREAD                pthread_self    ()
#define  THREAD_EQUAL               pthread_equal
#define  NEW_THREAD(th,f, a)        thread_new      ((th), (f), (a))
#define  END_THREAD                 thread_end
#define  DETACH_THREAD(thread)      pthread_detach  ((thread))
#define  CANCEL_THREAD(thread)      pthread_cancel  ((thread))
#define  EXIT_THREAD(retval)        pthread_exit    ((void *) (retval))
#define  JOIN_THREAD                thread_join                               
#define  THREAD_CANCEL_ENABLE       pthread_setcancelstate \
		(PTHREAD_CANCEL_ENABLE, NULL)
#define  THREAD_CANCEL_DISABLE      pthread_setcancelstate \
		(PTHREAD_CANCEL_DISABLE, NULL)
#define  THREAD_CANCEL_ASYNC        pthread_setcanceltype \
		(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)
#define  THREAD_CANCEL_DEFER        pthread_setcanceltype \
		(PTHREAD_CANCEL_DEFERRED, NULL)
#define  THREAD_TESTCANCEL          pthread_testcancel ()
#define  T_PROC_RET                 void *

#define  THREAD_MUTEX_T             pthread_mutex_t
#define  NEW_THREAD_MUTEX           thread_mutex_new
#define  FREE_THREAD_MUTEX          pthread_mutex_destroy
#define  LOCK_THREAD_MUTEX          thread_mutex_lock
#define  UNLOCK_THREAD_MUTEX        pthread_mutex_unlock

#define  THREAD_COND_T                 pthread_cond_t
#define  THREAD_COND_CS_T              pthread_mutex_t
#define  GET_THREAD_COND(c)            (*(c) = PTHREAD_COND_INITIALIZER)
#define  GET_THREAD_COND_CS(c)         (*(c) = PTHREAD_MUTEX_INITIALIZER)
#define  NEW_THREAD_COND               thread_cond_new
#define  NEW_THREAD_COND_CS            thread_mutex_new
#define  FREE_THREAD_COND              pthread_cond_destroy
#define  FREE_THREAD_COND_CS           pthread_mutex_destroy
#define  THREAD_COND_ENTER_CS(cs)      pthread_mutex_lock ((cs))
#define  THREAD_COND_LEAVE_CS(cs)      pthread_mutex_unlock ((cs))
#define  SIGNAL_THREAD_COND(cond)      pthread_cond_signal ((cond))
#define  BCAST_THREAD_COND(cond)       pthread_cond_broadcast ((cond))
#define  WAIT_THREAD_COND(cond, cs, t) thread_cond_wait ((cond), (cs), t)

#define  THREAD_ATEXIT_BEGIN        pthread_cleanup_push
#define  THREAD_ATEXIT_END          pthread_cleanup_pop

typedef T_PROC_RET (*scot_thread_entry_fptr) (void *);

void           thread_new        (THREAD_T *, T_PROC_RET (*)(void *), void*);
int            thread_join       (THREAD_T, uint32_t);
void           thread_end        (THREAD_T, uint32_t);
void           thread_mutex_new  (THREAD_MUTEX_T *);
void           thread_cond_new   (THREAD_COND_T *);
int            thread_mutex_lock (THREAD_MUTEX_T *);

int            thread_cond_wait    (THREAD_COND_T *, 
                                    THREAD_COND_CS_T *, uint32_t);

#endif   /* THREAD_H */