File: cleanup.c

package info (click to toggle)
ploop 1.15-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,372 kB
  • sloc: ansic: 16,133; sh: 413; makefile: 222; python: 144
file content (75 lines) | stat: -rw-r--r-- 1,914 bytes parent folder | download | duplicates (4)
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
/*
 *  Copyright (C) 2008-2012, Parallels, Inc. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>

#include "list.h"
#include "cleanup.h"
#include "ploop.h"

static __thread struct ploop_cancel_handle __cancel_data;

struct ploop_cancel_handle *ploop_get_cancel_handle(void)
{
	return &__cancel_data;
}

struct ploop_cleanup_hook *register_cleanup_hook(cleanup_FN fn, void *data)
{
	struct ploop_cleanup_hook *h;
	struct ploop_cancel_handle *handle = ploop_get_cancel_handle();
	list_head_t *head = &handle->head;

	if (head->next == NULL)
		list_head_init(head);

	h = malloc(sizeof(struct ploop_cleanup_hook));
	if (h == NULL)
		return NULL;
	h->fn = fn;
	h->data = data;
	list_add(&h->list, head);

	return h;
}

void unregister_cleanup_hook(struct ploop_cleanup_hook *h)
{
	if (h != NULL) {
		list_del(&h->list);
		free(h);
	}
}

void ploop_cancel_operation(void)
{
	struct ploop_cleanup_hook *it;
	struct ploop_cancel_handle *handle = ploop_get_cancel_handle();
	list_head_t *head = &handle->head;

	ploop_log(0, "Cancelling...");
	handle->flags = 1;

	if (head->next != NULL) {
		list_for_each(it, head, list) {
			it->fn(it->data);
		}
	}
}