/****************************************************************
 *                                                              *
 *  LIBDIST V1.0						*
 *                                                              *
 *  thread.c -- thread handling routines                        *
 *                                                              *
 *  Last changed: 17.02.96                                      *
 *  Author: Frank Kargl (frank.kargl@informatik.uni-ulm.de)     *
 *                                                              *
 *  Restrictions: works only for Solaris 2.6 or above           *
 *                                                              *
 ****************************************************************/

#include "libdist.h"
#include "config.h"

#include <sys/errno.h>  /* the error codes */

/***
 *** thread_t dl_thr_create(void *(*function)(void *),void *arg)
 ***
 *** Function: create a new thread starting with 'function'
 ***           that gets passed arg as argument
 *** Return  : thread identifier
 ***           DL_ERROR if error
 ***/

thread_t dl_thr_create(void *(*function)(void *),void *arg) {
    
    thread_t thread;	/* temporary pointer for storing result */

    /* try to create a thread */
    if (thr_create(NULL,	/* create stack on your own */
                    0,		/* default size */
                    function,	/* call function */
                    arg,	/* with arg */
                    THR_DETACHED | THR_NEW_LWP | THR_DAEMON,   /* options */
                    &thread	/* for returning the thread id */
                   )) {
#ifdef DEBUG
	fprintf(stderr,"LIBDIST-thr: error creating thread\n");
#endif
        return DL_ERROR;
    }

    /* success */
    return thread;
}

/***
 *** void dl_thr_exit(void)
 ***
 *** Function: exit the current thread
 *** Return  : -
 ***/

void dl_thr_exit(void) {

    /* just exit */
    thr_exit(NULL);

}

/***
 *** int dl_thr_kill(thread_t thread, int sig)
 *** 
 *** Function: send signal 'sig' to thread 'thread'
 *** Return  : values != DL_OK indicate an error
 ***           DL_THR_NSS = no such signal
 ***           DL_THR_NST = no such thread
 ***/

int dl_thr_kill(thread_t thread,int sig) {

    int retval;		/* the return value */

    /* send signal */
    retval=thr_kill(thread,sig);

    switch (retval) {
        case EINVAL: return DL_THR_NSS;
        case ESRCH:  return DL_THR_NST;
    }

    return DL_OK;
}
