| 1 |
/* |
| 2 |
* This File is part of Davix, The IO library for HTTP based protocols |
| 3 |
* Copyright (C) CERN 2013 |
| 4 |
* Author: Kwong Tat Cheung <[email protected]> |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "davix_taskqueue.hpp" |
| 23 |
#include <davix_internal.hpp> |
| 24 |
#include <utils/davix_logger_internal.hpp> |
| 25 |
|
| 26 |
namespace Davix{ |
| 27 |
|
| 28 |
DavixTaskQueue::DavixTaskQueue(){ |
| 29 |
pthread_mutex_init(&tq_mutex, NULL); |
| 30 |
pthread_cond_init(&popOpConvar, NULL); |
| 31 |
pthread_cond_init(&pushOpConvar, NULL); |
| 32 |
_shutdown = false; |
| 33 |
_no_cap = false; |
| 34 |
_queueType = QueueType::FIFO; |
| 35 |
} |
| 36 |
|
| 37 |
DavixTaskQueue::DavixTaskQueue(const Tool::OptParams& opts, QueueType::QueueType queueType){ |
| 38 |
pthread_mutex_init(&tq_mutex, NULL); |
| 39 |
pthread_cond_init(&popOpConvar, NULL); |
| 40 |
pthread_cond_init(&pushOpConvar, NULL); |
| 41 |
_shutdown = false; |
| 42 |
_no_cap = opts.no_cap; |
| 43 |
_queueType = queueType; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
int DavixTaskQueue::pushOp(DavixOp* op){ |
| 48 |
|
| 49 |
{ |
| 50 |
DavixMutex mutex(tq_mutex); |
| 51 |
|
| 52 |
// for recursive namespace crawling, we set a hard limit to the number of listing ops are allowed to prevent overflow |
| 53 |
// not the ideal solution, perhaps disk-backing the queue after a certain threshold would be better |
| 54 |
if((op->getOpType() == "LIST") || op->getOpType() == "LISTPP"){ |
| 55 |
if(!_no_cap && (taskQueue.size() > MAX_NUM_OF_LISTING_OPS)){ |
| 56 |
// queue size balloning out of control, warn and exit |
| 57 |
std::cerr << std::endl << "***Task queue's size has exceeded the maximum allowed, exiting programme!***" << std::endl; |
| 58 |
exit(-1); |
| 59 |
} |
| 60 |
} |
| 61 |
else{ |
| 62 |
while(taskQueue.size() >= DAVIX_DEFAULT_TASKQUEUE_SIZE) |
| 63 |
pthread_cond_wait(&pushOpConvar, mutex.getMutex()); |
| 64 |
} |
| 65 |
|
| 66 |
taskQueue.push_back(op); |
| 67 |
|
| 68 |
} |
| 69 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_CORE, "(DavixTaskQueue) {} Op pushed to taskQueue, target is {}, queue size is now: {}", op->getOpType(), op->getTargetUrl(), taskQueue.size()); |
| 70 |
pthread_cond_signal(&popOpConvar); |
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
DavixOp* DavixTaskQueue::popOp(){ |
| 75 |
bool dosig = false; |
| 76 |
DavixOp* op = NULL; |
| 77 |
//struct timespec to; |
| 78 |
{ |
| 79 |
DavixMutex mutex(tq_mutex); |
| 80 |
|
| 81 |
|
| 82 |
while(taskQueue.empty() && !_shutdown){ |
| 83 |
/* |
| 84 |
* to.tv_sec = time(NULL) + DEFAULT_WAIT_TIME; |
| 85 |
* to.tv_nsec = 0; |
| 86 |
* int rc = pthread_cond_timedwait(&popOpConvar, mutex.getMutex(), &to); |
| 87 |
* |
| 88 |
* if(rc == ETIMEDOUT) |
| 89 |
* { |
| 90 |
* std::cerr << std::endl << "(DavixTaskQueue) PopOp() timed out." << std::endl; |
| 91 |
* return NULL; |
| 92 |
} |
| 93 |
} |
| 94 |
*/ |
| 95 |
pthread_cond_wait(&popOpConvar, mutex.getMutex()); |
| 96 |
|
| 97 |
if(_shutdown){ |
| 98 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_CORE, "(DavixTaskQueue) Shutting down task queue, queue size is: {}", taskQueue.size()); |
| 99 |
return NULL; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if(taskQueue.size() > 0){ |
| 104 |
switch(_queueType){ |
| 105 |
case QueueType::FIFO: |
| 106 |
{ |
| 107 |
op = taskQueue.front(); |
| 108 |
taskQueue.pop_front(); |
| 109 |
break; |
| 110 |
} |
| 111 |
case QueueType::LIFO: |
| 112 |
{ |
| 113 |
op = taskQueue.back(); |
| 114 |
taskQueue.pop_back(); |
| 115 |
break; |
| 116 |
} |
| 117 |
} // end switch |
| 118 |
dosig = true; |
| 119 |
} |
| 120 |
|
| 121 |
} // end mutex scope |
| 122 |
|
| 123 |
if (dosig) { |
| 124 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_CORE, "(DavixTaskQueue) {} Op popped from taskQueue, queue size is now: {}", op->getOpType(), taskQueue.size()); |
| 125 |
|
| 126 |
pthread_cond_signal(&pushOpConvar); |
| 127 |
return op; |
| 128 |
} |
| 129 |
|
| 130 |
return NULL; |
| 131 |
} |
| 132 |
|
| 133 |
int DavixTaskQueue::getSize(){ |
| 134 |
DavixMutex mutex(tq_mutex); |
| 135 |
return taskQueue.size(); |
| 136 |
} |
| 137 |
|
| 138 |
bool DavixTaskQueue::isEmpty(){ |
| 139 |
DavixMutex mutex(tq_mutex); |
| 140 |
return taskQueue.empty(); |
| 141 |
} |
| 142 |
|
| 143 |
void DavixTaskQueue::shutdown(){ |
| 144 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_CORE, "(DavixTaskQueue) Commencing shutdown..."); |
| 145 |
_shutdown = true; |
| 146 |
pthread_cond_broadcast(&pushOpConvar); |
| 147 |
pthread_cond_broadcast(&popOpConvar); |
| 148 |
} |
| 149 |
|
| 150 |
bool DavixTaskQueue::isStopped(){ |
| 151 |
return _shutdown; |
| 152 |
} |
| 153 |
|
| 154 |
} |