/*************************************************  
  File name     : workqueue.h
  Module        : 
  Author        : amir 
  Version       : 0.1   
  Created on    : 2022-02-07  
  Description   :
      Data structure and function definitions required by the socket interface

  Modify History:   
  1. Date:        Author:         Modification:
*************************************************/
#include "threadpool.h"
#include "workqueue.h"
#include "hlog.h"


workqueue *workqueue_init(uint32_t max_cnt){
    workqueue *wq = threadpool_create(max_cnt);
	assert(wq);
	return wq;
}

void workqueue_deinit(workqueue *wq){
	if (wq){
		threadpool_dump(wq);
		threadpool_destroy(wq);
	}
}

uint32_t workqueue_add(workqueue *wq, thread_routine routine, kpacket *packet){
	return threadpool_add(wq, routine, packet );
}

static void *workqueue_test_handler(void *param){
    kpacket *packet = (kpacket *)param;
	hlogi("%s V:%u", __func__, *((int32_t *) packet->tlv.V));
	return NULL;
};

void workqueue_test()
{
    hlogi("%s start", __func__);
    workqueue *wq = workqueue_init(5);
    uint32_t i;
    for(i=0; i<1000000;i++){
      kpacket *packet = kpacket(1, 10); 
      hlogi("%s i:%u packet:%p", __func__, i, packet);
      if (!packet){
        hloge("%s i:%u packet:%p", __func__, i, packet);
        hv_usleep(1000);
        continue;
      }
     *((int32_t *) packet->tlv.V) = i;
      workqueue_add(wq, workqueue_test_handler, packet);
      kpacket_dec(packet);
    }
    workqueue_deinit(wq);
    hlogi("%s stop", __func__);
}

void workqueue_dump(workqueue *wq)
{
    threadpool_dump(wq);
}


