1 //----------------------------------------------------------------------
2 // Copyright (C) 2006 - 2007 by the FRESCOR consortium:
4 // Universidad de Cantabria, SPAIN
5 // University of York, UK
6 // Scuola Superiore Sant'Anna, ITALY
7 // Kaiserslautern University, GERMANY
8 // Univ. Politecnica Valencia, SPAIN
9 // Czech Technical University in Prague, CZECH REPUBLIC
11 // Thales Communication S.A. FRANCE
12 // Visual Tools S.A. SPAIN
13 // Rapita Systems Ltd UK
16 // See http://www.frescor.org
18 // The FRESCOR project (FP6/2005/IST/5-034026) is funded
19 // in part by the European Union Sixth Framework Programme
20 // The European Union is not liable of any use that may be
24 // based on previous work (FSF) done in the FIRST project
26 // Copyright (C) 2005 Mälardalen University, SWEDEN
27 // Scuola Superiore S.Anna, ITALY
28 // Universidad de Cantabria, SPAIN
29 // University of York, UK
31 // This file is part of DTM (Distributed Transaction Manager)
33 // DTM is free software; you can redistribute it and/or modify it
34 // under terms of the GNU General Public License as published by the
35 // Free Software Foundation; either version 2, or (at your option) any
36 // later version. DTM is distributed in the hope that it will be
37 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
38 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 // General Public License for more details. You should have received a
40 // copy of the GNU General Public License along with DTM; see file
41 // COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
42 // Cambridge, MA 02139, USA.
44 // As a special exception, including DTM header files in a file,
45 // instantiating DTM generics or templates, or linking other files
46 // with DTM objects to produce an executable application, does not
47 // by itself cause the resulting executable application to be covered
48 // by the GNU General Public License. This exception does not
49 // however invalidate any other reasons why the executable file might be
50 // covered by the GNU Public License.
51 // -----------------------------------------------------------------------
56 * @brief Synchronization object used in FORB
59 * Michael Gonzalez Harbour <mgh@unican.es>
60 * Michal Sojka <sojkam1@fel.cvut.cz>
61 * Daniel Sangorrin <daniel.sangorrin@unican.es>
65 * This module contains the definition of the data object and operations to
66 * create a pool of objects, obtain the id of an unused object, wait upon it,
67 * signal it, and finish using it.
71 * See the COPYING file in the FORB's root directory
78 * Initializes the synchronization object.
80 * @return Zero on success, FOSA error code on error.
83 int forb_syncobj_init(forb_syncobj_t *syncobj, int ceiling)
87 err = fosa_cond_init(&syncobj->cond);
88 if (err != 0) return err;
90 syncobj->is_work_done = false;
92 err = fosa_mutex_init(&syncobj->mutex, ceiling);
93 if (err != 0) return err;
99 * Destroys all resources related to the synchronization object.
102 int forb_syncobj_destroy(forb_syncobj_t *syncobj)
106 err = fosa_cond_destroy(&syncobj->cond);
107 if (err != 0) return err;
109 err = fosa_mutex_destroy(&syncobj->mutex);
110 if (err != 0) return err;
116 * Signal the synchronization object.
119 int forb_syncobj_signal(forb_syncobj_t *syncobj)
123 err = fosa_mutex_lock(&syncobj->mutex);
124 if (err != 0) return err;
126 syncobj->is_work_done = true;
128 err = fosa_cond_signal(&syncobj->cond);
129 if (err != 0) goto locked_error;
131 err = fosa_mutex_unlock(&syncobj->mutex);
132 if (err != 0) return err;
137 fosa_mutex_unlock(&syncobj->mutex);
142 syncobj_cleanup(void *syncobj)
144 forb_syncobj_t *s = syncobj;
145 fosa_mutex_unlock(&s->mutex);
149 * Wait on the synchronization object.
152 int forb_syncobj_wait(forb_syncobj_t *syncobj)
156 err = fosa_mutex_lock(&syncobj->mutex);
157 if (err != 0) return err;
158 pthread_cleanup_push(syncobj_cleanup, syncobj);
160 while (syncobj->is_work_done == false) {
161 err = fosa_cond_wait(&syncobj->cond,
163 if (err != 0) goto locked_error;
166 syncobj->is_work_done = false;
169 pthread_cleanup_pop(1);
175 * Wait on the synchronization object with a timeout.
177 * @return Zero on success, ETIMEDOUT on timeout or other FOSA error
181 int forb_syncobj_timedwait(forb_syncobj_t *syncobj,
182 const struct timespec *abstime)
186 err = fosa_mutex_lock(&syncobj->mutex);
187 if (err != 0) return err;
188 pthread_cleanup_push(syncobj_cleanup, syncobj);
190 while (syncobj->is_work_done == false) {
191 err = fosa_cond_timedwait(&syncobj->cond,
194 if (err != 0) goto locked_error;
197 syncobj->is_work_done = false;
200 pthread_cleanup_pop(1);