~ubuntu-branches/ubuntu/quantal/open-vm-tools/quantal-201210021442

« back to all changes in this revision

Viewing changes to lib/include/vmware/tools/threadPool.h

  • Committer: Bazaar Package Importer
  • Author(s): Serge Hallyn
  • Date: 2011-03-31 14:20:05 UTC
  • mfrom: (1.4.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110331142005-3n9red91p7ogkweo
Tags: 2011.03.28-387002-0ubuntu1
* Merge latest upstream git tag.  This has the unlocked_ioctl change
  needed to fix dkms build failures (LP: #727342)
* Changes in debian/rules:
  - work around a bug in toolbox/Makefile, where install-exec-hook is
    not happening.  This needs to get fixed the right way.
  - don't install 'vmware-user' which seems to no longer exist
  - move /etc/xdg into open-vm-toolbox (which should be done using .install)
* debian/open-vm-tools.init: add 'modprobe [-r] vmblock'. (LP: #332323)
* debian/rules and debian/open-vm-toolbox.lintian-overrides:
  - Make vmware-user-suid-wrapper suid-root (LP: #332323)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*********************************************************
 
2
 * Copyright (C) 2010 VMware, Inc. All rights reserved.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License as published
 
6
 * by the Free Software Foundation version 2.1 and no later version.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
10
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 
11
 * License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 
15
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 
16
 *
 
17
 *********************************************************/
 
18
 
 
19
#ifndef _THREADPOOL_H_
 
20
#define _THREADPOOL_H_
 
21
 
 
22
/**
 
23
 * @file threadPool.h
 
24
 *
 
25
 * Public interface for vmtoolsd's thread pool.
 
26
 *
 
27
 * vmtoolsd provides a worker thread pool for use by plugins. This pool is
 
28
 * shared among all plugins, and is configurable from the Tools config file.
 
29
 * Plugins can submit tasks to the thread pool by using one of the inline
 
30
 * functions declared in this header.
 
31
 *
 
32
 * The thread pool is a shared resource, so code whose execution time may be
 
33
 * very long might want to, instead, create a dedicated thread for execution.
 
34
 * The shared thread pool also provides a facility to more easily do that,
 
35
 * with the lifecycle of the new thread managed by the thread pool so that it
 
36
 * is properly notified of service shutdown.
 
37
 *
 
38
 * Finally, depending on the configuration, the shared thread pool might not
 
39
 * be a thread pool at all: if the configuration has disabled threading, tasks
 
40
 * destined to the shared thread pool will be executed on the main service
 
41
 * thread.
 
42
 */
 
43
 
 
44
#include <glib-object.h>
 
45
#include "vmware/tools/plugin.h"
 
46
 
 
47
#define TOOLS_CORE_PROP_TPOOL "tcs_prop_thread_pool"
 
48
 
 
49
/** Type of callback function used to register tasks with the pool. */
 
50
typedef void (*ToolsCorePoolCb)(ToolsAppCtx *ctx,
 
51
                                gpointer data);
 
52
 
 
53
/**
 
54
 * @brief Public interface of the shared thread pool.
 
55
 *
 
56
 * This struct is published in the service's TOOLS_CORE_PROP_TPOOL property,
 
57
 * and contains function pointers to the internal implementation of the
 
58
 * thread pool's functions. In general, applications may prefer to use the
 
59
 * inline functions provided below instead, since they take care of some of
 
60
 * the boilerplate code.
 
61
 */
 
62
typedef struct ToolsCorePool {
 
63
   guint (*submit)(ToolsAppCtx *ctx,
 
64
                   ToolsCorePoolCb cb,
 
65
                   gpointer data,
 
66
                   GDestroyNotify dtor);
 
67
   void (*cancel)(guint id);
 
68
   gboolean (*start)(ToolsAppCtx *ctx,
 
69
                     ToolsCorePoolCb cb,
 
70
                     ToolsCorePoolCb interrupt,
 
71
                     gpointer data,
 
72
                     GDestroyNotify dtor);
 
73
} ToolsCorePool;
 
74
 
 
75
 
 
76
/*
 
77
 *******************************************************************************
 
78
 * ToolsCorePool_GetPool --                                               */ /**
 
79
 *
 
80
 * @brief Returns the thread pool instance for the service.
 
81
 *
 
82
 * @param[in] ctx Application context.
 
83
 *
 
84
 * @return The thread pool instance, or NULL if it's not available.
 
85
 *
 
86
 *******************************************************************************
 
87
 */
 
88
 
 
89
G_INLINE_FUNC ToolsCorePool *
 
90
ToolsCorePool_GetPool(ToolsAppCtx *ctx)
 
91
{
 
92
   ToolsCorePool *pool = NULL;
 
93
   g_object_get(ctx->serviceObj, TOOLS_CORE_PROP_TPOOL, &pool, NULL);
 
94
   return pool;
 
95
}
 
96
 
 
97
 
 
98
/*
 
99
 *******************************************************************************
 
100
 * ToolsCorePool_SubmitTask --                                            */ /**
 
101
 *
 
102
 * @brief Submits a task for execution in the thread pool.
 
103
 *
 
104
 * The task is queued in the thread pool and will be executed as soon as a
 
105
 * worker thread is available. If the thread pool is disabled, the task will
 
106
 * be executed on the main service thread as soon as the main loop is idle.
 
107
 *
 
108
 * The task data's destructor will be called after the task finishes executing,
 
109
 * or in case the thread pool is destroyed before the task is executed.
 
110
 *
 
111
 * @param[in] ctx    Application context.
 
112
 * @param[in] cb     Function to execute the task.
 
113
 * @param[in] data   Opaque data for the task.
 
114
 * @param[in] dtor   Destructor for the task data.
 
115
 *
 
116
 * @return An identifier for the task, or 0 on error.
 
117
 *
 
118
 *******************************************************************************
 
119
 */
 
120
 
 
121
G_INLINE_FUNC guint
 
122
ToolsCorePool_SubmitTask(ToolsAppCtx *ctx,
 
123
                         ToolsCorePoolCb cb,
 
124
                         gpointer data,
 
125
                         GDestroyNotify dtor)
 
126
{
 
127
   ToolsCorePool *pool = ToolsCorePool_GetPool(ctx);
 
128
   if (pool != NULL) {
 
129
      return pool->submit(ctx, cb, data, dtor);
 
130
   }
 
131
   return 0;
 
132
}
 
133
 
 
134
 
 
135
/*
 
136
 *******************************************************************************
 
137
 * ToolsCorePool_CancelTask --                                            */ /**
 
138
 *
 
139
 * @brief Cancels a task previously submitted to the pool.
 
140
 *
 
141
 * If the task is currently being executed, this function does nothing.
 
142
 * Otherwise, the task is removed from the task queue, and its destructor
 
143
 * (if any) is called.
 
144
 *
 
145
 * @param[in] ctx    Application context.
 
146
 * @param[in] taskId Task ID returned by ToolsCorePool_SubmitTask().
 
147
 *
 
148
 *******************************************************************************
 
149
 */
 
150
 
 
151
G_INLINE_FUNC void
 
152
ToolsCorePool_CancelTask(ToolsAppCtx *ctx,
 
153
                         guint taskId)
 
154
{
 
155
   ToolsCorePool *pool = ToolsCorePool_GetPool(ctx);
 
156
   if (pool != NULL) {
 
157
      pool->cancel(taskId);
 
158
   }
 
159
}
 
160
 
 
161
 
 
162
/*
 
163
 *******************************************************************************
 
164
 * ToolsCorePool_StartThread --                                           */ /**
 
165
 *
 
166
 * @brief Starts a task on its own thread.
 
167
 *
 
168
 * This function will run a task on a dedicated thread that is not part of
 
169
 * the shared thread pool. The thread will be managed by the thread pool, so
 
170
 * that it's properly cleaned up when the service is shutting down.
 
171
 *
 
172
 * Threads started by this function cannot be stopped by using the cancel
 
173
 * function. Instead, if the application itself wants to stop the thread, it
 
174
 * should call the interrupt function it provided to the thread pool, or use
 
175
 * some other method of communicating with the thread.
 
176
 *
 
177
 * @param[in] ctx          Application context.
 
178
 * @param[in] cb           Function that implements the task to execute.
 
179
 * @param[in] interrupt    A function that will request the task to be
 
180
 *                         interrupted. This will be called when the pool
 
181
 *                         needs to stop all managed threads (e.g. during
 
182
 *                         service shutdown). The task should stop what it's
 
183
 *                         doing and end the thread soon after this callback
 
184
 *                         is fired.
 
185
 * @param[in] data         Opaque data for both task callback and interrupt
 
186
 *                         functions.
 
187
 * @param[in] dtor         Destructor for the task data.
 
188
 *
 
189
 * @return TRUE iff thread was successfully started.
 
190
 *
 
191
 *******************************************************************************
 
192
 */
 
193
 
 
194
G_INLINE_FUNC gboolean
 
195
ToolsCorePool_StartThread(ToolsAppCtx *ctx,
 
196
                          ToolsCorePoolCb cb,
 
197
                          ToolsCorePoolCb interrupt,
 
198
                          gpointer data,
 
199
                          GDestroyNotify dtor)
 
200
{
 
201
   ToolsCorePool *pool = ToolsCorePool_GetPool(ctx);
 
202
   if (pool != NULL) {
 
203
      return pool->start(ctx, cb, interrupt, data, dtor);
 
204
   }
 
205
   return FALSE;
 
206
}
 
207
 
 
208
 
 
209
#endif /* _THREADPOOL_H_ */
 
210