
#include <stdio.h>
#include <stdlib.h>

#include "process_watcher_entry.h"
#include "threadpool.h"

struct s_ProcessWatcherEntry
{
    int pid;
    ProcessWatcherDelegate delegate;
    void *context;
    int status;
};

ProcessWatcherEntry *
process_watcher_entry_new (int pid, ProcessWatcherDelegate delegate, void *context)
{
    ProcessWatcherEntry *instance = (ProcessWatcherEntry *) malloc (sizeof (ProcessWatcherEntry));

    instance->pid = pid;
    instance->delegate = delegate;
    instance->context = context;
    instance->status = 0;

    return (instance);
}

void
process_watcher_entry_destroy (void *ptr)
{
    ProcessWatcherEntry *instance = (ProcessWatcherEntry *) ptr;

    free (instance);
}

static void
process_watcher_entry_internalInvoke (void *context)
{
    ProcessWatcherEntry *instance = (ProcessWatcherEntry *) context;

    instance->delegate (instance->context, instance->status);

    process_watcher_entry_destroy (instance);
}

void
process_watcher_entry_invoke (ProcessWatcherEntry *instance, 
        int status)
{
    instance->status = status;

    /*
    threadpool_queueUserWorkItem (threadpool_getInstance(),
            process_watcher_entry_internalInvoke, instance);
            */
    process_watcher_entry_internalInvoke (instance);
}
