/*****************************************************************************
 * file    lib/management/thread_monitor.c
 * author  Yuliang
 * version 1.0.0
 * date    08-Oct-2021
 * brief   This file provides all the thread monitor related operation functions.
 ******************************************************************************
 * Attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2021 LandPower</center></h2>
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *   1. Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the documentation
 *      and/or other materials provided with the distribution.
 *   3. Neither the name of LandPower nor the names of its contributors may be used to 
 *      endorse or promote products derived from this software without specific
 *      prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************/

/* Includes ------------------------------------------------------------------*/
//#ifdef HAVE_CONFIG_H
#include "config.h"
//#endif

#include "array.h"
#include "cmd.h"
#include "thread_monitor.h"

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/
#define THREAD_M_SIZE 32

#define THREAD_LOCK pthread_mutex_lock(&thread_mutex)
#define THREAD_UNLOCK pthread_mutex_unlock(&thread_mutex)

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/
static pthread_mutex_t thread_mutex;
static array_t *thread_a;

/* Private function prototypes -----------------------------------------------*/
extern int pthread_tryjoin_np(pthread_t thread, void **retval);

/* Internal functions --------------------------------------------------------*/
CMD(thread_show, 
    thread_show_cmd,
    "show thread",
    SHOW_STR
    "thread\n")
{    
    thread_m_t *node = NULL;
    int32_t i = 0;

    for (i = 0; i < thread_a->active; i++)
    {
        if (thread_a->index[i] != NULL)
        {
            node = (thread_m_t*)thread_a->index[i];
            /* 当返回不为EBUSY时说明线程退出或者出错了,但是再次查询又会变为EBUSY,
               所以这里一旦设置为FALSE,就无法再变为TRUE了. */
            if (pthread_tryjoin_np(node->pid, NULL) != EBUSY)
            {
                node->alive = FALSE;
            }
            vty_out(vty, "%-32s %-2d%s", node->name, node->alive, VTY_NEWLINE);
        }
    }
    vty_out(vty, "%s", VTY_NEWLINE);
    
    return CMD_SUCCESS;
}

/* Interface functions -------------------------------------------------------*/
/* description: 添加需要监控的线程.
   param:
   return:  (E_NONE)成功,(其他)失败 */
int32_t thread_m_add(char *str, pthread_t pid)
{
    thread_m_t *node = NULL;

    node = XMALLOC(MTYPE_THREAD_MONITOR, sizeof(thread_m_t));
    snprintf(node->name, THREAD_M_NAME_LEN, "%s", str);
    node->pid = pid;
    node->alive = TRUE;
    
    THREAD_LOCK;
    array_append(thread_a, node, MTYPE_THREAD_MONITOR);
    THREAD_UNLOCK;

    return E_NONE;
}

/* description: 线程监控初始化.
   param:
   return:  (E_NONE)成功,(其他)失败 */
int32_t thread_m_init(void)
{
    /* 初始化线程锁. */
    pthread_mutex_init(&thread_mutex, NULL);

    thread_a = array_init(THREAD_M_SIZE, MTYPE_THREAD_MONITOR);
    cmd_install_element(COMMON_NODE, &thread_show_cmd);

    return E_NONE;
}

/************************ (C) COPYRIGHT LandPower ***** END OF FILE ****/
