corosync  2.3.5
schedwrk.c
Go to the documentation of this file.
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 
46  int (*schedwrk_fn) (const void *);
47  const void *context;
49  int lock;
50 };
51 
52 union u {
54  const void *v;
55 };
56 static hdb_handle_t
57 void2handle (const void *v) { union u u; u.v = v; return u.h; }
58 static const void *
59 handle2void (hdb_handle_t h) { union u u; u.h = h; return u.v; }
60 
61 static int schedwrk_do (enum totem_callback_token_type type, const void *context)
62 {
63  hdb_handle_t handle = void2handle (context);
64  struct schedwrk_instance *instance;
65  int res;
66 
67  res = hdb_handle_get (&schedwrk_instance_database,
68  hdb_nocheck_convert (handle),
69  (void *)&instance);
70  if (res != 0) {
71  goto error_exit;
72  }
73 
74  if (instance->lock)
75  serialize_lock ();
76 
77  res = instance->schedwrk_fn (instance->context);
78 
79  if (instance->lock)
80  serialize_unlock ();
81 
82  if (res == 0) {
83  hdb_handle_destroy (&schedwrk_instance_database, hdb_nocheck_convert (handle));
84  }
85  hdb_handle_put (&schedwrk_instance_database,
86  hdb_nocheck_convert (handle));
87  return (res);
88 
89 error_exit:
90  return (-1);
91 }
92 
94  void (*serialize_lock_fn) (void),
95  void (*serialize_unlock_fn) (void))
96 {
97  serialize_lock = serialize_lock_fn;
98  serialize_unlock = serialize_unlock_fn;
99 }
100 
101 static int schedwrk_internal_create (
102  hdb_handle_t *handle,
103  int (schedwrk_fn) (const void *),
104  const void *context,
105  int lock)
106 {
107  struct schedwrk_instance *instance;
108  int res;
109 
110  res = hdb_handle_create (&schedwrk_instance_database,
111  sizeof (struct schedwrk_instance), handle);
112  if (res != 0) {
113  goto error_exit;
114  }
115  res = hdb_handle_get (&schedwrk_instance_database, *handle,
116  (void *)&instance);
117  if (res != 0) {
118  goto error_destroy;
119  }
120 
122  &instance->callback_handle,
124  1,
125  schedwrk_do,
126  handle2void (*handle));
127 
128  instance->schedwrk_fn = schedwrk_fn;
129  instance->context = context;
130  instance->lock = lock;
131 
132  hdb_handle_put (&schedwrk_instance_database, *handle);
133 
134  return (0);
135 
136 error_destroy:
137  hdb_handle_destroy (&schedwrk_instance_database, *handle);
138 
139 error_exit:
140  return (-1);
141 }
142 
144  hdb_handle_t *handle,
145  int (schedwrk_fn) (const void *),
146  const void *context)
147 {
148  return schedwrk_internal_create (handle, schedwrk_fn, context, 1);
149 }
150 
152  hdb_handle_t *handle,
153  int (schedwrk_fn) (const void *),
154  const void *context)
155 {
156  return schedwrk_internal_create (handle, schedwrk_fn, context, 0);
157 }
158 
160 {
161  hdb_handle_destroy (&schedwrk_instance_database, handle);
162 }
Definition: exec/cpg.c:1902
Totem Single Ring Protocol.
const void * context
Definition: schedwrk.c:47
void * callback_handle
Definition: schedwrk.c:48
void schedwrk_init(void(*serialize_lock_fn)(void), void(*serialize_unlock_fn)(void))
Definition: schedwrk.c:93
void schedwrk_destroy(hdb_handle_t handle)
Definition: schedwrk.c:159
int schedwrk_create_nolock(hdb_handle_t *handle, int(schedwrk_fn)(const void *), const void *context)
Definition: schedwrk.c:151
hdb_handle_t h
Definition: schedwrk.c:53
const void * v
Definition: schedwrk.c:54
qb_handle_t hdb_handle_t
Definition: hdb.h:52
int(* schedwrk_fn)(const void *)
Definition: schedwrk.c:46
int totempg_callback_token_create(void **handle_out, enum totem_callback_token_type type, int delete, int(*callback_fn)(enum totem_callback_token_type type, const void *), const void *data)
Definition: totempg.c:1077
char type
Definition: totemrrp.c:518
DECLARE_HDB_DATABASE(schedwrk_instance_database, NULL)
int schedwrk_create(hdb_handle_t *handle, int(schedwrk_fn)(const void *), const void *context)
Definition: schedwrk.c:143
totem_callback_token_type
Definition: coroapi.h:117