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
|
/*
* virthreadjob.c: code for tracking job associated with current thread
*
* Copyright (C) 2013-2015 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include "internal.h"
#include "virerror.h"
#include "virlog.h"
#include "virthread.h"
#include "virthreadjob.h"
#define VIR_FROM_THIS VIR_FROM_THREAD
VIR_LOG_INIT("util.threadjob");
virThreadLocal virThreadJobWorker;
virThreadLocal virThreadJobName;
static int
virThreadJobOnceInit(void)
{
if (virThreadLocalInit(&virThreadJobWorker, NULL) < 0 ||
virThreadLocalInit(&virThreadJobName, NULL) < 0)
return -1;
return 0;
}
VIR_ONCE_GLOBAL_INIT(virThreadJob);
const char *
virThreadJobGet(void)
{
const char *job;
if (virThreadJobInitialize() < 0)
return NULL;
job = virThreadLocalGet(&virThreadJobName);
if (!job)
job = virThreadLocalGet(&virThreadJobWorker);
return job;
}
void
virThreadJobSetWorker(const char *worker)
{
if (!worker || virThreadJobInitialize() < 0)
return;
if (virThreadLocalSet(&virThreadJobWorker, (void *) worker) < 0)
virReportSystemError(errno,
_("cannot set worker name to %1$s"),
worker);
VIR_DEBUG("Thread %llu is running worker %s", virThreadSelfID(), worker);
}
void
virThreadJobSet(const char *caller)
{
const char *worker;
if (!caller || virThreadJobInitialize() < 0)
return;
if (virThreadLocalSet(&virThreadJobName, (void *) caller) < 0)
virReportSystemError(errno,
_("cannot set current job to %1$s"),
caller);
if ((worker = virThreadLocalGet(&virThreadJobWorker))) {
VIR_DEBUG("Thread %llu (%s) is now running job %s",
virThreadSelfID(), worker, caller);
} else {
VIR_DEBUG("Thread %llu is now running job %s",
virThreadSelfID(), caller);
}
}
void
virThreadJobClear(int rv)
{
const char *old;
const char *worker;
if (virThreadJobInitialize() < 0)
return;
if (!(old = virThreadLocalGet(&virThreadJobName)))
return;
if (virThreadLocalSet(&virThreadJobName, NULL) < 0)
virReportSystemError(errno, "%s", _("cannot reset current job"));
if ((worker = virThreadLocalGet(&virThreadJobWorker))) {
VIR_DEBUG("Thread %llu (%s) finished job %s with ret=%d",
virThreadSelfID(), worker, old, rv);
} else {
VIR_DEBUG("Thread %llu finished job %s with ret=%d",
virThreadSelfID(), old, rv);
}
}
|