Encoder-nyuenc / src / main / utils.h
utils.h
Raw
#ifndef _UTILS_H_
#define _UTILS_H_

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

// Define constants and structures
#define MAXFILECOUNT 100                                        // Maximum number of files
#define CHUNKSIZE 4096                                          // The size of each chunk (4KB into bytes)
#define MAXFILESIZE 1073741824                                  // Maximum total size of all files (1GB into bytes)
#define MAXCHUNKCOUNT 262144                                    // Maximum number of chunks
#ifdef DEBUG
#define DEBUG 1
#else
#define DEBUG 0
#endif


// Global variables and structures
int jobs;                                                       // The number of threads
int fileCount;                                                  // The number of files
char *fileContents[MAXFILECOUNT];                               // The array of file contents
int fileLengths[MAXFILECOUNT];                                  // The array of file lengths
struct indexer {                                                // The indexer structure for locating data in the file array
    int             fileIndex;                                  // The index of the file to access
    int             fileCursor;                                 // The cursor for the starting character in that file
};
struct indexer *fileIndexer;                                    // The pointer to the indexer for locating data in the file array

// Global variables and structures for the task queue
struct task {                                                   // The structure of a task
    int             fileIndex;                                  // The index of the file to access for the task
    int             fileCursor;                                 // The cursor for the starting character in that file for the task
};
int firstTask;                                                  // The index of the first task in the task queue to dequeue
int lastTask;                                                   // The index of the last task in the task queue after which to enqueue
struct task **taskQueue;                                        // The task queue of pointers to tasks
pthread_mutex_t taskQueueAccess;                                // The mutual exclusion for access to the task queue
pthread_cond_t taskInQueue;                                     // The conditional variable signaling there are tasks in queue
pthread_cond_t taskCleared;                                     // The conditional variable signaling there are no tasks in queue

// Global variables and structures for the result queue
struct result {                                                 // The structure of an result
    unsigned char   *charArray;                                 // The array representing the characters in the result
    uint8_t         *countArray;                                // The array representing the counts of the each character in the result
    int             resultLength;                               // The length of the output arrays
};
int firstResult;                                                // The index of the first result in the result queue
int lastResult;                                                 // The index of the last result in the result queue
struct result **resultQueue;                                    // The result queue of pointers to results

// Global variables and structures for the thread pool
int allowShutDown;                                              // Whether the shutdown of threads are allowed
struct thread {                                                 // The structure of a thread containing its information
    pthread_t       threadId;                                   // The thread id returned at creation
    int             threadIndex;                                // The index of the thread in the thread pool
};
struct thread **threads;                                        // The array of pointer to the threads


// ========== ========== ========== ========== ========== //
//                                                        //
//                        PARSER.C                        //
//                                                        //
// ========== ========== ========== ========== ========== //

/**
 * Parses the command line for optional argument [-j jobs] and file names.
 * Rearranges the command line tokens so that non-optional tokens are in the last.
 * Updates the file contents array correspondingly.
 * -- Modification note --
 * This function no longer performs any jobs other than parsing the command and updating jobs.
 * Rearrangement of command line tokens are default by getopt() function.
 * Files are mapped into the array of file contents, and file lengths are correspondingly updated.
 * 
 * Input:
 * argc             The number of tokens in the command line.
 * argv             The array of tokens in the command line.
 * 
 * Output:
 *                  None.
**/
void parseCommand(int argc, char **argv);


// ========== ========== ========== ========== ========== //
//                                                        //
//                      THREADPOOL.C                      //
//                                                        //
// ========== ========== ========== ========== ========== //

/**
 * Initializes a thread pool with specified number of threads.
 * Calls startThread() for each thread initialized in the thread pool.
 * 
 * Input:
 *                  None.
 * 
 * Output:
 *                  None.
**/
void initThreadPool();

/**
 * Starts a thread and keep it alive until the thread pool is destroyed.
 * Ready to be assigned new tasks when idle.
 * 
 * Input:
 * thread           The thread pointer containing thread id and thread index.
 * 
 * Output:
 *                  None.
**/
void *startThread(void *thread);

/**
 * Maintains the thread pool until all threads are idle.
 * -- Modification note --
 * Now uses pthread_join to automatically terminate and free threads.
 * Modified the threads array as pointer to thread structures instead of statically allocating,
 * so this function frees each thread structure and the threads array as well.
 * 
 * Input:
 *                  None.
 * 
 * Output:
 *                  None.
**/
void maintainThreadPool();


// ========== ========== ========== ========== ========== //
//                                                        //
//                      TASKQUEUE.C                       //
//                                                        //
// ========== ========== ========== ========== ========== //

/**
 * Dispatch all tasks into the task queue.
 * 
 * Input:
 *                  None.
 * 
 * Output:
 *                  None.
**/
void dispatchTasks();


// ========== ========== ========== ========== ========== //
//                                                        //
//                     RESULTQUEUE.C                      //
//                                                        //
// ========== ========== ========== ========== ========== //

/**
 * Extracts the final results from the result queue and print to stdout.
 * 
 * Input:
 *                  None.
 * 
 * Output:
 *                  None.
**/
void extractFinalResult();


// ========== ========== ========== ========== ========== //
//                                                        //
//                        ENCODER.C                       //
//                                                        //
// ========== ========== ========== ========== ========== //

/**
 * Encodes a single chunk by run-length encoding (RLE).
 * -- Modification note --
 * This function now returns an encoderResults structure for temporary storage.
 * It is then passed to the merge function to update and merge into the actual result queue.
 * -- Modification note --
 * This function does not return now, and instead directly modify *ONLY* the corresponding result.
 * Hence, mutex for result queue is no longer needed, and the merging tasks if left after everything done.
 * -- Modification note --
 * The current version replaced the linked list by C array.
 * Hence, array index are used instead of real tasks as input in this function.
 * 
 * Input:
 * taskId           The index of the current encoding task in the task queue.
 * 
 * Output:
 *                  None.
**/
void encodeRLE(int taskId);


#endif