1 /**
2 * UGENE - Integrated Bioinformatics Tools.
3 * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4 * http://ugene.net
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program 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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22 #include "TLSTask.h"
23
24 namespace U2 {
25
26 QThreadStorage<TLSContextRef *> TLSUtils::tls;
27
28 /************************************************************************/
29 /* TaskLocalData */
30 /************************************************************************/
current(QString contextId)31 TLSContext *TLSUtils::current(QString contextId) {
32 Q_UNUSED(contextId)
33 TLSContextRef *ref = tls.localData();
34 if (ref != nullptr) {
35 assert(ref->ctx != nullptr);
36 assert(ref->ctx->id == contextId);
37 return ref->ctx;
38 }
39 assert(0);
40 return nullptr;
41 }
42
bindToTLSContext(TLSContext * ctx)43 void TLSUtils::bindToTLSContext(TLSContext *ctx) {
44 assert(ctx != nullptr);
45 assert(!tls.hasLocalData());
46 tls.setLocalData(new TLSContextRef(ctx));
47 }
48
detachTLSContext()49 void TLSUtils::detachTLSContext() {
50 TLSContextRef *ref = tls.localData();
51 assert(ref != nullptr && ref->ctx != nullptr);
52 ref->ctx = nullptr;
53 tls.setLocalData(nullptr);
54 }
55
56 /************************************************************************/
57 /* TLSTask */
58 /************************************************************************/
59
TLSTask(const QString & _name,TaskFlags _flags,bool _deleteContext)60 TLSTask::TLSTask(const QString &_name, TaskFlags _flags, bool _deleteContext)
61 : Task(_name, _flags), taskContext(nullptr), deleteContext(_deleteContext) {
62 }
63
prepare()64 void TLSTask::prepare() {
65 taskContext = createContextInstance();
66 }
67
run()68 void TLSTask::run() {
69 TLSUtils::bindToTLSContext(taskContext);
70 try {
71 _run();
72 } catch (...) {
73 stateInfo.setError("_run() throws exception");
74 }
75 TLSUtils::detachTLSContext();
76 }
77
~TLSTask()78 TLSTask::~TLSTask() {
79 if (deleteContext)
80 delete taskContext;
81 taskContext = nullptr;
82 }
83 } // namespace U2
84