]> git.proxmox.com Git - mirror_corosync.git/blob - exec/schedwrk.c
config: Don't free pointers used by transports
[mirror_corosync.git] / exec / schedwrk.c
1 /*
2 * Copyright (c) 2009-2010 Red Hat, Inc.
3 *
4 * All rights reserved.
5 *
6 * Author: Steven Dake (sdake@redhat.com)
7 *
8 * This software licensed under BSD license, the text of which follows:
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 *
13 * - Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 * - Neither the name of the MontaVista Software, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived from this
20 * software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <config.h>
36 #include <corosync/totem/totempg.h>
37 #include <corosync/hdb.h>
38 #include "schedwrk.h"
39
40 static void (*serialize_lock) (void);
41 static void (*serialize_unlock) (void);
42
43 DECLARE_HDB_DATABASE (schedwrk_instance_database,NULL);
44
45 struct schedwrk_instance {
46 int (*schedwrk_fn) (const void *);
47 const void *context;
48 void *callback_handle;
49 int lock;
50 };
51
52 static int schedwrk_do (enum totem_callback_token_type type, const void *context)
53 {
54 hdb_handle_t handle = *((hdb_handle_t *)context);
55 struct schedwrk_instance *instance;
56 int res;
57
58 res = hdb_handle_get (&schedwrk_instance_database,
59 handle,
60 (void *)&instance);
61 if (res != 0) {
62 goto error_exit;
63 }
64
65 if (instance->lock)
66 serialize_lock ();
67
68 res = instance->schedwrk_fn (instance->context);
69
70 if (instance->lock)
71 serialize_unlock ();
72
73 if (res == 0) {
74 hdb_handle_destroy (&schedwrk_instance_database, handle);
75 }
76 hdb_handle_put (&schedwrk_instance_database, handle);
77 return (res);
78
79 error_exit:
80 return (-1);
81 }
82
83 void schedwrk_init (
84 void (*serialize_lock_fn) (void),
85 void (*serialize_unlock_fn) (void))
86 {
87 serialize_lock = serialize_lock_fn;
88 serialize_unlock = serialize_unlock_fn;
89 }
90
91 static int schedwrk_internal_create (
92 hdb_handle_t *handle,
93 int (schedwrk_fn) (const void *),
94 const void *context,
95 int lock)
96 {
97 struct schedwrk_instance *instance;
98 int res;
99
100 res = hdb_handle_create (&schedwrk_instance_database,
101 sizeof (struct schedwrk_instance), handle);
102 if (res != 0) {
103 goto error_exit;
104 }
105 res = hdb_handle_get (&schedwrk_instance_database, *handle,
106 (void *)&instance);
107 if (res != 0) {
108 goto error_destroy;
109 }
110
111 totempg_callback_token_create (
112 &instance->callback_handle,
113 TOTEM_CALLBACK_TOKEN_SENT,
114 1,
115 schedwrk_do,
116 handle);
117
118 instance->schedwrk_fn = schedwrk_fn;
119 instance->context = context;
120 instance->lock = lock;
121
122 hdb_handle_put (&schedwrk_instance_database, *handle);
123
124 return (0);
125
126 error_destroy:
127 hdb_handle_destroy (&schedwrk_instance_database, *handle);
128
129 error_exit:
130 return (-1);
131 }
132
133 /*
134 * handle pointer is internally used by totempg_callback_token_create. To make schedwrk work,
135 * handle must be pointer to ether heap or .text or static memory (not stack) which is not
136 * changed by caller.
137 */
138 int schedwrk_create (
139 hdb_handle_t *handle,
140 int (schedwrk_fn) (const void *),
141 const void *context)
142 {
143 return schedwrk_internal_create (handle, schedwrk_fn, context, 1);
144 }
145
146 int schedwrk_create_nolock (
147 hdb_handle_t *handle,
148 int (schedwrk_fn) (const void *),
149 const void *context)
150 {
151 return schedwrk_internal_create (handle, schedwrk_fn, context, 0);
152 }
153
154 void schedwrk_destroy (hdb_handle_t handle)
155 {
156 hdb_handle_destroy (&schedwrk_instance_database, handle);
157 }