XThread.h 5.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/* NiuTrans.Tensor - an open-source tensor library
 * Copyright (C) 2017, Natural Language Processing Lab, Northestern University. 
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * 
 * a naive implementation of thread pool (actually it is a pool)
 *
 * $Created by: XIAO Tong (xiaotong@mail.neu.edu.cn) 2016-03-08
 *
 */

#ifndef __XTHREAD_H__
#define __XTHREAD_H__

#include "XList.h"

#ifndef _WIN32
#define USE_PTHREAD // for linux
#endif

//////////////////////////////////////////////////
// neccessary libs
#ifdef USE_PTHREAD
#include <pthread.h> // use "-lpthread" when compiling on linux systems
#else
#ifdef _WIN32
#include <windows.h>
#include <process.h>
#endif
#endif

46 47 48
/* the nts (NiuTrans.Tensor) namespace */
namespace nts{

49 50 51
#if(defined(_WIN32) && !defined (__CYGWIN__))
#define CRFPP_USE_THREAD 1
#define BEGINTHREAD(src, stack, func, arg, flag, id) \
52 53 54
                   (HANDLE)_beginthreadex((void *)(src), (unsigned)(stack), \
                   (unsigned(_stdcall *)(void *))(func), (void *)(arg), \
                   (unsigned)(flag), (unsigned *)(id))
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
#endif

//////////////////////////////////////////////////
// mutex
#ifdef WIN32
#define      THREAD_HANDLE            HANDLE
#define      MUTEX_HANDLE             CRITICAL_SECTION
#define      COND_HANDLE              HANDLE
#define      MUTEX_INIT( x )          InitializeCriticalSection( &(x) )
#define      MUTEX_DELE( x )          DeleteCriticalSection( &(x) )
#define      MUTEX_LOCK( x )          EnterCriticalSection( &(x) )
#define      MUTEX_UNLOCK( x )        LeaveCriticalSection( &(x) )
#define      COND_INIT( x )           ( x = CreateEvent( NULL, false, false, NULL ) )
#define      COND_DELE( x )           CloseHandle( (x) )
#define      COND_WAIT( x, y )        WaitForSingleObject( (x), INFINITE )
#define      COND_SIGNAL( x )         SetEvent( (x) )
#define      COND_RESET( x)           ResetEvent( (x) )
#else
#define      THREAD_HANDLE            pthread_t
#define      MUTEX_HANDLE             pthread_mutex_t
#define      COND_HANDLE              pthread_cond_t
#define      MUTEX_INIT( x )          pthread_mutex_init( &(x), NULL )
#define      MUTEX_DELE( x )          pthread_mutex_destroy( &(x) )
#define      MUTEX_LOCK( x )          pthread_mutex_lock( &(x) )
#define      MUTEX_UNLOCK( x )        pthread_mutex_unlock( &(x) )
#define      COND_INIT( x )           pthread_cond_init( &(x), NULL )
#define      COND_DELE( x )           pthread_cond_destroy( &(x) )
#define      COND_WAIT( x, y )        pthread_cond_wait( &(x), &(y) )
#define      COND_SIGNAL( x )         pthread_cond_signal( &(x) )
#define      COND_BROADCAST( x )      pthread_cond_broadcast( &(x) )

#endif

typedef void (*TFunction) (volatile XList*);

/*
This is a class that wraps the standard implementation of threading
(for both windows and linux OS)
*/
class XThread
{

public:
    /* thread id */
    THREAD_HANDLE hnd;

    /* to information outside caller */
    MUTEX_HANDLE gMutex;

    /* working state */
    int working;

    /* a lock to protect the working state */
    MUTEX_HANDLE workingMutex;

    /* to inform the job when it is ready */
    COND_HANDLE jobCond;

    /* indicate whether the thread is running */
    bool isRunning;

#ifdef USE_PTHREAD

    /* a mutex lock */
    MUTEX_HANDLE mutex;

    /* condition lock */
    COND_HANDLE cond;

    /* scheduling for threads */
    sched_param schedParam;
#else
#endif

public:
    /* function to run */
    volatile
    TFunction function;

    /* arguments (for the function to run) */
    volatile
    XList * argv;

    /* a flag to break */
    volatile
    bool toBreak;

    /* number of jobs that are waiting */
    volatile
    int jobCount;

public:
    /* constructor */
    XThread();

    /* deconstructor */
    ~XThread();

public:
    /* a wrapper for the start-routine parameter in pthread_create */
    static void * Wrapper(void * ptr);

    /* 
    Core of the thread. It is very very native impelementation.
    We loop and wait for a singnal to activate the job processing.
    After that, we wait again if there is no new job.
    */
    void Run();

    /* create and run the thread */
    bool Start();

    /* end the thread */
    void End();

    /* wait for thread termination */
    void Join();

    /* let the thread process a job */
    void LetItGo();

    /* waith for a singal */
    static
    void Wait(COND_HANDLE * c, MUTEX_HANDLE * m);
};

/*
a counter with mutex
*/
class XCounter
{
private:
    /* count */
    int count;

    /* lock */
    MUTEX_HANDLE mutex;

public:
    /* constructor */
    XCounter();

    /* deconstructor */
    ~XCounter();

    /* add the counter by 1 */
    void Add();

    /* get the counting number */
    int Get();
};

} /* end of the nts (NiuTrans.Tensor) namespace */

#endif