summaryrefslogtreecommitdiff
path: root/src/common/WorkQueue.cc
blob: a3f67e0a0d2257e63e091942058263b05b3bb7a8 (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software 
 * Foundation.  See file COPYING.
 * 
 */

#include "include/types.h"
#include "WorkQueue.h"

#include "config.h"

#define DOUT_SUBSYS tp
#undef dout_prefix
#define dout_prefix *_dout << name << " "


void ThreadPool::worker()
{
  _lock.Lock();
  dout(10) << "worker start" << dendl;
  while (!_stop) {
    if (!_pause && work_queues.size()) {
      WorkQueue_* wq;
      int tries = work_queues.size();
      bool did = false;
      while (tries--) {
	last_work_queue++;
	last_work_queue %= work_queues.size();
	wq = work_queues[last_work_queue];
	
	void *item = wq->_void_dequeue();
	if (item) {
	  processing++;
	  dout(12) << "worker wq " << wq->name << " start processing " << item << dendl;
	  _lock.Unlock();
	  wq->_void_process(item);
	  _lock.Lock();
	  wq->_void_process_finish(item);
	  dout(15) << "worker wq " << wq->name << " done processing " << item << dendl;
	  processing--;
	  if (_pause || _draining)
	    _wait_cond.Signal();
	  did = true;
	  break;
	}
      }
      if (did)
	continue;
    }
    dout(15) << "worker waiting" << dendl;
    _cond.Wait(_lock);
  }
  dout(0) << "worker finish" << dendl;
  _lock.Unlock();
}

void ThreadPool::start()
{
  dout(10) << "start" << dendl;
  for (set<WorkThread*>::iterator p = _threads.begin();
       p != _threads.end();
       p++)
    (*p)->create();
  dout(15) << "started" << dendl;
}
void ThreadPool::stop(bool clear_after)
{
  dout(10) << "stop" << dendl;
  _lock.Lock();
  _stop = true;
  _cond.Signal();
  _lock.Unlock();
  for (set<WorkThread*>::iterator p = _threads.begin();
       p != _threads.end();
       p++)
    (*p)->join();
  _lock.Lock();
  for (unsigned i=0; i<work_queues.size(); i++)
    work_queues[i]->_clear();
  _lock.Unlock();    
  dout(15) << "stopped" << dendl;
}


void ThreadPool::pause()
{
  dout(10) << "pause" << dendl;
  _lock.Lock();
  assert(!_pause);
  _pause = true;
  while (processing)
    _wait_cond.Wait(_lock);
  _lock.Unlock();
  dout(15) << "paused" << dendl;
}

void ThreadPool::pause_new()
{
  dout(10) << "pause_new" << dendl;
  _lock.Lock();
  assert(!_pause);
  _pause = true;
  _lock.Unlock();
}

void ThreadPool::unpause()
{
  dout(10) << "unpause" << dendl;
  _lock.Lock();
  assert(_pause);
  _pause = false;
  _cond.Signal();
  _lock.Unlock();
}

void ThreadPool::drain(WorkQueue_* wq)
{
  dout(10) << "drain" << dendl;
  _lock.Lock();
  _draining++;
  while (processing || (wq != NULL && !wq->_empty()))
    _wait_cond.Wait(_lock);
  _draining--;
  _lock.Unlock();
}