2 libparted - a library for manipulating disk partitions
3 Copyright (C) 2001, 2007, 2009-2012 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * \addtogroup PedTimer
24 * \brief A PedTimer keeps track of the progress of a single (possibly
25 * compound) operation.
27 * The user of libparted constructs a PedTimer, and passes it to libparted
28 * functions that are likely to be expensive operations
29 * (like ped_file_system_resize). Use of timers is optional... you may
32 * When you create a PedTimer, you must specify a timer handler function.
33 * This will be called when there's an update on how work is progressing.
35 * Timers may be nested. When a timer is constructed, you can choose
36 * to assign it a parent, along with an estimate of what proportion of
37 * the total (parent's) time will be used in the nested operation. In
38 * this case, the nested timer's handler is internal to libparted,
39 * and simply updates the parent's progress, and calls its handler.
46 #include <parted/parted.h>
47 #include <parted/debug.h>
57 * \brief Creates a timer.
59 * Context will be passed in the \p context
60 * argument to the \p handler, when it is invoked.
62 * \return a new PedTimer
65 ped_timer_new (PedTimerHandler* handler, void* context)
69 PED_ASSERT (handler != NULL);
71 timer = (PedTimer*) ped_malloc (sizeof (PedTimer));
75 timer->handler = handler;
76 timer->context = context;
77 ped_timer_reset (timer);
83 * \brief Destroys a \p timer.
86 ped_timer_destroy (PedTimer* timer)
94 /* This function is used by ped_timer_new_nested() as the timer->handler
98 _nest_handler (PedTimer* timer, void* context)
100 NestedContext* ncontext = (NestedContext*) context;
104 ncontext->start_frac + ncontext->nest_frac * timer->frac);
109 * \brief Creates a new nested timer.
111 * This function creates a "nested" timer that describes the progress
112 * of a subtask. \p parent is the parent timer, and \p nested_frac is
113 * the estimated proportion (between 0 and 1) of the time that will be
114 * spent doing the nested timer's operation. The timer should only be
115 * constructed immediately prior to starting the nested operation.
116 * (It will be inaccurate, otherwise).
117 * Updates to the progress of the subtask are propagated
118 * back through to the parent task's timer.
120 * \return nested timer
123 ped_timer_new_nested (PedTimer* parent, float nest_frac)
125 NestedContext* context;
130 PED_ASSERT (nest_frac >= 0.0f);
131 PED_ASSERT (nest_frac <= 1.0f);
133 context = (NestedContext*) ped_malloc (sizeof (NestedContext));
136 context->parent = parent;
137 context->nest_frac = nest_frac;
138 context->start_frac = parent->frac;
140 return ped_timer_new (_nest_handler, context);
144 * \brief Destroys a nested \p timer.
147 ped_timer_destroy_nested (PedTimer* timer)
152 free (timer->context);
153 ped_timer_destroy (timer);
159 * \brief This function calls the update handler, making sure that it has
162 * First it updates \p timer->now and recomputes \p timer->predicted_end,
163 * and then calls the handler.
166 ped_timer_touch (PedTimer* timer)
171 timer->now = time (NULL);
172 if (timer->now > timer->predicted_end)
173 timer->predicted_end = timer->now;
175 timer->handler (timer, timer->context);
181 * \brief This function sets the \p timer into a "start of task" position.
183 * It resets the \p timer, by setting \p timer->start and \p timer->now
184 * to the current time.
187 ped_timer_reset (PedTimer* timer)
192 timer->start = timer->now = timer->predicted_end = time (NULL);
193 timer->state_name = NULL;
196 ped_timer_touch (timer);
202 * \brief This function tells a \p timer what fraction \p frac of the task
203 * has been completed.
205 * Sets the new \p timer->frac, and calls ped_timer_touch().
208 ped_timer_update (PedTimer* timer, float frac)
213 timer->now = time (NULL);
219 + (long) ((timer->now - timer->start) / frac);
221 ped_timer_touch (timer);
227 * \brief This function changes the description of the current task that the
228 * \p timer describes.
230 * Sets a new name - \p state_name - for the current "phase" of the operation,
231 * and calls ped_timer_touch().
234 ped_timer_set_state_name (PedTimer* timer, const char* state_name)
239 timer->state_name = state_name;
240 ped_timer_touch (timer);