[go: up one dir, main page]

Menu

[8f591a]: / io / diskqueue.cpp  Maximize  Restore  History

Download this file

151 lines (116 with data), 2.9 kB

  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
#ifndef DISKQUEUE_HEADER
#define DISKQUEUE_HEADER
/***************************************************************************
* diskqueue.cpp
*
* Sat Aug 24 23:52:06 2002
* Copyright 2002 Roman Dementiev
* dementiev@mpi-sb.mpg.de
****************************************************************************/
#include "iobase.h"
#include "../common/gprof.h"
#ifdef STXXL_THREAD_PROFILING
#define pthread_create gprof_pthread_create
#endif
namespace stxxl
{
disk_queue::disk_queue (int n):sem (0), _priority_op (WRITE) // n is ignored
{
// cout << "disk_queue created." << endl;
#ifdef STXXL_IO_STATS
iostats = stats::get_instance ();
#endif
stxxl_nassert(pthread_create(&thread, NULL, (thread_function_t) worker, static_cast<void *>(this)));
}
void disk_queue::add_readreq (request_ptr & req)
{
read_mutex.lock ();
read_queue.push (req);
read_mutex.unlock ();
sem++;
}
void disk_queue::add_writereq (request_ptr & req)
{
// cout << req << " submitted" << endl;
write_mutex.lock ();
write_queue.push (req);
write_mutex.unlock ();
sem++;
// cout << req << " added"<< endl;
}
disk_queue::~disk_queue ()
{
stxxl_nassert (pthread_cancel (thread));
}
void *disk_queue::worker (void *arg)
{
disk_queue *pthis = static_cast<disk_queue*>(arg);
request_ptr req;
stxxl_nassert (pthread_setcancelstate
(PTHREAD_CANCEL_ENABLE, NULL));
stxxl_nassert (pthread_setcanceltype
(PTHREAD_CANCEL_DEFERRED, NULL));
// Allow cancellation in semaphore operator-- call
bool write_phase = true;
for (;;)
{
pthis->sem--;
if (write_phase)
{
pthis->write_mutex.lock ();
if (!pthis->write_queue.empty ())
{
req = pthis->write_queue.front ();
pthis->write_queue.pop ();
pthis->write_mutex.unlock ();
#ifdef STXXL_IO_STATS
pthis->iostats->write_started ();
#endif
req->serve ();
#ifdef STXXL_IO_STATS
pthis->iostats->write_finished ();
#endif
}
else
{
pthis->write_mutex.unlock ();
pthis->sem++;
if (pthis->_priority_op == WRITE)
write_phase = false;
}
if (pthis->_priority_op == NONE
|| pthis->_priority_op == READ)
write_phase = false;
}
else
{
pthis->read_mutex.lock ();
if (!pthis->read_queue.empty ())
{
req = pthis->read_queue.front ();
pthis->read_queue.pop ();
pthis->read_mutex.unlock ();
#ifdef STXXL_IO_STATS
pthis->iostats->read_started ();
#endif
req->serve ();
#ifdef STXXL_IO_STATS
pthis->iostats->read_finished ();
#endif
}
else
{
pthis->read_mutex.unlock ();
pthis->sem++;
if (pthis->_priority_op == READ)
write_phase = true;
}
if (pthis->_priority_op == NONE
|| pthis->_priority_op == WRITE)
write_phase = true;
}
}
return NULL;
}
};
#endif