summaryrefslogtreecommitdiff
path: root/src/thrd/pool.cpp
blob: efea94857eba579bd8c9c6dcfc59d2d712224f87 (plain)
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
46
47
48
49
50
51
52
53
54
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#ifndef POOL_CPP
#define POOL_CPP

#include "pool.h"

using namespace std;

int pool::i_thrd_used = 0;

pool::pool()
{
    i_thrd_pool_size  = tool::string2int( wrap::CONF->get_elem( "httpd.thread.initpoolsize" ) );
    i_thrd_pool_queue = tool::string2int( wrap::CONF->get_elem( "httpd.thread.queuesize" ) );
    tpool_init( &thread_pool, i_thrd_pool_size, i_thrd_pool_queue);
}

void
pool::tpool_init( tpool_t *tpoolp, int num_worker_threads, int max_queue_size)
{
    int i, rtn;
    tpool_t tpool;

    // allocate a pool data structure
    if (( tpool = (tpool_t) malloc( sizeof( struct tpool ) ) ) == 0 )
    {
     wrap::system_message( POOLERR );
     exit(-1);
    }

    // initialize th fields
    tpool->num_threads    = num_worker_threads;
    tpool->max_queue_size = max_queue_size;

    if ( ( tpool->threads = (pthread_t*) malloc( sizeof(pthread_t)*num_worker_threads ) ) == 0 )
    {
        wrap::system_message( POOLERR );
        exit(-1);
    }

    tpool->cur_queue_size = 0;
    tpool->queue_head     = 0;
    tpool->queue_tail     = 0;

    if ( ( rtn = pthread_mutex_init( &(tpool->queue_lock), 0 ) ) != 0 )
    {
        string s_err( "pthread_mutex_init " );
        s_err.append( strerror( rtn ) );

        wrap::system_message( s_err );

        exit(-1);
    }

    else if ( ( rtn = pthread_cond_init( &(tpool->queue_not_empty), 0 ) ) != 0 )
    {
        string s_err( "pthread_cond_init (1): " );
        s_err.append( strerror( rtn ) );

        wrap::system_message( s_err );

        exit(1);
    }

    else if ( ( rtn = pthread_cond_init( &(tpool->queue_not_full), 0 ) ) != 0 )
    {
        string s_err( "pthread_cond_init (2): " );
        s_err.append( strerror( rtn ) );

        wrap::system_message( s_err );

        exit(1);
    }

    else if ( ( rtn = pthread_cond_init( &(tpool->queue_empty), 0 ) ) != 0 )
    {
        string s_err( "pthread_mutex_init " );
        s_err.append( strerror( rtn ) );

        wrap::system_message( s_err );

        exit(1);
    }

    // create threads
    for ( i = 0; i < num_worker_threads; i++ )
        pthread_create( &(tpool->threads[i]) , 0, tpool_thread, (void*)tpool );

    *tpoolp = tpool;
}

void*
pool::tpool_thread( void* p_void )
{
    tpool_t tpool = (tpool_t) p_void;
    tpool_work_t *my_workp;

    for( ;; )
    {
        pthread_mutex_lock( &(tpool->queue_lock) );

        while ( tpool->cur_queue_size == 0) 
           pthread_cond_wait( &(tpool->queue_not_empty), &(tpool->queue_lock) );

        my_workp = tpool->queue_head;
        tpool->cur_queue_size--;

        if ( tpool->cur_queue_size == 0)
            tpool->queue_head = tpool->queue_tail = 0;

        else
            tpool->queue_head = my_workp->next;

        if ( tpool->cur_queue_size == ( tpool->max_queue_size - 1 ) )
            pthread_cond_signal( &(tpool->queue_not_full) );

        if ( tpool->cur_queue_size == 0 ) 
            pthread_cond_signal( &(tpool->queue_empty) );

        pthread_mutex_unlock( &(tpool->queue_lock) );

        (*(my_workp->routine))(my_workp->p_void);

        pthread_mutex_lock( &(tpool->queue_lock) );
        --i_thrd_used;
#ifdef NCURSES
        print_threads(i_thrd_used);
#endif
        pthread_mutex_unlock( &(tpool->queue_lock) );

        //free( (void*) my_workp );
        free( my_workp );
    }
}

void pool::run_func( void *p_void )
{
    int* p_sock = (int*)p_void;
    wrap::SOCK->read_write( p_sock );
    delete p_sock;
}

int
pool::tpool_add_work( tpool_t tpool, void(*routine)(void*), void* p_void ) ///
{
    tpool_work_t *workp;
    pthread_mutex_lock( &(tpool->queue_lock) );

    if ( ++i_thrd_used == tpool->num_threads ) 
    {
      int i_max_pool_size = tool::string2int( wrap::CONF->get_elem( "httpd.thread.maxpoolsize" ) );
      if ( i_max_pool_size != 0 && i_thrd_used > i_max_pool_size )
      {
        wrap::system_message(POOLER2+tool::int2string(i_thrd_used)+")");
      }

      else
      {
        int i_size = tpool->num_threads * 2;

        wrap::system_message(POOLFLL+tool::int2string(i_size)+")");

        tpool->threads = (pthread_t*)realloc((void*)tpool->threads, sizeof(pthread_t)*tpool->num_threads);

        for ( int i = tpool->num_threads; i < i_size; ++i )
          pthread_create( &(tpool->threads[i]) , 0, tpool_thread, (void*)tpool );

        i_thrd_pool_size = tpool->num_threads = i_size;
#ifdef NCURSES
        print_pool_size();
#endif
      }
    }

#ifdef NCURSES
    print_threads(i_thrd_used);
#endif

    while (tpool->cur_queue_size == tpool->max_queue_size) 
        pthread_cond_wait( &(tpool->queue_not_full), &(tpool->queue_lock) );

    // allocate work structure:
    workp = (tpool_work_t*) malloc( sizeof( tpool_work_t ) );

    workp->routine = routine;
    workp->p_void  = p_void;
    workp->next    = 0;

    if (tpool->cur_queue_size == 0 )
    {
        tpool->queue_tail = tpool->queue_head = workp;
    }

    else
    {
        (tpool->queue_tail)->next = workp;
        tpool->queue_tail         = workp;
    }

    pthread_cond_signal( &tpool->queue_not_empty );
    tpool->cur_queue_size++;
    pthread_mutex_unlock( &(tpool->queue_lock) );

    return 0;
}

#ifdef NCURSES
void
pool::print_threads(int i_thrd_used)
{
    if ( wrap::NCUR->is_ready() )
    {
     mvprintw( NCUR_POOL_RUNNING_X,NCUR_POOL_RUNNING_Y, "In use: %d ", i_thrd_used);
     refresh();
   }
}

void
pool::print_pool_size()
{
    if ( wrap::NCUR->is_ready() )
    {
     mvprintw( NCUR_POOL_SIZE_X,NCUR_POOL_SIZE_Y, "Size: %d %d", i_thrd_pool_size, i_thrd_pool_queue );
     refresh();
   }
}
#endif

#endif