/*
 *  thread.h
 *  RatLib
 *
 *  Created by Curtis Jones on 2008.10.20.
 *  Copyright 2008 Curtis Jones. All rights reserved.
 *
 */

#ifndef __THREAD_H__
#define __THREAD_H__

#include <stdint.h>
#include <pthread.h>
#include "cobject.h"
#include "opool.h"

//
// thread_state
//
typedef enum
{
	THREAD_STATE_ERROR   = -1,
	THREAD_STATE_STARTED = (1<<0),
	THREAD_STATE_STOPPED = (1<<1),
	THREAD_STATE_STOPPING = (1<<2)
} thread_state;

//
// thread
//
struct thread
{
	cobject_t cobject;								// super class
	
	pthread_t pthread;								// posix thread
	thread_state state;								// state
	
	void *(*start_func)(void*);				// start func
	void *arg;												// arg
};
typedef struct thread thread_t;

//
// thread_start_func
//
typedef void * (*thread_start_func) (void *);

/**
 *
 */
int thread_init (thread_t*, thread_start_func, void*restrict, opool_t*);

/**
 *
 */
int thread_destroy (thread_t*);

/**
 *
 */
int thread_start (thread_t*);

/**
 *
 */
int thread_stop (thread_t*);

/**
 *
 */
thread_state thread_getstate (thread_t*);

/**
 *
 */
int thread_signal (thread_t*, int);

/**
 *
 */
thread_t* thread_retain (thread_t*);

/**
 *
 */
void thread_release (thread_t*);

#endif /* __THREAD_H__ */
