#ifndef __THREADUTIL_H__
#define __THREADUTIL_H__

#ifdef __cplusplus
#ifndef BEGIN_DECLS
#define BEGIN_DECLS extern "C" {
#endif
#ifndef END_DECLS
#define END_DECLS }
#endif
#else
#ifndef BEGIN_DECLS
#define BEGIN_DECLS
#endif
#ifndef END_DECLS
#define END_DECLS
#endif
#endif

BEGIN_DECLS

#if defined(_WIN32) && defined(UNICODE)
#include <wchar.h>
#endif
#include <stdlib.h>
#include <amqp.h>
#include <amqp_tcp_socket.h>
#include <amqp_framing.h>
#include <pthread.h>
#include <stdbool.h>
#include <unistd.h>
#include <signal.h>
#include <sys/stat.h>
#include <limits.h>
#include <string.h>

#include "Common.h"
#include "SecureCodingUtil.h"
#include "Config.h"
#include "Log.h"
#include "DataStructure.h"

#define THREAD_ERR_SUCCESS                  0
#define THREAD_ERR_CONFIG                   -1

#define THREAD_CONF_THREAD_COUNT_KEY        "THREAD_COUNT"

enum THREAD_STATE
{
    THREAD_STATE_INIT = 0,
    THREAD_STATE_RUNNING,
    THREAD_STATE_SUSPEND,
    THREAD_STATE_TERMINATED,
    THREAD_STATE_COUNT,
};

#ifndef __STRUCT_MQ_CONNECTION__
#define __STRUCT_MQ_CONNECTION__

struct MQ_CONNECTION
{
    amqp_socket_t *psMQSocket;
    amqp_connection_state_t sMQConnectionState;
    pthread_mutex_t sMQMutex;
    int bMQConnection;
    int bMQChannelOpen;
};

#endif

struct THREAD_INFO
{
    int bThreadLoop;
    int nRunState;
    pthread_mutex_t sContextMutex;
    pthread_t nThreadID;
    int nThreadIndex;
/*
    amqp_socket_t *psMQSocket;
    amqp_connection_state_t sMQConnectionState;
    pthread_mutex_t sMessageQueueMutex;
    int bMQConnection;
    int bChannelOpen;
    pthread_mutex_t sMessageQueueMutex;
*/
    struct MQ_CONNECTION sPublishMQ;
    struct MQ_CONNECTION sConsumeMQ;
};

int InitThreadConfig();
int InitThread(void *(*pWorkThreadFunc)(void *));
int InitThreadWait(void *(*pWorkThreadFunc)(void *));
void DaemonStart();
void SignalHandler(int nSignal);

#include "Socket.h"

END_DECLS

#endif
